ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.57
Committed: Sat May 7 11:15:04 2011 UTC (13 years ago) by dl
Branch: MAIN
Changes since 1.56: +3 -4 lines
Log Message:
Revert a v56 change that inadvertantly required unguaranteed ordering

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