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.22 by dl, Tue May 3 16:02:00 2005 UTC vs.
Revision 1.27 by dl, Thu May 18 10:29:23 2006 UTC

# Line 84 | Line 84 | public class ReentrantReadWriteLockTest
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
87 +        assertTrue(rl.writeLock().isHeldByCurrentThread());
88          assertEquals(0, rl.getReadLockCount());
89          rl.writeLock().unlock();
90          assertFalse(rl.isWriteLocked());
91          assertFalse(rl.isWriteLockedByCurrentThread());
92 +        assertFalse(rl.writeLock().isHeldByCurrentThread());
93          assertEquals(0, rl.getReadLockCount());
94          rl.readLock().lock();
95          assertFalse(rl.isWriteLocked());
# Line 108 | Line 110 | public class ReentrantReadWriteLockTest
110          rl.writeLock().lock();
111          assertTrue(rl.isWriteLocked());
112          assertTrue(rl.isWriteLockedByCurrentThread());
113 +        assertTrue(rl.writeLock().isHeldByCurrentThread());
114          assertEquals(0, rl.getReadLockCount());
115          rl.writeLock().unlock();
116          assertFalse(rl.isWriteLocked());
117          assertFalse(rl.isWriteLockedByCurrentThread());
118 +        assertFalse(rl.writeLock().isHeldByCurrentThread());
119          assertEquals(0, rl.getReadLockCount());
120          rl.readLock().lock();
121          assertFalse(rl.isWriteLocked());
# Line 126 | Line 130 | public class ReentrantReadWriteLockTest
130      /**
131       * getWriteHoldCount returns number of recursive holds
132       */
133 <    public void testGetHoldCount() {
133 >    public void testGetWriteHoldCount() {
134          ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135          for(int i = 1; i <= SIZE; i++) {
136              lock.writeLock().lock();
# Line 137 | Line 141 | public class ReentrantReadWriteLockTest
141              assertEquals(i-1,lock.getWriteHoldCount());
142          }
143      }
144 +
145 +    /**
146 +     * WriteLock.getHoldCount returns number of recursive holds
147 +     */
148 +    public void testGetHoldCount() {
149 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 +        for(int i = 1; i <= SIZE; i++) {
151 +            lock.writeLock().lock();
152 +            assertEquals(i,lock.writeLock().getHoldCount());
153 +        }
154 +        for(int i = SIZE; i > 0; i--) {
155 +            lock.writeLock().unlock();
156 +            assertEquals(i-1,lock.writeLock().getHoldCount());
157 +        }
158 +    }
159 +
160 +    /**
161 +     * getReadHoldCount returns number of recursive holds
162 +     */
163 +    public void testGetReadHoldCount() {
164 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 +        for(int i = 1; i <= SIZE; i++) {
166 +            lock.readLock().lock();
167 +            assertEquals(i,lock.getReadHoldCount());
168 +        }
169 +        for(int i = SIZE; i > 0; i--) {
170 +            lock.readLock().unlock();
171 +            assertEquals(i-1,lock.getReadHoldCount());
172 +        }
173 +    }
174      
175  
176      /**
# Line 169 | Line 203 | public class ReentrantReadWriteLockTest
203          try {
204              lock.writeLock().lock();
205              t.start();
206 +            Thread.sleep(SHORT_DELAY_MS);
207              t.interrupt();
208 +            Thread.sleep(SHORT_DELAY_MS);
209              lock.writeLock().unlock();
210              t.join();
211          } catch(Exception e){
# Line 215 | Line 251 | public class ReentrantReadWriteLockTest
251              });
252          try {
253              t.start();
254 +            Thread.sleep(SHORT_DELAY_MS);
255              t.interrupt();
256 +            Thread.sleep(SHORT_DELAY_MS);
257              lock.writeLock().unlock();
258              t.join();
259          } catch(Exception e){
# Line 389 | Line 427 | public class ReentrantReadWriteLockTest
427  
428      /**
429       * Read lock succeeds if write locked by current thread even if
430 <     * other threads are waiting
430 >     * other threads are waiting for readlock
431       */
432      public void testReadHoldingWriteLock2() {
433          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 427 | Line 465 | public class ReentrantReadWriteLockTest
465      }
466  
467      /**
468 +     *  Read lock succeeds if write locked by current thread even if
469 +     * other threads are waiting for writelock
470 +     */
471 +    public void testReadHoldingWriteLock3() {
472 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
473 +        lock.writeLock().lock();
474 +        Thread t1 = new Thread(new Runnable() {
475 +                public void run() {
476 +                    lock.writeLock().lock();
477 +                    lock.writeLock().unlock();
478 +                }
479 +            });
480 +        Thread t2 = new Thread(new Runnable() {
481 +                public void run() {
482 +                    lock.writeLock().lock();
483 +                    lock.writeLock().unlock();
484 +                }
485 +            });
486 +
487 +        try {
488 +            t1.start();
489 +            t2.start();
490 +            lock.readLock().lock();
491 +            lock.readLock().unlock();
492 +            Thread.sleep(SHORT_DELAY_MS);
493 +            lock.readLock().lock();
494 +            lock.readLock().unlock();
495 +            lock.writeLock().unlock();
496 +            t1.join(MEDIUM_DELAY_MS);
497 +            t2.join(MEDIUM_DELAY_MS);
498 +            assertTrue(!t1.isAlive());
499 +            assertTrue(!t2.isAlive());
500 +          
501 +        } catch(Exception e){
502 +            unexpectedException();
503 +        }
504 +    }
505 +
506 +
507 +    /**
508 +     *  Write lock succeeds if write locked by current thread even if
509 +     * other threads are waiting for writelock
510 +     */
511 +    public void testWriteHoldingWriteLock4() {
512 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
513 +        lock.writeLock().lock();
514 +        Thread t1 = new Thread(new Runnable() {
515 +                public void run() {
516 +                    lock.writeLock().lock();
517 +                    lock.writeLock().unlock();
518 +                }
519 +            });
520 +        Thread t2 = new Thread(new Runnable() {
521 +                public void run() {
522 +                    lock.writeLock().lock();
523 +                    lock.writeLock().unlock();
524 +                }
525 +            });
526 +
527 +        try {
528 +            t1.start();
529 +            t2.start();
530 +            lock.writeLock().lock();
531 +            lock.writeLock().unlock();
532 +            Thread.sleep(SHORT_DELAY_MS);
533 +            lock.writeLock().lock();
534 +            lock.writeLock().unlock();
535 +            lock.writeLock().unlock();
536 +            t1.join(MEDIUM_DELAY_MS);
537 +            t2.join(MEDIUM_DELAY_MS);
538 +            assertTrue(!t1.isAlive());
539 +            assertTrue(!t2.isAlive());
540 +          
541 +        } catch(Exception e){
542 +            unexpectedException();
543 +        }
544 +    }
545 +
546 +
547 +    /**
548       * Fair Read trylock succeeds if write locked by current thread
549       */
550      public void testReadHoldingWriteLockFair() {
# Line 439 | Line 557 | public class ReentrantReadWriteLockTest
557  
558      /**
559       * Fair Read lock succeeds if write locked by current thread even if
560 <     * other threads are waiting
560 >     * other threads are waiting for readlock
561       */
562      public void testReadHoldingWriteLockFair2() {
563          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
# Line 478 | Line 596 | public class ReentrantReadWriteLockTest
596  
597  
598      /**
599 +     * Fair Read lock succeeds if write locked by current thread even if
600 +     * other threads are waiting for writelock
601 +     */
602 +    public void testReadHoldingWriteLockFair3() {
603 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
604 +        lock.writeLock().lock();
605 +        Thread t1 = new Thread(new Runnable() {
606 +                public void run() {
607 +                    lock.writeLock().lock();
608 +                    lock.writeLock().unlock();
609 +                }
610 +            });
611 +        Thread t2 = new Thread(new Runnable() {
612 +                public void run() {
613 +                    lock.writeLock().lock();
614 +                    lock.writeLock().unlock();
615 +                }
616 +            });
617 +
618 +        try {
619 +            t1.start();
620 +            t2.start();
621 +            lock.readLock().lock();
622 +            lock.readLock().unlock();
623 +            Thread.sleep(SHORT_DELAY_MS);
624 +            lock.readLock().lock();
625 +            lock.readLock().unlock();
626 +            lock.writeLock().unlock();
627 +            t1.join(MEDIUM_DELAY_MS);
628 +            t2.join(MEDIUM_DELAY_MS);
629 +            assertTrue(!t1.isAlive());
630 +            assertTrue(!t2.isAlive());
631 +          
632 +        } catch(Exception e){
633 +            unexpectedException();
634 +        }
635 +    }
636 +
637 +
638 +    /**
639 +     * Fair Write lock succeeds if write locked by current thread even if
640 +     * other threads are waiting for writelock
641 +     */
642 +    public void testWriteHoldingWriteLockFair4() {
643 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
644 +        lock.writeLock().lock();
645 +        Thread t1 = new Thread(new Runnable() {
646 +                public void run() {
647 +                    lock.writeLock().lock();
648 +                    lock.writeLock().unlock();
649 +                }
650 +            });
651 +        Thread t2 = new Thread(new Runnable() {
652 +                public void run() {
653 +                    lock.writeLock().lock();
654 +                    lock.writeLock().unlock();
655 +                }
656 +            });
657 +
658 +        try {
659 +            t1.start();
660 +            t2.start();
661 +            Thread.sleep(SHORT_DELAY_MS);
662 +            assertTrue(lock.isWriteLockedByCurrentThread());
663 +            assertTrue(lock.getWriteHoldCount() == 1);
664 +            lock.writeLock().lock();
665 +            assertTrue(lock.getWriteHoldCount() == 2);
666 +            lock.writeLock().unlock();
667 +            lock.writeLock().lock();
668 +            lock.writeLock().unlock();
669 +            lock.writeLock().unlock();
670 +            t1.join(MEDIUM_DELAY_MS);
671 +            t2.join(MEDIUM_DELAY_MS);
672 +            assertTrue(!t1.isAlive());
673 +            assertTrue(!t2.isAlive());
674 +          
675 +        } catch(Exception e){
676 +            unexpectedException();
677 +        }
678 +    }
679 +
680 +
681 +    /**
682       * Read tryLock succeeds if readlocked but not writelocked
683       */
684      public void testTryLockWhenReadLocked() {
# Line 520 | Line 721 | public class ReentrantReadWriteLockTest
721          }
722      }
723  
724 +
725 +    /**
726 +     * Fair Read tryLock succeeds if readlocked but not writelocked
727 +     */
728 +    public void testTryLockWhenReadLockedFair() {
729 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
730 +        lock.readLock().lock();
731 +        Thread t = new Thread(new Runnable() {
732 +                public void run() {
733 +                    threadAssertTrue(lock.readLock().tryLock());
734 +                    lock.readLock().unlock();
735 +                }
736 +            });
737 +        try {
738 +            t.start();
739 +            t.join();
740 +            lock.readLock().unlock();
741 +        } catch(Exception e){
742 +            unexpectedException();
743 +        }
744 +    }
745 +
746 +    
747 +
748 +    /**
749 +     * Fair write tryLock fails when readlocked
750 +     */
751 +    public void testWriteTryLockWhenReadLockedFair() {
752 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
753 +        lock.readLock().lock();
754 +        Thread t = new Thread(new Runnable() {
755 +                public void run() {
756 +                    threadAssertFalse(lock.writeLock().tryLock());
757 +                }
758 +            });
759 +        try {
760 +            t.start();
761 +            t.join();
762 +            lock.readLock().unlock();
763 +        } catch(Exception e){
764 +            unexpectedException();
765 +        }
766 +    }
767 +
768      
769  
770      /**
# Line 593 | Line 838 | public class ReentrantReadWriteLockTest
838              });
839          try {
840              t.start();
841 +            Thread.sleep(SHORT_DELAY_MS);
842              t.interrupt();
843 +            Thread.sleep(SHORT_DELAY_MS);
844              t.join();
845              lock.writeLock().unlock();
846          } catch(Exception e){
# Line 623 | Line 870 | public class ReentrantReadWriteLockTest
870              });
871          try {
872              t.start();
873 +            Thread.sleep(SHORT_DELAY_MS);
874              t.interrupt();
875              t.join();
876              lock.writeLock().unlock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines