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.25 by dl, Sun Jan 29 21:17:38 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 376 | Line 410 | public class ReentrantReadWriteLockTest
410          }
411      }
412  
413 +    /**
414 +     * Read trylock succeeds if write locked by current thread
415 +     */
416 +    public void testReadHoldingWriteLock() {
417 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
418 +        lock.writeLock().lock();
419 +        assertTrue(lock.readLock().tryLock());
420 +        lock.readLock().unlock();
421 +        lock.writeLock().unlock();
422 +    }
423 +
424 +    /**
425 +     * Read lock succeeds if write locked by current thread even if
426 +     * other threads are waiting for readlock
427 +     */
428 +    public void testReadHoldingWriteLock2() {
429 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
430 +        lock.writeLock().lock();
431 +        Thread t1 = new Thread(new Runnable() {
432 +                public void run() {
433 +                    lock.readLock().lock();
434 +                    lock.readLock().unlock();
435 +                }
436 +            });
437 +        Thread t2 = new Thread(new Runnable() {
438 +                public void run() {
439 +                    lock.readLock().lock();
440 +                    lock.readLock().unlock();
441 +                }
442 +            });
443 +
444 +        try {
445 +            t1.start();
446 +            t2.start();
447 +            lock.readLock().lock();
448 +            lock.readLock().unlock();
449 +            Thread.sleep(SHORT_DELAY_MS);
450 +            lock.readLock().lock();
451 +            lock.readLock().unlock();
452 +            lock.writeLock().unlock();
453 +            t1.join(MEDIUM_DELAY_MS);
454 +            t2.join(MEDIUM_DELAY_MS);
455 +            assertTrue(!t1.isAlive());
456 +            assertTrue(!t2.isAlive());
457 +          
458 +        } catch(Exception e){
459 +            unexpectedException();
460 +        }
461 +    }
462 +
463 +    /**
464 +     *  Read lock succeeds if write locked by current thread even if
465 +     * other threads are waiting for writelock
466 +     */
467 +    public void testReadHoldingWriteLock3() {
468 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
469 +        lock.writeLock().lock();
470 +        Thread t1 = new Thread(new Runnable() {
471 +                public void run() {
472 +                    lock.writeLock().lock();
473 +                    lock.writeLock().unlock();
474 +                }
475 +            });
476 +        Thread t2 = new Thread(new Runnable() {
477 +                public void run() {
478 +                    lock.writeLock().lock();
479 +                    lock.writeLock().unlock();
480 +                }
481 +            });
482 +
483 +        try {
484 +            t1.start();
485 +            t2.start();
486 +            lock.readLock().lock();
487 +            lock.readLock().unlock();
488 +            Thread.sleep(SHORT_DELAY_MS);
489 +            lock.readLock().lock();
490 +            lock.readLock().unlock();
491 +            lock.writeLock().unlock();
492 +            t1.join(MEDIUM_DELAY_MS);
493 +            t2.join(MEDIUM_DELAY_MS);
494 +            assertTrue(!t1.isAlive());
495 +            assertTrue(!t2.isAlive());
496 +          
497 +        } catch(Exception e){
498 +            unexpectedException();
499 +        }
500 +    }
501 +
502 +
503 +    /**
504 +     *  Write lock succeeds if write locked by current thread even if
505 +     * other threads are waiting for writelock
506 +     */
507 +    public void testWriteHoldingWriteLock4() {
508 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
509 +        lock.writeLock().lock();
510 +        Thread t1 = new Thread(new Runnable() {
511 +                public void run() {
512 +                    lock.writeLock().lock();
513 +                    lock.writeLock().unlock();
514 +                }
515 +            });
516 +        Thread t2 = new Thread(new Runnable() {
517 +                public void run() {
518 +                    lock.writeLock().lock();
519 +                    lock.writeLock().unlock();
520 +                }
521 +            });
522 +
523 +        try {
524 +            t1.start();
525 +            t2.start();
526 +            lock.writeLock().lock();
527 +            lock.writeLock().unlock();
528 +            Thread.sleep(SHORT_DELAY_MS);
529 +            lock.writeLock().lock();
530 +            lock.writeLock().unlock();
531 +            lock.writeLock().unlock();
532 +            t1.join(MEDIUM_DELAY_MS);
533 +            t2.join(MEDIUM_DELAY_MS);
534 +            assertTrue(!t1.isAlive());
535 +            assertTrue(!t2.isAlive());
536 +          
537 +        } catch(Exception e){
538 +            unexpectedException();
539 +        }
540 +    }
541 +
542 +
543 +    /**
544 +     * Fair Read trylock succeeds if write locked by current thread
545 +     */
546 +    public void testReadHoldingWriteLockFair() {
547 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
548 +        lock.writeLock().lock();
549 +        assertTrue(lock.readLock().tryLock());
550 +        lock.readLock().unlock();
551 +        lock.writeLock().unlock();
552 +    }
553 +
554 +    /**
555 +     * Fair Read lock succeeds if write locked by current thread even if
556 +     * other threads are waiting for readlock
557 +     */
558 +    public void testReadHoldingWriteLockFair2() {
559 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
560 +        lock.writeLock().lock();
561 +        Thread t1 = new Thread(new Runnable() {
562 +                public void run() {
563 +                    lock.readLock().lock();
564 +                    lock.readLock().unlock();
565 +                }
566 +            });
567 +        Thread t2 = new Thread(new Runnable() {
568 +                public void run() {
569 +                    lock.readLock().lock();
570 +                    lock.readLock().unlock();
571 +                }
572 +            });
573 +
574 +        try {
575 +            t1.start();
576 +            t2.start();
577 +            lock.readLock().lock();
578 +            lock.readLock().unlock();
579 +            Thread.sleep(SHORT_DELAY_MS);
580 +            lock.readLock().lock();
581 +            lock.readLock().unlock();
582 +            lock.writeLock().unlock();
583 +            t1.join(MEDIUM_DELAY_MS);
584 +            t2.join(MEDIUM_DELAY_MS);
585 +            assertTrue(!t1.isAlive());
586 +            assertTrue(!t2.isAlive());
587 +          
588 +        } catch(Exception e){
589 +            unexpectedException();
590 +        }
591 +    }
592 +
593 +
594 +    /**
595 +     * Fair Read lock succeeds if write locked by current thread even if
596 +     * other threads are waiting for writelock
597 +     */
598 +    public void testReadHoldingWriteLockFair3() {
599 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
600 +        lock.writeLock().lock();
601 +        Thread t1 = new Thread(new Runnable() {
602 +                public void run() {
603 +                    lock.writeLock().lock();
604 +                    lock.writeLock().unlock();
605 +                }
606 +            });
607 +        Thread t2 = new Thread(new Runnable() {
608 +                public void run() {
609 +                    lock.writeLock().lock();
610 +                    lock.writeLock().unlock();
611 +                }
612 +            });
613 +
614 +        try {
615 +            t1.start();
616 +            t2.start();
617 +            lock.readLock().lock();
618 +            lock.readLock().unlock();
619 +            Thread.sleep(SHORT_DELAY_MS);
620 +            lock.readLock().lock();
621 +            lock.readLock().unlock();
622 +            lock.writeLock().unlock();
623 +            t1.join(MEDIUM_DELAY_MS);
624 +            t2.join(MEDIUM_DELAY_MS);
625 +            assertTrue(!t1.isAlive());
626 +            assertTrue(!t2.isAlive());
627 +          
628 +        } catch(Exception e){
629 +            unexpectedException();
630 +        }
631 +    }
632 +
633 +
634 +    /**
635 +     * Fair Write lock succeeds if write locked by current thread even if
636 +     * other threads are waiting for writelock
637 +     */
638 +    public void testWriteHoldingWriteLockFair4() {
639 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
640 +        lock.writeLock().lock();
641 +        Thread t1 = new Thread(new Runnable() {
642 +                public void run() {
643 +                    lock.writeLock().lock();
644 +                    lock.writeLock().unlock();
645 +                }
646 +            });
647 +        Thread t2 = new Thread(new Runnable() {
648 +                public void run() {
649 +                    lock.writeLock().lock();
650 +                    lock.writeLock().unlock();
651 +                }
652 +            });
653 +
654 +        try {
655 +            t1.start();
656 +            t2.start();
657 +            Thread.sleep(SHORT_DELAY_MS);
658 +            assertTrue(lock.isWriteLockedByCurrentThread());
659 +            assertTrue(lock.getWriteHoldCount() == 1);
660 +            lock.writeLock().lock();
661 +            assertTrue(lock.getWriteHoldCount() == 2);
662 +            lock.writeLock().unlock();
663 +            lock.writeLock().lock();
664 +            lock.writeLock().unlock();
665 +            lock.writeLock().unlock();
666 +            t1.join(MEDIUM_DELAY_MS);
667 +            t2.join(MEDIUM_DELAY_MS);
668 +            assertTrue(!t1.isAlive());
669 +            assertTrue(!t2.isAlive());
670 +          
671 +        } catch(Exception e){
672 +            unexpectedException();
673 +        }
674 +    }
675 +
676  
677      /**
678       * Read tryLock succeeds if readlocked but not writelocked
# Line 420 | Line 717 | public class ReentrantReadWriteLockTest
717          }
718      }
719  
720 +
721 +    /**
722 +     * Fair Read tryLock succeeds if readlocked but not writelocked
723 +     */
724 +    public void testTryLockWhenReadLockedFair() {
725 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
726 +        lock.readLock().lock();
727 +        Thread t = new Thread(new Runnable() {
728 +                public void run() {
729 +                    threadAssertTrue(lock.readLock().tryLock());
730 +                    lock.readLock().unlock();
731 +                }
732 +            });
733 +        try {
734 +            t.start();
735 +            t.join();
736 +            lock.readLock().unlock();
737 +        } catch(Exception e){
738 +            unexpectedException();
739 +        }
740 +    }
741 +
742 +    
743 +
744 +    /**
745 +     * Fair write tryLock fails when readlocked
746 +     */
747 +    public void testWriteTryLockWhenReadLockedFair() {
748 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
749 +        lock.readLock().lock();
750 +        Thread t = new Thread(new Runnable() {
751 +                public void run() {
752 +                    threadAssertFalse(lock.writeLock().tryLock());
753 +                }
754 +            });
755 +        try {
756 +            t.start();
757 +            t.join();
758 +            lock.readLock().unlock();
759 +        } catch(Exception e){
760 +            unexpectedException();
761 +        }
762 +    }
763 +
764      
765  
766      /**
# Line 591 | Line 932 | public class ReentrantReadWriteLockTest
932          final Condition c = lock.writeLock().newCondition();
933          try {
934              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
935              lock.writeLock().unlock();
936          }
937          catch (Exception ex) {
# Line 608 | Line 948 | public class ReentrantReadWriteLockTest
948          try {
949              lock.writeLock().lock();
950              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
951              lock.writeLock().unlock();
952          }
953          catch (Exception ex) {
# Line 649 | Line 988 | public class ReentrantReadWriteLockTest
988          }
989      }
990  
991 +    /** A helper class for uninterruptible wait tests */
992 +    class UninterruptableThread extends Thread {
993 +        private Lock lock;
994 +        private Condition c;
995 +        
996 +        public volatile boolean canAwake = false;
997 +        public volatile boolean interrupted = false;
998 +        public volatile boolean lockStarted = false;
999 +        
1000 +        public UninterruptableThread(Lock lock, Condition c) {
1001 +            this.lock = lock;
1002 +            this.c = c;
1003 +        }
1004 +        
1005 +        public synchronized void run() {
1006 +            lock.lock();
1007 +            lockStarted = true;
1008 +            
1009 +            while (!canAwake) {
1010 +                c.awaitUninterruptibly();
1011 +            }
1012 +            
1013 +            interrupted = isInterrupted();
1014 +            lock.unlock();
1015 +        }
1016 +    }
1017 +
1018      /**
1019       * awaitUninterruptibly doesn't abort on interrupt
1020       */
1021      public void testAwaitUninterruptibly() {
1022 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1022 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1023          final Condition c = lock.writeLock().newCondition();
1024 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    lock.writeLock().lock();
661 <                    c.awaitUninterruptibly();
662 <                    lock.writeLock().unlock();
663 <                }
664 <            });
1024 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1025  
1026          try {
1027 <            t.start();
1028 <            Thread.sleep(SHORT_DELAY_MS);
1029 <            t.interrupt();
1027 >            thread.start();
1028 >
1029 >            while (!thread.lockStarted) {
1030 >                Thread.sleep(100);
1031 >            }
1032 >
1033              lock.writeLock().lock();
1034 <            c.signal();
1035 <            lock.writeLock().unlock();
1036 <            assert(t.isInterrupted());
1037 <            t.join(SHORT_DELAY_MS);
1038 <            assertFalse(t.isAlive());
1039 <        }
1040 <        catch (Exception ex) {
1034 >            try {
1035 >                thread.interrupt();
1036 >                thread.canAwake = true;
1037 >                c.signal();
1038 >            } finally {
1039 >                lock.writeLock().unlock();
1040 >            }
1041 >
1042 >            thread.join();
1043 >            assertTrue(thread.interrupted);
1044 >            assertFalse(thread.isAlive());
1045 >        } catch (Exception ex) {
1046              unexpectedException();
1047          }
1048      }
# Line 877 | Line 1245 | public class ReentrantReadWriteLockTest
1245      }
1246  
1247      /**
1248 +     * hasQueuedThread(null) throws NPE
1249 +     */
1250 +    public void testHasQueuedThreadNPE() {
1251 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1252 +        try {
1253 +            sync.hasQueuedThread(null);
1254 +            shouldThrow();
1255 +        } catch (NullPointerException success) {
1256 +        }
1257 +    }
1258 +
1259 +    /**
1260 +     * hasQueuedThread reports whether a thread is queued.
1261 +     */
1262 +    public void testHasQueuedThread() {
1263 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1264 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1265 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1266 +        try {
1267 +            assertFalse(sync.hasQueuedThread(t1));
1268 +            assertFalse(sync.hasQueuedThread(t2));
1269 +            sync.writeLock().lock();
1270 +            t1.start();
1271 +            Thread.sleep(SHORT_DELAY_MS);
1272 +            assertTrue(sync.hasQueuedThread(t1));
1273 +            t2.start();
1274 +            Thread.sleep(SHORT_DELAY_MS);
1275 +            assertTrue(sync.hasQueuedThread(t1));
1276 +            assertTrue(sync.hasQueuedThread(t2));
1277 +            t1.interrupt();
1278 +            Thread.sleep(SHORT_DELAY_MS);
1279 +            assertFalse(sync.hasQueuedThread(t1));
1280 +            assertTrue(sync.hasQueuedThread(t2));
1281 +            sync.writeLock().unlock();
1282 +            Thread.sleep(SHORT_DELAY_MS);
1283 +            assertFalse(sync.hasQueuedThread(t1));
1284 +            Thread.sleep(SHORT_DELAY_MS);
1285 +            assertFalse(sync.hasQueuedThread(t2));
1286 +            t1.join();
1287 +            t2.join();
1288 +        } catch(Exception e){
1289 +            unexpectedException();
1290 +        }
1291 +    }
1292 +
1293 +
1294 +    /**
1295       * getQueueLength reports number of waiting threads
1296       */
1297      public void testGetQueueLength() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines