ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.61
Committed: Fri May 13 21:48:59 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.60: +57 -27 lines
Log Message:
More cross-pollination of RL and RRWL tests; don't rely on thread.join not returning early

File Contents

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