ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.56
Committed: Tue May 3 06:08:49 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.55: +369 -233 lines
Log Message:
rewrite ReentrantReadWriteLockTest

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.2
380 jsr166 1.56 awaitTermination(t1);
381     waitForQueuedThread(lock, t2);
382     assertNotWriteLocked(lock);
383 jsr166 1.32 lock.readLock().unlock();
384 jsr166 1.56 awaitTermination(t2);
385 jsr166 1.28 }
386 dl 1.2
387 dl 1.4 /**
388 jsr166 1.56 * Readlocks succeed only after a writing thread unlocks
389 dl 1.4 */
390 jsr166 1.32 public void testReadAfterWriteLock() throws InterruptedException {
391 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
392     lock.writeLock().lock();
393 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
394 jsr166 1.37 public void realRun() {
395     lock.readLock().lock();
396     lock.readLock().unlock();
397     }});
398 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
399 jsr166 1.37 public void realRun() {
400     lock.readLock().lock();
401     lock.readLock().unlock();
402     }});
403 dl 1.2
404 jsr166 1.56 waitForQueuedThread(lock, t1);
405     waitForQueuedThread(lock, t2);
406 jsr166 1.54 releaseWriteLock(lock);
407 jsr166 1.56 awaitTermination(t1);
408     awaitTermination(t2);
409 jsr166 1.28 }
410 dl 1.2
411 dl 1.20 /**
412     * Read trylock succeeds if write locked by current thread
413     */
414 jsr166 1.28 public void testReadHoldingWriteLock() {
415 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
416     lock.writeLock().lock();
417 dl 1.20 assertTrue(lock.readLock().tryLock());
418     lock.readLock().unlock();
419     lock.writeLock().unlock();
420 jsr166 1.28 }
421 dl 1.20
422     /**
423     * Read lock succeeds if write locked by current thread even if
424 dl 1.23 * other threads are waiting for readlock
425 dl 1.20 */
426 jsr166 1.32 public void testReadHoldingWriteLock2() throws InterruptedException {
427 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
428     lock.writeLock().lock();
429 jsr166 1.56 lock.readLock().lock();
430     lock.readLock().unlock();
431    
432 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
433 jsr166 1.37 public void realRun() {
434     lock.readLock().lock();
435     lock.readLock().unlock();
436     }});
437 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
438 jsr166 1.37 public void realRun() {
439     lock.readLock().lock();
440     lock.readLock().unlock();
441     }});
442 dl 1.20
443 jsr166 1.56 waitForQueuedThread(lock, t1);
444     waitForQueuedThread(lock, t2);
445     assertTrue(lock.isWriteLockedByCurrentThread());
446 jsr166 1.32 lock.readLock().lock();
447     lock.readLock().unlock();
448 jsr166 1.56 releaseWriteLock(lock);
449     awaitTermination(t1);
450     awaitTermination(t2);
451 jsr166 1.28 }
452 dl 1.20
453     /**
454 jsr166 1.43 * Read lock succeeds if write locked by current thread even if
455 dl 1.23 * other threads are waiting for writelock
456     */
457 jsr166 1.32 public void testReadHoldingWriteLock3() throws InterruptedException {
458 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
459     lock.writeLock().lock();
460 jsr166 1.56 lock.readLock().lock();
461     lock.readLock().unlock();
462    
463 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
464 jsr166 1.37 public void realRun() {
465     lock.writeLock().lock();
466     lock.writeLock().unlock();
467     }});
468 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
469 jsr166 1.37 public void realRun() {
470     lock.writeLock().lock();
471     lock.writeLock().unlock();
472     }});
473 dl 1.23
474 jsr166 1.56 waitForQueuedThread(lock, t1);
475     waitForQueuedThread(lock, t2);
476     assertTrue(lock.isWriteLockedByCurrentThread());
477 jsr166 1.32 lock.readLock().lock();
478     lock.readLock().unlock();
479 jsr166 1.56 releaseWriteLock(lock);
480     awaitTermination(t1);
481     awaitTermination(t2);
482 jsr166 1.28 }
483 dl 1.23
484     /**
485 jsr166 1.43 * Write lock succeeds if write locked by current thread even if
486 dl 1.23 * other threads are waiting for writelock
487     */
488 jsr166 1.32 public void testWriteHoldingWriteLock4() throws InterruptedException {
489 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
490     lock.writeLock().lock();
491 jsr166 1.56 lock.writeLock().lock();
492     lock.writeLock().unlock();
493    
494 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
495 jsr166 1.37 public void realRun() {
496     lock.writeLock().lock();
497     lock.writeLock().unlock();
498     }});
499 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
500 jsr166 1.37 public void realRun() {
501     lock.writeLock().lock();
502     lock.writeLock().unlock();
503     }});
504 dl 1.23
505 jsr166 1.56 waitForQueuedThread(lock, t1);
506     waitForQueuedThread(lock, t2);
507     assertTrue(lock.isWriteLockedByCurrentThread());
508     assertEquals(1, lock.getWriteHoldCount());
509 jsr166 1.32 lock.writeLock().lock();
510 jsr166 1.56 assertTrue(lock.isWriteLockedByCurrentThread());
511     assertEquals(2, lock.getWriteHoldCount());
512 jsr166 1.32 lock.writeLock().unlock();
513 jsr166 1.56 releaseWriteLock(lock);
514     awaitTermination(t1);
515     awaitTermination(t2);
516 jsr166 1.28 }
517 dl 1.23
518     /**
519 dl 1.20 * Fair Read trylock succeeds if write locked by current thread
520     */
521 jsr166 1.28 public void testReadHoldingWriteLockFair() {
522 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
523     lock.writeLock().lock();
524 dl 1.20 assertTrue(lock.readLock().tryLock());
525     lock.readLock().unlock();
526     lock.writeLock().unlock();
527 jsr166 1.28 }
528 dl 1.20
529     /**
530     * Fair Read lock succeeds if write locked by current thread even if
531 dl 1.23 * other threads are waiting for readlock
532 dl 1.20 */
533 jsr166 1.32 public void testReadHoldingWriteLockFair2() throws InterruptedException {
534 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
535     lock.writeLock().lock();
536 jsr166 1.56 lock.readLock().lock();
537     lock.readLock().unlock();
538    
539 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
540 jsr166 1.37 public void realRun() {
541     lock.readLock().lock();
542     lock.readLock().unlock();
543     }});
544 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
545 jsr166 1.37 public void realRun() {
546     lock.readLock().lock();
547     lock.readLock().unlock();
548     }});
549 dl 1.20
550 jsr166 1.56 waitForQueuedThread(lock, t1);
551     waitForQueuedThread(lock, t2);
552     assertTrue(lock.isWriteLockedByCurrentThread());
553 jsr166 1.32 lock.readLock().lock();
554     lock.readLock().unlock();
555 jsr166 1.56 releaseWriteLock(lock);
556     awaitTermination(t1);
557     awaitTermination(t2);
558 jsr166 1.28 }
559 dl 1.20
560 dl 1.4 /**
561 dl 1.23 * Fair Read lock succeeds if write locked by current thread even if
562     * other threads are waiting for writelock
563     */
564 jsr166 1.32 public void testReadHoldingWriteLockFair3() throws InterruptedException {
565 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
566     lock.writeLock().lock();
567 jsr166 1.56 lock.readLock().lock();
568     lock.readLock().unlock();
569    
570 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
571 jsr166 1.37 public void realRun() {
572     lock.writeLock().lock();
573     lock.writeLock().unlock();
574     }});
575 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
576 jsr166 1.37 public void realRun() {
577     lock.writeLock().lock();
578     lock.writeLock().unlock();
579     }});
580 dl 1.23
581 jsr166 1.56 waitForQueuedThread(lock, t1);
582     waitForQueuedThread(lock, t2);
583     assertTrue(lock.isWriteLockedByCurrentThread());
584 jsr166 1.32 lock.readLock().lock();
585     lock.readLock().unlock();
586 jsr166 1.56 releaseWriteLock(lock);
587     awaitTermination(t1);
588     awaitTermination(t2);
589 jsr166 1.28 }
590 dl 1.23
591     /**
592     * Fair Write lock succeeds if write locked by current thread even if
593     * other threads are waiting for writelock
594     */
595 jsr166 1.32 public void testWriteHoldingWriteLockFair4() throws InterruptedException {
596 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
597     lock.writeLock().lock();
598 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
599 jsr166 1.37 public void realRun() {
600     lock.writeLock().lock();
601     lock.writeLock().unlock();
602     }});
603 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
604 jsr166 1.37 public void realRun() {
605     lock.writeLock().lock();
606     lock.writeLock().unlock();
607     }});
608 dl 1.23
609 jsr166 1.56 waitForQueuedThread(lock, t1);
610     waitForQueuedThread(lock, t2);
611 jsr166 1.32 assertTrue(lock.isWriteLockedByCurrentThread());
612 jsr166 1.42 assertEquals(1, lock.getWriteHoldCount());
613 jsr166 1.32 lock.writeLock().lock();
614 jsr166 1.42 assertEquals(2, lock.getWriteHoldCount());
615 jsr166 1.32 lock.writeLock().unlock();
616     lock.writeLock().lock();
617     lock.writeLock().unlock();
618 jsr166 1.56 releaseWriteLock(lock);
619     awaitTermination(t1);
620     awaitTermination(t2);
621 jsr166 1.28 }
622 dl 1.23
623     /**
624 dl 1.15 * Read tryLock succeeds if readlocked but not writelocked
625 dl 1.4 */
626 jsr166 1.32 public void testTryLockWhenReadLocked() throws InterruptedException {
627 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
628     lock.readLock().lock();
629 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
630 jsr166 1.37 public void realRun() {
631 jsr166 1.42 assertTrue(lock.readLock().tryLock());
632 jsr166 1.37 lock.readLock().unlock();
633     }});
634 jsr166 1.32
635 jsr166 1.56 awaitTermination(t);
636 jsr166 1.32 lock.readLock().unlock();
637 jsr166 1.28 }
638    
639 dl 1.4 /**
640 dl 1.15 * write tryLock fails when readlocked
641 dl 1.4 */
642 jsr166 1.32 public void testWriteTryLockWhenReadLocked() throws InterruptedException {
643 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
644     lock.readLock().lock();
645 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
646 jsr166 1.37 public void realRun() {
647 jsr166 1.42 assertFalse(lock.writeLock().tryLock());
648 jsr166 1.37 }});
649 jsr166 1.32
650 jsr166 1.56 awaitTermination(t);
651 jsr166 1.32 lock.readLock().unlock();
652 jsr166 1.28 }
653 dl 1.2
654 dl 1.24 /**
655     * Fair Read tryLock succeeds if readlocked but not writelocked
656     */
657 jsr166 1.32 public void testTryLockWhenReadLockedFair() throws InterruptedException {
658 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
659     lock.readLock().lock();
660 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
661 jsr166 1.37 public void realRun() {
662 jsr166 1.42 assertTrue(lock.readLock().tryLock());
663 jsr166 1.37 lock.readLock().unlock();
664     }});
665 jsr166 1.32
666 jsr166 1.56 awaitTermination(t);
667 jsr166 1.32 lock.readLock().unlock();
668 jsr166 1.28 }
669    
670 dl 1.24 /**
671     * Fair write tryLock fails when readlocked
672     */
673 jsr166 1.32 public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
674 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
675     lock.readLock().lock();
676 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
677 jsr166 1.37 public void realRun() {
678 jsr166 1.42 assertFalse(lock.writeLock().tryLock());
679 jsr166 1.37 }});
680 jsr166 1.32
681 jsr166 1.56 awaitTermination(t);
682 jsr166 1.32 lock.readLock().unlock();
683 jsr166 1.28 }
684    
685 dl 1.4 /**
686 dl 1.15 * write timed tryLock times out if locked
687 dl 1.4 */
688 jsr166 1.32 public void testWriteTryLock_Timeout() throws InterruptedException {
689 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
690     lock.writeLock().lock();
691 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
692 jsr166 1.33 public void realRun() throws InterruptedException {
693 jsr166 1.42 assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
694 jsr166 1.33 }});
695 jsr166 1.32
696 jsr166 1.56 awaitTermination(t);
697 jsr166 1.34 assertTrue(lock.writeLock().isHeldByCurrentThread());
698 jsr166 1.32 lock.writeLock().unlock();
699 jsr166 1.28 }
700 dl 1.2
701 dl 1.4 /**
702 dl 1.15 * read timed tryLock times out if write-locked
703 dl 1.4 */
704 jsr166 1.32 public void testReadTryLock_Timeout() throws InterruptedException {
705 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
706     lock.writeLock().lock();
707 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
708 jsr166 1.33 public void realRun() throws InterruptedException {
709 jsr166 1.42 assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
710 jsr166 1.33 }});
711 jsr166 1.32
712 jsr166 1.56 awaitTermination(t);
713 jsr166 1.34 assertTrue(lock.writeLock().isHeldByCurrentThread());
714 jsr166 1.32 lock.writeLock().unlock();
715 jsr166 1.28 }
716 dl 1.2
717 dl 1.4 /**
718     * write lockInterruptibly succeeds if lock free else is interruptible
719     */
720 jsr166 1.32 public void testWriteLockInterruptibly() throws InterruptedException {
721 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
722 jsr166 1.32 lock.writeLock().lockInterruptibly();
723 jsr166 1.45 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
724 jsr166 1.33 public void realRun() throws InterruptedException {
725     lock.writeLock().lockInterruptibly();
726     }});
727 jsr166 1.32
728 jsr166 1.56 waitForQueuedThread(lock, t);
729 jsr166 1.32 t.interrupt();
730 jsr166 1.56 awaitTermination(t);
731 jsr166 1.54 releaseWriteLock(lock);
732 dl 1.2 }
733    
734 dl 1.4 /**
735 jsr166 1.43 * read lockInterruptibly succeeds if lock free else is interruptible
736 dl 1.4 */
737 jsr166 1.32 public void testReadLockInterruptibly() throws InterruptedException {
738 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739 jsr166 1.32 lock.writeLock().lockInterruptibly();
740 jsr166 1.45 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
741 jsr166 1.33 public void realRun() throws InterruptedException {
742     lock.readLock().lockInterruptibly();
743     }});
744 jsr166 1.32
745 jsr166 1.56 waitForQueuedThread(lock, t);
746 jsr166 1.32 t.interrupt();
747 jsr166 1.56 awaitTermination(t);
748 jsr166 1.54 releaseWriteLock(lock);
749 dl 1.2 }
750    
751 dl 1.4 /**
752     * Calling await without holding lock throws IllegalMonitorStateException
753     */
754 jsr166 1.32 public void testAwait_IllegalMonitor() throws InterruptedException {
755 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
756 dl 1.2 final Condition c = lock.writeLock().newCondition();
757     try {
758     c.await();
759 dl 1.4 shouldThrow();
760 jsr166 1.32 } catch (IllegalMonitorStateException success) {}
761 jsr166 1.56 try {
762     c.await(LONG_DELAY_MS, MILLISECONDS);
763     shouldThrow();
764     } catch (IllegalMonitorStateException success) {}
765     try {
766     c.awaitNanos(100);
767     shouldThrow();
768     } catch (IllegalMonitorStateException success) {}
769     try {
770     c.awaitUninterruptibly();
771     shouldThrow();
772     } catch (IllegalMonitorStateException success) {}
773 dl 1.2 }
774    
775 dl 1.4 /**
776     * Calling signal without holding lock throws IllegalMonitorStateException
777     */
778 dl 1.2 public void testSignal_IllegalMonitor() {
779 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
780 dl 1.2 final Condition c = lock.writeLock().newCondition();
781     try {
782     c.signal();
783 dl 1.4 shouldThrow();
784 jsr166 1.32 } catch (IllegalMonitorStateException success) {}
785 dl 1.2 }
786    
787 dl 1.4 /**
788 jsr166 1.56 * Calling signalAll without holding lock throws IllegalMonitorStateException
789     */
790     public void testSignalAll_IllegalMonitor() {
791     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
792     final Condition c = lock.writeLock().newCondition();
793     try {
794     c.signalAll();
795     shouldThrow();
796     } catch (IllegalMonitorStateException success) {}
797     }
798    
799     /**
800 dl 1.4 * awaitNanos without a signal times out
801     */
802 jsr166 1.32 public void testAwaitNanos_Timeout() throws InterruptedException {
803 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
804 dl 1.2 final Condition c = lock.writeLock().newCondition();
805 jsr166 1.32 lock.writeLock().lock();
806 jsr166 1.56 long startTime = System.nanoTime();
807     long timeoutMillis = 10;
808     long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
809     long nanosRemaining = c.awaitNanos(timeoutNanos);
810     assertTrue(nanosRemaining <= 0);
811     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
812 jsr166 1.32 lock.writeLock().unlock();
813 dl 1.2 }
814    
815 dl 1.4 /**
816 jsr166 1.43 * timed await without a signal times out
817 dl 1.4 */
818 jsr166 1.34 public void testAwait_Timeout() throws InterruptedException {
819 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
820 dl 1.2 final Condition c = lock.writeLock().newCondition();
821 jsr166 1.32 lock.writeLock().lock();
822 jsr166 1.56 long startTime = System.nanoTime();
823     long timeoutMillis = 10;
824     assertFalse(c.await(timeoutMillis, MILLISECONDS));
825     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
826 jsr166 1.32 lock.writeLock().unlock();
827 dl 1.2 }
828    
829 dl 1.4 /**
830     * awaitUntil without a signal times out
831     */
832 jsr166 1.34 public void testAwaitUntil_Timeout() throws InterruptedException {
833 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
834 dl 1.2 final Condition c = lock.writeLock().newCondition();
835 jsr166 1.32 lock.writeLock().lock();
836     java.util.Date d = new java.util.Date();
837 jsr166 1.34 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
838 jsr166 1.32 lock.writeLock().unlock();
839 dl 1.2 }
840 dl 1.1
841 dl 1.4 /**
842     * await returns when signalled
843     */
844 jsr166 1.32 public void testAwait() throws InterruptedException {
845 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
846 dl 1.2 final Condition c = lock.writeLock().newCondition();
847 jsr166 1.56 final CountDownLatch locked = new CountDownLatch(1);
848 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
849 jsr166 1.33 public void realRun() throws InterruptedException {
850     lock.writeLock().lock();
851 jsr166 1.56 locked.countDown();
852 jsr166 1.33 c.await();
853     lock.writeLock().unlock();
854     }});
855 dl 1.2
856 jsr166 1.56 locked.await();
857 jsr166 1.32 lock.writeLock().lock();
858     c.signal();
859 jsr166 1.56 assertTrue(t.isAlive());
860 jsr166 1.32 lock.writeLock().unlock();
861 jsr166 1.56 awaitTermination(t);
862 dl 1.22 }
863    
864 dl 1.4 /**
865     * awaitUninterruptibly doesn't abort on interrupt
866     */
867 jsr166 1.32 public void testAwaitUninterruptibly() throws InterruptedException {
868 dl 1.22 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
869 dl 1.2 final Condition c = lock.writeLock().newCondition();
870 jsr166 1.56 final CountDownLatch locked = new CountDownLatch(1);
871     final AtomicBoolean canAwake = new AtomicBoolean(false);
872     Thread t = newStartedThread(new CheckedRunnable() {
873     public void realRun() {
874     lock.writeLock().lock();
875     locked.countDown();
876     c.awaitUninterruptibly();
877     assertTrue(Thread.interrupted());
878     lock.writeLock().unlock();
879     }});
880 dl 1.22
881 jsr166 1.56 locked.await();
882     lock.writeLock().lock();
883     lock.writeLock().unlock();
884     t.interrupt();
885     t.join(10);
886     assertTrue(t.isAlive());
887 jsr166 1.32 lock.writeLock().lock();
888 jsr166 1.56 c.signal();
889     lock.writeLock().unlock();
890     awaitTermination(t);
891 dl 1.2 }
892    
893 dl 1.4 /**
894     * await is interruptible
895     */
896 jsr166 1.32 public void testAwait_Interrupt() throws InterruptedException {
897 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
898 dl 1.2 final Condition c = lock.writeLock().newCondition();
899 jsr166 1.52 final CountDownLatch locked = new CountDownLatch(1);
900 jsr166 1.45 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
901 jsr166 1.33 public void realRun() throws InterruptedException {
902     lock.writeLock().lock();
903 jsr166 1.52 assertTrue(lock.isWriteLocked());
904 jsr166 1.56 assertTrue(lock.isWriteLockedByCurrentThread());
905     assertHasNoWaiters(lock, c);
906 jsr166 1.52 locked.countDown();
907 jsr166 1.56 try {
908     c.await();
909     } finally {
910     assertTrue(lock.isWriteLocked());
911     assertTrue(lock.isWriteLockedByCurrentThread());
912     assertHasNoWaiters(lock, c);
913     lock.writeLock().unlock();
914     assertFalse(Thread.interrupted());
915     }
916 jsr166 1.33 }});
917 dl 1.2
918 jsr166 1.52 locked.await();
919 jsr166 1.56 assertHasWaiters(lock, c, t);
920 jsr166 1.32 t.interrupt();
921 jsr166 1.56 awaitTermination(t);
922 jsr166 1.52 assertFalse(lock.isWriteLocked());
923 dl 1.2 }
924    
925 dl 1.4 /**
926     * awaitNanos is interruptible
927     */
928 jsr166 1.32 public void testAwaitNanos_Interrupt() throws InterruptedException {
929 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
930 dl 1.2 final Condition c = lock.writeLock().newCondition();
931 jsr166 1.52 final CountDownLatch locked = new CountDownLatch(1);
932 jsr166 1.45 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
933 jsr166 1.33 public void realRun() throws InterruptedException {
934     lock.writeLock().lock();
935 jsr166 1.52 assertTrue(lock.isWriteLocked());
936 jsr166 1.56 assertTrue(lock.isWriteLockedByCurrentThread());
937     assertHasNoWaiters(lock, c);
938 jsr166 1.52 locked.countDown();
939 jsr166 1.56 try {
940     c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
941     } finally {
942     assertTrue(lock.isWriteLocked());
943     assertTrue(lock.isWriteLockedByCurrentThread());
944     assertHasNoWaiters(lock, c);
945     lock.writeLock().unlock();
946     assertFalse(Thread.interrupted());
947     }
948 jsr166 1.33 }});
949 dl 1.2
950 jsr166 1.52 locked.await();
951 jsr166 1.56 assertHasWaiters(lock, c, t);
952 jsr166 1.32 t.interrupt();
953 jsr166 1.56 awaitTermination(t);
954 jsr166 1.52 assertFalse(lock.isWriteLocked());
955 dl 1.2 }
956 dl 1.1
957 dl 1.4 /**
958     * awaitUntil is interruptible
959     */
960 jsr166 1.32 public void testAwaitUntil_Interrupt() throws InterruptedException {
961 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
962 dl 1.2 final Condition c = lock.writeLock().newCondition();
963 jsr166 1.52 final CountDownLatch locked = new CountDownLatch(1);
964 jsr166 1.45 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
965 jsr166 1.33 public void realRun() throws InterruptedException {
966     lock.writeLock().lock();
967 jsr166 1.52 assertTrue(lock.isWriteLocked());
968 jsr166 1.56 assertTrue(lock.isWriteLockedByCurrentThread());
969     assertHasNoWaiters(lock, c);
970 jsr166 1.52 locked.countDown();
971 jsr166 1.33 java.util.Date d = new java.util.Date();
972 jsr166 1.56 try {
973     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
974     } finally {
975     assertTrue(lock.isWriteLocked());
976     assertTrue(lock.isWriteLockedByCurrentThread());
977     assertHasNoWaiters(lock, c);
978     lock.writeLock().unlock();
979     assertFalse(Thread.interrupted());
980     }
981 jsr166 1.33 }});
982 dl 1.2
983 jsr166 1.52 locked.await();
984 jsr166 1.56 assertHasWaiters(lock, c, t);
985 jsr166 1.32 t.interrupt();
986 jsr166 1.56 awaitTermination(t);
987 jsr166 1.52 assertFalse(lock.isWriteLocked());
988 dl 1.2 }
989    
990 dl 1.4 /**
991     * signalAll wakes up all threads
992     */
993 jsr166 1.32 public void testSignalAll() throws InterruptedException {
994 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
995 dl 1.2 final Condition c = lock.writeLock().newCondition();
996 jsr166 1.56 final CountDownLatch locked = new CountDownLatch(2);
997     final Lock writeLock = lock.writeLock();
998 jsr166 1.45 Thread t1 = newStartedThread(new CheckedRunnable() {
999 jsr166 1.33 public void realRun() throws InterruptedException {
1000 jsr166 1.56 writeLock.lock();
1001     locked.countDown();
1002 jsr166 1.33 c.await();
1003 jsr166 1.56 writeLock.unlock();
1004 jsr166 1.33 }});
1005    
1006 jsr166 1.45 Thread t2 = newStartedThread(new CheckedRunnable() {
1007 jsr166 1.33 public void realRun() throws InterruptedException {
1008 jsr166 1.56 writeLock.lock();
1009     locked.countDown();
1010 jsr166 1.33 c.await();
1011 jsr166 1.56 writeLock.unlock();
1012 jsr166 1.33 }});
1013 dl 1.2
1014 jsr166 1.56 locked.await();
1015     writeLock.lock();
1016     assertHasWaiters(lock, c, t1, t2);
1017 jsr166 1.32 c.signalAll();
1018 jsr166 1.56 assertHasNoWaiters(lock, c);
1019     writeLock.unlock();
1020     awaitTermination(t1);
1021     awaitTermination(t2);
1022     }
1023    
1024     /**
1025     * signal wakes up waiting threads in FIFO order.
1026     */
1027     public void testSignalWakesFifo() throws InterruptedException {
1028     final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1029     final Condition c = lock.writeLock().newCondition();
1030     final CountDownLatch locked1 = new CountDownLatch(1);
1031     final CountDownLatch locked2 = new CountDownLatch(1);
1032     final Lock writeLock = lock.writeLock();
1033     Thread t1 = newStartedThread(new CheckedRunnable() {
1034     public void realRun() throws InterruptedException {
1035     writeLock.lock();
1036     locked1.countDown();
1037     c.await();
1038     writeLock.unlock();
1039     }});
1040    
1041     locked1.await();
1042    
1043     Thread t2 = newStartedThread(new CheckedRunnable() {
1044     public void realRun() throws InterruptedException {
1045     writeLock.lock();
1046     locked2.countDown();
1047     c.await();
1048     writeLock.unlock();
1049     }});
1050    
1051     locked2.await();
1052    
1053     writeLock.lock();
1054     assertHasWaiters(lock, c, t1, t2);
1055     assertFalse(lock.hasQueuedThreads());
1056     c.signal();
1057     assertHasWaiters(lock, c, t2);
1058     assertTrue(lock.hasQueuedThread(t1));
1059     assertFalse(lock.hasQueuedThread(t2));
1060     c.signal();
1061     assertHasNoWaiters(lock, c);
1062     assertTrue(lock.hasQueuedThread(t1));
1063     assertTrue(lock.hasQueuedThread(t2));
1064     writeLock.unlock();
1065     awaitTermination(t1);
1066     awaitTermination(t2);
1067 dl 1.2 }
1068    
1069 dl 1.4 /**
1070     * A serialized lock deserializes as unlocked
1071     */
1072 jsr166 1.32 public void testSerialization() throws Exception {
1073 dl 1.2 ReentrantReadWriteLock l = new ReentrantReadWriteLock();
1074     l.readLock().lock();
1075     l.readLock().unlock();
1076    
1077 jsr166 1.32 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1078     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1079     out.writeObject(l);
1080     out.close();
1081    
1082     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1083     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1084     ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
1085     r.readLock().lock();
1086     r.readLock().unlock();
1087 dl 1.1 }
1088 dl 1.2
1089 dl 1.6 /**
1090 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
1091     */
1092 jsr166 1.32 public void testhasQueuedThreads() throws InterruptedException {
1093 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1094 dl 1.13 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1095     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1096 jsr166 1.32 assertFalse(lock.hasQueuedThreads());
1097     lock.writeLock().lock();
1098 jsr166 1.56 assertFalse(lock.hasQueuedThreads());
1099     long startTime = System.nanoTime();
1100 jsr166 1.32 t1.start();
1101 jsr166 1.56 waitForQueuedThread(lock, t1);
1102 jsr166 1.32 t2.start();
1103 jsr166 1.56 waitForQueuedThread(lock, t2);
1104 jsr166 1.32 assertTrue(lock.hasQueuedThreads());
1105     t1.interrupt();
1106 jsr166 1.56 awaitTermination(t1);
1107 jsr166 1.32 assertTrue(lock.hasQueuedThreads());
1108     lock.writeLock().unlock();
1109 jsr166 1.56 awaitTermination(t2);
1110 jsr166 1.32 assertFalse(lock.hasQueuedThreads());
1111 jsr166 1.28 }
1112 dl 1.13
1113     /**
1114 dl 1.19 * hasQueuedThread(null) throws NPE
1115     */
1116 jsr166 1.28 public void testHasQueuedThreadNPE() {
1117 jsr166 1.35 final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1118 dl 1.19 try {
1119     sync.hasQueuedThread(null);
1120     shouldThrow();
1121 jsr166 1.33 } catch (NullPointerException success) {}
1122 dl 1.19 }
1123    
1124     /**
1125     * hasQueuedThread reports whether a thread is queued.
1126     */
1127 jsr166 1.32 public void testHasQueuedThread() throws InterruptedException {
1128 jsr166 1.56 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1129     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1130     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1131     assertFalse(lock.hasQueuedThread(t1));
1132     assertFalse(lock.hasQueuedThread(t2));
1133     lock.writeLock().lock();
1134     long startTime = System.nanoTime();
1135 jsr166 1.32 t1.start();
1136 jsr166 1.56 waitForQueuedThread(lock, t1);
1137     assertTrue(lock.hasQueuedThread(t1));
1138     assertFalse(lock.hasQueuedThread(t2));
1139 jsr166 1.32 t2.start();
1140 jsr166 1.56 waitForQueuedThread(lock, t2);
1141     assertTrue(lock.hasQueuedThread(t1));
1142     assertTrue(lock.hasQueuedThread(t2));
1143 jsr166 1.32 t1.interrupt();
1144 jsr166 1.56 awaitTermination(t1);
1145     assertFalse(lock.hasQueuedThread(t1));
1146     assertTrue(lock.hasQueuedThread(t2));
1147     lock.writeLock().unlock();
1148     awaitTermination(t2);
1149     assertFalse(lock.hasQueuedThread(t1));
1150     assertFalse(lock.hasQueuedThread(t2));
1151 jsr166 1.28 }
1152 dl 1.19
1153     /**
1154 dl 1.6 * getQueueLength reports number of waiting threads
1155     */
1156 jsr166 1.32 public void testGetQueueLength() throws InterruptedException {
1157 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1158 dl 1.6 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1159     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1160 jsr166 1.32 assertEquals(0, lock.getQueueLength());
1161     lock.writeLock().lock();
1162 jsr166 1.56 long startTime = System.nanoTime();
1163 jsr166 1.32 t1.start();
1164 jsr166 1.56 waitForQueuedThread(lock, t1);
1165 jsr166 1.32 assertEquals(1, lock.getQueueLength());
1166     t2.start();
1167 jsr166 1.56 waitForQueuedThread(lock, t2);
1168 jsr166 1.32 assertEquals(2, lock.getQueueLength());
1169     t1.interrupt();
1170 jsr166 1.56 awaitTermination(t1);
1171 jsr166 1.32 assertEquals(1, lock.getQueueLength());
1172     lock.writeLock().unlock();
1173 jsr166 1.56 awaitTermination(t2);
1174 jsr166 1.32 assertEquals(0, lock.getQueueLength());
1175 jsr166 1.28 }
1176 dl 1.6
1177     /**
1178     * getQueuedThreads includes waiting threads
1179     */
1180 jsr166 1.32 public void testGetQueuedThreads() throws InterruptedException {
1181 jsr166 1.35 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1182 dl 1.6 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1183     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1184 jsr166 1.32 assertTrue(lock.getQueuedThreads().isEmpty());
1185     lock.writeLock().lock();
1186 jsr166 1.56 long startTime = System.nanoTime();
1187 jsr166 1.32 assertTrue(lock.getQueuedThreads().isEmpty());
1188     t1.start();
1189 jsr166 1.56 waitForQueuedThread(lock, t1);
1190     assertEquals(1, lock.getQueuedThreads().size());
1191 jsr166 1.32 assertTrue(lock.getQueuedThreads().contains(t1));
1192     t2.start();
1193 jsr166 1.56 waitForQueuedThread(lock, t2);
1194     assertEquals(2, lock.getQueuedThreads().size());
1195 jsr166 1.32 assertTrue(lock.getQueuedThreads().contains(t1));
1196     assertTrue(lock.getQueuedThreads().contains(t2));
1197     t1.interrupt();
1198 jsr166 1.56 awaitTermination(t1);
1199 jsr166 1.32 assertFalse(lock.getQueuedThreads().contains(t1));
1200     assertTrue(lock.getQueuedThreads().contains(t2));
1201 jsr166 1.56 assertEquals(1, lock.getQueuedThreads().size());
1202 jsr166 1.32 lock.writeLock().unlock();
1203 jsr166 1.56 awaitTermination(t2);
1204 jsr166 1.32 assertTrue(lock.getQueuedThreads().isEmpty());
1205 jsr166 1.28 }
1206 dl 1.6
1207     /**
1208 dl 1.14 * hasWaiters throws NPE if null
1209     */
1210     public void testHasWaitersNPE() {
1211 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1212 dl 1.14 try {
1213     lock.hasWaiters(null);
1214     shouldThrow();
1215 jsr166 1.32 } catch (NullPointerException success) {}
1216 dl 1.14 }
1217    
1218     /**
1219     * getWaitQueueLength throws NPE if null
1220     */
1221     public void testGetWaitQueueLengthNPE() {
1222 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1223 dl 1.14 try {
1224     lock.getWaitQueueLength(null);
1225     shouldThrow();
1226 jsr166 1.32 } catch (NullPointerException success) {}
1227 dl 1.14 }
1228    
1229     /**
1230     * getWaitingThreads throws NPE if null
1231     */
1232     public void testGetWaitingThreadsNPE() {
1233 jsr166 1.35 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1234 dl 1.14 try {
1235     lock.getWaitingThreads(null);
1236     shouldThrow();
1237 jsr166 1.32 } catch (NullPointerException success) {}
1238 dl 1.14 }
1239    
1240     /**
1241 dl 1.13 * hasWaiters throws IAE if not owned
1242     */
1243     public void testHasWaitersIAE() {
1244 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1245 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1246 jsr166 1.35 final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1247 dl 1.13 try {
1248     lock2.hasWaiters(c);
1249     shouldThrow();
1250 jsr166 1.32 } catch (IllegalArgumentException success) {}
1251 dl 1.13 }
1252    
1253     /**
1254     * hasWaiters throws IMSE if not locked
1255     */
1256     public void testHasWaitersIMSE() {
1257 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1258 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1259 dl 1.13 try {
1260     lock.hasWaiters(c);
1261     shouldThrow();
1262 jsr166 1.32 } catch (IllegalMonitorStateException success) {}
1263 dl 1.13 }
1264    
1265     /**
1266     * getWaitQueueLength throws IAE if not owned
1267     */
1268     public void testGetWaitQueueLengthIAE() {
1269 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1270 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1271 jsr166 1.35 final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1272 dl 1.13 try {
1273     lock2.getWaitQueueLength(c);
1274     shouldThrow();
1275 jsr166 1.32 } catch (IllegalArgumentException success) {}
1276 dl 1.13 }
1277    
1278     /**
1279     * getWaitQueueLength throws IMSE if not locked
1280     */
1281     public void testGetWaitQueueLengthIMSE() {
1282 jsr166 1.35 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1283 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1284 dl 1.13 try {
1285     lock.getWaitQueueLength(c);
1286     shouldThrow();
1287 jsr166 1.32 } catch (IllegalMonitorStateException success) {}
1288 dl 1.13 }
1289    
1290     /**
1291     * getWaitingThreads throws IAE if not owned
1292     */
1293     public void testGetWaitingThreadsIAE() {
1294 jsr166 1.35 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1295 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1296 jsr166 1.35 final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1297 dl 1.13 try {
1298     lock2.getWaitingThreads(c);
1299     shouldThrow();
1300 jsr166 1.32 } catch (IllegalArgumentException success) {}
1301 dl 1.13 }
1302    
1303     /**
1304     * getWaitingThreads throws IMSE if not locked
1305     */
1306     public void testGetWaitingThreadsIMSE() {
1307 jsr166 1.35 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1308 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1309 dl 1.13 try {
1310     lock.getWaitingThreads(c);
1311     shouldThrow();
1312 jsr166 1.32 } catch (IllegalMonitorStateException success) {}
1313 dl 1.13 }
1314    
1315     /**
1316 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
1317     */
1318 jsr166 1.32 public void testHasWaiters() throws InterruptedException {
1319 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1320 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1321 jsr166 1.56 final CountDownLatch locked = new CountDownLatch(1);
1322 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
1323 jsr166 1.33 public void realRun() throws InterruptedException {
1324     lock.writeLock().lock();
1325 jsr166 1.42 assertFalse(lock.hasWaiters(c));
1326 jsr166 1.56 locked.countDown();
1327 jsr166 1.42 assertEquals(0, lock.getWaitQueueLength(c));
1328 jsr166 1.33 c.await();
1329     lock.writeLock().unlock();
1330     }});
1331 dl 1.6
1332 jsr166 1.56 locked.await();
1333 jsr166 1.32 lock.writeLock().lock();
1334     assertTrue(lock.hasWaiters(c));
1335     assertEquals(1, lock.getWaitQueueLength(c));
1336     c.signal();
1337 jsr166 1.56 assertHasNoWaiters(lock, c);
1338 jsr166 1.32 lock.writeLock().unlock();
1339 jsr166 1.56 awaitTermination(t);
1340     assertHasNoWaiters(lock, c);
1341 dl 1.6 }
1342    
1343     /**
1344     * getWaitQueueLength returns number of waiting threads
1345     */
1346 jsr166 1.32 public void testGetWaitQueueLength() throws InterruptedException {
1347 jsr166 1.56 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1348 jsr166 1.31 final Condition c = lock.writeLock().newCondition();
1349 jsr166 1.56 final CountDownLatch locked = new CountDownLatch(1);
1350 jsr166 1.45 Thread t = newStartedThread(new CheckedRunnable() {
1351 jsr166 1.33 public void realRun() throws InterruptedException {
1352     lock.writeLock().lock();
1353 jsr166 1.42 assertEquals(0, lock.getWaitQueueLength(c));
1354 jsr166 1.56 locked.countDown();
1355 jsr166 1.33 c.await();
1356     lock.writeLock().unlock();
1357     }});
1358 dl 1.13
1359 jsr166 1.56 locked.await();
1360 jsr166 1.32 lock.writeLock().lock();
1361 jsr166 1.56 assertHasWaiters(lock, c, t);
1362 jsr166 1.32 assertEquals(1, lock.getWaitQueueLength(c));
1363     c.signal();
1364 jsr166 1.56 assertHasNoWaiters(lock, c);
1365 jsr166 1.32 assertEquals(0, lock.getWaitQueueLength(c));
1366     lock.writeLock().unlock();
1367 jsr166 1.56 awaitTermination(t);
1368 dl 1.13 }
1369    
1370     /**
1371     * getWaitingThreads returns only and all waiting threads
1372     */
1373 jsr166 1.32 public void testGetWaitingThreads() throws InterruptedException {
1374 jsr166 1.35 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1375 dl 1.13 final Condition c = lock.writeLock().newCondition();
1376 jsr166 1.56 final CountDownLatch locked1 = new CountDownLatch(1);
1377     final CountDownLatch locked2 = new CountDownLatch(1);
1378 jsr166 1.35 Thread t1 = new Thread(new CheckedRunnable() {
1379 jsr166 1.33 public void realRun() throws InterruptedException {
1380     lock.writeLock().lock();
1381 jsr166 1.42 assertTrue(lock.getWaitingThreads(c).isEmpty());
1382 jsr166 1.56 locked1.countDown();
1383 jsr166 1.33 c.await();
1384     lock.writeLock().unlock();
1385     }});
1386    
1387 jsr166 1.35 Thread t2 = new Thread(new CheckedRunnable() {
1388 jsr166 1.33 public void realRun() throws InterruptedException {
1389     lock.writeLock().lock();
1390 jsr166 1.42 assertFalse(lock.getWaitingThreads(c).isEmpty());
1391 jsr166 1.56 locked2.countDown();
1392 jsr166 1.33 c.await();
1393     lock.writeLock().unlock();
1394     }});
1395 dl 1.6
1396 jsr166 1.32 lock.writeLock().lock();
1397     assertTrue(lock.getWaitingThreads(c).isEmpty());
1398     lock.writeLock().unlock();
1399 jsr166 1.56
1400 jsr166 1.32 t1.start();
1401 jsr166 1.56 locked1.await();
1402 jsr166 1.32 t2.start();
1403 jsr166 1.56 locked2.await();
1404    
1405 jsr166 1.32 lock.writeLock().lock();
1406     assertTrue(lock.hasWaiters(c));
1407     assertTrue(lock.getWaitingThreads(c).contains(t1));
1408     assertTrue(lock.getWaitingThreads(c).contains(t2));
1409 jsr166 1.56 assertEquals(2, lock.getWaitingThreads(c).size());
1410 jsr166 1.32 c.signalAll();
1411 jsr166 1.56 assertHasNoWaiters(lock, c);
1412 jsr166 1.32 lock.writeLock().unlock();
1413 jsr166 1.56
1414     awaitTermination(t1);
1415     awaitTermination(t2);
1416    
1417     assertHasNoWaiters(lock, c);
1418 dl 1.6 }
1419 dl 1.13
1420 dl 1.18 /**
1421     * toString indicates current lock state
1422     */
1423     public void testToString() {
1424     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1425     String us = lock.toString();
1426     assertTrue(us.indexOf("Write locks = 0") >= 0);
1427     assertTrue(us.indexOf("Read locks = 0") >= 0);
1428     lock.writeLock().lock();
1429     String ws = lock.toString();
1430     assertTrue(ws.indexOf("Write locks = 1") >= 0);
1431     assertTrue(ws.indexOf("Read locks = 0") >= 0);
1432     lock.writeLock().unlock();
1433     lock.readLock().lock();
1434     lock.readLock().lock();
1435     String rs = lock.toString();
1436     assertTrue(rs.indexOf("Write locks = 0") >= 0);
1437     assertTrue(rs.indexOf("Read locks = 2") >= 0);
1438     }
1439    
1440     /**
1441     * readLock.toString indicates current lock state
1442     */
1443     public void testReadLockToString() {
1444     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1445     String us = lock.readLock().toString();
1446     assertTrue(us.indexOf("Read locks = 0") >= 0);
1447     lock.readLock().lock();
1448     lock.readLock().lock();
1449     String rs = lock.readLock().toString();
1450     assertTrue(rs.indexOf("Read locks = 2") >= 0);
1451     }
1452    
1453     /**
1454     * writeLock.toString indicates current lock state
1455     */
1456     public void testWriteLockToString() {
1457     ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1458     String us = lock.writeLock().toString();
1459     assertTrue(us.indexOf("Unlocked") >= 0);
1460     lock.writeLock().lock();
1461     String ls = lock.writeLock().toString();
1462     assertTrue(ls.indexOf("Locked") >= 0);
1463     }
1464    
1465 dl 1.1 }