ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/StampedLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/StampedLockTest.java (file contents):
Revision 1.1 by jsr166, Fri Feb 1 19:07:36 2013 UTC vs.
Revision 1.13 by jsr166, Sat Apr 25 04:55:31 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines