ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.59
Committed: Sat May 7 19:03:26 2011 UTC (13 years ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.58: +79 -26 lines
Log Message:
Improve ReentrantLock and ReentrantReadWriteLock tests

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