• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/publicdomain/zero/1.0/
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8 
9 package jsr166;
10 
11 import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.atomic.AtomicBoolean;
18 import java.util.concurrent.locks.Condition;
19 import java.util.concurrent.locks.Lock;
20 import java.util.concurrent.locks.ReentrantReadWriteLock;
21 
22 import junit.framework.AssertionFailedError;
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 
26 public class ReentrantReadWriteLockTest extends JSR166TestCase {
27     // android-note: Removed because the CTS runner does a bad job of
28     // retrying tests that have suite() declarations.
29     //
30     // public static void main(String[] args) {
31     //     main(suite(), args);
32     // }
33     // public static Test suite() {
34     //     return new TestSuite(...);
35     // }
36 
37     /**
38      * A runnable calling lockInterruptibly
39      */
40     class InterruptibleLockRunnable extends CheckedRunnable {
41         final ReentrantReadWriteLock lock;
InterruptibleLockRunnable(ReentrantReadWriteLock l)42         InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
realRun()43         public void realRun() throws InterruptedException {
44             lock.writeLock().lockInterruptibly();
45         }
46     }
47 
48     /**
49      * A runnable calling lockInterruptibly that expects to be
50      * interrupted
51      */
52     class InterruptedLockRunnable extends CheckedInterruptedRunnable {
53         final ReentrantReadWriteLock lock;
InterruptedLockRunnable(ReentrantReadWriteLock l)54         InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
realRun()55         public void realRun() throws InterruptedException {
56             lock.writeLock().lockInterruptibly();
57         }
58     }
59 
60     /**
61      * Subclass to expose protected methods
62      */
63     static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
PublicReentrantReadWriteLock()64         PublicReentrantReadWriteLock() { super(); }
PublicReentrantReadWriteLock(boolean fair)65         PublicReentrantReadWriteLock(boolean fair) { super(fair); }
getOwner()66         public Thread getOwner() {
67             return super.getOwner();
68         }
getQueuedThreads()69         public Collection<Thread> getQueuedThreads() {
70             return super.getQueuedThreads();
71         }
getWaitingThreads(Condition c)72         public Collection<Thread> getWaitingThreads(Condition c) {
73             return super.getWaitingThreads(c);
74         }
75     }
76 
77     /**
78      * Releases write lock, checking that it had a hold count of 1.
79      */
releaseWriteLock(PublicReentrantReadWriteLock lock)80     void releaseWriteLock(PublicReentrantReadWriteLock lock) {
81         ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
82         assertWriteLockedByMoi(lock);
83         assertEquals(1, lock.getWriteHoldCount());
84         writeLock.unlock();
85         assertNotWriteLocked(lock);
86     }
87 
88     /**
89      * Spin-waits until lock.hasQueuedThread(t) becomes true.
90      */
waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t)91     void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
92         long startTime = System.nanoTime();
93         while (!lock.hasQueuedThread(t)) {
94             if (millisElapsedSince(startTime) > LONG_DELAY_MS)
95                 throw new AssertionFailedError("timed out");
96             Thread.yield();
97         }
98         assertTrue(t.isAlive());
99         assertNotSame(t, lock.getOwner());
100     }
101 
102     /**
103      * Checks that lock is not write-locked.
104      */
assertNotWriteLocked(PublicReentrantReadWriteLock lock)105     void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
106         assertFalse(lock.isWriteLocked());
107         assertFalse(lock.isWriteLockedByCurrentThread());
108         assertFalse(lock.writeLock().isHeldByCurrentThread());
109         assertEquals(0, lock.getWriteHoldCount());
110         assertEquals(0, lock.writeLock().getHoldCount());
111         assertNull(lock.getOwner());
112     }
113 
114     /**
115      * Checks that lock is write-locked by the given thread.
116      */
assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t)117     void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
118         assertTrue(lock.isWriteLocked());
119         assertSame(t, lock.getOwner());
120         assertEquals(t == Thread.currentThread(),
121                      lock.isWriteLockedByCurrentThread());
122         assertEquals(t == Thread.currentThread(),
123                      lock.writeLock().isHeldByCurrentThread());
124         assertEquals(t == Thread.currentThread(),
125                      lock.getWriteHoldCount() > 0);
126         assertEquals(t == Thread.currentThread(),
127                      lock.writeLock().getHoldCount() > 0);
128         assertEquals(0, lock.getReadLockCount());
129     }
130 
131     /**
132      * Checks that lock is write-locked by the current thread.
133      */
assertWriteLockedByMoi(PublicReentrantReadWriteLock lock)134     void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
135         assertWriteLockedBy(lock, Thread.currentThread());
136     }
137 
138     /**
139      * Checks that condition c has no waiters.
140      */
assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c)141     void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
142         assertHasWaiters(lock, c, new Thread[] {});
143     }
144 
145     /**
146      * Checks that condition c has exactly the given waiter threads.
147      */
assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c, Thread... threads)148     void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
149                           Thread... threads) {
150         lock.writeLock().lock();
151         assertEquals(threads.length > 0, lock.hasWaiters(c));
152         assertEquals(threads.length, lock.getWaitQueueLength(c));
153         assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
154         assertEquals(threads.length, lock.getWaitingThreads(c).size());
155         assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
156                      new HashSet<Thread>(Arrays.asList(threads)));
157         lock.writeLock().unlock();
158     }
159 
160     enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
161 
162     /**
163      * Awaits condition using the specified AwaitMethod.
164      */
await(Condition c, AwaitMethod awaitMethod)165     void await(Condition c, AwaitMethod awaitMethod)
166             throws InterruptedException {
167         switch (awaitMethod) {
168         case await:
169             c.await();
170             break;
171         case awaitTimed:
172             assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
173             break;
174         case awaitNanos:
175             long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
176             assertTrue(nanosRemaining > 0);
177             break;
178         case awaitUntil:
179             java.util.Date d = new java.util.Date();
180             assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
181             break;
182         default:
183             throw new AssertionError();
184         }
185     }
186 
187     /**
188      * Constructor sets given fairness, and is in unlocked state
189      */
testConstructor()190     public void testConstructor() {
191         PublicReentrantReadWriteLock lock;
192 
193         lock = new PublicReentrantReadWriteLock();
194         assertFalse(lock.isFair());
195         assertNotWriteLocked(lock);
196         assertEquals(0, lock.getReadLockCount());
197 
198         lock = new PublicReentrantReadWriteLock(true);
199         assertTrue(lock.isFair());
200         assertNotWriteLocked(lock);
201         assertEquals(0, lock.getReadLockCount());
202 
203         lock = new PublicReentrantReadWriteLock(false);
204         assertFalse(lock.isFair());
205         assertNotWriteLocked(lock);
206         assertEquals(0, lock.getReadLockCount());
207     }
208 
209     /**
210      * write-locking and read-locking an unlocked lock succeed
211      */
testLock()212     public void testLock()      { testLock(false); }
testLock_fair()213     public void testLock_fair() { testLock(true); }
testLock(boolean fair)214     public void testLock(boolean fair) {
215         PublicReentrantReadWriteLock lock =
216             new PublicReentrantReadWriteLock(fair);
217         assertNotWriteLocked(lock);
218         lock.writeLock().lock();
219         assertWriteLockedByMoi(lock);
220         lock.writeLock().unlock();
221         assertNotWriteLocked(lock);
222         assertEquals(0, lock.getReadLockCount());
223         lock.readLock().lock();
224         assertNotWriteLocked(lock);
225         assertEquals(1, lock.getReadLockCount());
226         lock.readLock().unlock();
227         assertNotWriteLocked(lock);
228         assertEquals(0, lock.getReadLockCount());
229     }
230 
231     /**
232      * getWriteHoldCount returns number of recursive holds
233      */
testGetWriteHoldCount()234     public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
testGetWriteHoldCount_fair()235     public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
testGetWriteHoldCount(boolean fair)236     public void testGetWriteHoldCount(boolean fair) {
237         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
238         for (int i = 1; i <= SIZE; i++) {
239             lock.writeLock().lock();
240             assertEquals(i,lock.getWriteHoldCount());
241         }
242         for (int i = SIZE; i > 0; i--) {
243             lock.writeLock().unlock();
244             assertEquals(i-1,lock.getWriteHoldCount());
245         }
246     }
247 
248     /**
249      * writelock.getHoldCount returns number of recursive holds
250      */
testGetHoldCount()251     public void testGetHoldCount()      { testGetHoldCount(false); }
testGetHoldCount_fair()252     public void testGetHoldCount_fair() { testGetHoldCount(true); }
testGetHoldCount(boolean fair)253     public void testGetHoldCount(boolean fair) {
254         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
255         for (int i = 1; i <= SIZE; i++) {
256             lock.writeLock().lock();
257             assertEquals(i,lock.writeLock().getHoldCount());
258         }
259         for (int i = SIZE; i > 0; i--) {
260             lock.writeLock().unlock();
261             assertEquals(i-1,lock.writeLock().getHoldCount());
262         }
263     }
264 
265     /**
266      * getReadHoldCount returns number of recursive holds
267      */
testGetReadHoldCount()268     public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
testGetReadHoldCount_fair()269     public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
testGetReadHoldCount(boolean fair)270     public void testGetReadHoldCount(boolean fair) {
271         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
272         for (int i = 1; i <= SIZE; i++) {
273             lock.readLock().lock();
274             assertEquals(i,lock.getReadHoldCount());
275         }
276         for (int i = SIZE; i > 0; i--) {
277             lock.readLock().unlock();
278             assertEquals(i-1,lock.getReadHoldCount());
279         }
280     }
281 
282     /**
283      * write-unlocking an unlocked lock throws IllegalMonitorStateException
284      */
testWriteUnlock_IMSE()285     public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
testWriteUnlock_IMSE_fair()286     public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
testWriteUnlock_IMSE(boolean fair)287     public void testWriteUnlock_IMSE(boolean fair) {
288         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
289         try {
290             lock.writeLock().unlock();
291             shouldThrow();
292         } catch (IllegalMonitorStateException success) {}
293     }
294 
295     /**
296      * read-unlocking an unlocked lock throws IllegalMonitorStateException
297      */
testReadUnlock_IMSE()298     public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
testReadUnlock_IMSE_fair()299     public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
testReadUnlock_IMSE(boolean fair)300     public void testReadUnlock_IMSE(boolean fair) {
301         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
302         try {
303             lock.readLock().unlock();
304             shouldThrow();
305         } catch (IllegalMonitorStateException success) {}
306     }
307 
308     /**
309      * write-lockInterruptibly is interruptible
310      */
testWriteLockInterruptibly_Interruptible()311     public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
testWriteLockInterruptibly_Interruptible_fair()312     public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
testWriteLockInterruptibly_Interruptible(boolean fair)313     public void testWriteLockInterruptibly_Interruptible(boolean fair) {
314         final PublicReentrantReadWriteLock lock =
315             new PublicReentrantReadWriteLock(fair);
316         lock.writeLock().lock();
317         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
318             public void realRun() throws InterruptedException {
319                 lock.writeLock().lockInterruptibly();
320             }});
321 
322         waitForQueuedThread(lock, t);
323         t.interrupt();
324         awaitTermination(t);
325         releaseWriteLock(lock);
326     }
327 
328     /**
329      * timed write-tryLock is interruptible
330      */
testWriteTryLock_Interruptible()331     public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
testWriteTryLock_Interruptible_fair()332     public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
testWriteTryLock_Interruptible(boolean fair)333     public void testWriteTryLock_Interruptible(boolean fair) {
334         final PublicReentrantReadWriteLock lock =
335             new PublicReentrantReadWriteLock(fair);
336         lock.writeLock().lock();
337         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
338             public void realRun() throws InterruptedException {
339                 lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
340             }});
341 
342         waitForQueuedThread(lock, t);
343         t.interrupt();
344         awaitTermination(t);
345         releaseWriteLock(lock);
346     }
347 
348     /**
349      * read-lockInterruptibly is interruptible
350      */
testReadLockInterruptibly_Interruptible()351     public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
testReadLockInterruptibly_Interruptible_fair()352     public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
testReadLockInterruptibly_Interruptible(boolean fair)353     public void testReadLockInterruptibly_Interruptible(boolean fair) {
354         final PublicReentrantReadWriteLock lock =
355             new PublicReentrantReadWriteLock(fair);
356         lock.writeLock().lock();
357         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
358             public void realRun() throws InterruptedException {
359                 lock.readLock().lockInterruptibly();
360             }});
361 
362         waitForQueuedThread(lock, t);
363         t.interrupt();
364         awaitTermination(t);
365         releaseWriteLock(lock);
366     }
367 
368     /**
369      * timed read-tryLock is interruptible
370      */
testReadTryLock_Interruptible()371     public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
testReadTryLock_Interruptible_fair()372     public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
testReadTryLock_Interruptible(boolean fair)373     public void testReadTryLock_Interruptible(boolean fair) {
374         final PublicReentrantReadWriteLock lock =
375             new PublicReentrantReadWriteLock(fair);
376         lock.writeLock().lock();
377         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
378             public void realRun() throws InterruptedException {
379                 lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
380             }});
381 
382         waitForQueuedThread(lock, t);
383         t.interrupt();
384         awaitTermination(t);
385         releaseWriteLock(lock);
386     }
387 
388     /**
389      * write-tryLock on an unlocked lock succeeds
390      */
testWriteTryLock()391     public void testWriteTryLock()      { testWriteTryLock(false); }
testWriteTryLock_fair()392     public void testWriteTryLock_fair() { testWriteTryLock(true); }
testWriteTryLock(boolean fair)393     public void testWriteTryLock(boolean fair) {
394         final PublicReentrantReadWriteLock lock =
395             new PublicReentrantReadWriteLock(fair);
396         assertTrue(lock.writeLock().tryLock());
397         assertWriteLockedByMoi(lock);
398         assertTrue(lock.writeLock().tryLock());
399         assertWriteLockedByMoi(lock);
400         lock.writeLock().unlock();
401         releaseWriteLock(lock);
402     }
403 
404     /**
405      * write-tryLock fails if locked
406      */
testWriteTryLockWhenLocked()407     public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
testWriteTryLockWhenLocked_fair()408     public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
testWriteTryLockWhenLocked(boolean fair)409     public void testWriteTryLockWhenLocked(boolean fair) {
410         final PublicReentrantReadWriteLock lock =
411             new PublicReentrantReadWriteLock(fair);
412         lock.writeLock().lock();
413         Thread t = newStartedThread(new CheckedRunnable() {
414             public void realRun() {
415                 assertFalse(lock.writeLock().tryLock());
416             }});
417 
418         awaitTermination(t);
419         releaseWriteLock(lock);
420     }
421 
422     /**
423      * read-tryLock fails if locked
424      */
testReadTryLockWhenLocked()425     public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
testReadTryLockWhenLocked_fair()426     public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
testReadTryLockWhenLocked(boolean fair)427     public void testReadTryLockWhenLocked(boolean fair) {
428         final PublicReentrantReadWriteLock lock =
429             new PublicReentrantReadWriteLock(fair);
430         lock.writeLock().lock();
431         Thread t = newStartedThread(new CheckedRunnable() {
432             public void realRun() {
433                 assertFalse(lock.readLock().tryLock());
434             }});
435 
436         awaitTermination(t);
437         releaseWriteLock(lock);
438     }
439 
440     /**
441      * Multiple threads can hold a read lock when not write-locked
442      */
testMultipleReadLocks()443     public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
testMultipleReadLocks_fair()444     public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
testMultipleReadLocks(boolean fair)445     public void testMultipleReadLocks(boolean fair) {
446         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
447         lock.readLock().lock();
448         Thread t = newStartedThread(new CheckedRunnable() {
449             public void realRun() throws InterruptedException {
450                 assertTrue(lock.readLock().tryLock());
451                 lock.readLock().unlock();
452                 assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
453                 lock.readLock().unlock();
454                 lock.readLock().lock();
455                 lock.readLock().unlock();
456             }});
457 
458         awaitTermination(t);
459         lock.readLock().unlock();
460     }
461 
462     /**
463      * A writelock succeeds only after a reading thread unlocks
464      */
testWriteAfterReadLock()465     public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
testWriteAfterReadLock_fair()466     public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
testWriteAfterReadLock(boolean fair)467     public void testWriteAfterReadLock(boolean fair) {
468         final PublicReentrantReadWriteLock lock =
469             new PublicReentrantReadWriteLock(fair);
470         lock.readLock().lock();
471         Thread t = newStartedThread(new CheckedRunnable() {
472             public void realRun() {
473                 assertEquals(1, lock.getReadLockCount());
474                 lock.writeLock().lock();
475                 assertEquals(0, lock.getReadLockCount());
476                 lock.writeLock().unlock();
477             }});
478         waitForQueuedThread(lock, t);
479         assertNotWriteLocked(lock);
480         assertEquals(1, lock.getReadLockCount());
481         lock.readLock().unlock();
482         assertEquals(0, lock.getReadLockCount());
483         awaitTermination(t);
484         assertNotWriteLocked(lock);
485     }
486 
487     /**
488      * A writelock succeeds only after reading threads unlock
489      */
testWriteAfterMultipleReadLocks()490     public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
testWriteAfterMultipleReadLocks_fair()491     public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
testWriteAfterMultipleReadLocks(boolean fair)492     public void testWriteAfterMultipleReadLocks(boolean fair) {
493         final PublicReentrantReadWriteLock lock =
494             new PublicReentrantReadWriteLock(fair);
495         lock.readLock().lock();
496         lock.readLock().lock();
497         Thread t1 = newStartedThread(new CheckedRunnable() {
498             public void realRun() {
499                 lock.readLock().lock();
500                 assertEquals(3, lock.getReadLockCount());
501                 lock.readLock().unlock();
502             }});
503         awaitTermination(t1);
504 
505         Thread t2 = newStartedThread(new CheckedRunnable() {
506             public void realRun() {
507                 assertEquals(2, lock.getReadLockCount());
508                 lock.writeLock().lock();
509                 assertEquals(0, lock.getReadLockCount());
510                 lock.writeLock().unlock();
511             }});
512         waitForQueuedThread(lock, t2);
513         assertNotWriteLocked(lock);
514         assertEquals(2, lock.getReadLockCount());
515         lock.readLock().unlock();
516         lock.readLock().unlock();
517         assertEquals(0, lock.getReadLockCount());
518         awaitTermination(t2);
519         assertNotWriteLocked(lock);
520     }
521 
522     /**
523      * A thread that tries to acquire a fair read lock (non-reentrantly)
524      * will block if there is a waiting writer thread
525      */
testReaderWriterReaderFairFifo()526     public void testReaderWriterReaderFairFifo() {
527         final PublicReentrantReadWriteLock lock =
528             new PublicReentrantReadWriteLock(true);
529         final AtomicBoolean t1GotLock = new AtomicBoolean(false);
530 
531         lock.readLock().lock();
532         Thread t1 = newStartedThread(new CheckedRunnable() {
533             public void realRun() {
534                 assertEquals(1, lock.getReadLockCount());
535                 lock.writeLock().lock();
536                 assertEquals(0, lock.getReadLockCount());
537                 t1GotLock.set(true);
538                 lock.writeLock().unlock();
539             }});
540         waitForQueuedThread(lock, t1);
541 
542         Thread t2 = newStartedThread(new CheckedRunnable() {
543             public void realRun() {
544                 assertEquals(1, lock.getReadLockCount());
545                 lock.readLock().lock();
546                 assertEquals(1, lock.getReadLockCount());
547                 assertTrue(t1GotLock.get());
548                 lock.readLock().unlock();
549             }});
550         waitForQueuedThread(lock, t2);
551         assertTrue(t1.isAlive());
552         assertNotWriteLocked(lock);
553         assertEquals(1, lock.getReadLockCount());
554         lock.readLock().unlock();
555         awaitTermination(t1);
556         awaitTermination(t2);
557         assertNotWriteLocked(lock);
558     }
559 
560     /**
561      * Readlocks succeed only after a writing thread unlocks
562      */
testReadAfterWriteLock()563     public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
testReadAfterWriteLock_fair()564     public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
testReadAfterWriteLock(boolean fair)565     public void testReadAfterWriteLock(boolean fair) {
566         final PublicReentrantReadWriteLock lock =
567             new PublicReentrantReadWriteLock(fair);
568         lock.writeLock().lock();
569         Thread t1 = newStartedThread(new CheckedRunnable() {
570             public void realRun() {
571                 lock.readLock().lock();
572                 lock.readLock().unlock();
573             }});
574         Thread t2 = newStartedThread(new CheckedRunnable() {
575             public void realRun() {
576                 lock.readLock().lock();
577                 lock.readLock().unlock();
578             }});
579 
580         waitForQueuedThread(lock, t1);
581         waitForQueuedThread(lock, t2);
582         releaseWriteLock(lock);
583         awaitTermination(t1);
584         awaitTermination(t2);
585     }
586 
587     /**
588      * Read trylock succeeds if write locked by current thread
589      */
testReadHoldingWriteLock()590     public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
testReadHoldingWriteLock_fair()591     public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
testReadHoldingWriteLock(boolean fair)592     public void testReadHoldingWriteLock(boolean fair) {
593         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
594         lock.writeLock().lock();
595         assertTrue(lock.readLock().tryLock());
596         lock.readLock().unlock();
597         lock.writeLock().unlock();
598     }
599 
600     /**
601      * Read trylock succeeds (barging) even in the presence of waiting
602      * readers and/or writers
603      */
testReadTryLockBarging()604     public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
testReadTryLockBarging_fair()605     public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
testReadTryLockBarging(boolean fair)606     public void testReadTryLockBarging(boolean fair) {
607         final PublicReentrantReadWriteLock lock =
608             new PublicReentrantReadWriteLock(fair);
609         lock.readLock().lock();
610 
611         Thread t1 = newStartedThread(new CheckedRunnable() {
612             public void realRun() {
613                 lock.writeLock().lock();
614                 lock.writeLock().unlock();
615             }});
616 
617         waitForQueuedThread(lock, t1);
618 
619         Thread t2 = newStartedThread(new CheckedRunnable() {
620             public void realRun() {
621                 lock.readLock().lock();
622                 lock.readLock().unlock();
623             }});
624 
625         if (fair)
626             waitForQueuedThread(lock, t2);
627 
628         Thread t3 = newStartedThread(new CheckedRunnable() {
629             public void realRun() {
630                 lock.readLock().tryLock();
631                 lock.readLock().unlock();
632             }});
633 
634         assertTrue(lock.getReadLockCount() > 0);
635         awaitTermination(t3);
636         assertTrue(t1.isAlive());
637         if (fair) assertTrue(t2.isAlive());
638         lock.readLock().unlock();
639         awaitTermination(t1);
640         awaitTermination(t2);
641     }
642 
643     /**
644      * Read lock succeeds if write locked by current thread even if
645      * other threads are waiting for readlock
646      */
testReadHoldingWriteLock2()647     public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
testReadHoldingWriteLock2_fair()648     public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
testReadHoldingWriteLock2(boolean fair)649     public void testReadHoldingWriteLock2(boolean fair) {
650         final PublicReentrantReadWriteLock lock =
651             new PublicReentrantReadWriteLock(fair);
652         lock.writeLock().lock();
653         lock.readLock().lock();
654         lock.readLock().unlock();
655 
656         Thread t1 = newStartedThread(new CheckedRunnable() {
657             public void realRun() {
658                 lock.readLock().lock();
659                 lock.readLock().unlock();
660             }});
661         Thread t2 = newStartedThread(new CheckedRunnable() {
662             public void realRun() {
663                 lock.readLock().lock();
664                 lock.readLock().unlock();
665             }});
666 
667         waitForQueuedThread(lock, t1);
668         waitForQueuedThread(lock, t2);
669         assertWriteLockedByMoi(lock);
670         lock.readLock().lock();
671         lock.readLock().unlock();
672         releaseWriteLock(lock);
673         awaitTermination(t1);
674         awaitTermination(t2);
675     }
676 
677     /**
678      * Read lock succeeds if write locked by current thread even if
679      * other threads are waiting for writelock
680      */
testReadHoldingWriteLock3()681     public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
testReadHoldingWriteLock3_fair()682     public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
testReadHoldingWriteLock3(boolean fair)683     public void testReadHoldingWriteLock3(boolean fair) {
684         final PublicReentrantReadWriteLock lock =
685             new PublicReentrantReadWriteLock(fair);
686         lock.writeLock().lock();
687         lock.readLock().lock();
688         lock.readLock().unlock();
689 
690         Thread t1 = newStartedThread(new CheckedRunnable() {
691             public void realRun() {
692                 lock.writeLock().lock();
693                 lock.writeLock().unlock();
694             }});
695         Thread t2 = newStartedThread(new CheckedRunnable() {
696             public void realRun() {
697                 lock.writeLock().lock();
698                 lock.writeLock().unlock();
699             }});
700 
701         waitForQueuedThread(lock, t1);
702         waitForQueuedThread(lock, t2);
703         assertWriteLockedByMoi(lock);
704         lock.readLock().lock();
705         lock.readLock().unlock();
706         assertWriteLockedByMoi(lock);
707         lock.writeLock().unlock();
708         awaitTermination(t1);
709         awaitTermination(t2);
710     }
711 
712     /**
713      * Write lock succeeds if write locked by current thread even if
714      * other threads are waiting for writelock
715      */
testWriteHoldingWriteLock4()716     public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
testWriteHoldingWriteLock4_fair()717     public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
testWriteHoldingWriteLock4(boolean fair)718     public void testWriteHoldingWriteLock4(boolean fair) {
719         final PublicReentrantReadWriteLock lock =
720             new PublicReentrantReadWriteLock(fair);
721         lock.writeLock().lock();
722         lock.writeLock().lock();
723         lock.writeLock().unlock();
724 
725         Thread t1 = newStartedThread(new CheckedRunnable() {
726             public void realRun() {
727                 lock.writeLock().lock();
728                 lock.writeLock().unlock();
729             }});
730         Thread t2 = newStartedThread(new CheckedRunnable() {
731             public void realRun() {
732                 lock.writeLock().lock();
733                 lock.writeLock().unlock();
734             }});
735 
736         waitForQueuedThread(lock, t1);
737         waitForQueuedThread(lock, t2);
738         assertWriteLockedByMoi(lock);
739         assertEquals(1, lock.getWriteHoldCount());
740         lock.writeLock().lock();
741         assertWriteLockedByMoi(lock);
742         assertEquals(2, lock.getWriteHoldCount());
743         lock.writeLock().unlock();
744         assertWriteLockedByMoi(lock);
745         assertEquals(1, lock.getWriteHoldCount());
746         lock.writeLock().unlock();
747         awaitTermination(t1);
748         awaitTermination(t2);
749     }
750 
751     /**
752      * Read tryLock succeeds if readlocked but not writelocked
753      */
testTryLockWhenReadLocked()754     public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
testTryLockWhenReadLocked_fair()755     public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
testTryLockWhenReadLocked(boolean fair)756     public void testTryLockWhenReadLocked(boolean fair) {
757         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
758         lock.readLock().lock();
759         Thread t = newStartedThread(new CheckedRunnable() {
760             public void realRun() {
761                 assertTrue(lock.readLock().tryLock());
762                 lock.readLock().unlock();
763             }});
764 
765         awaitTermination(t);
766         lock.readLock().unlock();
767     }
768 
769     /**
770      * write tryLock fails when readlocked
771      */
testWriteTryLockWhenReadLocked()772     public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
testWriteTryLockWhenReadLocked_fair()773     public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
testWriteTryLockWhenReadLocked(boolean fair)774     public void testWriteTryLockWhenReadLocked(boolean fair) {
775         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
776         lock.readLock().lock();
777         Thread t = newStartedThread(new CheckedRunnable() {
778             public void realRun() {
779                 assertFalse(lock.writeLock().tryLock());
780             }});
781 
782         awaitTermination(t);
783         lock.readLock().unlock();
784     }
785 
786     /**
787      * write timed tryLock times out if locked
788      */
testWriteTryLock_Timeout()789     public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
testWriteTryLock_Timeout_fair()790     public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
testWriteTryLock_Timeout(boolean fair)791     public void testWriteTryLock_Timeout(boolean fair) {
792         final PublicReentrantReadWriteLock lock =
793             new PublicReentrantReadWriteLock(fair);
794         lock.writeLock().lock();
795         Thread t = newStartedThread(new CheckedRunnable() {
796             public void realRun() throws InterruptedException {
797                 long startTime = System.nanoTime();
798                 long timeoutMillis = 10;
799                 assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
800                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
801             }});
802 
803         awaitTermination(t);
804         releaseWriteLock(lock);
805     }
806 
807     /**
808      * read timed tryLock times out if write-locked
809      */
testReadTryLock_Timeout()810     public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
testReadTryLock_Timeout_fair()811     public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
testReadTryLock_Timeout(boolean fair)812     public void testReadTryLock_Timeout(boolean fair) {
813         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
814         lock.writeLock().lock();
815         Thread t = newStartedThread(new CheckedRunnable() {
816             public void realRun() throws InterruptedException {
817                 long startTime = System.nanoTime();
818                 long timeoutMillis = 10;
819                 assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
820                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
821             }});
822 
823         awaitTermination(t);
824         assertTrue(lock.writeLock().isHeldByCurrentThread());
825         lock.writeLock().unlock();
826     }
827 
828     /**
829      * write lockInterruptibly succeeds if unlocked, else is interruptible
830      */
testWriteLockInterruptibly()831     public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
testWriteLockInterruptibly_fair()832     public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
testWriteLockInterruptibly(boolean fair)833     public void testWriteLockInterruptibly(boolean fair) {
834         final PublicReentrantReadWriteLock lock =
835             new PublicReentrantReadWriteLock(fair);
836         try {
837             lock.writeLock().lockInterruptibly();
838         } catch (InterruptedException fail) { threadUnexpectedException(fail); }
839         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
840             public void realRun() throws InterruptedException {
841                 lock.writeLock().lockInterruptibly();
842             }});
843 
844         waitForQueuedThread(lock, t);
845         t.interrupt();
846         assertTrue(lock.writeLock().isHeldByCurrentThread());
847         awaitTermination(t);
848         releaseWriteLock(lock);
849     }
850 
851     /**
852      * read lockInterruptibly succeeds if lock free else is interruptible
853      */
testReadLockInterruptibly()854     public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
testReadLockInterruptibly_fair()855     public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
testReadLockInterruptibly(boolean fair)856     public void testReadLockInterruptibly(boolean fair) {
857         final PublicReentrantReadWriteLock lock =
858             new PublicReentrantReadWriteLock(fair);
859         try {
860             lock.readLock().lockInterruptibly();
861             lock.readLock().unlock();
862             lock.writeLock().lockInterruptibly();
863         } catch (InterruptedException fail) { threadUnexpectedException(fail); }
864         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
865             public void realRun() throws InterruptedException {
866                 lock.readLock().lockInterruptibly();
867             }});
868 
869         waitForQueuedThread(lock, t);
870         t.interrupt();
871         awaitTermination(t);
872         releaseWriteLock(lock);
873     }
874 
875     /**
876      * Calling await without holding lock throws IllegalMonitorStateException
877      */
testAwait_IMSE()878     public void testAwait_IMSE()      { testAwait_IMSE(false); }
testAwait_IMSE_fair()879     public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
testAwait_IMSE(boolean fair)880     public void testAwait_IMSE(boolean fair) {
881         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
882         final Condition c = lock.writeLock().newCondition();
883         for (AwaitMethod awaitMethod : AwaitMethod.values()) {
884             long startTime = System.nanoTime();
885             try {
886                 await(c, awaitMethod);
887                 shouldThrow();
888             } catch (IllegalMonitorStateException success) {
889             } catch (InterruptedException fail) {
890                 threadUnexpectedException(fail);
891             }
892             assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
893         }
894     }
895 
896     /**
897      * Calling signal without holding lock throws IllegalMonitorStateException
898      */
899     public void testSignal_IMSE()      { testSignal_IMSE(false); }
900     public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
901     public void testSignal_IMSE(boolean fair) {
902         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
903         final Condition c = lock.writeLock().newCondition();
904         try {
905             c.signal();
906             shouldThrow();
907         } catch (IllegalMonitorStateException success) {}
908     }
909 
910     /**
911      * Calling signalAll without holding lock throws IllegalMonitorStateException
912      */
913     public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
914     public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
915     public void testSignalAll_IMSE(boolean fair) {
916         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
917         final Condition c = lock.writeLock().newCondition();
918         try {
919             c.signalAll();
920             shouldThrow();
921         } catch (IllegalMonitorStateException success) {}
922     }
923 
924     /**
925      * awaitNanos without a signal times out
926      */
927     public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
928     public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
929     public void testAwaitNanos_Timeout(boolean fair) {
930         try {
931             final ReentrantReadWriteLock lock =
932                 new ReentrantReadWriteLock(fair);
933             final Condition c = lock.writeLock().newCondition();
934             lock.writeLock().lock();
935             long startTime = System.nanoTime();
936             long timeoutMillis = 10;
937             long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
938             long nanosRemaining = c.awaitNanos(timeoutNanos);
939             assertTrue(nanosRemaining <= 0);
940             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
941             lock.writeLock().unlock();
942         } catch (InterruptedException fail) { threadUnexpectedException(fail); }
943     }
944 
945     /**
946      * timed await without a signal times out
947      */
948     public void testAwait_Timeout()      { testAwait_Timeout(false); }
949     public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
950     public void testAwait_Timeout(boolean fair) {
951         try {
952             final ReentrantReadWriteLock lock =
953                 new ReentrantReadWriteLock(fair);
954             final Condition c = lock.writeLock().newCondition();
955             lock.writeLock().lock();
956             long startTime = System.nanoTime();
957             long timeoutMillis = 10;
958             assertFalse(c.await(timeoutMillis, MILLISECONDS));
959             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
960             lock.writeLock().unlock();
961         } catch (InterruptedException fail) { threadUnexpectedException(fail); }
962     }
963 
964     /**
965      * awaitUntil without a signal times out
966      */
testAwaitUntil_Timeout()967     public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
testAwaitUntil_Timeout_fair()968     public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
testAwaitUntil_Timeout(boolean fair)969     public void testAwaitUntil_Timeout(boolean fair) {
970         try {
971             final ReentrantReadWriteLock lock =
972                 new ReentrantReadWriteLock(fair);
973             final Condition c = lock.writeLock().newCondition();
974             lock.writeLock().lock();
975             long startTime = System.nanoTime();
976             long timeoutMillis = 10;
977             java.util.Date d = new java.util.Date();
978             assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
979             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
980             lock.writeLock().unlock();
981         } catch (InterruptedException fail) { threadUnexpectedException(fail); }
982     }
983 
984     /**
985      * await returns when signalled
986      */
testAwait()987     public void testAwait()      { testAwait(false); }
testAwait_fair()988     public void testAwait_fair() { testAwait(true); }
testAwait(boolean fair)989     public void testAwait(boolean fair) {
990         final PublicReentrantReadWriteLock lock =
991             new PublicReentrantReadWriteLock(fair);
992         final Condition c = lock.writeLock().newCondition();
993         final CountDownLatch locked = new CountDownLatch(1);
994         Thread t = newStartedThread(new CheckedRunnable() {
995             public void realRun() throws InterruptedException {
996                 lock.writeLock().lock();
997                 locked.countDown();
998                 c.await();
999                 lock.writeLock().unlock();
1000             }});
1001 
1002         await(locked);
1003         lock.writeLock().lock();
1004         assertHasWaiters(lock, c, t);
1005         c.signal();
1006         assertHasNoWaiters(lock, c);
1007         assertTrue(t.isAlive());
1008         lock.writeLock().unlock();
1009         awaitTermination(t);
1010     }
1011 
1012     /**
1013      * awaitUninterruptibly is uninterruptible
1014      */
testAwaitUninterruptibly()1015     public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
testAwaitUninterruptibly_fair()1016     public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
testAwaitUninterruptibly(boolean fair)1017     public void testAwaitUninterruptibly(boolean fair) {
1018         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1019         final Condition c = lock.writeLock().newCondition();
1020         final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1021 
1022         Thread t1 = newStartedThread(new CheckedRunnable() {
1023             public void realRun() {
1024                 // Interrupt before awaitUninterruptibly
1025                 lock.writeLock().lock();
1026                 pleaseInterrupt.countDown();
1027                 Thread.currentThread().interrupt();
1028                 c.awaitUninterruptibly();
1029                 assertTrue(Thread.interrupted());
1030                 lock.writeLock().unlock();
1031             }});
1032 
1033         Thread t2 = newStartedThread(new CheckedRunnable() {
1034             public void realRun() {
1035                 // Interrupt during awaitUninterruptibly
1036                 lock.writeLock().lock();
1037                 pleaseInterrupt.countDown();
1038                 c.awaitUninterruptibly();
1039                 assertTrue(Thread.interrupted());
1040                 lock.writeLock().unlock();
1041             }});
1042 
1043         await(pleaseInterrupt);
1044         lock.writeLock().lock();
1045         lock.writeLock().unlock();
1046         t2.interrupt();
1047 
1048         assertThreadStaysAlive(t1);
1049         assertTrue(t2.isAlive());
1050 
1051         lock.writeLock().lock();
1052         c.signalAll();
1053         lock.writeLock().unlock();
1054 
1055         awaitTermination(t1);
1056         awaitTermination(t2);
1057     }
1058 
1059     /**
1060      * await/awaitNanos/awaitUntil is interruptible
1061      */
testInterruptible_await()1062     public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
testInterruptible_await_fair()1063     public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
testInterruptible_awaitTimed()1064     public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
testInterruptible_awaitTimed_fair()1065     public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
testInterruptible_awaitNanos()1066     public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
testInterruptible_awaitNanos_fair()1067     public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
testInterruptible_awaitUntil()1068     public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
testInterruptible_awaitUntil_fair()1069     public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
testInterruptible(boolean fair, final AwaitMethod awaitMethod)1070     public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1071         final PublicReentrantReadWriteLock lock =
1072             new PublicReentrantReadWriteLock(fair);
1073         final Condition c = lock.writeLock().newCondition();
1074         final CountDownLatch locked = new CountDownLatch(1);
1075         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1076             public void realRun() throws InterruptedException {
1077                 lock.writeLock().lock();
1078                 assertWriteLockedByMoi(lock);
1079                 assertHasNoWaiters(lock, c);
1080                 locked.countDown();
1081                 try {
1082                     await(c, awaitMethod);
1083                 } finally {
1084                     assertWriteLockedByMoi(lock);
1085                     assertHasNoWaiters(lock, c);
1086                     lock.writeLock().unlock();
1087                     assertFalse(Thread.interrupted());
1088                 }
1089             }});
1090 
1091         await(locked);
1092         assertHasWaiters(lock, c, t);
1093         t.interrupt();
1094         awaitTermination(t);
1095         assertNotWriteLocked(lock);
1096     }
1097 
1098     /**
1099      * signalAll wakes up all threads
1100      */
testSignalAll_await()1101     public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
testSignalAll_await_fair()1102     public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
testSignalAll_awaitTimed()1103     public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
testSignalAll_awaitTimed_fair()1104     public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
testSignalAll_awaitNanos()1105     public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
testSignalAll_awaitNanos_fair()1106     public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
testSignalAll_awaitUntil()1107     public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
testSignalAll_awaitUntil_fair()1108     public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
testSignalAll(boolean fair, final AwaitMethod awaitMethod)1109     public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1110         final PublicReentrantReadWriteLock lock =
1111             new PublicReentrantReadWriteLock(fair);
1112         final Condition c = lock.writeLock().newCondition();
1113         final CountDownLatch locked = new CountDownLatch(2);
1114         final Lock writeLock = lock.writeLock();
1115         class Awaiter extends CheckedRunnable {
1116             public void realRun() throws InterruptedException {
1117                 writeLock.lock();
1118                 locked.countDown();
1119                 await(c, awaitMethod);
1120                 writeLock.unlock();
1121             }
1122         }
1123 
1124         Thread t1 = newStartedThread(new Awaiter());
1125         Thread t2 = newStartedThread(new Awaiter());
1126 
1127         await(locked);
1128         writeLock.lock();
1129         assertHasWaiters(lock, c, t1, t2);
1130         c.signalAll();
1131         assertHasNoWaiters(lock, c);
1132         writeLock.unlock();
1133         awaitTermination(t1);
1134         awaitTermination(t2);
1135     }
1136 
1137     /**
1138      * signal wakes up waiting threads in FIFO order
1139      */
testSignalWakesFifo()1140     public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
testSignalWakesFifo_fair()1141     public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
testSignalWakesFifo(boolean fair)1142     public void testSignalWakesFifo(boolean fair) {
1143         final PublicReentrantReadWriteLock lock =
1144             new PublicReentrantReadWriteLock(fair);
1145         final Condition c = lock.writeLock().newCondition();
1146         final CountDownLatch locked1 = new CountDownLatch(1);
1147         final CountDownLatch locked2 = new CountDownLatch(1);
1148         final Lock writeLock = lock.writeLock();
1149         Thread t1 = newStartedThread(new CheckedRunnable() {
1150             public void realRun() throws InterruptedException {
1151                 writeLock.lock();
1152                 locked1.countDown();
1153                 c.await();
1154                 writeLock.unlock();
1155             }});
1156 
1157         await(locked1);
1158 
1159         Thread t2 = newStartedThread(new CheckedRunnable() {
1160             public void realRun() throws InterruptedException {
1161                 writeLock.lock();
1162                 locked2.countDown();
1163                 c.await();
1164                 writeLock.unlock();
1165             }});
1166 
1167         await(locked2);
1168 
1169         writeLock.lock();
1170         assertHasWaiters(lock, c, t1, t2);
1171         assertFalse(lock.hasQueuedThreads());
1172         c.signal();
1173         assertHasWaiters(lock, c, t2);
1174         assertTrue(lock.hasQueuedThread(t1));
1175         assertFalse(lock.hasQueuedThread(t2));
1176         c.signal();
1177         assertHasNoWaiters(lock, c);
1178         assertTrue(lock.hasQueuedThread(t1));
1179         assertTrue(lock.hasQueuedThread(t2));
1180         writeLock.unlock();
1181         awaitTermination(t1);
1182         awaitTermination(t2);
1183     }
1184 
1185     /**
1186      * await after multiple reentrant locking preserves lock count
1187      */
testAwaitLockCount()1188     public void testAwaitLockCount()      { testAwaitLockCount(false); }
testAwaitLockCount_fair()1189     public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
testAwaitLockCount(boolean fair)1190     public void testAwaitLockCount(boolean fair) {
1191         final PublicReentrantReadWriteLock lock =
1192             new PublicReentrantReadWriteLock(fair);
1193         final Condition c = lock.writeLock().newCondition();
1194         final CountDownLatch locked = new CountDownLatch(2);
1195         Thread t1 = newStartedThread(new CheckedRunnable() {
1196             public void realRun() throws InterruptedException {
1197                 lock.writeLock().lock();
1198                 assertWriteLockedByMoi(lock);
1199                 assertEquals(1, lock.writeLock().getHoldCount());
1200                 locked.countDown();
1201                 c.await();
1202                 assertWriteLockedByMoi(lock);
1203                 assertEquals(1, lock.writeLock().getHoldCount());
1204                 lock.writeLock().unlock();
1205             }});
1206 
1207         Thread t2 = newStartedThread(new CheckedRunnable() {
1208             public void realRun() throws InterruptedException {
1209                 lock.writeLock().lock();
1210                 lock.writeLock().lock();
1211                 assertWriteLockedByMoi(lock);
1212                 assertEquals(2, lock.writeLock().getHoldCount());
1213                 locked.countDown();
1214                 c.await();
1215                 assertWriteLockedByMoi(lock);
1216                 assertEquals(2, lock.writeLock().getHoldCount());
1217                 lock.writeLock().unlock();
1218                 lock.writeLock().unlock();
1219             }});
1220 
1221         await(locked);
1222         lock.writeLock().lock();
1223         assertHasWaiters(lock, c, t1, t2);
1224         c.signalAll();
1225         assertHasNoWaiters(lock, c);
1226         lock.writeLock().unlock();
1227         awaitTermination(t1);
1228         awaitTermination(t2);
1229     }
1230 
1231     /**
1232      * A serialized lock deserializes as unlocked
1233      */
testSerialization()1234     public void testSerialization()      { testSerialization(false); }
testSerialization_fair()1235     public void testSerialization_fair() { testSerialization(true); }
testSerialization(boolean fair)1236     public void testSerialization(boolean fair) {
1237         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1238         lock.writeLock().lock();
1239         lock.readLock().lock();
1240 
1241         ReentrantReadWriteLock clone = serialClone(lock);
1242         assertEquals(lock.isFair(), clone.isFair());
1243         assertTrue(lock.isWriteLocked());
1244         assertFalse(clone.isWriteLocked());
1245         assertEquals(1, lock.getReadLockCount());
1246         assertEquals(0, clone.getReadLockCount());
1247         clone.writeLock().lock();
1248         clone.readLock().lock();
1249         assertTrue(clone.isWriteLocked());
1250         assertEquals(1, clone.getReadLockCount());
1251         clone.readLock().unlock();
1252         clone.writeLock().unlock();
1253         assertFalse(clone.isWriteLocked());
1254         assertEquals(1, lock.getReadLockCount());
1255         assertEquals(0, clone.getReadLockCount());
1256     }
1257 
1258     /**
1259      * hasQueuedThreads reports whether there are waiting threads
1260      */
testHasQueuedThreads()1261     public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
testHasQueuedThreads_fair()1262     public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
testHasQueuedThreads(boolean fair)1263     public void testHasQueuedThreads(boolean fair) {
1264         final PublicReentrantReadWriteLock lock =
1265             new PublicReentrantReadWriteLock(fair);
1266         Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1267         Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1268         assertFalse(lock.hasQueuedThreads());
1269         lock.writeLock().lock();
1270         assertFalse(lock.hasQueuedThreads());
1271         t1.start();
1272         waitForQueuedThread(lock, t1);
1273         assertTrue(lock.hasQueuedThreads());
1274         t2.start();
1275         waitForQueuedThread(lock, t2);
1276         assertTrue(lock.hasQueuedThreads());
1277         t1.interrupt();
1278         awaitTermination(t1);
1279         assertTrue(lock.hasQueuedThreads());
1280         lock.writeLock().unlock();
1281         awaitTermination(t2);
1282         assertFalse(lock.hasQueuedThreads());
1283     }
1284 
1285     /**
1286      * hasQueuedThread(null) throws NPE
1287      */
testHasQueuedThreadNPE()1288     public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
testHasQueuedThreadNPE_fair()1289     public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
testHasQueuedThreadNPE(boolean fair)1290     public void testHasQueuedThreadNPE(boolean fair) {
1291         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1292         try {
1293             lock.hasQueuedThread(null);
1294             shouldThrow();
1295         } catch (NullPointerException success) {}
1296     }
1297 
1298     /**
1299      * hasQueuedThread reports whether a thread is queued
1300      */
testHasQueuedThread()1301     public void testHasQueuedThread()      { testHasQueuedThread(false); }
testHasQueuedThread_fair()1302     public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
testHasQueuedThread(boolean fair)1303     public void testHasQueuedThread(boolean fair) {
1304         final PublicReentrantReadWriteLock lock =
1305             new PublicReentrantReadWriteLock(fair);
1306         Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1307         Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1308         assertFalse(lock.hasQueuedThread(t1));
1309         assertFalse(lock.hasQueuedThread(t2));
1310         lock.writeLock().lock();
1311         t1.start();
1312         waitForQueuedThread(lock, t1);
1313         assertTrue(lock.hasQueuedThread(t1));
1314         assertFalse(lock.hasQueuedThread(t2));
1315         t2.start();
1316         waitForQueuedThread(lock, t2);
1317         assertTrue(lock.hasQueuedThread(t1));
1318         assertTrue(lock.hasQueuedThread(t2));
1319         t1.interrupt();
1320         awaitTermination(t1);
1321         assertFalse(lock.hasQueuedThread(t1));
1322         assertTrue(lock.hasQueuedThread(t2));
1323         lock.writeLock().unlock();
1324         awaitTermination(t2);
1325         assertFalse(lock.hasQueuedThread(t1));
1326         assertFalse(lock.hasQueuedThread(t2));
1327     }
1328 
1329     /**
1330      * getQueueLength reports number of waiting threads
1331      */
testGetQueueLength()1332     public void testGetQueueLength()      { testGetQueueLength(false); }
testGetQueueLength_fair()1333     public void testGetQueueLength_fair() { testGetQueueLength(true); }
testGetQueueLength(boolean fair)1334     public void testGetQueueLength(boolean fair) {
1335         final PublicReentrantReadWriteLock lock =
1336             new PublicReentrantReadWriteLock(fair);
1337         Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1338         Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1339         assertEquals(0, lock.getQueueLength());
1340         lock.writeLock().lock();
1341         t1.start();
1342         waitForQueuedThread(lock, t1);
1343         assertEquals(1, lock.getQueueLength());
1344         t2.start();
1345         waitForQueuedThread(lock, t2);
1346         assertEquals(2, lock.getQueueLength());
1347         t1.interrupt();
1348         awaitTermination(t1);
1349         assertEquals(1, lock.getQueueLength());
1350         lock.writeLock().unlock();
1351         awaitTermination(t2);
1352         assertEquals(0, lock.getQueueLength());
1353     }
1354 
1355     /**
1356      * getQueuedThreads includes waiting threads
1357      */
testGetQueuedThreads()1358     public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
testGetQueuedThreads_fair()1359     public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
testGetQueuedThreads(boolean fair)1360     public void testGetQueuedThreads(boolean fair) {
1361         final PublicReentrantReadWriteLock lock =
1362             new PublicReentrantReadWriteLock(fair);
1363         Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1364         Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1365         assertTrue(lock.getQueuedThreads().isEmpty());
1366         lock.writeLock().lock();
1367         assertTrue(lock.getQueuedThreads().isEmpty());
1368         t1.start();
1369         waitForQueuedThread(lock, t1);
1370         assertEquals(1, lock.getQueuedThreads().size());
1371         assertTrue(lock.getQueuedThreads().contains(t1));
1372         t2.start();
1373         waitForQueuedThread(lock, t2);
1374         assertEquals(2, lock.getQueuedThreads().size());
1375         assertTrue(lock.getQueuedThreads().contains(t1));
1376         assertTrue(lock.getQueuedThreads().contains(t2));
1377         t1.interrupt();
1378         awaitTermination(t1);
1379         assertFalse(lock.getQueuedThreads().contains(t1));
1380         assertTrue(lock.getQueuedThreads().contains(t2));
1381         assertEquals(1, lock.getQueuedThreads().size());
1382         lock.writeLock().unlock();
1383         awaitTermination(t2);
1384         assertTrue(lock.getQueuedThreads().isEmpty());
1385     }
1386 
1387     /**
1388      * hasWaiters throws NPE if null
1389      */
testHasWaitersNPE()1390     public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
testHasWaitersNPE_fair()1391     public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
testHasWaitersNPE(boolean fair)1392     public void testHasWaitersNPE(boolean fair) {
1393         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1394         try {
1395             lock.hasWaiters(null);
1396             shouldThrow();
1397         } catch (NullPointerException success) {}
1398     }
1399 
1400     /**
1401      * getWaitQueueLength throws NPE if null
1402      */
testGetWaitQueueLengthNPE()1403     public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
testGetWaitQueueLengthNPE_fair()1404     public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
testGetWaitQueueLengthNPE(boolean fair)1405     public void testGetWaitQueueLengthNPE(boolean fair) {
1406         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1407         try {
1408             lock.getWaitQueueLength(null);
1409             shouldThrow();
1410         } catch (NullPointerException success) {}
1411     }
1412 
1413     /**
1414      * getWaitingThreads throws NPE if null
1415      */
testGetWaitingThreadsNPE()1416     public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
testGetWaitingThreadsNPE_fair()1417     public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
testGetWaitingThreadsNPE(boolean fair)1418     public void testGetWaitingThreadsNPE(boolean fair) {
1419         final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1420         try {
1421             lock.getWaitingThreads(null);
1422             shouldThrow();
1423         } catch (NullPointerException success) {}
1424     }
1425 
1426     /**
1427      * hasWaiters throws IllegalArgumentException if not owned
1428      */
testHasWaitersIAE()1429     public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
testHasWaitersIAE_fair()1430     public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
testHasWaitersIAE(boolean fair)1431     public void testHasWaitersIAE(boolean fair) {
1432         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1433         final Condition c = lock.writeLock().newCondition();
1434         final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1435         try {
1436             lock2.hasWaiters(c);
1437             shouldThrow();
1438         } catch (IllegalArgumentException success) {}
1439     }
1440 
1441     /**
1442      * hasWaiters throws IllegalMonitorStateException if not locked
1443      */
testHasWaitersIMSE()1444     public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
testHasWaitersIMSE_fair()1445     public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
testHasWaitersIMSE(boolean fair)1446     public void testHasWaitersIMSE(boolean fair) {
1447         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1448         final Condition c = lock.writeLock().newCondition();
1449         try {
1450             lock.hasWaiters(c);
1451             shouldThrow();
1452         } catch (IllegalMonitorStateException success) {}
1453     }
1454 
1455     /**
1456      * getWaitQueueLength throws IllegalArgumentException if not owned
1457      */
testGetWaitQueueLengthIAE()1458     public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
testGetWaitQueueLengthIAE_fair()1459     public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
testGetWaitQueueLengthIAE(boolean fair)1460     public void testGetWaitQueueLengthIAE(boolean fair) {
1461         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1462         final Condition c = lock.writeLock().newCondition();
1463         final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1464         try {
1465             lock2.getWaitQueueLength(c);
1466             shouldThrow();
1467         } catch (IllegalArgumentException success) {}
1468     }
1469 
1470     /**
1471      * getWaitQueueLength throws IllegalMonitorStateException if not locked
1472      */
testGetWaitQueueLengthIMSE()1473     public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
testGetWaitQueueLengthIMSE_fair()1474     public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
testGetWaitQueueLengthIMSE(boolean fair)1475     public void testGetWaitQueueLengthIMSE(boolean fair) {
1476         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1477         final Condition c = lock.writeLock().newCondition();
1478         try {
1479             lock.getWaitQueueLength(c);
1480             shouldThrow();
1481         } catch (IllegalMonitorStateException success) {}
1482     }
1483 
1484     /**
1485      * getWaitingThreads throws IllegalArgumentException if not owned
1486      */
testGetWaitingThreadsIAE()1487     public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
testGetWaitingThreadsIAE_fair()1488     public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
testGetWaitingThreadsIAE(boolean fair)1489     public void testGetWaitingThreadsIAE(boolean fair) {
1490         final PublicReentrantReadWriteLock lock =
1491             new PublicReentrantReadWriteLock(fair);
1492         final Condition c = lock.writeLock().newCondition();
1493         final PublicReentrantReadWriteLock lock2 =
1494             new PublicReentrantReadWriteLock(fair);
1495         try {
1496             lock2.getWaitingThreads(c);
1497             shouldThrow();
1498         } catch (IllegalArgumentException success) {}
1499     }
1500 
1501     /**
1502      * getWaitingThreads throws IllegalMonitorStateException if not locked
1503      */
testGetWaitingThreadsIMSE()1504     public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
testGetWaitingThreadsIMSE_fair()1505     public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
testGetWaitingThreadsIMSE(boolean fair)1506     public void testGetWaitingThreadsIMSE(boolean fair) {
1507         final PublicReentrantReadWriteLock lock =
1508             new PublicReentrantReadWriteLock(fair);
1509         final Condition c = lock.writeLock().newCondition();
1510         try {
1511             lock.getWaitingThreads(c);
1512             shouldThrow();
1513         } catch (IllegalMonitorStateException success) {}
1514     }
1515 
1516     /**
1517      * hasWaiters returns true when a thread is waiting, else false
1518      */
testHasWaiters()1519     public void testHasWaiters()      { testHasWaiters(false); }
testHasWaiters_fair()1520     public void testHasWaiters_fair() { testHasWaiters(true); }
testHasWaiters(boolean fair)1521     public void testHasWaiters(boolean fair) {
1522         final PublicReentrantReadWriteLock lock =
1523             new PublicReentrantReadWriteLock(fair);
1524         final Condition c = lock.writeLock().newCondition();
1525         final CountDownLatch locked = new CountDownLatch(1);
1526         Thread t = newStartedThread(new CheckedRunnable() {
1527             public void realRun() throws InterruptedException {
1528                 lock.writeLock().lock();
1529                 assertHasNoWaiters(lock, c);
1530                 assertFalse(lock.hasWaiters(c));
1531                 locked.countDown();
1532                 c.await();
1533                 assertHasNoWaiters(lock, c);
1534                 assertFalse(lock.hasWaiters(c));
1535                 lock.writeLock().unlock();
1536             }});
1537 
1538         await(locked);
1539         lock.writeLock().lock();
1540         assertHasWaiters(lock, c, t);
1541         assertTrue(lock.hasWaiters(c));
1542         c.signal();
1543         assertHasNoWaiters(lock, c);
1544         assertFalse(lock.hasWaiters(c));
1545         lock.writeLock().unlock();
1546         awaitTermination(t);
1547         assertHasNoWaiters(lock, c);
1548     }
1549 
1550     /**
1551      * getWaitQueueLength returns number of waiting threads
1552      */
testGetWaitQueueLength()1553     public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
testGetWaitQueueLength_fair()1554     public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
testGetWaitQueueLength(boolean fair)1555     public void testGetWaitQueueLength(boolean fair) {
1556         final PublicReentrantReadWriteLock lock =
1557             new PublicReentrantReadWriteLock(fair);
1558         final Condition c = lock.writeLock().newCondition();
1559         final CountDownLatch locked = new CountDownLatch(1);
1560         Thread t = newStartedThread(new CheckedRunnable() {
1561             public void realRun() throws InterruptedException {
1562                 lock.writeLock().lock();
1563                 assertEquals(0, lock.getWaitQueueLength(c));
1564                 locked.countDown();
1565                 c.await();
1566                 lock.writeLock().unlock();
1567             }});
1568 
1569         await(locked);
1570         lock.writeLock().lock();
1571         assertHasWaiters(lock, c, t);
1572         assertEquals(1, lock.getWaitQueueLength(c));
1573         c.signal();
1574         assertHasNoWaiters(lock, c);
1575         assertEquals(0, lock.getWaitQueueLength(c));
1576         lock.writeLock().unlock();
1577         awaitTermination(t);
1578     }
1579 
1580     /**
1581      * getWaitingThreads returns only and all waiting threads
1582      */
testGetWaitingThreads()1583     public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
testGetWaitingThreads_fair()1584     public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
testGetWaitingThreads(boolean fair)1585     public void testGetWaitingThreads(boolean fair) {
1586         final PublicReentrantReadWriteLock lock =
1587             new PublicReentrantReadWriteLock(fair);
1588         final Condition c = lock.writeLock().newCondition();
1589         final CountDownLatch locked1 = new CountDownLatch(1);
1590         final CountDownLatch locked2 = new CountDownLatch(1);
1591         Thread t1 = new Thread(new CheckedRunnable() {
1592             public void realRun() throws InterruptedException {
1593                 lock.writeLock().lock();
1594                 assertTrue(lock.getWaitingThreads(c).isEmpty());
1595                 locked1.countDown();
1596                 c.await();
1597                 lock.writeLock().unlock();
1598             }});
1599 
1600         Thread t2 = new Thread(new CheckedRunnable() {
1601             public void realRun() throws InterruptedException {
1602                 lock.writeLock().lock();
1603                 assertFalse(lock.getWaitingThreads(c).isEmpty());
1604                 locked2.countDown();
1605                 c.await();
1606                 lock.writeLock().unlock();
1607             }});
1608 
1609         lock.writeLock().lock();
1610         assertTrue(lock.getWaitingThreads(c).isEmpty());
1611         lock.writeLock().unlock();
1612 
1613         t1.start();
1614         await(locked1);
1615         t2.start();
1616         await(locked2);
1617 
1618         lock.writeLock().lock();
1619         assertTrue(lock.hasWaiters(c));
1620         assertTrue(lock.getWaitingThreads(c).contains(t1));
1621         assertTrue(lock.getWaitingThreads(c).contains(t2));
1622         assertEquals(2, lock.getWaitingThreads(c).size());
1623         c.signalAll();
1624         assertHasNoWaiters(lock, c);
1625         lock.writeLock().unlock();
1626 
1627         awaitTermination(t1);
1628         awaitTermination(t2);
1629 
1630         assertHasNoWaiters(lock, c);
1631     }
1632 
1633     /**
1634      * toString indicates current lock state
1635      */
testToString()1636     public void testToString()      { testToString(false); }
testToString_fair()1637     public void testToString_fair() { testToString(true); }
testToString(boolean fair)1638     public void testToString(boolean fair) {
1639         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1640         assertTrue(lock.toString().contains("Write locks = 0"));
1641         assertTrue(lock.toString().contains("Read locks = 0"));
1642         lock.writeLock().lock();
1643         assertTrue(lock.toString().contains("Write locks = 1"));
1644         assertTrue(lock.toString().contains("Read locks = 0"));
1645         lock.writeLock().unlock();
1646         lock.readLock().lock();
1647         lock.readLock().lock();
1648         assertTrue(lock.toString().contains("Write locks = 0"));
1649         assertTrue(lock.toString().contains("Read locks = 2"));
1650     }
1651 
1652     /**
1653      * readLock.toString indicates current lock state
1654      */
testReadLockToString()1655     public void testReadLockToString()      { testReadLockToString(false); }
testReadLockToString_fair()1656     public void testReadLockToString_fair() { testReadLockToString(true); }
testReadLockToString(boolean fair)1657     public void testReadLockToString(boolean fair) {
1658         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1659         assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1660         lock.readLock().lock();
1661         lock.readLock().lock();
1662         assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1663     }
1664 
1665     /**
1666      * writeLock.toString indicates current lock state
1667      */
testWriteLockToString()1668     public void testWriteLockToString()      { testWriteLockToString(false); }
testWriteLockToString_fair()1669     public void testWriteLockToString_fair() { testWriteLockToString(true); }
testWriteLockToString(boolean fair)1670     public void testWriteLockToString(boolean fair) {
1671         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1672         assertTrue(lock.writeLock().toString().contains("Unlocked"));
1673         lock.writeLock().lock();
1674         assertTrue(lock.writeLock().toString().contains("Locked"));
1675         lock.writeLock().unlock();
1676         assertTrue(lock.writeLock().toString().contains("Unlocked"));
1677     }
1678 
1679 }
1680