ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.69
Committed: Wed Dec 31 19:05:43 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.68: +10 -4 lines
Log Message:
no wildcard imports

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