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.26 by dl, Fri Feb 24 00:03:16 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              lock.writeLock().unlock();
209              t.join();
# Line 215 | Line 250 | public class ReentrantReadWriteLockTest
250              });
251          try {
252              t.start();
253 +            Thread.sleep(SHORT_DELAY_MS);
254              t.interrupt();
255              lock.writeLock().unlock();
256              t.join();
# Line 389 | Line 425 | public class ReentrantReadWriteLockTest
425  
426      /**
427       * Read lock succeeds if write locked by current thread even if
428 <     * other threads are waiting
428 >     * other threads are waiting for readlock
429       */
430      public void testReadHoldingWriteLock2() {
431          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 427 | Line 463 | public class ReentrantReadWriteLockTest
463      }
464  
465      /**
466 +     *  Read lock succeeds if write locked by current thread even if
467 +     * other threads are waiting for writelock
468 +     */
469 +    public void testReadHoldingWriteLock3() {
470 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
471 +        lock.writeLock().lock();
472 +        Thread t1 = new Thread(new Runnable() {
473 +                public void run() {
474 +                    lock.writeLock().lock();
475 +                    lock.writeLock().unlock();
476 +                }
477 +            });
478 +        Thread t2 = new Thread(new Runnable() {
479 +                public void run() {
480 +                    lock.writeLock().lock();
481 +                    lock.writeLock().unlock();
482 +                }
483 +            });
484 +
485 +        try {
486 +            t1.start();
487 +            t2.start();
488 +            lock.readLock().lock();
489 +            lock.readLock().unlock();
490 +            Thread.sleep(SHORT_DELAY_MS);
491 +            lock.readLock().lock();
492 +            lock.readLock().unlock();
493 +            lock.writeLock().unlock();
494 +            t1.join(MEDIUM_DELAY_MS);
495 +            t2.join(MEDIUM_DELAY_MS);
496 +            assertTrue(!t1.isAlive());
497 +            assertTrue(!t2.isAlive());
498 +          
499 +        } catch(Exception e){
500 +            unexpectedException();
501 +        }
502 +    }
503 +
504 +
505 +    /**
506 +     *  Write lock succeeds if write locked by current thread even if
507 +     * other threads are waiting for writelock
508 +     */
509 +    public void testWriteHoldingWriteLock4() {
510 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
511 +        lock.writeLock().lock();
512 +        Thread t1 = new Thread(new Runnable() {
513 +                public void run() {
514 +                    lock.writeLock().lock();
515 +                    lock.writeLock().unlock();
516 +                }
517 +            });
518 +        Thread t2 = new Thread(new Runnable() {
519 +                public void run() {
520 +                    lock.writeLock().lock();
521 +                    lock.writeLock().unlock();
522 +                }
523 +            });
524 +
525 +        try {
526 +            t1.start();
527 +            t2.start();
528 +            lock.writeLock().lock();
529 +            lock.writeLock().unlock();
530 +            Thread.sleep(SHORT_DELAY_MS);
531 +            lock.writeLock().lock();
532 +            lock.writeLock().unlock();
533 +            lock.writeLock().unlock();
534 +            t1.join(MEDIUM_DELAY_MS);
535 +            t2.join(MEDIUM_DELAY_MS);
536 +            assertTrue(!t1.isAlive());
537 +            assertTrue(!t2.isAlive());
538 +          
539 +        } catch(Exception e){
540 +            unexpectedException();
541 +        }
542 +    }
543 +
544 +
545 +    /**
546       * Fair Read trylock succeeds if write locked by current thread
547       */
548      public void testReadHoldingWriteLockFair() {
# Line 439 | Line 555 | public class ReentrantReadWriteLockTest
555  
556      /**
557       * Fair Read lock succeeds if write locked by current thread even if
558 <     * other threads are waiting
558 >     * other threads are waiting for readlock
559       */
560      public void testReadHoldingWriteLockFair2() {
561          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
# Line 478 | Line 594 | public class ReentrantReadWriteLockTest
594  
595  
596      /**
597 +     * Fair Read lock succeeds if write locked by current thread even if
598 +     * other threads are waiting for writelock
599 +     */
600 +    public void testReadHoldingWriteLockFair3() {
601 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
602 +        lock.writeLock().lock();
603 +        Thread t1 = new Thread(new Runnable() {
604 +                public void run() {
605 +                    lock.writeLock().lock();
606 +                    lock.writeLock().unlock();
607 +                }
608 +            });
609 +        Thread t2 = new Thread(new Runnable() {
610 +                public void run() {
611 +                    lock.writeLock().lock();
612 +                    lock.writeLock().unlock();
613 +                }
614 +            });
615 +
616 +        try {
617 +            t1.start();
618 +            t2.start();
619 +            lock.readLock().lock();
620 +            lock.readLock().unlock();
621 +            Thread.sleep(SHORT_DELAY_MS);
622 +            lock.readLock().lock();
623 +            lock.readLock().unlock();
624 +            lock.writeLock().unlock();
625 +            t1.join(MEDIUM_DELAY_MS);
626 +            t2.join(MEDIUM_DELAY_MS);
627 +            assertTrue(!t1.isAlive());
628 +            assertTrue(!t2.isAlive());
629 +          
630 +        } catch(Exception e){
631 +            unexpectedException();
632 +        }
633 +    }
634 +
635 +
636 +    /**
637 +     * Fair Write lock succeeds if write locked by current thread even if
638 +     * other threads are waiting for writelock
639 +     */
640 +    public void testWriteHoldingWriteLockFair4() {
641 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
642 +        lock.writeLock().lock();
643 +        Thread t1 = new Thread(new Runnable() {
644 +                public void run() {
645 +                    lock.writeLock().lock();
646 +                    lock.writeLock().unlock();
647 +                }
648 +            });
649 +        Thread t2 = new Thread(new Runnable() {
650 +                public void run() {
651 +                    lock.writeLock().lock();
652 +                    lock.writeLock().unlock();
653 +                }
654 +            });
655 +
656 +        try {
657 +            t1.start();
658 +            t2.start();
659 +            Thread.sleep(SHORT_DELAY_MS);
660 +            assertTrue(lock.isWriteLockedByCurrentThread());
661 +            assertTrue(lock.getWriteHoldCount() == 1);
662 +            lock.writeLock().lock();
663 +            assertTrue(lock.getWriteHoldCount() == 2);
664 +            lock.writeLock().unlock();
665 +            lock.writeLock().lock();
666 +            lock.writeLock().unlock();
667 +            lock.writeLock().unlock();
668 +            t1.join(MEDIUM_DELAY_MS);
669 +            t2.join(MEDIUM_DELAY_MS);
670 +            assertTrue(!t1.isAlive());
671 +            assertTrue(!t2.isAlive());
672 +          
673 +        } catch(Exception e){
674 +            unexpectedException();
675 +        }
676 +    }
677 +
678 +
679 +    /**
680       * Read tryLock succeeds if readlocked but not writelocked
681       */
682      public void testTryLockWhenReadLocked() {
# Line 520 | Line 719 | public class ReentrantReadWriteLockTest
719          }
720      }
721  
722 +
723 +    /**
724 +     * Fair Read tryLock succeeds if readlocked but not writelocked
725 +     */
726 +    public void testTryLockWhenReadLockedFair() {
727 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
728 +        lock.readLock().lock();
729 +        Thread t = new Thread(new Runnable() {
730 +                public void run() {
731 +                    threadAssertTrue(lock.readLock().tryLock());
732 +                    lock.readLock().unlock();
733 +                }
734 +            });
735 +        try {
736 +            t.start();
737 +            t.join();
738 +            lock.readLock().unlock();
739 +        } catch(Exception e){
740 +            unexpectedException();
741 +        }
742 +    }
743 +
744 +    
745 +
746 +    /**
747 +     * Fair write tryLock fails when readlocked
748 +     */
749 +    public void testWriteTryLockWhenReadLockedFair() {
750 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
751 +        lock.readLock().lock();
752 +        Thread t = new Thread(new Runnable() {
753 +                public void run() {
754 +                    threadAssertFalse(lock.writeLock().tryLock());
755 +                }
756 +            });
757 +        try {
758 +            t.start();
759 +            t.join();
760 +            lock.readLock().unlock();
761 +        } catch(Exception e){
762 +            unexpectedException();
763 +        }
764 +    }
765 +
766      
767  
768      /**
# Line 593 | Line 836 | public class ReentrantReadWriteLockTest
836              });
837          try {
838              t.start();
839 +            Thread.sleep(SHORT_DELAY_MS);
840              t.interrupt();
841              t.join();
842              lock.writeLock().unlock();
# Line 623 | Line 867 | public class ReentrantReadWriteLockTest
867              });
868          try {
869              t.start();
870 +            Thread.sleep(SHORT_DELAY_MS);
871              t.interrupt();
872              t.join();
873              lock.writeLock().unlock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines