ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.64
Committed: Tue May 24 23:40:14 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.63: +18 -22 lines
Log Message:
pretty up testToString

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