ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.84
Committed: Thu Sep 26 20:48:53 2019 UTC (4 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.83: +8 -8 lines
Log Message:
whitespace

File Contents

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