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.9 by jsr166, Wed Dec 31 16:44:02 2014 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.*;
11 import java.util.concurrent.atomic.AtomicBoolean;
12 import java.util.concurrent.locks.Condition;
9   import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReadWriteLock;
10   import java.util.concurrent.locks.StampedLock;
11   import java.util.concurrent.CountDownLatch;
12   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.util.*;
14 + import java.util.concurrent.*;
15  
16   public class StampedLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
# Line 25 | Line 21 | public class StampedLockTest extends JSR
21          return new TestSuite(StampedLockTest.class);
22      }
23  
24 <    // XXXX Just a skeleton implementation for now.
25 <    public void testTODO() {
26 <        fail("Please add some real tests!");
27 <    }
28 <
29 <    public void testConstructor() { new StampedLock(); }
30 <
31 < //     /**
32 < //      * A runnable calling lockInterruptibly
33 < //      */
34 < //     class InterruptibleLockRunnable extends CheckedRunnable {
35 < //         final ReentrantReadWriteLock lock;
36 < //         InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
37 < //         public void realRun() throws InterruptedException {
38 < //             lock.writeLock().lockInterruptibly();
39 < //         }
40 < //     }
41 <
42 < //     /**
43 < //      * A runnable calling lockInterruptibly that expects to be
44 < //      * interrupted
45 < //      */
46 < //     class InterruptedLockRunnable extends CheckedInterruptedRunnable {
47 < //         final ReentrantReadWriteLock lock;
48 < //         InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
49 < //         public void realRun() throws InterruptedException {
50 < //             lock.writeLock().lockInterruptibly();
51 < //         }
52 < //     }
53 <
54 < //     /**
55 < //      * Subclass to expose protected methods
56 < //      */
57 < //     static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
58 < //         PublicReentrantReadWriteLock() { super(); }
59 < //         PublicReentrantReadWriteLock(boolean fair) { super(fair); }
60 < //         public Thread getOwner() {
61 < //             return super.getOwner();
62 < //         }
63 < //         public Collection<Thread> getQueuedThreads() {
64 < //             return super.getQueuedThreads();
65 < //         }
66 < //         public Collection<Thread> getWaitingThreads(Condition c) {
67 < //             return super.getWaitingThreads(c);
68 < //         }
69 < //     }
70 <
71 < //     /**
72 < //      * Releases write lock, checking that it had a hold count of 1.
73 < //      */
74 < //     void releaseWriteLock(PublicReentrantReadWriteLock lock) {
75 < //         ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
76 < //         assertWriteLockedByMoi(lock);
77 < //         assertEquals(1, lock.getWriteHoldCount());
78 < //         writeLock.unlock();
79 < //         assertNotWriteLocked(lock);
80 < //     }
81 <
82 < //     /**
83 < //      * Spin-waits until lock.hasQueuedThread(t) becomes true.
84 < //      */
85 < //     void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
86 < //         long startTime = System.nanoTime();
87 < //         while (!lock.hasQueuedThread(t)) {
88 < //             if (millisElapsedSince(startTime) > LONG_DELAY_MS)
89 < //                 throw new AssertionFailedError("timed out");
90 < //             Thread.yield();
91 < //         }
92 < //         assertTrue(t.isAlive());
93 < //         assertTrue(lock.getOwner() != t);
94 < //     }
95 <
96 < //     /**
97 < //      * Checks that lock is not write-locked.
98 < //      */
99 < //     void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
100 < //         assertFalse(lock.isWriteLocked());
101 < //         assertFalse(lock.isWriteLockedByCurrentThread());
102 < //         assertFalse(lock.writeLock().isHeldByCurrentThread());
103 < //         assertEquals(0, lock.getWriteHoldCount());
104 < //         assertEquals(0, lock.writeLock().getHoldCount());
105 < //         assertNull(lock.getOwner());
106 < //     }
107 <
108 < //     /**
109 < //      * Checks that lock is write-locked by the given thread.
110 < //      */
111 < //     void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
112 < //         assertTrue(lock.isWriteLocked());
113 < //         assertSame(t, lock.getOwner());
114 < //         assertEquals(t == Thread.currentThread(),
115 < //                      lock.isWriteLockedByCurrentThread());
116 < //         assertEquals(t == Thread.currentThread(),
117 < //                      lock.writeLock().isHeldByCurrentThread());
118 < //         assertEquals(t == Thread.currentThread(),
119 < //                      lock.getWriteHoldCount() > 0);
120 < //         assertEquals(t == Thread.currentThread(),
121 < //                      lock.writeLock().getHoldCount() > 0);
122 < //         assertEquals(0, lock.getReadLockCount());
123 < //     }
124 <
125 < //     /**
126 < //      * Checks that lock is write-locked by the current thread.
127 < //      */
128 < //     void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
129 < //         assertWriteLockedBy(lock, Thread.currentThread());
130 < //     }
131 <
132 < //     /**
133 < //      * Checks that condition c has no waiters.
134 < //      */
135 < //     void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
136 < //         assertHasWaiters(lock, c, new Thread[] {});
137 < //     }
138 <
139 < //     /**
140 < //      * Checks that condition c has exactly the given waiter threads.
141 < //      */
142 < //     void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
143 < //                           Thread... threads) {
144 < //         lock.writeLock().lock();
145 < //         assertEquals(threads.length > 0, lock.hasWaiters(c));
146 < //         assertEquals(threads.length, lock.getWaitQueueLength(c));
147 < //         assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
148 < //         assertEquals(threads.length, lock.getWaitingThreads(c).size());
149 < //         assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
150 < //                      new HashSet<Thread>(Arrays.asList(threads)));
151 < //         lock.writeLock().unlock();
152 < //     }
153 <
154 < //     enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
155 <
156 < //     /**
157 < //      * Awaits condition using the specified AwaitMethod.
158 < //      */
159 < //     void await(Condition c, AwaitMethod awaitMethod)
160 < //             throws InterruptedException {
161 < //         switch (awaitMethod) {
162 < //         case await:
163 < //             c.await();
164 < //             break;
165 < //         case awaitTimed:
166 < //             assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
167 < //             break;
168 < //         case awaitNanos:
169 < //             long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
170 < //             assertTrue(nanosRemaining > 0);
171 < //             break;
172 < //         case awaitUntil:
173 < //             java.util.Date d = new java.util.Date();
174 < //             assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
175 < //             break;
176 < //         }
177 < //     }
178 <
179 < //     /**
180 < //      * Constructor sets given fairness, and is in unlocked state
181 < //      */
182 < //     public void testConstructor() {
183 < //         PublicReentrantReadWriteLock lock;
184 <
185 < //         lock = new PublicReentrantReadWriteLock();
186 < //         assertFalse(lock.isFair());
187 < //         assertNotWriteLocked(lock);
188 < //         assertEquals(0, lock.getReadLockCount());
189 <
190 < //         lock = new PublicReentrantReadWriteLock(true);
191 < //         assertTrue(lock.isFair());
192 < //         assertNotWriteLocked(lock);
193 < //         assertEquals(0, lock.getReadLockCount());
194 <
195 < //         lock = new PublicReentrantReadWriteLock(false);
196 < //         assertFalse(lock.isFair());
197 < //         assertNotWriteLocked(lock);
198 < //         assertEquals(0, lock.getReadLockCount());
199 < //     }
200 <
201 < //     /**
202 < //      * write-locking and read-locking an unlocked lock succeed
203 < //      */
204 < //     public void testLock()      { testLock(false); }
205 < //     public void testLock_fair() { testLock(true); }
206 < //     public void testLock(boolean fair) {
207 < //         PublicReentrantReadWriteLock lock =
208 < //             new PublicReentrantReadWriteLock(fair);
209 < //         assertNotWriteLocked(lock);
210 < //         lock.writeLock().lock();
211 < //         assertWriteLockedByMoi(lock);
212 < //         lock.writeLock().unlock();
213 < //         assertNotWriteLocked(lock);
214 < //         assertEquals(0, lock.getReadLockCount());
215 < //         lock.readLock().lock();
216 < //         assertNotWriteLocked(lock);
217 < //         assertEquals(1, lock.getReadLockCount());
218 < //         lock.readLock().unlock();
219 < //         assertNotWriteLocked(lock);
220 < //         assertEquals(0, lock.getReadLockCount());
221 < //     }
222 <
223 < //     /**
224 < //      * getWriteHoldCount returns number of recursive holds
225 < //      */
226 < //     public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
227 < //     public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
228 < //     public void testGetWriteHoldCount(boolean fair) {
229 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
230 < //         for (int i = 1; i <= SIZE; i++) {
231 < //             lock.writeLock().lock();
232 < //             assertEquals(i,lock.getWriteHoldCount());
233 < //         }
234 < //         for (int i = SIZE; i > 0; i--) {
235 < //             lock.writeLock().unlock();
236 < //             assertEquals(i-1,lock.getWriteHoldCount());
237 < //         }
238 < //     }
239 <
240 < //     /**
241 < //      * writelock.getHoldCount returns number of recursive holds
242 < //      */
243 < //     public void testGetHoldCount()      { testGetHoldCount(false); }
244 < //     public void testGetHoldCount_fair() { testGetHoldCount(true); }
245 < //     public void testGetHoldCount(boolean fair) {
246 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
247 < //         for (int i = 1; i <= SIZE; i++) {
248 < //             lock.writeLock().lock();
249 < //             assertEquals(i,lock.writeLock().getHoldCount());
250 < //         }
251 < //         for (int i = SIZE; i > 0; i--) {
252 < //             lock.writeLock().unlock();
253 < //             assertEquals(i-1,lock.writeLock().getHoldCount());
254 < //         }
255 < //     }
256 <
257 < //     /**
258 < //      * getReadHoldCount returns number of recursive holds
259 < //      */
260 < //     public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
261 < //     public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
262 < //     public void testGetReadHoldCount(boolean fair) {
263 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
264 < //         for (int i = 1; i <= SIZE; i++) {
265 < //             lock.readLock().lock();
266 < //             assertEquals(i,lock.getReadHoldCount());
267 < //         }
268 < //         for (int i = SIZE; i > 0; i--) {
269 < //             lock.readLock().unlock();
270 < //             assertEquals(i-1,lock.getReadHoldCount());
271 < //         }
272 < //     }
273 <
274 < //     /**
275 < //      * write-unlocking an unlocked lock throws IllegalMonitorStateException
276 < //      */
277 < //     public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
278 < //     public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
279 < //     public void testWriteUnlock_IMSE(boolean fair) {
280 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
281 < //         try {
282 < //             lock.writeLock().unlock();
283 < //             shouldThrow();
284 < //         } catch (IllegalMonitorStateException success) {}
285 < //     }
286 <
287 < //     /**
288 < //      * read-unlocking an unlocked lock throws IllegalMonitorStateException
289 < //      */
290 < //     public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
291 < //     public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
292 < //     public void testReadUnlock_IMSE(boolean fair) {
293 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
294 < //         try {
295 < //             lock.readLock().unlock();
296 < //             shouldThrow();
297 < //         } catch (IllegalMonitorStateException success) {}
298 < //     }
299 <
300 < //     /**
301 < //      * write-lockInterruptibly is interruptible
302 < //      */
303 < //     public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
304 < //     public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
305 < //     public void testWriteLockInterruptibly_Interruptible(boolean fair) {
306 < //         final PublicReentrantReadWriteLock lock =
307 < //             new PublicReentrantReadWriteLock(fair);
308 < //         lock.writeLock().lock();
309 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
310 < //             public void realRun() throws InterruptedException {
311 < //                 lock.writeLock().lockInterruptibly();
312 < //             }});
313 <
314 < //         waitForQueuedThread(lock, t);
315 < //         t.interrupt();
316 < //         awaitTermination(t);
317 < //         releaseWriteLock(lock);
318 < //     }
319 <
320 < //     /**
321 < //      * timed write-tryLock is interruptible
322 < //      */
323 < //     public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
324 < //     public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
325 < //     public void testWriteTryLock_Interruptible(boolean fair) {
326 < //         final PublicReentrantReadWriteLock lock =
327 < //             new PublicReentrantReadWriteLock(fair);
328 < //         lock.writeLock().lock();
329 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
330 < //             public void realRun() throws InterruptedException {
331 < //                 lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
332 < //             }});
333 <
334 < //         waitForQueuedThread(lock, t);
335 < //         t.interrupt();
336 < //         awaitTermination(t);
337 < //         releaseWriteLock(lock);
338 < //     }
339 <
340 < //     /**
341 < //      * read-lockInterruptibly is interruptible
342 < //      */
343 < //     public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
344 < //     public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
345 < //     public void testReadLockInterruptibly_Interruptible(boolean fair) {
346 < //         final PublicReentrantReadWriteLock lock =
347 < //             new PublicReentrantReadWriteLock(fair);
348 < //         lock.writeLock().lock();
349 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
350 < //             public void realRun() throws InterruptedException {
351 < //                 lock.readLock().lockInterruptibly();
352 < //             }});
353 <
354 < //         waitForQueuedThread(lock, t);
355 < //         t.interrupt();
356 < //         awaitTermination(t);
357 < //         releaseWriteLock(lock);
358 < //     }
359 <
360 < //     /**
361 < //      * timed read-tryLock is interruptible
362 < //      */
363 < //     public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
364 < //     public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
365 < //     public void testReadTryLock_Interruptible(boolean fair) {
366 < //         final PublicReentrantReadWriteLock lock =
367 < //             new PublicReentrantReadWriteLock(fair);
368 < //         lock.writeLock().lock();
369 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
370 < //             public void realRun() throws InterruptedException {
371 < //                 lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
372 < //             }});
373 <
374 < //         waitForQueuedThread(lock, t);
375 < //         t.interrupt();
376 < //         awaitTermination(t);
377 < //         releaseWriteLock(lock);
378 < //     }
379 <
380 < //     /**
381 < //      * write-tryLock on an unlocked lock succeeds
382 < //      */
383 < //     public void testWriteTryLock()      { testWriteTryLock(false); }
384 < //     public void testWriteTryLock_fair() { testWriteTryLock(true); }
385 < //     public void testWriteTryLock(boolean fair) {
386 < //         final PublicReentrantReadWriteLock lock =
387 < //             new PublicReentrantReadWriteLock(fair);
388 < //         assertTrue(lock.writeLock().tryLock());
389 < //         assertWriteLockedByMoi(lock);
390 < //         assertTrue(lock.writeLock().tryLock());
391 < //         assertWriteLockedByMoi(lock);
392 < //         lock.writeLock().unlock();
393 < //         releaseWriteLock(lock);
394 < //     }
395 <
396 < //     /**
397 < //      * write-tryLock fails if locked
398 < //      */
399 < //     public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
400 < //     public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
401 < //     public void testWriteTryLockWhenLocked(boolean fair) {
402 < //         final PublicReentrantReadWriteLock lock =
403 < //             new PublicReentrantReadWriteLock(fair);
404 < //         lock.writeLock().lock();
405 < //         Thread t = newStartedThread(new CheckedRunnable() {
406 < //             public void realRun() {
407 < //                 assertFalse(lock.writeLock().tryLock());
408 < //             }});
409 <
410 < //         awaitTermination(t);
411 < //         releaseWriteLock(lock);
412 < //     }
413 <
414 < //     /**
415 < //      * read-tryLock fails if locked
416 < //      */
417 < //     public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
418 < //     public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
419 < //     public void testReadTryLockWhenLocked(boolean fair) {
420 < //         final PublicReentrantReadWriteLock lock =
421 < //             new PublicReentrantReadWriteLock(fair);
422 < //         lock.writeLock().lock();
423 < //         Thread t = newStartedThread(new CheckedRunnable() {
424 < //             public void realRun() {
425 < //                 assertFalse(lock.readLock().tryLock());
426 < //             }});
427 <
428 < //         awaitTermination(t);
429 < //         releaseWriteLock(lock);
430 < //     }
431 <
432 < //     /**
433 < //      * Multiple threads can hold a read lock when not write-locked
434 < //      */
435 < //     public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
436 < //     public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
437 < //     public void testMultipleReadLocks(boolean fair) {
438 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
439 < //         lock.readLock().lock();
440 < //         Thread t = newStartedThread(new CheckedRunnable() {
441 < //             public void realRun() throws InterruptedException {
442 < //                 assertTrue(lock.readLock().tryLock());
443 < //                 lock.readLock().unlock();
444 < //                 assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
445 < //                 lock.readLock().unlock();
446 < //                 lock.readLock().lock();
447 < //                 lock.readLock().unlock();
448 < //             }});
449 <
450 < //         awaitTermination(t);
451 < //         lock.readLock().unlock();
452 < //     }
453 <
454 < //     /**
455 < //      * A writelock succeeds only after a reading thread unlocks
456 < //      */
457 < //     public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
458 < //     public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
459 < //     public void testWriteAfterReadLock(boolean fair) {
460 < //         final PublicReentrantReadWriteLock lock =
461 < //             new PublicReentrantReadWriteLock(fair);
462 < //         lock.readLock().lock();
463 < //         Thread t = newStartedThread(new CheckedRunnable() {
464 < //             public void realRun() {
465 < //                 assertEquals(1, lock.getReadLockCount());
466 < //                 lock.writeLock().lock();
467 < //                 assertEquals(0, lock.getReadLockCount());
468 < //                 lock.writeLock().unlock();
469 < //             }});
470 < //         waitForQueuedThread(lock, t);
471 < //         assertNotWriteLocked(lock);
472 < //         assertEquals(1, lock.getReadLockCount());
473 < //         lock.readLock().unlock();
474 < //         assertEquals(0, lock.getReadLockCount());
475 < //         awaitTermination(t);
476 < //         assertNotWriteLocked(lock);
477 < //     }
478 <
479 < //     /**
480 < //      * A writelock succeeds only after reading threads unlock
481 < //      */
482 < //     public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
483 < //     public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
484 < //     public void testWriteAfterMultipleReadLocks(boolean fair) {
485 < //         final PublicReentrantReadWriteLock lock =
486 < //             new PublicReentrantReadWriteLock(fair);
487 < //         lock.readLock().lock();
488 < //         lock.readLock().lock();
489 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
490 < //             public void realRun() {
491 < //                 lock.readLock().lock();
492 < //                 assertEquals(3, lock.getReadLockCount());
493 < //                 lock.readLock().unlock();
494 < //             }});
495 < //         awaitTermination(t1);
496 <
497 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
498 < //             public void realRun() {
499 < //                 assertEquals(2, lock.getReadLockCount());
500 < //                 lock.writeLock().lock();
501 < //                 assertEquals(0, lock.getReadLockCount());
502 < //                 lock.writeLock().unlock();
503 < //             }});
504 < //         waitForQueuedThread(lock, t2);
505 < //         assertNotWriteLocked(lock);
506 < //         assertEquals(2, lock.getReadLockCount());
507 < //         lock.readLock().unlock();
508 < //         lock.readLock().unlock();
509 < //         assertEquals(0, lock.getReadLockCount());
510 < //         awaitTermination(t2);
511 < //         assertNotWriteLocked(lock);
512 < //     }
513 <
514 < //     /**
515 < //      * A thread that tries to acquire a fair read lock (non-reentrantly)
516 < //      * will block if there is a waiting writer thread
517 < //      */
518 < //     public void testReaderWriterReaderFairFifo() {
519 < //         final PublicReentrantReadWriteLock lock =
520 < //             new PublicReentrantReadWriteLock(true);
521 < //         final AtomicBoolean t1GotLock = new AtomicBoolean(false);
522 <
523 < //         lock.readLock().lock();
524 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
525 < //             public void realRun() {
526 < //                 assertEquals(1, lock.getReadLockCount());
527 < //                 lock.writeLock().lock();
528 < //                 assertEquals(0, lock.getReadLockCount());
529 < //                 t1GotLock.set(true);
530 < //                 lock.writeLock().unlock();
531 < //             }});
532 < //         waitForQueuedThread(lock, t1);
533 <
534 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
535 < //             public void realRun() {
536 < //                 assertEquals(1, lock.getReadLockCount());
537 < //                 lock.readLock().lock();
538 < //                 assertEquals(1, lock.getReadLockCount());
539 < //                 assertTrue(t1GotLock.get());
540 < //                 lock.readLock().unlock();
541 < //             }});
542 < //         waitForQueuedThread(lock, t2);
543 < //         assertTrue(t1.isAlive());
544 < //         assertNotWriteLocked(lock);
545 < //         assertEquals(1, lock.getReadLockCount());
546 < //         lock.readLock().unlock();
547 < //         awaitTermination(t1);
548 < //         awaitTermination(t2);
549 < //         assertNotWriteLocked(lock);
550 < //     }
551 <
552 < //     /**
553 < //      * Readlocks succeed only after a writing thread unlocks
554 < //      */
555 < //     public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
556 < //     public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
557 < //     public void testReadAfterWriteLock(boolean fair) {
558 < //         final PublicReentrantReadWriteLock lock =
559 < //             new PublicReentrantReadWriteLock(fair);
560 < //         lock.writeLock().lock();
561 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
562 < //             public void realRun() {
563 < //                 lock.readLock().lock();
564 < //                 lock.readLock().unlock();
565 < //             }});
566 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
567 < //             public void realRun() {
568 < //                 lock.readLock().lock();
569 < //                 lock.readLock().unlock();
570 < //             }});
571 <
572 < //         waitForQueuedThread(lock, t1);
573 < //         waitForQueuedThread(lock, t2);
574 < //         releaseWriteLock(lock);
575 < //         awaitTermination(t1);
576 < //         awaitTermination(t2);
577 < //     }
578 <
579 < //     /**
580 < //      * Read trylock succeeds if write locked by current thread
581 < //      */
582 < //     public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
583 < //     public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
584 < //     public void testReadHoldingWriteLock(boolean fair) {
585 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
586 < //         lock.writeLock().lock();
587 < //         assertTrue(lock.readLock().tryLock());
588 < //         lock.readLock().unlock();
589 < //         lock.writeLock().unlock();
590 < //     }
591 <
592 < //     /**
593 < //      * Read trylock succeeds (barging) even in the presence of waiting
594 < //      * readers and/or writers
595 < //      */
596 < //     public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
597 < //     public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
598 < //     public void testReadTryLockBarging(boolean fair) {
599 < //         final PublicReentrantReadWriteLock lock =
600 < //             new PublicReentrantReadWriteLock(fair);
601 < //         lock.readLock().lock();
602 <
603 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
604 < //             public void realRun() {
605 < //                 lock.writeLock().lock();
606 < //                 lock.writeLock().unlock();
607 < //             }});
608 <
609 < //         waitForQueuedThread(lock, t1);
610 <
611 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
612 < //             public void realRun() {
613 < //                 lock.readLock().lock();
614 < //                 lock.readLock().unlock();
615 < //             }});
616 <
617 < //         if (fair)
618 < //             waitForQueuedThread(lock, t2);
619 <
620 < //         Thread t3 = newStartedThread(new CheckedRunnable() {
621 < //             public void realRun() {
622 < //                 lock.readLock().tryLock();
623 < //                 lock.readLock().unlock();
624 < //             }});
625 <
626 < //         assertTrue(lock.getReadLockCount() > 0);
627 < //         awaitTermination(t3);
628 < //         assertTrue(t1.isAlive());
629 < //         if (fair) assertTrue(t2.isAlive());
630 < //         lock.readLock().unlock();
631 < //         awaitTermination(t1);
632 < //         awaitTermination(t2);
633 < //     }
634 <
635 < //     /**
636 < //      * Read lock succeeds if write locked by current thread even if
637 < //      * other threads are waiting for readlock
638 < //      */
639 < //     public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
640 < //     public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
641 < //     public void testReadHoldingWriteLock2(boolean fair) {
642 < //         final PublicReentrantReadWriteLock lock =
643 < //             new PublicReentrantReadWriteLock(fair);
644 < //         lock.writeLock().lock();
645 < //         lock.readLock().lock();
646 < //         lock.readLock().unlock();
647 <
648 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
649 < //             public void realRun() {
650 < //                 lock.readLock().lock();
651 < //                 lock.readLock().unlock();
652 < //             }});
653 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
654 < //             public void realRun() {
655 < //                 lock.readLock().lock();
656 < //                 lock.readLock().unlock();
657 < //             }});
658 <
659 < //         waitForQueuedThread(lock, t1);
660 < //         waitForQueuedThread(lock, t2);
661 < //         assertWriteLockedByMoi(lock);
662 < //         lock.readLock().lock();
663 < //         lock.readLock().unlock();
664 < //         releaseWriteLock(lock);
665 < //         awaitTermination(t1);
666 < //         awaitTermination(t2);
667 < //     }
668 <
669 < //     /**
670 < //      * Read lock succeeds if write locked by current thread even if
671 < //      * other threads are waiting for writelock
672 < //      */
673 < //     public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
674 < //     public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
675 < //     public void testReadHoldingWriteLock3(boolean fair) {
676 < //         final PublicReentrantReadWriteLock lock =
677 < //             new PublicReentrantReadWriteLock(fair);
678 < //         lock.writeLock().lock();
679 < //         lock.readLock().lock();
680 < //         lock.readLock().unlock();
681 <
682 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
683 < //             public void realRun() {
684 < //                 lock.writeLock().lock();
685 < //                 lock.writeLock().unlock();
686 < //             }});
687 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
688 < //             public void realRun() {
689 < //                 lock.writeLock().lock();
690 < //                 lock.writeLock().unlock();
691 < //             }});
692 <
693 < //         waitForQueuedThread(lock, t1);
694 < //         waitForQueuedThread(lock, t2);
695 < //         assertWriteLockedByMoi(lock);
696 < //         lock.readLock().lock();
697 < //         lock.readLock().unlock();
698 < //         assertWriteLockedByMoi(lock);
699 < //         lock.writeLock().unlock();
700 < //         awaitTermination(t1);
701 < //         awaitTermination(t2);
702 < //     }
703 <
704 < //     /**
705 < //      * Write lock succeeds if write locked by current thread even if
706 < //      * other threads are waiting for writelock
707 < //      */
708 < //     public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
709 < //     public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
710 < //     public void testWriteHoldingWriteLock4(boolean fair) {
711 < //         final PublicReentrantReadWriteLock lock =
712 < //             new PublicReentrantReadWriteLock(fair);
713 < //         lock.writeLock().lock();
714 < //         lock.writeLock().lock();
715 < //         lock.writeLock().unlock();
716 <
717 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
718 < //             public void realRun() {
719 < //                 lock.writeLock().lock();
720 < //                 lock.writeLock().unlock();
721 < //             }});
722 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
723 < //             public void realRun() {
724 < //                 lock.writeLock().lock();
725 < //                 lock.writeLock().unlock();
726 < //             }});
727 <
728 < //         waitForQueuedThread(lock, t1);
729 < //         waitForQueuedThread(lock, t2);
730 < //         assertWriteLockedByMoi(lock);
731 < //         assertEquals(1, lock.getWriteHoldCount());
732 < //         lock.writeLock().lock();
733 < //         assertWriteLockedByMoi(lock);
734 < //         assertEquals(2, lock.getWriteHoldCount());
735 < //         lock.writeLock().unlock();
736 < //         assertWriteLockedByMoi(lock);
737 < //         assertEquals(1, lock.getWriteHoldCount());
738 < //         lock.writeLock().unlock();
739 < //         awaitTermination(t1);
740 < //         awaitTermination(t2);
741 < //     }
742 <
743 < //     /**
744 < //      * Read tryLock succeeds if readlocked but not writelocked
745 < //      */
746 < //     public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
747 < //     public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
748 < //     public void testTryLockWhenReadLocked(boolean fair) {
749 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
750 < //         lock.readLock().lock();
751 < //         Thread t = newStartedThread(new CheckedRunnable() {
752 < //             public void realRun() {
753 < //                 assertTrue(lock.readLock().tryLock());
754 < //                 lock.readLock().unlock();
755 < //             }});
756 <
757 < //         awaitTermination(t);
758 < //         lock.readLock().unlock();
759 < //     }
760 <
761 < //     /**
762 < //      * write tryLock fails when readlocked
763 < //      */
764 < //     public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
765 < //     public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
766 < //     public void testWriteTryLockWhenReadLocked(boolean fair) {
767 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
768 < //         lock.readLock().lock();
769 < //         Thread t = newStartedThread(new CheckedRunnable() {
770 < //             public void realRun() {
771 < //                 assertFalse(lock.writeLock().tryLock());
772 < //             }});
773 <
774 < //         awaitTermination(t);
775 < //         lock.readLock().unlock();
776 < //     }
777 <
778 < //     /**
779 < //      * write timed tryLock times out if locked
780 < //      */
781 < //     public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
782 < //     public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
783 < //     public void testWriteTryLock_Timeout(boolean fair) {
784 < //         final PublicReentrantReadWriteLock lock =
785 < //             new PublicReentrantReadWriteLock(fair);
786 < //         lock.writeLock().lock();
787 < //         Thread t = newStartedThread(new CheckedRunnable() {
788 < //             public void realRun() throws InterruptedException {
789 < //                 long startTime = System.nanoTime();
790 < //                 long timeoutMillis = 10;
791 < //                 assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
792 < //                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
793 < //             }});
794 <
795 < //         awaitTermination(t);
796 < //         releaseWriteLock(lock);
797 < //     }
798 <
799 < //     /**
800 < //      * read timed tryLock times out if write-locked
801 < //      */
802 < //     public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
803 < //     public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
804 < //     public void testReadTryLock_Timeout(boolean fair) {
805 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
806 < //         lock.writeLock().lock();
807 < //         Thread t = newStartedThread(new CheckedRunnable() {
808 < //             public void realRun() throws InterruptedException {
809 < //                 long startTime = System.nanoTime();
810 < //                 long timeoutMillis = 10;
811 < //                 assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
812 < //                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
813 < //             }});
814 <
815 < //         awaitTermination(t);
816 < //         assertTrue(lock.writeLock().isHeldByCurrentThread());
817 < //         lock.writeLock().unlock();
818 < //     }
819 <
820 < //     /**
821 < //      * write lockInterruptibly succeeds if unlocked, else is interruptible
822 < //      */
823 < //     public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
824 < //     public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
825 < //     public void testWriteLockInterruptibly(boolean fair) {
826 < //         final PublicReentrantReadWriteLock lock =
827 < //             new PublicReentrantReadWriteLock(fair);
828 < //         try {
829 < //             lock.writeLock().lockInterruptibly();
830 < //         } catch (InterruptedException ie) {
831 < //             threadUnexpectedException(ie);
832 < //         }
833 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
834 < //             public void realRun() throws InterruptedException {
835 < //                 lock.writeLock().lockInterruptibly();
836 < //             }});
837 <
838 < //         waitForQueuedThread(lock, t);
839 < //         t.interrupt();
840 < //         assertTrue(lock.writeLock().isHeldByCurrentThread());
841 < //         awaitTermination(t);
842 < //         releaseWriteLock(lock);
843 < //     }
844 <
845 < //     /**
846 < //      * read lockInterruptibly succeeds if lock free else is interruptible
847 < //      */
848 < //     public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
849 < //     public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
850 < //     public void testReadLockInterruptibly(boolean fair) {
851 < //         final PublicReentrantReadWriteLock lock =
852 < //             new PublicReentrantReadWriteLock(fair);
853 < //         try {
854 < //             lock.readLock().lockInterruptibly();
855 < //             lock.readLock().unlock();
856 < //             lock.writeLock().lockInterruptibly();
857 < //         } catch (InterruptedException ie) {
858 < //             threadUnexpectedException(ie);
859 < //         }
860 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
861 < //             public void realRun() throws InterruptedException {
862 < //                 lock.readLock().lockInterruptibly();
863 < //             }});
864 <
865 < //         waitForQueuedThread(lock, t);
866 < //         t.interrupt();
867 < //         awaitTermination(t);
868 < //         releaseWriteLock(lock);
869 < //     }
870 <
871 < //     /**
872 < //      * Calling await without holding lock throws IllegalMonitorStateException
873 < //      */
874 < //     public void testAwait_IMSE()      { testAwait_IMSE(false); }
875 < //     public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
876 < //     public void testAwait_IMSE(boolean fair) {
877 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
878 < //         final Condition c = lock.writeLock().newCondition();
879 < //         for (AwaitMethod awaitMethod : AwaitMethod.values()) {
880 < //             long startTime = System.nanoTime();
881 < //             try {
882 < //                 await(c, awaitMethod);
883 < //                 shouldThrow();
884 < //             } catch (IllegalMonitorStateException success) {
885 < //             } catch (InterruptedException e) { threadUnexpectedException(e); }
886 < //             assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
887 < //         }
888 < //     }
889 <
890 < //     /**
891 < //      * Calling signal without holding lock throws IllegalMonitorStateException
892 < //      */
893 < //     public void testSignal_IMSE()      { testSignal_IMSE(false); }
894 < //     public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
895 < //     public void testSignal_IMSE(boolean fair) {
896 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
897 < //         final Condition c = lock.writeLock().newCondition();
898 < //         try {
899 < //             c.signal();
900 < //             shouldThrow();
901 < //         } catch (IllegalMonitorStateException success) {}
902 < //     }
903 <
904 < //     /**
905 < //      * Calling signalAll without holding lock throws IllegalMonitorStateException
906 < //      */
907 < //     public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
908 < //     public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
909 < //     public void testSignalAll_IMSE(boolean fair) {
910 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
911 < //         final Condition c = lock.writeLock().newCondition();
912 < //         try {
913 < //             c.signalAll();
914 < //             shouldThrow();
915 < //         } catch (IllegalMonitorStateException success) {}
916 < //     }
917 <
918 < //     /**
919 < //      * awaitNanos without a signal times out
920 < //      */
921 < //     public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
922 < //     public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
923 < //     public void testAwaitNanos_Timeout(boolean fair) {
924 < //         try {
925 < //             final ReentrantReadWriteLock lock =
926 < //                 new ReentrantReadWriteLock(fair);
927 < //             final Condition c = lock.writeLock().newCondition();
928 < //             lock.writeLock().lock();
929 < //             long startTime = System.nanoTime();
930 < //             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 < //     }
24 >    /**
25 >     * A runnable calling writeLockInterruptibly
26 >     */
27 >    class InterruptibleLockRunnable extends CheckedRunnable {
28 >        final StampedLock lock;
29 >        InterruptibleLockRunnable(StampedLock l) { lock = l; }
30 >        public void realRun() throws InterruptedException {
31 >            lock.writeLockInterruptibly();
32 >        }
33 >    }
34 >
35 >    /**
36 >     * A runnable calling writeLockInterruptibly that expects to be
37 >     * interrupted
38 >     */
39 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
40 >        final StampedLock lock;
41 >        InterruptedLockRunnable(StampedLock l) { lock = l; }
42 >        public void realRun() throws InterruptedException {
43 >            lock.writeLockInterruptibly();
44 >        }
45 >    }
46 >
47 >    /**
48 >     * Releases write lock, checking isWriteLocked before and after
49 >     */
50 >    void releaseWriteLock(StampedLock lock, long s) {
51 >        assertTrue(lock.isWriteLocked());
52 >        lock.unlockWrite(s);
53 >        assertFalse(lock.isWriteLocked());
54 >    }
55 >
56 >    /**
57 >     * Constructed StampedLock is in unlocked state
58 >     */
59 >    public void testConstructor() {
60 >        StampedLock lock;
61 >        lock = new StampedLock();
62 >        assertFalse(lock.isWriteLocked());
63 >        assertFalse(lock.isReadLocked());
64 >        assertEquals(lock.getReadLockCount(), 0);
65 >    }
66 >
67 >    /**
68 >     * write-locking and read-locking an unlocked lock succeed
69 >     */
70 >    public void testLock() {
71 >        StampedLock lock = new StampedLock();
72 >        assertFalse(lock.isWriteLocked());
73 >        assertFalse(lock.isReadLocked());
74 >        assertEquals(lock.getReadLockCount(), 0);
75 >        long s = lock.writeLock();
76 >        assertTrue(lock.isWriteLocked());
77 >        assertFalse(lock.isReadLocked());
78 >        assertEquals(lock.getReadLockCount(), 0);
79 >        lock.unlockWrite(s);
80 >        assertFalse(lock.isWriteLocked());
81 >        assertFalse(lock.isReadLocked());
82 >        assertEquals(lock.getReadLockCount(), 0);
83 >        long rs = lock.readLock();
84 >        assertFalse(lock.isWriteLocked());
85 >        assertTrue(lock.isReadLocked());
86 >        assertEquals(lock.getReadLockCount(), 1);
87 >        lock.unlockRead(rs);
88 >        assertFalse(lock.isWriteLocked());
89 >        assertFalse(lock.isReadLocked());
90 >        assertEquals(lock.getReadLockCount(), 0);
91 >    }
92 >
93 >    /**
94 >     * unlock releases either a read or write lock
95 >     */
96 >    public void testUnlock() {
97 >        StampedLock lock = new StampedLock();
98 >        assertFalse(lock.isWriteLocked());
99 >        assertFalse(lock.isReadLocked());
100 >        assertEquals(lock.getReadLockCount(), 0);
101 >        long s = lock.writeLock();
102 >        assertTrue(lock.isWriteLocked());
103 >        assertFalse(lock.isReadLocked());
104 >        assertEquals(lock.getReadLockCount(), 0);
105 >        lock.unlock(s);
106 >        assertFalse(lock.isWriteLocked());
107 >        assertFalse(lock.isReadLocked());
108 >        assertEquals(lock.getReadLockCount(), 0);
109 >        long rs = lock.readLock();
110 >        assertFalse(lock.isWriteLocked());
111 >        assertTrue(lock.isReadLocked());
112 >        assertEquals(lock.getReadLockCount(), 1);
113 >        lock.unlock(rs);
114 >        assertFalse(lock.isWriteLocked());
115 >        assertFalse(lock.isReadLocked());
116 >        assertEquals(lock.getReadLockCount(), 0);
117 >    }
118 >
119 >    /**
120 >     * tryUnlockRead/Write succeeds if locked in associated mode else
121 >     * returns false
122 >     */
123 >    public void testTryUnlock() {
124 >        StampedLock lock = new StampedLock();
125 >        assertFalse(lock.isWriteLocked());
126 >        assertFalse(lock.isReadLocked());
127 >        assertEquals(lock.getReadLockCount(), 0);
128 >        long s = lock.writeLock();
129 >        assertTrue(lock.isWriteLocked());
130 >        assertFalse(lock.isReadLocked());
131 >        assertEquals(lock.getReadLockCount(), 0);
132 >        assertFalse(lock.tryUnlockRead());
133 >        assertTrue(lock.tryUnlockWrite());
134 >        assertFalse(lock.tryUnlockWrite());
135 >        assertFalse(lock.tryUnlockRead());
136 >        assertFalse(lock.isWriteLocked());
137 >        assertFalse(lock.isReadLocked());
138 >        assertEquals(lock.getReadLockCount(), 0);
139 >        long rs = lock.readLock();
140 >        assertFalse(lock.isWriteLocked());
141 >        assertTrue(lock.isReadLocked());
142 >        assertEquals(lock.getReadLockCount(), 1);
143 >        assertFalse(lock.tryUnlockWrite());
144 >        assertTrue(lock.tryUnlockRead());
145 >        assertFalse(lock.tryUnlockRead());
146 >        assertFalse(lock.tryUnlockWrite());
147 >        assertFalse(lock.isWriteLocked());
148 >        assertFalse(lock.isReadLocked());
149 >        assertEquals(lock.getReadLockCount(), 0);
150 >    }
151 >
152 >    /**
153 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
154 >     */
155 >    public void testWriteUnlock_IMSE() {
156 >        StampedLock lock = new StampedLock();
157 >        try {
158 >            lock.unlockWrite(0L);
159 >            shouldThrow();
160 >        } catch (IllegalMonitorStateException success) {}
161 >    }
162 >
163 >    /**
164 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
165 >     */
166 >    public void testWriteUnlock_IMSE2() {
167 >        StampedLock lock = new StampedLock();
168 >        try {
169 >            long s = lock.writeLock();
170 >            lock.unlockWrite(s);
171 >            lock.unlockWrite(s);
172 >            shouldThrow();
173 >        } catch (IllegalMonitorStateException success) {}
174 >    }
175 >
176 >    /**
177 >     * write-unlocking after readlock throws IllegalMonitorStateException
178 >     */
179 >    public void testWriteUnlock_IMSE3() {
180 >        StampedLock lock = new StampedLock();
181 >        try {
182 >            long s = lock.readLock();
183 >            lock.unlockWrite(s);
184 >            shouldThrow();
185 >        } catch (IllegalMonitorStateException success) {}
186 >    }
187 >
188 >    /**
189 >     * read-unlocking an unlocked lock throws IllegalMonitorStateException
190 >     */
191 >    public void testReadUnlock_IMSE() {
192 >        StampedLock lock = new StampedLock();
193 >        try {
194 >            long s = lock.readLock();
195 >            lock.unlockRead(s);
196 >            lock.unlockRead(s);
197 >            shouldThrow();
198 >        } catch (IllegalMonitorStateException success) {}
199 >    }
200 >
201 >    /**
202 >     * read-unlocking an unlocked lock throws IllegalMonitorStateException
203 >     */
204 >    public void testReadUnlock_IMSE2() {
205 >        StampedLock lock = new StampedLock();
206 >        try {
207 >            lock.unlockRead(0L);
208 >            shouldThrow();
209 >        } catch (IllegalMonitorStateException success) {}
210 >    }
211 >
212 >    /**
213 >     * read-unlocking after writeLock throws IllegalMonitorStateException
214 >     */
215 >    public void testReadUnlock_IMSE3() {
216 >        StampedLock lock = new StampedLock();
217 >        try {
218 >            long s = lock.writeLock();
219 >            lock.unlockRead(s);
220 >            shouldThrow();
221 >        } catch (IllegalMonitorStateException success) {}
222 >    }
223 >
224 >    /**
225 >     * validate(0) fails
226 >     */
227 >    public void testValidate0() {
228 >        StampedLock lock = new StampedLock();
229 >        assertFalse(lock.validate(0L));
230 >    }
231 >
232 >    /**
233 >     * A stamp obtained from a successful lock operation validates
234 >     */
235 >    public void testValidate() {
236 >        try {
237 >            StampedLock lock = new StampedLock();
238 >            long s = lock.writeLock();
239 >            assertTrue(lock.validate(s));
240 >            lock.unlockWrite(s);
241 >            s = lock.readLock();
242 >            assertTrue(lock.validate(s));
243 >            lock.unlockRead(s);
244 >            assertTrue((s = lock.tryWriteLock()) != 0L);
245 >            assertTrue(lock.validate(s));
246 >            lock.unlockWrite(s);
247 >            assertTrue((s = lock.tryReadLock()) != 0L);
248 >            assertTrue(lock.validate(s));
249 >            lock.unlockRead(s);
250 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
251 >            assertTrue(lock.validate(s));
252 >            lock.unlockWrite(s);
253 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
254 >            assertTrue(lock.validate(s));
255 >            lock.unlockRead(s);
256 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
257 >        } catch (InterruptedException ie) {
258 >            threadUnexpectedException(ie);
259 >        }
260 >    }
261 >
262 >    /**
263 >     * A stamp obtained from an unsuccessful lock operation does not validate
264 >     */
265 >    public void testValidate2() {
266 >        try {
267 >            StampedLock lock = new StampedLock();
268 >            long s;
269 >            assertTrue((s = lock.writeLock()) != 0L);
270 >            assertTrue(lock.validate(s));
271 >            assertFalse(lock.validate(lock.tryWriteLock()));
272 >            assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
273 >            assertFalse(lock.validate(lock.tryReadLock()));
274 >            assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
275 >            assertFalse(lock.validate(lock.tryOptimisticRead()));
276 >            lock.unlockWrite(s);
277 >        } catch (InterruptedException ie) {
278 >            threadUnexpectedException(ie);
279 >        }
280 >    }
281 >
282 >    /**
283 >     * writeLockInterruptibly is interruptible
284 >     */
285 >    public void testWriteLockInterruptibly_Interruptible() {
286 >        final CountDownLatch running = new CountDownLatch(1);
287 >        final StampedLock lock = new StampedLock();
288 >        long s = lock.writeLock();
289 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
290 >            public void realRun() throws InterruptedException {
291 >                running.countDown();
292 >                lock.writeLockInterruptibly();
293 >            }});
294 >        try {
295 >            running.await();
296 >            waitForThreadToEnterWaitState(t, 100);
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();
319 >            waitForThreadToEnterWaitState(t, 100);
320 >            t.interrupt();
321 >            awaitTermination(t);
322 >            releaseWriteLock(lock, s);
323 >        } catch (InterruptedException ie) {
324 >            threadUnexpectedException(ie);
325 >        }
326 >    }
327 >
328 >    /**
329 >     * readLockInterruptibly is interruptible
330 >     */
331 >    public void testReadLockInterruptibly_Interruptible() {
332 >        final CountDownLatch running = new CountDownLatch(1);
333 >        final StampedLock lock = new StampedLock();
334 >        long s = lock.writeLock();
335 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
336 >            public void realRun() throws InterruptedException {
337 >                running.countDown();
338 >                lock.readLockInterruptibly();
339 >            }});
340 >        try {
341 >            running.await();
342 >            waitForThreadToEnterWaitState(t, 100);
343 >            t.interrupt();
344 >            awaitTermination(t);
345 >            releaseWriteLock(lock, s);
346 >        } catch (InterruptedException ie) {
347 >            threadUnexpectedException(ie);
348 >        }
349 >    }
350 >
351 >    /**
352 >     * timed tryReadLock is interruptible
353 >     */
354 >    public void testReadTryLock_Interruptible() {
355 >        final CountDownLatch running = new CountDownLatch(1);
356 >        final StampedLock lock = new StampedLock();
357 >        long s = lock.writeLock();
358 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
359 >            public void realRun() throws InterruptedException {
360 >                running.countDown();
361 >                lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
362 >            }});
363 >        try {
364 >            running.await();
365 >            waitForThreadToEnterWaitState(t, 100);
366 >            t.interrupt();
367 >            awaitTermination(t);
368 >            releaseWriteLock(lock, s);
369 >        } catch (InterruptedException ie) {
370 >            threadUnexpectedException(ie);
371 >        }
372 >    }
373 >
374 >    /**
375 >     * tryWriteLock on an unlocked lock succeeds
376 >     */
377 >    public void testWriteTryLock() {
378 >        final StampedLock lock = new StampedLock();
379 >        long s = lock.tryWriteLock();
380 >        assertTrue(s != 0L);
381 >        assertTrue(lock.isWriteLocked());
382 >        long s2 = lock.tryWriteLock();
383 >        assertEquals(s2, 0L);
384 >        releaseWriteLock(lock, s);
385 >    }
386 >
387 >    /**
388 >     * tryWriteLock fails if locked
389 >     */
390 >    public void testWriteTryLockWhenLocked() {
391 >        final StampedLock lock = new StampedLock();
392 >        long s = lock.writeLock();
393 >        Thread t = newStartedThread(new CheckedRunnable() {
394 >            public void realRun() {
395 >                long ws = lock.tryWriteLock();
396 >                assertTrue(ws == 0L);
397 >            }});
398 >
399 >        awaitTermination(t);
400 >        releaseWriteLock(lock, s);
401 >    }
402 >
403 >    /**
404 >     * tryReadLock fails if write-locked
405 >     */
406 >    public void testReadTryLockWhenLocked() {
407 >        final StampedLock lock = new StampedLock();
408 >        long s = lock.writeLock();
409 >        Thread t = newStartedThread(new CheckedRunnable() {
410 >            public void realRun() {
411 >                long rs = lock.tryReadLock();
412 >                assertEquals(rs, 0L);
413 >            }});
414 >
415 >        awaitTermination(t);
416 >        releaseWriteLock(lock, s);
417 >    }
418 >
419 >    /**
420 >     * Multiple threads can hold a read lock when not write-locked
421 >     */
422 >    public void testMultipleReadLocks() {
423 >        final StampedLock lock = new StampedLock();
424 >        final long s = lock.readLock();
425 >        Thread t = newStartedThread(new CheckedRunnable() {
426 >            public void realRun() throws InterruptedException {
427 >                long s2 = lock.tryReadLock();
428 >                assertTrue(s2 != 0L);
429 >                lock.unlockRead(s2);
430 >                long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
431 >                assertTrue(s3 != 0L);
432 >                lock.unlockRead(s3);
433 >                long s4 = lock.readLock();
434 >                lock.unlockRead(s4);
435 >            }});
436 >
437 >        awaitTermination(t);
438 >        lock.unlockRead(s);
439 >    }
440 >
441 >    /**
442 >     * A writelock succeeds only after a reading thread unlocks
443 >     */
444 >    public void testWriteAfterReadLock() {
445 >        final CountDownLatch running = new CountDownLatch(1);
446 >        final StampedLock lock = new StampedLock();
447 >        long rs = lock.readLock();
448 >        Thread t = newStartedThread(new CheckedRunnable() {
449 >            public void realRun() {
450 >                running.countDown();
451 >                long s = lock.writeLock();
452 >                lock.unlockWrite(s);
453 >            }});
454 >        try {
455 >            running.await();
456 >            waitForThreadToEnterWaitState(t, 100);
457 >            assertFalse(lock.isWriteLocked());
458 >            lock.unlockRead(rs);
459 >            awaitTermination(t);
460 >            assertFalse(lock.isWriteLocked());
461 >        } catch (InterruptedException ie) {
462 >            threadUnexpectedException(ie);
463 >        }
464 >    }
465 >
466 >    /**
467 >     * A writelock succeeds only after reading threads unlock
468 >     */
469 >    public void testWriteAfterMultipleReadLocks() {
470 >        final StampedLock lock = new StampedLock();
471 >        long s = lock.readLock();
472 >        Thread t1 = newStartedThread(new CheckedRunnable() {
473 >            public void realRun() {
474 >                long rs = lock.readLock();
475 >                lock.unlockRead(rs);
476 >            }});
477 >        awaitTermination(t1);
478 >
479 >        Thread t2 = newStartedThread(new CheckedRunnable() {
480 >            public void realRun() {
481 >                long ws = lock.writeLock();
482 >                lock.unlockWrite(ws);
483 >            }});
484 >        assertFalse(lock.isWriteLocked());
485 >        lock.unlockRead(s);
486 >        awaitTermination(t2);
487 >        assertFalse(lock.isWriteLocked());
488 >    }
489 >
490 >    /**
491 >     * Readlocks succeed only after a writing thread unlocks
492 >     */
493 >    public void testReadAfterWriteLock() {
494 >        final StampedLock lock = new StampedLock();
495 >        final long s = lock.writeLock();
496 >        Thread t1 = newStartedThread(new CheckedRunnable() {
497 >            public void realRun() {
498 >                long rs = lock.readLock();
499 >                lock.unlockRead(rs);
500 >            }});
501 >        Thread t2 = newStartedThread(new CheckedRunnable() {
502 >            public void realRun() {
503 >                long rs = lock.readLock();
504 >                lock.unlockRead(rs);
505 >            }});
506 >
507 >        releaseWriteLock(lock, s);
508 >        awaitTermination(t1);
509 >        awaitTermination(t2);
510 >    }
511 >
512 >    /**
513 >     * tryReadLock succeeds if readlocked but not writelocked
514 >     */
515 >    public void testTryLockWhenReadLocked() {
516 >        final StampedLock lock = new StampedLock();
517 >        long s = lock.readLock();
518 >        Thread t = newStartedThread(new CheckedRunnable() {
519 >            public void realRun() {
520 >                long rs = lock.tryReadLock();
521 >                threadAssertTrue(rs != 0L);
522 >                lock.unlockRead(rs);
523 >            }});
524 >
525 >        awaitTermination(t);
526 >        lock.unlockRead(s);
527 >    }
528 >
529 >    /**
530 >     * tryWriteLock fails when readlocked
531 >     */
532 >    public void testWriteTryLockWhenReadLocked() {
533 >        final StampedLock lock = new StampedLock();
534 >        long s = lock.readLock();
535 >        Thread t = newStartedThread(new CheckedRunnable() {
536 >            public void realRun() {
537 >                long ws = lock.tryWriteLock();
538 >                threadAssertEquals(ws, 0L);
539 >            }});
540 >
541 >        awaitTermination(t);
542 >        lock.unlockRead(s);
543 >    }
544 >
545 >    /**
546 >     * timed tryWriteLock times out if locked
547 >     */
548 >    public void testWriteTryLock_Timeout() {
549 >        final StampedLock lock = new StampedLock();
550 >        long s = lock.writeLock();
551 >        Thread t = newStartedThread(new CheckedRunnable() {
552 >            public void realRun() throws InterruptedException {
553 >                long startTime = System.nanoTime();
554 >                long timeoutMillis = 10;
555 >                long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
556 >                assertEquals(ws, 0L);
557 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
558 >            }});
559 >
560 >        awaitTermination(t);
561 >        releaseWriteLock(lock, s);
562 >    }
563 >
564 >    /**
565 >     * timed tryReadLock times out if write-locked
566 >     */
567 >    public void testReadTryLock_Timeout() {
568 >        final StampedLock lock = new StampedLock();
569 >        long s = lock.writeLock();
570 >        Thread t = newStartedThread(new CheckedRunnable() {
571 >            public void realRun() throws InterruptedException {
572 >                long startTime = System.nanoTime();
573 >                long timeoutMillis = 10;
574 >                long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
575 >                assertEquals(rs, 0L);
576 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
577 >            }});
578 >
579 >        awaitTermination(t);
580 >        assertTrue(lock.isWriteLocked());
581 >        lock.unlockWrite(s);
582 >    }
583 >
584 >    /**
585 >     * writeLockInterruptibly succeeds if unlocked, else is interruptible
586 >     */
587 >    public void testWriteLockInterruptibly() {
588 >        final CountDownLatch running = new CountDownLatch(1);
589 >        final StampedLock lock = new StampedLock();
590 >        long s = 0L;
591 >        try {
592 >            s = lock.writeLockInterruptibly();
593 >        } catch (InterruptedException ie) {
594 >            threadUnexpectedException(ie);
595 >        }
596 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
597 >            public void realRun() throws InterruptedException {
598 >                running.countDown();
599 >                lock.writeLockInterruptibly();
600 >            }});
601 >
602 >        try {
603 >            running.await();
604 >            waitForThreadToEnterWaitState(t, 100);
605 >            t.interrupt();
606 >            assertTrue(lock.isWriteLocked());
607 >            awaitTermination(t);
608 >            releaseWriteLock(lock, s);
609 >        } catch (InterruptedException ie) {
610 >            threadUnexpectedException(ie);
611 >        }
612 >
613 >    }
614 >
615 >    /**
616 >     * readLockInterruptibly succeeds if lock free else is interruptible
617 >     */
618 >    public void testReadLockInterruptibly() {
619 >        final CountDownLatch running = new CountDownLatch(1);
620 >        final StampedLock lock = new StampedLock();
621 >        long s = 0L;
622 >        try {
623 >            s = lock.readLockInterruptibly();
624 >            lock.unlockRead(s);
625 >            s = lock.writeLockInterruptibly();
626 >        } catch (InterruptedException ie) {
627 >            threadUnexpectedException(ie);
628 >        }
629 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
630 >            public void realRun() throws InterruptedException {
631 >                running.countDown();
632 >                lock.readLockInterruptibly();
633 >            }});
634 >        try {
635 >            running.await();
636 >            waitForThreadToEnterWaitState(t, 100);
637 >            t.interrupt();
638 >            awaitTermination(t);
639 >            releaseWriteLock(lock, s);
640 >        } catch (InterruptedException ie) {
641 >            threadUnexpectedException(ie);
642 >        }
643 >    }
644 >
645 >    /**
646 >     * A serialized lock deserializes as unlocked
647 >     */
648 >    public void testSerialization() {
649 >        StampedLock lock = new StampedLock();
650 >        lock.writeLock();
651 >        StampedLock clone = serialClone(lock);
652 >        assertTrue(lock.isWriteLocked());
653 >        assertFalse(clone.isWriteLocked());
654 >        long s = clone.writeLock();
655 >        assertTrue(clone.isWriteLocked());
656 >        clone.unlockWrite(s);
657 >        assertFalse(clone.isWriteLocked());
658 >    }
659 >
660 >    /**
661 >     * toString indicates current lock state
662 >     */
663 >    public void testToString() {
664 >        StampedLock lock = new StampedLock();
665 >        assertTrue(lock.toString().contains("Unlocked"));
666 >        long s = lock.writeLock();
667 >        assertTrue(lock.toString().contains("Write-locked"));
668 >        lock.unlockWrite(s);
669 >        s = lock.readLock();
670 >        assertTrue(lock.toString().contains("Read-locks"));
671 >    }
672 >
673 >    /**
674 >     * tryOptimisticRead succeeds and validates if unlocked, fails if locked
675 >     */
676 >    public void testValidateOptimistic() {
677 >        try {
678 >            StampedLock lock = new StampedLock();
679 >            long s, p;
680 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
681 >            assertTrue(lock.validate(p));
682 >            assertTrue((s = lock.writeLock()) != 0L);
683 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
684 >            assertTrue(lock.validate(s));
685 >            lock.unlockWrite(s);
686 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
687 >            assertTrue(lock.validate(p));
688 >            assertTrue((s = lock.readLock()) != 0L);
689 >            assertTrue(lock.validate(s));
690 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
691 >            assertTrue(lock.validate(p));
692 >            lock.unlockRead(s);
693 >            assertTrue((s = lock.tryWriteLock()) != 0L);
694 >            assertTrue(lock.validate(s));
695 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
696 >            lock.unlockWrite(s);
697 >            assertTrue((s = lock.tryReadLock()) != 0L);
698 >            assertTrue(lock.validate(s));
699 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
700 >            lock.unlockRead(s);
701 >            assertTrue(lock.validate(p));
702 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
703 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
704 >            assertTrue(lock.validate(s));
705 >            lock.unlockWrite(s);
706 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
707 >            assertTrue(lock.validate(s));
708 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
709 >            lock.unlockRead(s);
710 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
711 >        } catch (InterruptedException ie) {
712 >            threadUnexpectedException(ie);
713 >        }
714 >    }
715 >
716 >    /**
717 >     * tryOptimisticRead stamp does not validate if a write lock intervenes
718 >     */
719 >    public void testValidateOptimisticWriteLocked() {
720 >        StampedLock lock = new StampedLock();
721 >        long s, p;
722 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
723 >        assertTrue((s = lock.writeLock()) != 0L);
724 >        assertFalse(lock.validate(p));
725 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
726 >        assertTrue(lock.validate(s));
727 >        lock.unlockWrite(s);
728 >    }
729 >
730 >    /**
731 >     * tryOptimisticRead stamp does not validate if a write lock
732 >     * intervenes in another thread
733 >     */
734 >    public void testValidateOptimisticWriteLocked2() {
735 >        final CountDownLatch running = new CountDownLatch(1);
736 >        final StampedLock lock = new StampedLock();
737 >        long s, p;
738 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
739 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
740 >                public void realRun() throws InterruptedException {
741 >                    lock.writeLockInterruptibly();
742 >                    running.countDown();
743 >                    lock.writeLockInterruptibly();
744 >                }});
745 >        try {
746 >            running.await();
747 >            assertFalse(lock.validate(p));
748 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
749 >            t.interrupt();
750 >            awaitTermination(t);
751 >        } catch (InterruptedException ie) {
752 >            threadUnexpectedException(ie);
753 >        }
754 >    }
755 >
756 >    /**
757 >     * tryConvertToOptimisticRead succeeds and validates if successfully locked,
758 >     */
759 >    public void testTryConvertToOptimisticRead() {
760 >        try {
761 >            StampedLock lock = new StampedLock();
762 >            long s, p;
763 >            s = 0L;
764 >            assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
765 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
766 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
767 >            assertTrue((s = lock.writeLock()) != 0L);
768 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
769 >            assertTrue(lock.validate(p));
770 >            assertTrue((s = lock.readLock()) != 0L);
771 >            assertTrue(lock.validate(s));
772 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
773 >            assertTrue(lock.validate(p));
774 >            assertTrue((s = lock.tryWriteLock()) != 0L);
775 >            assertTrue(lock.validate(s));
776 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
777 >            assertTrue(lock.validate(p));
778 >            assertTrue((s = lock.tryReadLock()) != 0L);
779 >            assertTrue(lock.validate(s));
780 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
781 >            assertTrue(lock.validate(p));
782 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
783 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
784 >            assertTrue(lock.validate(p));
785 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
786 >            assertTrue(lock.validate(s));
787 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
788 >            assertTrue(lock.validate(p));
789 >        } catch (InterruptedException ie) {
790 >            threadUnexpectedException(ie);
791 >        }
792 >    }
793 >
794 >    /**
795 >     * tryConvertToReadLock succeeds and validates if successfully locked
796 >     * or lock free;
797 >     */
798 >    public void testTryConvertToReadLock() {
799 >        try {
800 >            StampedLock lock = new StampedLock();
801 >            long s, p;
802 >            s = 0L;
803 >            assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
804 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
805 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
806 >            lock.unlockRead(p);
807 >            assertTrue((s = lock.writeLock()) != 0L);
808 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
809 >            assertTrue(lock.validate(p));
810 >            lock.unlockRead(p);
811 >            assertTrue((s = lock.readLock()) != 0L);
812 >            assertTrue(lock.validate(s));
813 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
814 >            assertTrue(lock.validate(p));
815 >            lock.unlockRead(p);
816 >            assertTrue((s = lock.tryWriteLock()) != 0L);
817 >            assertTrue(lock.validate(s));
818 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
819 >            assertTrue(lock.validate(p));
820 >            lock.unlockRead(p);
821 >            assertTrue((s = lock.tryReadLock()) != 0L);
822 >            assertTrue(lock.validate(s));
823 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
824 >            assertTrue(lock.validate(p));
825 >            lock.unlockRead(p);
826 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
827 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
828 >            assertTrue(lock.validate(p));
829 >            lock.unlockRead(p);
830 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
831 >            assertTrue(lock.validate(s));
832 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
833 >            assertTrue(lock.validate(p));
834 >            lock.unlockRead(p);
835 >        } catch (InterruptedException ie) {
836 >            threadUnexpectedException(ie);
837 >        }
838 >    }
839 >
840 >    /**
841 >     * tryConvertToWriteLock succeeds and validates if successfully locked
842 >     * or lock free;
843 >     */
844 >    public void testTryConvertToWriteLock() {
845 >        try {
846 >            StampedLock lock = new StampedLock();
847 >            long s, p;
848 >            s = 0L;
849 >            assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
850 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
851 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
852 >            lock.unlockWrite(p);
853 >            assertTrue((s = lock.writeLock()) != 0L);
854 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
855 >            assertTrue(lock.validate(p));
856 >            lock.unlockWrite(p);
857 >            assertTrue((s = lock.readLock()) != 0L);
858 >            assertTrue(lock.validate(s));
859 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
860 >            assertTrue(lock.validate(p));
861 >            lock.unlockWrite(p);
862 >            assertTrue((s = lock.tryWriteLock()) != 0L);
863 >            assertTrue(lock.validate(s));
864 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
865 >            assertTrue(lock.validate(p));
866 >            lock.unlockWrite(p);
867 >            assertTrue((s = lock.tryReadLock()) != 0L);
868 >            assertTrue(lock.validate(s));
869 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
870 >            assertTrue(lock.validate(p));
871 >            lock.unlockWrite(p);
872 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
873 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
874 >            assertTrue(lock.validate(p));
875 >            lock.unlockWrite(p);
876 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
877 >            assertTrue(lock.validate(s));
878 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
879 >            assertTrue(lock.validate(p));
880 >            lock.unlockWrite(p);
881 >        } catch (InterruptedException ie) {
882 >            threadUnexpectedException(ie);
883 >        }
884 >    }
885 >
886 >    /**
887 >     * asWriteLock can be locked and unlocked
888 >     */
889 >    public void testAsWriteLock() {
890 >        StampedLock sl = new StampedLock();
891 >        Lock lock = sl.asWriteLock();
892 >        lock.lock();
893 >        assertFalse(lock.tryLock());
894 >        lock.unlock();
895 >        assertTrue(lock.tryLock());
896 >    }
897 >
898 >    /**
899 >     * asReadLock can be locked and unlocked
900 >     */
901 >    public void testAsReadLock() {
902 >        StampedLock sl = new StampedLock();
903 >        Lock lock = sl.asReadLock();
904 >        lock.lock();
905 >        lock.unlock();
906 >        assertTrue(lock.tryLock());
907 >    }
908 >
909 >    /**
910 >     * asReadWriteLock.writeLock can be locked and unlocked
911 >     */
912 >    public void testAsReadWriteLockWriteLock() {
913 >        StampedLock sl = new StampedLock();
914 >        Lock lock = sl.asReadWriteLock().writeLock();
915 >        lock.lock();
916 >        assertFalse(lock.tryLock());
917 >        lock.unlock();
918 >        assertTrue(lock.tryLock());
919 >    }
920 >
921 >    /**
922 >     * asReadWriteLock.readLock can be locked and unlocked
923 >     */
924 >    public void testAsReadWriteLockReadLock() {
925 >        StampedLock sl = new StampedLock();
926 >        Lock lock = sl.asReadWriteLock().readLock();
927 >        lock.lock();
928 >        lock.unlock();
929 >        assertTrue(lock.tryLock());
930 >    }
931  
932   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines