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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines