ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.21 by dl, Thu Jan 20 00:39:13 2005 UTC vs.
Revision 1.24 by dl, Tue Aug 2 11:34:04 2005 UTC

# Line 126 | Line 126 | public class ReentrantReadWriteLockTest
126      /**
127       * getWriteHoldCount returns number of recursive holds
128       */
129 <    public void testGetHoldCount() {
129 >    public void testGetWriteHoldCount() {
130          ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
131          for(int i = 1; i <= SIZE; i++) {
132              lock.writeLock().lock();
# Line 137 | Line 137 | public class ReentrantReadWriteLockTest
137              assertEquals(i-1,lock.getWriteHoldCount());
138          }
139      }
140 +
141 +    /**
142 +     * getReadHoldCount returns number of recursive holds
143 +     */
144 +    public void testGetReadHoldCount() {
145 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
146 +        for(int i = 1; i <= SIZE; i++) {
147 +            lock.readLock().lock();
148 +            assertEquals(i,lock.getReadHoldCount());
149 +        }
150 +        for(int i = SIZE; i > 0; i--) {
151 +            lock.readLock().unlock();
152 +            assertEquals(i-1,lock.getReadHoldCount());
153 +        }
154 +    }
155      
156  
157      /**
# Line 389 | Line 404 | public class ReentrantReadWriteLockTest
404  
405      /**
406       * Read lock succeeds if write locked by current thread even if
407 <     * other threads are waiting
407 >     * other threads are waiting for readlock
408       */
409      public void testReadHoldingWriteLock2() {
410          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 427 | Line 442 | public class ReentrantReadWriteLockTest
442      }
443  
444      /**
445 +     *  Read lock succeeds if write locked by current thread even if
446 +     * other threads are waiting for writelock
447 +     */
448 +    public void testReadHoldingWriteLock3() {
449 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450 +        lock.writeLock().lock();
451 +        Thread t1 = new Thread(new Runnable() {
452 +                public void run() {
453 +                    lock.writeLock().lock();
454 +                    lock.writeLock().unlock();
455 +                }
456 +            });
457 +        Thread t2 = new Thread(new Runnable() {
458 +                public void run() {
459 +                    lock.writeLock().lock();
460 +                    lock.writeLock().unlock();
461 +                }
462 +            });
463 +
464 +        try {
465 +            t1.start();
466 +            t2.start();
467 +            lock.readLock().lock();
468 +            lock.readLock().unlock();
469 +            Thread.sleep(SHORT_DELAY_MS);
470 +            lock.readLock().lock();
471 +            lock.readLock().unlock();
472 +            lock.writeLock().unlock();
473 +            t1.join(MEDIUM_DELAY_MS);
474 +            t2.join(MEDIUM_DELAY_MS);
475 +            assertTrue(!t1.isAlive());
476 +            assertTrue(!t2.isAlive());
477 +          
478 +        } catch(Exception e){
479 +            unexpectedException();
480 +        }
481 +    }
482 +
483 +
484 +    /**
485 +     *  Write lock succeeds if write locked by current thread even if
486 +     * other threads are waiting for writelock
487 +     */
488 +    public void testWriteHoldingWriteLock4() {
489 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
490 +        lock.writeLock().lock();
491 +        Thread t1 = new Thread(new Runnable() {
492 +                public void run() {
493 +                    lock.writeLock().lock();
494 +                    lock.writeLock().unlock();
495 +                }
496 +            });
497 +        Thread t2 = new Thread(new Runnable() {
498 +                public void run() {
499 +                    lock.writeLock().lock();
500 +                    lock.writeLock().unlock();
501 +                }
502 +            });
503 +
504 +        try {
505 +            t1.start();
506 +            t2.start();
507 +            lock.writeLock().lock();
508 +            lock.writeLock().unlock();
509 +            Thread.sleep(SHORT_DELAY_MS);
510 +            lock.writeLock().lock();
511 +            lock.writeLock().unlock();
512 +            lock.writeLock().unlock();
513 +            t1.join(MEDIUM_DELAY_MS);
514 +            t2.join(MEDIUM_DELAY_MS);
515 +            assertTrue(!t1.isAlive());
516 +            assertTrue(!t2.isAlive());
517 +          
518 +        } catch(Exception e){
519 +            unexpectedException();
520 +        }
521 +    }
522 +
523 +
524 +    /**
525       * Fair Read trylock succeeds if write locked by current thread
526       */
527      public void testReadHoldingWriteLockFair() {
# Line 439 | Line 534 | public class ReentrantReadWriteLockTest
534  
535      /**
536       * Fair Read lock succeeds if write locked by current thread even if
537 <     * other threads are waiting
537 >     * other threads are waiting for readlock
538       */
539      public void testReadHoldingWriteLockFair2() {
540          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
# Line 478 | Line 573 | public class ReentrantReadWriteLockTest
573  
574  
575      /**
576 +     * Fair Read lock succeeds if write locked by current thread even if
577 +     * other threads are waiting for writelock
578 +     */
579 +    public void testReadHoldingWriteLockFair3() {
580 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
581 +        lock.writeLock().lock();
582 +        Thread t1 = new Thread(new Runnable() {
583 +                public void run() {
584 +                    lock.writeLock().lock();
585 +                    lock.writeLock().unlock();
586 +                }
587 +            });
588 +        Thread t2 = new Thread(new Runnable() {
589 +                public void run() {
590 +                    lock.writeLock().lock();
591 +                    lock.writeLock().unlock();
592 +                }
593 +            });
594 +
595 +        try {
596 +            t1.start();
597 +            t2.start();
598 +            lock.readLock().lock();
599 +            lock.readLock().unlock();
600 +            Thread.sleep(SHORT_DELAY_MS);
601 +            lock.readLock().lock();
602 +            lock.readLock().unlock();
603 +            lock.writeLock().unlock();
604 +            t1.join(MEDIUM_DELAY_MS);
605 +            t2.join(MEDIUM_DELAY_MS);
606 +            assertTrue(!t1.isAlive());
607 +            assertTrue(!t2.isAlive());
608 +          
609 +        } catch(Exception e){
610 +            unexpectedException();
611 +        }
612 +    }
613 +
614 +
615 +    /**
616 +     * Fair Write lock succeeds if write locked by current thread even if
617 +     * other threads are waiting for writelock
618 +     */
619 +    public void testWriteHoldingWriteLockFair4() {
620 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
621 +        lock.writeLock().lock();
622 +        Thread t1 = new Thread(new Runnable() {
623 +                public void run() {
624 +                    lock.writeLock().lock();
625 +                    lock.writeLock().unlock();
626 +                }
627 +            });
628 +        Thread t2 = new Thread(new Runnable() {
629 +                public void run() {
630 +                    lock.writeLock().lock();
631 +                    lock.writeLock().unlock();
632 +                }
633 +            });
634 +
635 +        try {
636 +            t1.start();
637 +            t2.start();
638 +            Thread.sleep(SHORT_DELAY_MS);
639 +            assertTrue(lock.isWriteLockedByCurrentThread());
640 +            assertTrue(lock.getWriteHoldCount() == 1);
641 +            lock.writeLock().lock();
642 +            assertTrue(lock.getWriteHoldCount() == 2);
643 +            lock.writeLock().unlock();
644 +            lock.writeLock().lock();
645 +            lock.writeLock().unlock();
646 +            lock.writeLock().unlock();
647 +            t1.join(MEDIUM_DELAY_MS);
648 +            t2.join(MEDIUM_DELAY_MS);
649 +            assertTrue(!t1.isAlive());
650 +            assertTrue(!t2.isAlive());
651 +          
652 +        } catch(Exception e){
653 +            unexpectedException();
654 +        }
655 +    }
656 +
657 +
658 +    /**
659       * Read tryLock succeeds if readlocked but not writelocked
660       */
661      public void testTryLockWhenReadLocked() {
# Line 520 | Line 698 | public class ReentrantReadWriteLockTest
698          }
699      }
700  
701 +
702 +    /**
703 +     * Fair Read tryLock succeeds if readlocked but not writelocked
704 +     */
705 +    public void testTryLockWhenReadLockedFair() {
706 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
707 +        lock.readLock().lock();
708 +        Thread t = new Thread(new Runnable() {
709 +                public void run() {
710 +                    threadAssertTrue(lock.readLock().tryLock());
711 +                    lock.readLock().unlock();
712 +                }
713 +            });
714 +        try {
715 +            t.start();
716 +            t.join();
717 +            lock.readLock().unlock();
718 +        } catch(Exception e){
719 +            unexpectedException();
720 +        }
721 +    }
722 +
723 +    
724 +
725 +    /**
726 +     * Fair write tryLock fails when readlocked
727 +     */
728 +    public void testWriteTryLockWhenReadLockedFair() {
729 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
730 +        lock.readLock().lock();
731 +        Thread t = new Thread(new Runnable() {
732 +                public void run() {
733 +                    threadAssertFalse(lock.writeLock().tryLock());
734 +                }
735 +            });
736 +        try {
737 +            t.start();
738 +            t.join();
739 +            lock.readLock().unlock();
740 +        } catch(Exception e){
741 +            unexpectedException();
742 +        }
743 +    }
744 +
745      
746  
747      /**
# Line 747 | Line 969 | public class ReentrantReadWriteLockTest
969          }
970      }
971  
972 +    /** A helper class for uninterruptible wait tests */
973 +    class UninterruptableThread extends Thread {
974 +        private Lock lock;
975 +        private Condition c;
976 +        
977 +        public volatile boolean canAwake = false;
978 +        public volatile boolean interrupted = false;
979 +        public volatile boolean lockStarted = false;
980 +        
981 +        public UninterruptableThread(Lock lock, Condition c) {
982 +            this.lock = lock;
983 +            this.c = c;
984 +        }
985 +        
986 +        public synchronized void run() {
987 +            lock.lock();
988 +            lockStarted = true;
989 +            
990 +            while (!canAwake) {
991 +                c.awaitUninterruptibly();
992 +            }
993 +            
994 +            interrupted = isInterrupted();
995 +            lock.unlock();
996 +        }
997 +    }
998 +
999      /**
1000       * awaitUninterruptibly doesn't abort on interrupt
1001       */
1002      public void testAwaitUninterruptibly() {
1003 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1003 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1004          final Condition c = lock.writeLock().newCondition();
1005 <        Thread t = new Thread(new Runnable() {
757 <                public void run() {
758 <                    lock.writeLock().lock();
759 <                    c.awaitUninterruptibly();
760 <                    lock.writeLock().unlock();
761 <                }
762 <            });
1005 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1006  
1007          try {
1008 <            t.start();
1009 <            Thread.sleep(SHORT_DELAY_MS);
1010 <            t.interrupt();
1008 >            thread.start();
1009 >
1010 >            while (!thread.lockStarted) {
1011 >                Thread.sleep(100);
1012 >            }
1013 >
1014              lock.writeLock().lock();
1015 <            c.signal();
1016 <            lock.writeLock().unlock();
1017 <            assert(t.isInterrupted());
1018 <            t.join(SHORT_DELAY_MS);
1019 <            assertFalse(t.isAlive());
1020 <        }
1021 <        catch (Exception ex) {
1015 >            try {
1016 >                thread.interrupt();
1017 >                thread.canAwake = true;
1018 >                c.signal();
1019 >            } finally {
1020 >                lock.writeLock().unlock();
1021 >            }
1022 >
1023 >            thread.join();
1024 >            assertTrue(thread.interrupted);
1025 >            assertFalse(thread.isAlive());
1026 >        } catch (Exception ex) {
1027              unexpectedException();
1028          }
1029      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines