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.18 by dl, Sat Jan 10 01:41:59 2004 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 376 | Line 412 | public class ReentrantReadWriteLockTest
412          }
413      }
414  
415 +    /**
416 +     * Read trylock succeeds if write locked by current thread
417 +     */
418 +    public void testReadHoldingWriteLock() {
419 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
420 +        lock.writeLock().lock();
421 +        assertTrue(lock.readLock().tryLock());
422 +        lock.readLock().unlock();
423 +        lock.writeLock().unlock();
424 +    }
425 +
426 +    /**
427 +     * Read lock succeeds if write locked by current thread even if
428 +     * other threads are waiting for readlock
429 +     */
430 +    public void testReadHoldingWriteLock2() {
431 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
432 +        lock.writeLock().lock();
433 +        Thread t1 = new Thread(new Runnable() {
434 +                public void run() {
435 +                    lock.readLock().lock();
436 +                    lock.readLock().unlock();
437 +                }
438 +            });
439 +        Thread t2 = new Thread(new Runnable() {
440 +                public void run() {
441 +                    lock.readLock().lock();
442 +                    lock.readLock().unlock();
443 +                }
444 +            });
445 +
446 +        try {
447 +            t1.start();
448 +            t2.start();
449 +            lock.readLock().lock();
450 +            lock.readLock().unlock();
451 +            Thread.sleep(SHORT_DELAY_MS);
452 +            lock.readLock().lock();
453 +            lock.readLock().unlock();
454 +            lock.writeLock().unlock();
455 +            t1.join(MEDIUM_DELAY_MS);
456 +            t2.join(MEDIUM_DELAY_MS);
457 +            assertTrue(!t1.isAlive());
458 +            assertTrue(!t2.isAlive());
459 +          
460 +        } catch(Exception e){
461 +            unexpectedException();
462 +        }
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() {
549 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
550 +        lock.writeLock().lock();
551 +        assertTrue(lock.readLock().tryLock());
552 +        lock.readLock().unlock();
553 +        lock.writeLock().unlock();
554 +    }
555 +
556 +    /**
557 +     * Fair Read lock succeeds if write locked by current thread even if
558 +     * other threads are waiting for readlock
559 +     */
560 +    public void testReadHoldingWriteLockFair2() {
561 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
562 +        lock.writeLock().lock();
563 +        Thread t1 = new Thread(new Runnable() {
564 +                public void run() {
565 +                    lock.readLock().lock();
566 +                    lock.readLock().unlock();
567 +                }
568 +            });
569 +        Thread t2 = new Thread(new Runnable() {
570 +                public void run() {
571 +                    lock.readLock().lock();
572 +                    lock.readLock().unlock();
573 +                }
574 +            });
575 +
576 +        try {
577 +            t1.start();
578 +            t2.start();
579 +            lock.readLock().lock();
580 +            lock.readLock().unlock();
581 +            Thread.sleep(SHORT_DELAY_MS);
582 +            lock.readLock().lock();
583 +            lock.readLock().unlock();
584 +            lock.writeLock().unlock();
585 +            t1.join(MEDIUM_DELAY_MS);
586 +            t2.join(MEDIUM_DELAY_MS);
587 +            assertTrue(!t1.isAlive());
588 +            assertTrue(!t2.isAlive());
589 +          
590 +        } catch(Exception e){
591 +            unexpectedException();
592 +        }
593 +    }
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
# Line 420 | 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 493 | 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 523 | 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();
# Line 591 | Line 936 | public class ReentrantReadWriteLockTest
936          final Condition c = lock.writeLock().newCondition();
937          try {
938              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
939              lock.writeLock().unlock();
940          }
941          catch (Exception ex) {
# Line 608 | Line 952 | public class ReentrantReadWriteLockTest
952          try {
953              lock.writeLock().lock();
954              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
955              lock.writeLock().unlock();
956          }
957          catch (Exception ex) {
# Line 649 | Line 992 | public class ReentrantReadWriteLockTest
992          }
993      }
994  
995 +    /** A helper class for uninterruptible wait tests */
996 +    class UninterruptableThread extends Thread {
997 +        private Lock lock;
998 +        private Condition c;
999 +        
1000 +        public volatile boolean canAwake = false;
1001 +        public volatile boolean interrupted = false;
1002 +        public volatile boolean lockStarted = false;
1003 +        
1004 +        public UninterruptableThread(Lock lock, Condition c) {
1005 +            this.lock = lock;
1006 +            this.c = c;
1007 +        }
1008 +        
1009 +        public synchronized void run() {
1010 +            lock.lock();
1011 +            lockStarted = true;
1012 +            
1013 +            while (!canAwake) {
1014 +                c.awaitUninterruptibly();
1015 +            }
1016 +            
1017 +            interrupted = isInterrupted();
1018 +            lock.unlock();
1019 +        }
1020 +    }
1021 +
1022      /**
1023       * awaitUninterruptibly doesn't abort on interrupt
1024       */
1025      public void testAwaitUninterruptibly() {
1026 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1026 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1027          final Condition c = lock.writeLock().newCondition();
1028 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    lock.writeLock().lock();
661 <                    c.awaitUninterruptibly();
662 <                    lock.writeLock().unlock();
663 <                }
664 <            });
1028 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1029  
1030          try {
1031 <            t.start();
1032 <            Thread.sleep(SHORT_DELAY_MS);
1033 <            t.interrupt();
1031 >            thread.start();
1032 >
1033 >            while (!thread.lockStarted) {
1034 >                Thread.sleep(100);
1035 >            }
1036 >
1037              lock.writeLock().lock();
1038 <            c.signal();
1039 <            lock.writeLock().unlock();
1040 <            assert(t.isInterrupted());
1041 <            t.join(SHORT_DELAY_MS);
1042 <            assertFalse(t.isAlive());
1043 <        }
1044 <        catch (Exception ex) {
1038 >            try {
1039 >                thread.interrupt();
1040 >                thread.canAwake = true;
1041 >                c.signal();
1042 >            } finally {
1043 >                lock.writeLock().unlock();
1044 >            }
1045 >
1046 >            thread.join();
1047 >            assertTrue(thread.interrupted);
1048 >            assertFalse(thread.isAlive());
1049 >        } catch (Exception ex) {
1050              unexpectedException();
1051          }
1052      }
# Line 877 | Line 1249 | public class ReentrantReadWriteLockTest
1249      }
1250  
1251      /**
1252 +     * hasQueuedThread(null) throws NPE
1253 +     */
1254 +    public void testHasQueuedThreadNPE() {
1255 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1256 +        try {
1257 +            sync.hasQueuedThread(null);
1258 +            shouldThrow();
1259 +        } catch (NullPointerException success) {
1260 +        }
1261 +    }
1262 +
1263 +    /**
1264 +     * hasQueuedThread reports whether a thread is queued.
1265 +     */
1266 +    public void testHasQueuedThread() {
1267 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1268 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1269 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1270 +        try {
1271 +            assertFalse(sync.hasQueuedThread(t1));
1272 +            assertFalse(sync.hasQueuedThread(t2));
1273 +            sync.writeLock().lock();
1274 +            t1.start();
1275 +            Thread.sleep(SHORT_DELAY_MS);
1276 +            assertTrue(sync.hasQueuedThread(t1));
1277 +            t2.start();
1278 +            Thread.sleep(SHORT_DELAY_MS);
1279 +            assertTrue(sync.hasQueuedThread(t1));
1280 +            assertTrue(sync.hasQueuedThread(t2));
1281 +            t1.interrupt();
1282 +            Thread.sleep(SHORT_DELAY_MS);
1283 +            assertFalse(sync.hasQueuedThread(t1));
1284 +            assertTrue(sync.hasQueuedThread(t2));
1285 +            sync.writeLock().unlock();
1286 +            Thread.sleep(SHORT_DELAY_MS);
1287 +            assertFalse(sync.hasQueuedThread(t1));
1288 +            Thread.sleep(SHORT_DELAY_MS);
1289 +            assertFalse(sync.hasQueuedThread(t2));
1290 +            t1.join();
1291 +            t2.join();
1292 +        } catch(Exception e){
1293 +            unexpectedException();
1294 +        }
1295 +    }
1296 +
1297 +
1298 +    /**
1299       * getQueueLength reports number of waiting threads
1300       */
1301      public void testGetQueueLength() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines