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.54 by jsr166, Mon May 2 01:07:15 2011 UTC vs.
Revision 1.57 by dl, Sat May 7 11:15:04 2011 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 + import java.util.concurrent.atomic.AtomicBoolean;
11   import java.util.concurrent.locks.*;
12   import java.util.concurrent.*;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 32 | Line 33 | public class ReentrantReadWriteLockTest
33          }
34      }
35  
35
36      /**
37       * A runnable calling lockInterruptibly that expects to be
38       * interrupted
# Line 69 | Line 69 | public class ReentrantReadWriteLockTest
69      }
70  
71      /**
72 +     * Spin-waits until lock.hasQueuedThread(t) becomes true.
73 +     */
74 +    void waitForQueuedThread(ReentrantReadWriteLock lock, Thread t) {
75 +        long startTime = System.nanoTime();
76 +        while (!lock.hasQueuedThread(t)) {
77 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
78 +                throw new AssertionError("timed out");
79 +            Thread.yield();
80 +        }
81 +    }
82 +
83 +    /**
84 +     * Checks that lock is not write-locked.
85 +     */
86 +    void assertNotWriteLocked(ReentrantReadWriteLock lock) {
87 +        assertFalse(lock.isWriteLocked());
88 +        assertFalse(lock.isWriteLockedByCurrentThread());
89 +        assertEquals(0, lock.getWriteHoldCount());
90 +    }
91 +
92 +    /**
93 +     * Checks that condition c has no waiters.
94 +     */
95 +    void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
96 +        assertHasWaiters(lock, c, new Thread[] {});
97 +    }
98 +
99 +    /**
100 +     * Checks that condition c has exactly the given waiter threads.
101 +     */
102 +    void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
103 +                          Thread... threads) {
104 +        lock.writeLock().lock();
105 +        assertEquals(threads.length > 0, lock.hasWaiters(c));
106 +        assertEquals(threads.length, lock.getWaitQueueLength(c));
107 +        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
108 +        assertEquals(threads.length, lock.getWaitingThreads(c).size());
109 +        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
110 +                     new HashSet<Thread>(Arrays.asList(threads)));
111 +        lock.writeLock().unlock();
112 +    }
113 +
114 +    /**
115       * Constructor sets given fairness, and is in unlocked state
116       */
117      public void testConstructor() {
# Line 111 | Line 154 | public class ReentrantReadWriteLockTest
154          assertEquals(0, rl.getReadLockCount());
155      }
156  
114
157      /**
158       * locking an unlocked fair lock succeeds
159       */
# Line 182 | Line 224 | public class ReentrantReadWriteLockTest
224          }
225      }
226  
185
227      /**
228       * write-unlocking an unlocked lock throws IllegalMonitorStateException
229       */
230 <    public void testUnlock_IllegalMonitorStateException() {
230 >    public void testWriteUnlock_IllegalMonitorStateException() {
231          ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
232          try {
233              rl.writeLock().unlock();
# Line 194 | Line 235 | public class ReentrantReadWriteLockTest
235          } catch (IllegalMonitorStateException success) {}
236      }
237  
238 +    /**
239 +     * read-unlocking an unlocked lock throws IllegalMonitorStateException
240 +     */
241 +    public void testReadUnlock_IllegalMonitorStateException() {
242 +        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
243 +        try {
244 +            rl.readLock().unlock();
245 +            shouldThrow();
246 +        } catch (IllegalMonitorStateException success) {}
247 +    }
248  
249      /**
250       * write-lockInterruptibly is interruptible
# Line 206 | Line 257 | public class ReentrantReadWriteLockTest
257                  lock.writeLock().lockInterruptibly();
258              }});
259  
260 <        Thread.sleep(SHORT_DELAY_MS);
260 >        waitForQueuedThread(lock, t);
261          t.interrupt();
262 <        awaitTermination(t, LONG_DELAY_MS);
262 >        awaitTermination(t);
263          releaseWriteLock(lock);
264      }
265  
# Line 220 | Line 271 | public class ReentrantReadWriteLockTest
271          lock.writeLock().lock();
272          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
273              public void realRun() throws InterruptedException {
274 <                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
274 >                lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
275              }});
276  
277 <        Thread.sleep(SHORT_DELAY_MS);
277 >        waitForQueuedThread(lock, t);
278          t.interrupt();
279 <        awaitTermination(t, LONG_DELAY_MS);
279 >        awaitTermination(t);
280          releaseWriteLock(lock);
281      }
282  
# Line 240 | Line 291 | public class ReentrantReadWriteLockTest
291                  lock.readLock().lockInterruptibly();
292              }});
293  
294 <        Thread.sleep(SHORT_DELAY_MS);
294 >        waitForQueuedThread(lock, t);
295          t.interrupt();
296 <        awaitTermination(t, LONG_DELAY_MS);
296 >        awaitTermination(t);
297          releaseWriteLock(lock);
298      }
299  
# Line 257 | Line 308 | public class ReentrantReadWriteLockTest
308                  lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
309              }});
310  
311 <        Thread.sleep(SHORT_DELAY_MS);
311 >        waitForQueuedThread(lock, t);
312          t.interrupt();
313 <        awaitTermination(t, LONG_DELAY_MS);
313 >        awaitTermination(t);
314          releaseWriteLock(lock);
315      }
316  
266
317      /**
318       * write-tryLock fails if locked
319       */
# Line 275 | Line 325 | public class ReentrantReadWriteLockTest
325                  assertFalse(lock.writeLock().tryLock());
326              }});
327  
328 <        awaitTermination(t, LONG_DELAY_MS);
328 >        awaitTermination(t);
329          releaseWriteLock(lock);
330      }
331  
# Line 290 | Line 340 | public class ReentrantReadWriteLockTest
340                  assertFalse(lock.readLock().tryLock());
341              }});
342  
343 <        awaitTermination(t, LONG_DELAY_MS);
343 >        awaitTermination(t);
344          releaseWriteLock(lock);
345      }
346  
# Line 306 | Line 356 | public class ReentrantReadWriteLockTest
356                  lock.readLock().unlock();
357              }});
358  
359 <        awaitTermination(t, LONG_DELAY_MS);
359 >        awaitTermination(t);
360          lock.readLock().unlock();
361      }
362  
363      /**
364 <     * A writelock succeeds after reading threads unlock
364 >     * A writelock succeeds only after reading threads unlock
365       */
366      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
367          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 326 | Line 376 | public class ReentrantReadWriteLockTest
376                  lock.writeLock().lock();
377                  lock.writeLock().unlock();
378              }});
379 <
330 <        Thread.sleep(SHORT_DELAY_MS);
379 >        delay(SHORT_DELAY_MS);
380          lock.readLock().unlock();
381 <        awaitTermination(t1, LONG_DELAY_MS);
382 <        awaitTermination(t2, LONG_DELAY_MS);
381 >        awaitTermination(t1);
382 >        awaitTermination(t2);
383 >        assertNotWriteLocked(lock);
384      }
385  
386      /**
387 <     * Readlocks succeed after a writing thread unlocks
387 >     * Readlocks succeed only after a writing thread unlocks
388       */
389      public void testReadAfterWriteLock() throws InterruptedException {
390          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 350 | Line 400 | public class ReentrantReadWriteLockTest
400                  lock.readLock().unlock();
401              }});
402  
403 <        Thread.sleep(SHORT_DELAY_MS);
403 >        waitForQueuedThread(lock, t1);
404 >        waitForQueuedThread(lock, t2);
405          releaseWriteLock(lock);
406 <        awaitTermination(t1, LONG_DELAY_MS);
407 <        awaitTermination(t2, LONG_DELAY_MS);
406 >        awaitTermination(t1);
407 >        awaitTermination(t2);
408      }
409  
410      /**
# Line 374 | Line 425 | public class ReentrantReadWriteLockTest
425      public void testReadHoldingWriteLock2() throws InterruptedException {
426          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
427          lock.writeLock().lock();
428 +        lock.readLock().lock();
429 +        lock.readLock().unlock();
430 +
431          Thread t1 = newStartedThread(new CheckedRunnable() {
432              public void realRun() {
433                  lock.readLock().lock();
# Line 385 | Line 439 | public class ReentrantReadWriteLockTest
439                  lock.readLock().unlock();
440              }});
441  
442 +        waitForQueuedThread(lock, t1);
443 +        waitForQueuedThread(lock, t2);
444 +        assertTrue(lock.isWriteLockedByCurrentThread());
445          lock.readLock().lock();
446          lock.readLock().unlock();
447 <        Thread.sleep(SHORT_DELAY_MS);
448 <        lock.readLock().lock();
449 <        lock.readLock().unlock();
393 <        lock.writeLock().unlock();
394 <        awaitTermination(t1, LONG_DELAY_MS);
395 <        awaitTermination(t2, LONG_DELAY_MS);
447 >        releaseWriteLock(lock);
448 >        awaitTermination(t1);
449 >        awaitTermination(t2);
450      }
451  
452      /**
# Line 402 | Line 456 | public class ReentrantReadWriteLockTest
456      public void testReadHoldingWriteLock3() throws InterruptedException {
457          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
458          lock.writeLock().lock();
459 +        lock.readLock().lock();
460 +        lock.readLock().unlock();
461 +
462          Thread t1 = newStartedThread(new CheckedRunnable() {
463              public void realRun() {
464                  lock.writeLock().lock();
# Line 413 | Line 470 | public class ReentrantReadWriteLockTest
470                  lock.writeLock().unlock();
471              }});
472  
473 +        waitForQueuedThread(lock, t1);
474 +        waitForQueuedThread(lock, t2);
475 +        assertTrue(lock.isWriteLockedByCurrentThread());
476          lock.readLock().lock();
477          lock.readLock().unlock();
478 <        Thread.sleep(SHORT_DELAY_MS);
479 <        lock.readLock().lock();
480 <        lock.readLock().unlock();
421 <        lock.writeLock().unlock();
422 <        awaitTermination(t1, LONG_DELAY_MS);
423 <        awaitTermination(t2, LONG_DELAY_MS);
478 >        releaseWriteLock(lock);
479 >        awaitTermination(t1);
480 >        awaitTermination(t2);
481      }
482  
426
483      /**
484       * Write lock succeeds if write locked by current thread even if
485       * other threads are waiting for writelock
# Line 431 | Line 487 | public class ReentrantReadWriteLockTest
487      public void testWriteHoldingWriteLock4() throws InterruptedException {
488          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
489          lock.writeLock().lock();
490 +        lock.writeLock().lock();
491 +        lock.writeLock().unlock();
492 +
493          Thread t1 = newStartedThread(new CheckedRunnable() {
494              public void realRun() {
495                  lock.writeLock().lock();
# Line 442 | Line 501 | public class ReentrantReadWriteLockTest
501                  lock.writeLock().unlock();
502              }});
503  
504 +        waitForQueuedThread(lock, t1);
505 +        waitForQueuedThread(lock, t2);
506 +        assertTrue(lock.isWriteLockedByCurrentThread());
507 +        assertEquals(1, lock.getWriteHoldCount());
508          lock.writeLock().lock();
509 +        assertTrue(lock.isWriteLockedByCurrentThread());
510 +        assertEquals(2, lock.getWriteHoldCount());
511          lock.writeLock().unlock();
512 <        Thread.sleep(SHORT_DELAY_MS);
513 <        lock.writeLock().lock();
514 <        lock.writeLock().unlock();
450 <        lock.writeLock().unlock();
451 <        awaitTermination(t1, LONG_DELAY_MS);
452 <        awaitTermination(t2, LONG_DELAY_MS);
512 >        releaseWriteLock(lock);
513 >        awaitTermination(t1);
514 >        awaitTermination(t2);
515      }
516  
455
517      /**
518       * Fair Read trylock succeeds if write locked by current thread
519       */
# Line 471 | Line 532 | public class ReentrantReadWriteLockTest
532      public void testReadHoldingWriteLockFair2() throws InterruptedException {
533          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
534          lock.writeLock().lock();
535 +        lock.readLock().lock();
536 +        lock.readLock().unlock();
537 +
538          Thread t1 = newStartedThread(new CheckedRunnable() {
539              public void realRun() {
540                  lock.readLock().lock();
# Line 482 | Line 546 | public class ReentrantReadWriteLockTest
546                  lock.readLock().unlock();
547              }});
548  
549 +        waitForQueuedThread(lock, t1);
550 +        waitForQueuedThread(lock, t2);
551 +        assertTrue(lock.isWriteLockedByCurrentThread());
552          lock.readLock().lock();
553          lock.readLock().unlock();
554 <        Thread.sleep(SHORT_DELAY_MS);
555 <        lock.readLock().lock();
556 <        lock.readLock().unlock();
490 <        lock.writeLock().unlock();
491 <        awaitTermination(t1, LONG_DELAY_MS);
492 <        awaitTermination(t2, LONG_DELAY_MS);
554 >        releaseWriteLock(lock);
555 >        awaitTermination(t1);
556 >        awaitTermination(t2);
557      }
558  
495
559      /**
560       * Fair Read lock succeeds if write locked by current thread even if
561       * other threads are waiting for writelock
# Line 500 | Line 563 | public class ReentrantReadWriteLockTest
563      public void testReadHoldingWriteLockFair3() throws InterruptedException {
564          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
565          lock.writeLock().lock();
566 +        lock.readLock().lock();
567 +        lock.readLock().unlock();
568 +
569          Thread t1 = newStartedThread(new CheckedRunnable() {
570              public void realRun() {
571                  lock.writeLock().lock();
# Line 511 | Line 577 | public class ReentrantReadWriteLockTest
577                  lock.writeLock().unlock();
578              }});
579  
580 +        waitForQueuedThread(lock, t1);
581 +        waitForQueuedThread(lock, t2);
582 +        assertTrue(lock.isWriteLockedByCurrentThread());
583          lock.readLock().lock();
584          lock.readLock().unlock();
585 <        Thread.sleep(SHORT_DELAY_MS);
586 <        lock.readLock().lock();
587 <        lock.readLock().unlock();
519 <        lock.writeLock().unlock();
520 <        awaitTermination(t1, LONG_DELAY_MS);
521 <        awaitTermination(t2, LONG_DELAY_MS);
585 >        releaseWriteLock(lock);
586 >        awaitTermination(t1);
587 >        awaitTermination(t2);
588      }
589  
524
590      /**
591       * Fair Write lock succeeds if write locked by current thread even if
592       * other threads are waiting for writelock
# Line 540 | Line 605 | public class ReentrantReadWriteLockTest
605                  lock.writeLock().unlock();
606              }});
607  
608 <        Thread.sleep(SHORT_DELAY_MS);
608 >        waitForQueuedThread(lock, t1);
609 >        waitForQueuedThread(lock, t2);
610          assertTrue(lock.isWriteLockedByCurrentThread());
611          assertEquals(1, lock.getWriteHoldCount());
612          lock.writeLock().lock();
# Line 548 | Line 614 | public class ReentrantReadWriteLockTest
614          lock.writeLock().unlock();
615          lock.writeLock().lock();
616          lock.writeLock().unlock();
617 <        lock.writeLock().unlock();
618 <        awaitTermination(t1, LONG_DELAY_MS);
619 <        awaitTermination(t2, LONG_DELAY_MS);
617 >        releaseWriteLock(lock);
618 >        awaitTermination(t1);
619 >        awaitTermination(t2);
620      }
621  
556
622      /**
623       * Read tryLock succeeds if readlocked but not writelocked
624       */
# Line 566 | Line 631 | public class ReentrantReadWriteLockTest
631                  lock.readLock().unlock();
632              }});
633  
634 <        awaitTermination(t, LONG_DELAY_MS);
634 >        awaitTermination(t);
635          lock.readLock().unlock();
636      }
637  
# Line 581 | Line 646 | public class ReentrantReadWriteLockTest
646                  assertFalse(lock.writeLock().tryLock());
647              }});
648  
649 <        awaitTermination(t, LONG_DELAY_MS);
649 >        awaitTermination(t);
650          lock.readLock().unlock();
651      }
652  
588
653      /**
654       * Fair Read tryLock succeeds if readlocked but not writelocked
655       */
# Line 598 | Line 662 | public class ReentrantReadWriteLockTest
662                  lock.readLock().unlock();
663              }});
664  
665 <        awaitTermination(t, LONG_DELAY_MS);
665 >        awaitTermination(t);
666          lock.readLock().unlock();
667      }
668  
605
606
669      /**
670       * Fair write tryLock fails when readlocked
671       */
# Line 615 | Line 677 | public class ReentrantReadWriteLockTest
677                  assertFalse(lock.writeLock().tryLock());
678              }});
679  
680 <        awaitTermination(t, LONG_DELAY_MS);
680 >        awaitTermination(t);
681          lock.readLock().unlock();
682      }
683  
622
623
684      /**
685       * write timed tryLock times out if locked
686       */
# Line 632 | Line 692 | public class ReentrantReadWriteLockTest
692                  assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
693              }});
694  
695 <        awaitTermination(t, LONG_DELAY_MS);
695 >        awaitTermination(t);
696          assertTrue(lock.writeLock().isHeldByCurrentThread());
697          lock.writeLock().unlock();
698      }
# Line 648 | Line 708 | public class ReentrantReadWriteLockTest
708                  assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
709              }});
710  
711 <        awaitTermination(t, LONG_DELAY_MS);
711 >        awaitTermination(t);
712          assertTrue(lock.writeLock().isHeldByCurrentThread());
713          lock.writeLock().unlock();
714      }
715  
656
716      /**
717       * write lockInterruptibly succeeds if lock free else is interruptible
718       */
# Line 665 | Line 724 | public class ReentrantReadWriteLockTest
724                  lock.writeLock().lockInterruptibly();
725              }});
726  
727 <        Thread.sleep(SHORT_DELAY_MS);
727 >        waitForQueuedThread(lock, t);
728          t.interrupt();
729 <        awaitTermination(t, LONG_DELAY_MS);
729 >        awaitTermination(t);
730          releaseWriteLock(lock);
731      }
732  
# Line 682 | Line 741 | public class ReentrantReadWriteLockTest
741                  lock.readLock().lockInterruptibly();
742              }});
743  
744 <        Thread.sleep(SHORT_DELAY_MS);
744 >        waitForQueuedThread(lock, t);
745          t.interrupt();
746 <        awaitTermination(t, LONG_DELAY_MS);
746 >        awaitTermination(t);
747          releaseWriteLock(lock);
748      }
749  
# Line 698 | Line 757 | public class ReentrantReadWriteLockTest
757              c.await();
758              shouldThrow();
759          } catch (IllegalMonitorStateException success) {}
760 +        try {
761 +            c.await(LONG_DELAY_MS, MILLISECONDS);
762 +            shouldThrow();
763 +        } catch (IllegalMonitorStateException success) {}
764 +        try {
765 +            c.awaitNanos(100);
766 +            shouldThrow();
767 +        } catch (IllegalMonitorStateException success) {}
768 +        try {
769 +            c.awaitUninterruptibly();
770 +            shouldThrow();
771 +        } catch (IllegalMonitorStateException success) {}
772      }
773  
774      /**
# Line 713 | Line 784 | public class ReentrantReadWriteLockTest
784      }
785  
786      /**
787 +     * Calling signalAll without holding lock throws IllegalMonitorStateException
788 +     */
789 +    public void testSignalAll_IllegalMonitor() {
790 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
791 +        final Condition c = lock.writeLock().newCondition();
792 +        try {
793 +            c.signalAll();
794 +            shouldThrow();
795 +        } catch (IllegalMonitorStateException success) {}
796 +    }
797 +
798 +    /**
799       * awaitNanos without a signal times out
800       */
801      public void testAwaitNanos_Timeout() throws InterruptedException {
802          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
803          final Condition c = lock.writeLock().newCondition();
721
804          lock.writeLock().lock();
805 <        long t = c.awaitNanos(100);
806 <        assertTrue(t <= 0);
805 >        long startTime = System.nanoTime();
806 >        long timeoutMillis = 10;
807 >        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
808 >        long nanosRemaining = c.awaitNanos(timeoutNanos);
809 >        assertTrue(nanosRemaining <= 0);
810 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
811          lock.writeLock().unlock();
812      }
813  
728
814      /**
815       * timed await without a signal times out
816       */
# Line 733 | Line 818 | public class ReentrantReadWriteLockTest
818          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
819          final Condition c = lock.writeLock().newCondition();
820          lock.writeLock().lock();
821 <        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
821 >        long startTime = System.nanoTime();
822 >        long timeoutMillis = 10;
823 >        assertFalse(c.await(timeoutMillis, MILLISECONDS));
824 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
825          lock.writeLock().unlock();
826      }
827  
# Line 755 | Line 843 | public class ReentrantReadWriteLockTest
843      public void testAwait() throws InterruptedException {
844          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
845          final Condition c = lock.writeLock().newCondition();
846 +        final CountDownLatch locked = new CountDownLatch(1);
847          Thread t = newStartedThread(new CheckedRunnable() {
848              public void realRun() throws InterruptedException {
849                  lock.writeLock().lock();
850 +                locked.countDown();
851                  c.await();
852                  lock.writeLock().unlock();
853              }});
854  
855 <        Thread.sleep(SHORT_DELAY_MS);
855 >        locked.await();
856          lock.writeLock().lock();
857          c.signal();
858 +        assertTrue(t.isAlive());
859          lock.writeLock().unlock();
860 <        awaitTermination(t, LONG_DELAY_MS);
770 <    }
771 <
772 <    /** A helper class for uninterruptible wait tests */
773 <    class UninterruptableThread extends Thread {
774 <        private Lock lock;
775 <        private Condition c;
776 <
777 <        public volatile boolean canAwake = false;
778 <        public volatile boolean interrupted = false;
779 <        public volatile boolean lockStarted = false;
780 <
781 <        public UninterruptableThread(Lock lock, Condition c) {
782 <            this.lock = lock;
783 <            this.c = c;
784 <        }
785 <
786 <        public synchronized void run() {
787 <            lock.lock();
788 <            lockStarted = true;
789 <
790 <            while (!canAwake) {
791 <                c.awaitUninterruptibly();
792 <            }
793 <
794 <            interrupted = isInterrupted();
795 <            lock.unlock();
796 <        }
860 >        awaitTermination(t);
861      }
862  
863      /**
# Line 802 | Line 866 | public class ReentrantReadWriteLockTest
866      public void testAwaitUninterruptibly() throws InterruptedException {
867          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
868          final Condition c = lock.writeLock().newCondition();
869 <        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
870 <
871 <        thread.start();
872 <
873 <        while (!thread.lockStarted) {
874 <            Thread.sleep(100);
875 <        }
869 >        final CountDownLatch locked = new CountDownLatch(1);
870 >        final AtomicBoolean canAwake = new AtomicBoolean(false);
871 >        Thread t = newStartedThread(new CheckedRunnable() {
872 >            public void realRun() {
873 >                lock.writeLock().lock();
874 >                locked.countDown();
875 >                c.awaitUninterruptibly();
876 >                assertTrue(Thread.interrupted());
877 >                lock.writeLock().unlock();
878 >            }});
879  
880 +        locked.await();
881          lock.writeLock().lock();
882 <        try {
883 <            thread.interrupt();
884 <            thread.canAwake = true;
885 <            c.signal();
886 <        } finally {
887 <            lock.writeLock().unlock();
888 <        }
889 <
822 <        awaitTermination(thread, LONG_DELAY_MS);
823 <        assertTrue(thread.interrupted);
882 >        lock.writeLock().unlock();
883 >        t.interrupt();
884 >        t.join(10);
885 >        assertTrue(t.isAlive());
886 >        lock.writeLock().lock();
887 >        c.signal();
888 >        lock.writeLock().unlock();
889 >        awaitTermination(t);
890      }
891  
892      /**
893       * await is interruptible
894       */
895      public void testAwait_Interrupt() throws InterruptedException {
896 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
896 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
897          final Condition c = lock.writeLock().newCondition();
898          final CountDownLatch locked = new CountDownLatch(1);
899          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
900              public void realRun() throws InterruptedException {
901                  lock.writeLock().lock();
902                  assertTrue(lock.isWriteLocked());
903 +                assertTrue(lock.isWriteLockedByCurrentThread());
904 +                assertHasNoWaiters(lock, c);
905                  locked.countDown();
906 <                try { c.await(); }
907 <                finally { lock.writeLock().unlock(); }
906 >                try {
907 >                    c.await();
908 >                } finally {
909 >                    assertTrue(lock.isWriteLocked());
910 >                    assertTrue(lock.isWriteLockedByCurrentThread());
911 >                    assertHasNoWaiters(lock, c);
912 >                    lock.writeLock().unlock();
913 >                    assertFalse(Thread.interrupted());
914 >                }
915              }});
916  
917          locked.await();
918 <        while (lock.isWriteLocked())
844 <            Thread.yield();
918 >        assertHasWaiters(lock, c, t);
919          t.interrupt();
920 <        awaitTermination(t, LONG_DELAY_MS);
920 >        awaitTermination(t);
921          assertFalse(lock.isWriteLocked());
922      }
923  
# Line 851 | Line 925 | public class ReentrantReadWriteLockTest
925       * awaitNanos is interruptible
926       */
927      public void testAwaitNanos_Interrupt() throws InterruptedException {
928 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
928 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
929          final Condition c = lock.writeLock().newCondition();
930          final CountDownLatch locked = new CountDownLatch(1);
931          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
932              public void realRun() throws InterruptedException {
933                  lock.writeLock().lock();
934                  assertTrue(lock.isWriteLocked());
935 +                assertTrue(lock.isWriteLockedByCurrentThread());
936 +                assertHasNoWaiters(lock, c);
937                  locked.countDown();
938 <                try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }
939 <                finally { lock.writeLock().unlock(); }
938 >                try {
939 >                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
940 >                } finally {
941 >                    assertTrue(lock.isWriteLocked());
942 >                    assertTrue(lock.isWriteLockedByCurrentThread());
943 >                    assertHasNoWaiters(lock, c);
944 >                    lock.writeLock().unlock();
945 >                    assertFalse(Thread.interrupted());
946 >                }
947              }});
948  
949          locked.await();
950 <        while (lock.isWriteLocked())
868 <            Thread.yield();
950 >        assertHasWaiters(lock, c, t);
951          t.interrupt();
952 <        awaitTermination(t, LONG_DELAY_MS);
952 >        awaitTermination(t);
953          assertFalse(lock.isWriteLocked());
954      }
955  
# Line 875 | Line 957 | public class ReentrantReadWriteLockTest
957       * awaitUntil is interruptible
958       */
959      public void testAwaitUntil_Interrupt() throws InterruptedException {
960 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
960 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
961          final Condition c = lock.writeLock().newCondition();
962          final CountDownLatch locked = new CountDownLatch(1);
963          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
964              public void realRun() throws InterruptedException {
965                  lock.writeLock().lock();
966                  assertTrue(lock.isWriteLocked());
967 +                assertTrue(lock.isWriteLockedByCurrentThread());
968 +                assertHasNoWaiters(lock, c);
969                  locked.countDown();
970                  java.util.Date d = new java.util.Date();
971 <                try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); }
972 <                finally { lock.writeLock().unlock(); }
971 >                try {
972 >                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
973 >                } finally {
974 >                    assertTrue(lock.isWriteLocked());
975 >                    assertTrue(lock.isWriteLockedByCurrentThread());
976 >                    assertHasNoWaiters(lock, c);
977 >                    lock.writeLock().unlock();
978 >                    assertFalse(Thread.interrupted());
979 >                }
980              }});
981  
982          locked.await();
983 <        while (lock.isWriteLocked())
893 <            Thread.yield();
983 >        assertHasWaiters(lock, c, t);
984          t.interrupt();
985 <        awaitTermination(t, LONG_DELAY_MS);
985 >        awaitTermination(t);
986          assertFalse(lock.isWriteLocked());
987      }
988  
# Line 900 | Line 990 | public class ReentrantReadWriteLockTest
990       * signalAll wakes up all threads
991       */
992      public void testSignalAll() throws InterruptedException {
993 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
993 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
994          final Condition c = lock.writeLock().newCondition();
995 +        final CountDownLatch locked = new CountDownLatch(2);
996 +        final Lock writeLock = lock.writeLock();
997          Thread t1 = newStartedThread(new CheckedRunnable() {
998              public void realRun() throws InterruptedException {
999 <                lock.writeLock().lock();
999 >                writeLock.lock();
1000 >                locked.countDown();
1001                  c.await();
1002 <                lock.writeLock().unlock();
1002 >                writeLock.unlock();
1003              }});
1004  
1005          Thread t2 = newStartedThread(new CheckedRunnable() {
1006              public void realRun() throws InterruptedException {
1007 <                lock.writeLock().lock();
1007 >                writeLock.lock();
1008 >                locked.countDown();
1009                  c.await();
1010 <                lock.writeLock().unlock();
1010 >                writeLock.unlock();
1011              }});
1012  
1013 <        Thread.sleep(SHORT_DELAY_MS);
1014 <        lock.writeLock().lock();
1013 >        locked.await();
1014 >        writeLock.lock();
1015 >        assertHasWaiters(lock, c, t1, t2);
1016          c.signalAll();
1017 <        lock.writeLock().unlock();
1018 <        awaitTermination(t1, LONG_DELAY_MS);
1019 <        awaitTermination(t2, LONG_DELAY_MS);
1017 >        assertHasNoWaiters(lock, c);
1018 >        writeLock.unlock();
1019 >        awaitTermination(t1);
1020 >        awaitTermination(t2);
1021 >    }
1022 >
1023 >    /**
1024 >     * signal wakes up waiting threads in FIFO order.
1025 >     */
1026 >    public void testSignalWakesFifo() throws InterruptedException {
1027 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1028 >        final Condition c = lock.writeLock().newCondition();
1029 >        final CountDownLatch locked1 = new CountDownLatch(1);
1030 >        final CountDownLatch locked2 = new CountDownLatch(1);
1031 >        final Lock writeLock = lock.writeLock();
1032 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1033 >            public void realRun() throws InterruptedException {
1034 >                writeLock.lock();
1035 >                locked1.countDown();
1036 >                c.await();
1037 >                writeLock.unlock();
1038 >            }});
1039 >
1040 >        locked1.await();
1041 >
1042 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1043 >            public void realRun() throws InterruptedException {
1044 >                writeLock.lock();
1045 >                locked2.countDown();
1046 >                c.await();
1047 >                writeLock.unlock();
1048 >            }});
1049 >
1050 >        locked2.await();
1051 >
1052 >        writeLock.lock();
1053 >        assertHasWaiters(lock, c, t1, t2);
1054 >        assertFalse(lock.hasQueuedThreads());
1055 >        c.signal();
1056 >        assertHasWaiters(lock, c, t2);
1057 >        assertTrue(lock.hasQueuedThread(t1));
1058 >        assertFalse(lock.hasQueuedThread(t2));
1059 >        c.signal();
1060 >        assertHasNoWaiters(lock, c);
1061 >        assertTrue(lock.hasQueuedThread(t1));
1062 >        assertTrue(lock.hasQueuedThread(t2));
1063 >        writeLock.unlock();
1064 >        awaitTermination(t1);
1065 >        awaitTermination(t2);
1066      }
1067  
1068      /**
# Line 953 | Line 1094 | public class ReentrantReadWriteLockTest
1094          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1095          assertFalse(lock.hasQueuedThreads());
1096          lock.writeLock().lock();
1097 +        assertFalse(lock.hasQueuedThreads());
1098 +        long startTime = System.nanoTime();
1099          t1.start();
1100 <        Thread.sleep(SHORT_DELAY_MS);
958 <        assertTrue(lock.hasQueuedThreads());
1100 >        waitForQueuedThread(lock, t1);
1101          t2.start();
1102 <        Thread.sleep(SHORT_DELAY_MS);
1102 >        waitForQueuedThread(lock, t2);
1103          assertTrue(lock.hasQueuedThreads());
1104          t1.interrupt();
1105 <        Thread.sleep(SHORT_DELAY_MS);
1105 >        awaitTermination(t1);
1106          assertTrue(lock.hasQueuedThreads());
1107          lock.writeLock().unlock();
1108 <        Thread.sleep(SHORT_DELAY_MS);
1108 >        awaitTermination(t2);
1109          assertFalse(lock.hasQueuedThreads());
968        awaitTermination(t1, LONG_DELAY_MS);
969        awaitTermination(t2, LONG_DELAY_MS);
1110      }
1111  
1112      /**
# Line 984 | Line 1124 | public class ReentrantReadWriteLockTest
1124       * hasQueuedThread reports whether a thread is queued.
1125       */
1126      public void testHasQueuedThread() throws InterruptedException {
1127 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1128 <        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1129 <        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1130 <        assertFalse(sync.hasQueuedThread(t1));
1131 <        assertFalse(sync.hasQueuedThread(t2));
1132 <        sync.writeLock().lock();
1127 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1128 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1129 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1130 >        assertFalse(lock.hasQueuedThread(t1));
1131 >        assertFalse(lock.hasQueuedThread(t2));
1132 >        lock.writeLock().lock();
1133 >        long startTime = System.nanoTime();
1134          t1.start();
1135 <        Thread.sleep(SHORT_DELAY_MS);
1136 <        assertTrue(sync.hasQueuedThread(t1));
1135 >        waitForQueuedThread(lock, t1);
1136 >        assertTrue(lock.hasQueuedThread(t1));
1137 >        assertFalse(lock.hasQueuedThread(t2));
1138          t2.start();
1139 <        Thread.sleep(SHORT_DELAY_MS);
1140 <        assertTrue(sync.hasQueuedThread(t1));
1141 <        assertTrue(sync.hasQueuedThread(t2));
1139 >        waitForQueuedThread(lock, t2);
1140 >        assertTrue(lock.hasQueuedThread(t1));
1141 >        assertTrue(lock.hasQueuedThread(t2));
1142          t1.interrupt();
1143 <        Thread.sleep(SHORT_DELAY_MS);
1144 <        assertFalse(sync.hasQueuedThread(t1));
1145 <        assertTrue(sync.hasQueuedThread(t2));
1146 <        sync.writeLock().unlock();
1147 <        Thread.sleep(SHORT_DELAY_MS);
1148 <        assertFalse(sync.hasQueuedThread(t1));
1149 <        Thread.sleep(SHORT_DELAY_MS);
1008 <        assertFalse(sync.hasQueuedThread(t2));
1009 <        awaitTermination(t1, LONG_DELAY_MS);
1010 <        awaitTermination(t2, LONG_DELAY_MS);
1143 >        awaitTermination(t1);
1144 >        assertFalse(lock.hasQueuedThread(t1));
1145 >        assertTrue(lock.hasQueuedThread(t2));
1146 >        lock.writeLock().unlock();
1147 >        awaitTermination(t2);
1148 >        assertFalse(lock.hasQueuedThread(t1));
1149 >        assertFalse(lock.hasQueuedThread(t2));
1150      }
1151  
1013
1152      /**
1153       * getQueueLength reports number of waiting threads
1154       */
# Line 1020 | Line 1158 | public class ReentrantReadWriteLockTest
1158          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1159          assertEquals(0, lock.getQueueLength());
1160          lock.writeLock().lock();
1161 +        long startTime = System.nanoTime();
1162          t1.start();
1163 <        Thread.sleep(SHORT_DELAY_MS);
1163 >        waitForQueuedThread(lock, t1);
1164          assertEquals(1, lock.getQueueLength());
1165          t2.start();
1166 <        Thread.sleep(SHORT_DELAY_MS);
1166 >        waitForQueuedThread(lock, t2);
1167          assertEquals(2, lock.getQueueLength());
1168          t1.interrupt();
1169 <        Thread.sleep(SHORT_DELAY_MS);
1169 >        awaitTermination(t1);
1170          assertEquals(1, lock.getQueueLength());
1171          lock.writeLock().unlock();
1172 <        Thread.sleep(SHORT_DELAY_MS);
1172 >        awaitTermination(t2);
1173          assertEquals(0, lock.getQueueLength());
1035        awaitTermination(t1, LONG_DELAY_MS);
1036        awaitTermination(t2, LONG_DELAY_MS);
1174      }
1175  
1176      /**
# Line 1045 | Line 1182 | public class ReentrantReadWriteLockTest
1182          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1183          assertTrue(lock.getQueuedThreads().isEmpty());
1184          lock.writeLock().lock();
1185 +        long startTime = System.nanoTime();
1186          assertTrue(lock.getQueuedThreads().isEmpty());
1187          t1.start();
1188 <        Thread.sleep(SHORT_DELAY_MS);
1188 >        waitForQueuedThread(lock, t1);
1189 >        assertEquals(1, lock.getQueuedThreads().size());
1190          assertTrue(lock.getQueuedThreads().contains(t1));
1191          t2.start();
1192 <        Thread.sleep(SHORT_DELAY_MS);
1192 >        waitForQueuedThread(lock, t2);
1193 >        assertEquals(2, lock.getQueuedThreads().size());
1194          assertTrue(lock.getQueuedThreads().contains(t1));
1195          assertTrue(lock.getQueuedThreads().contains(t2));
1196          t1.interrupt();
1197 <        Thread.sleep(SHORT_DELAY_MS);
1197 >        awaitTermination(t1);
1198          assertFalse(lock.getQueuedThreads().contains(t1));
1199          assertTrue(lock.getQueuedThreads().contains(t2));
1200 +        assertEquals(1, lock.getQueuedThreads().size());
1201          lock.writeLock().unlock();
1202 <        Thread.sleep(SHORT_DELAY_MS);
1202 >        awaitTermination(t2);
1203          assertTrue(lock.getQueuedThreads().isEmpty());
1063        awaitTermination(t1, LONG_DELAY_MS);
1064        awaitTermination(t2, LONG_DELAY_MS);
1204      }
1205  
1206      /**
# Line 1086 | Line 1225 | public class ReentrantReadWriteLockTest
1225          } catch (NullPointerException success) {}
1226      }
1227  
1089
1228      /**
1229       * getWaitingThreads throws NPE if null
1230       */
# Line 1123 | Line 1261 | public class ReentrantReadWriteLockTest
1261          } catch (IllegalMonitorStateException success) {}
1262      }
1263  
1126
1264      /**
1265       * getWaitQueueLength throws IAE if not owned
1266       */
# Line 1149 | Line 1286 | public class ReentrantReadWriteLockTest
1286          } catch (IllegalMonitorStateException success) {}
1287      }
1288  
1152
1289      /**
1290       * getWaitingThreads throws IAE if not owned
1291       */
# Line 1175 | Line 1311 | public class ReentrantReadWriteLockTest
1311          } catch (IllegalMonitorStateException success) {}
1312      }
1313  
1178
1314      /**
1315       * hasWaiters returns true when a thread is waiting, else false
1316       */
1317      public void testHasWaiters() throws InterruptedException {
1318 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1318 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1319          final Condition c = lock.writeLock().newCondition();
1320 +        final CountDownLatch locked = new CountDownLatch(1);
1321          Thread t = newStartedThread(new CheckedRunnable() {
1322              public void realRun() throws InterruptedException {
1323                  lock.writeLock().lock();
1324                  assertFalse(lock.hasWaiters(c));
1325 +                locked.countDown();
1326                  assertEquals(0, lock.getWaitQueueLength(c));
1327                  c.await();
1328                  lock.writeLock().unlock();
1329              }});
1330  
1331 <        Thread.sleep(SHORT_DELAY_MS);
1331 >        locked.await();
1332          lock.writeLock().lock();
1333          assertTrue(lock.hasWaiters(c));
1334          assertEquals(1, lock.getWaitQueueLength(c));
1335          c.signal();
1336 +        assertHasNoWaiters(lock, c);
1337          lock.writeLock().unlock();
1338 <        Thread.sleep(SHORT_DELAY_MS);
1339 <        lock.writeLock().lock();
1202 <        assertFalse(lock.hasWaiters(c));
1203 <        assertEquals(0, lock.getWaitQueueLength(c));
1204 <        lock.writeLock().unlock();
1205 <        awaitTermination(t, LONG_DELAY_MS);
1338 >        awaitTermination(t);
1339 >        assertHasNoWaiters(lock, c);
1340      }
1341  
1342      /**
1343       * getWaitQueueLength returns number of waiting threads
1344       */
1345      public void testGetWaitQueueLength() throws InterruptedException {
1346 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1346 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1347          final Condition c = lock.writeLock().newCondition();
1348 +        final CountDownLatch locked = new CountDownLatch(1);
1349          Thread t = newStartedThread(new CheckedRunnable() {
1350              public void realRun() throws InterruptedException {
1351                  lock.writeLock().lock();
1217                assertFalse(lock.hasWaiters(c));
1352                  assertEquals(0, lock.getWaitQueueLength(c));
1353 +                locked.countDown();
1354                  c.await();
1355                  lock.writeLock().unlock();
1356              }});
1357  
1358 <        Thread.sleep(SHORT_DELAY_MS);
1358 >        locked.await();
1359          lock.writeLock().lock();
1360 <        assertTrue(lock.hasWaiters(c));
1360 >        assertHasWaiters(lock, c, t);
1361          assertEquals(1, lock.getWaitQueueLength(c));
1362          c.signal();
1363 <        lock.writeLock().unlock();
1229 <        Thread.sleep(SHORT_DELAY_MS);
1230 <        lock.writeLock().lock();
1231 <        assertFalse(lock.hasWaiters(c));
1363 >        assertHasNoWaiters(lock, c);
1364          assertEquals(0, lock.getWaitQueueLength(c));
1365          lock.writeLock().unlock();
1366 <        awaitTermination(t, LONG_DELAY_MS);
1366 >        awaitTermination(t);
1367      }
1368  
1237
1369      /**
1370       * getWaitingThreads returns only and all waiting threads
1371       */
1372      public void testGetWaitingThreads() throws InterruptedException {
1373          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1374          final Condition c = lock.writeLock().newCondition();
1375 +        final CountDownLatch locked1 = new CountDownLatch(1);
1376 +        final CountDownLatch locked2 = new CountDownLatch(1);
1377          Thread t1 = new Thread(new CheckedRunnable() {
1378              public void realRun() throws InterruptedException {
1379                  lock.writeLock().lock();
1380                  assertTrue(lock.getWaitingThreads(c).isEmpty());
1381 +                locked1.countDown();
1382                  c.await();
1383                  lock.writeLock().unlock();
1384              }});
# Line 1253 | Line 1387 | public class ReentrantReadWriteLockTest
1387              public void realRun() throws InterruptedException {
1388                  lock.writeLock().lock();
1389                  assertFalse(lock.getWaitingThreads(c).isEmpty());
1390 +                locked2.countDown();
1391                  c.await();
1392                  lock.writeLock().unlock();
1393              }});
# Line 1260 | Line 1395 | public class ReentrantReadWriteLockTest
1395          lock.writeLock().lock();
1396          assertTrue(lock.getWaitingThreads(c).isEmpty());
1397          lock.writeLock().unlock();
1398 +
1399          t1.start();
1400 <        Thread.sleep(SHORT_DELAY_MS);
1400 >        locked1.await();
1401          t2.start();
1402 <        Thread.sleep(SHORT_DELAY_MS);
1402 >        locked2.await();
1403 >
1404          lock.writeLock().lock();
1405          assertTrue(lock.hasWaiters(c));
1406          assertTrue(lock.getWaitingThreads(c).contains(t1));
1407          assertTrue(lock.getWaitingThreads(c).contains(t2));
1408 +        assertEquals(2, lock.getWaitingThreads(c).size());
1409          c.signalAll();
1410 +        assertHasNoWaiters(lock, c);
1411          lock.writeLock().unlock();
1412 <        Thread.sleep(SHORT_DELAY_MS);
1413 <        lock.writeLock().lock();
1414 <        assertFalse(lock.hasWaiters(c));
1415 <        assertTrue(lock.getWaitingThreads(c).isEmpty());
1416 <        lock.writeLock().unlock();
1278 <        awaitTermination(t1, LONG_DELAY_MS);
1279 <        awaitTermination(t2, LONG_DELAY_MS);
1412 >
1413 >        awaitTermination(t1);
1414 >        awaitTermination(t2);
1415 >
1416 >        assertHasNoWaiters(lock, c);
1417      }
1418  
1419      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines