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.26 by jsr166, Sun Jul 17 03:06:50 2016 UTC

# Line 3 | Line 3
3   * with assistance from members of JCP JSR-166 Expert Group and
4   * released to the public domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
6 * Other contributors include Andrew Wright, Jeffrey Hayes,
7 * Pat Fisher, Mike Judd.
6   */
7  
8 < import junit.framework.*;
9 < import java.util.concurrent.atomic.AtomicBoolean;
10 < import java.util.concurrent.locks.Condition;
8 > import static java.util.concurrent.TimeUnit.DAYS;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.concurrent.CountDownLatch;
14 > import java.util.concurrent.TimeUnit;
15   import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReadWriteLock;
16   import java.util.concurrent.locks.StampedLock;
17 < import java.util.concurrent.CountDownLatch;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import java.util.*;
17 > import java.util.function.BiConsumer;
18 > import java.util.function.Consumer;
19 > import java.util.function.Function;
20 >
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class StampedLockTest extends JSR166TestCase {
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28      public static Test suite() {
29          return new TestSuite(StampedLockTest.class);
30      }
31  
32 <    // XXXX Just a skeleton implementation for now.
33 <    public void testTODO() {
34 <        fail("Please add some real tests!");
35 <    }
36 <
37 <    public void testConstructor() { new StampedLock(); }
38 <
39 < //     /**
40 < //      * A runnable calling lockInterruptibly
41 < //      */
42 < //     class InterruptibleLockRunnable extends CheckedRunnable {
43 < //         final ReentrantReadWriteLock lock;
44 < //         InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
45 < //         public void realRun() throws InterruptedException {
46 < //             lock.writeLock().lockInterruptibly();
47 < //         }
48 < //     }
49 <
50 < //     /**
51 < //      * A runnable calling lockInterruptibly that expects to be
52 < //      * interrupted
53 < //      */
54 < //     class InterruptedLockRunnable extends CheckedInterruptedRunnable {
55 < //         final ReentrantReadWriteLock lock;
56 < //         InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
57 < //         public void realRun() throws InterruptedException {
58 < //             lock.writeLock().lockInterruptibly();
59 < //         }
60 < //     }
61 <
62 < //     /**
63 < //      * Subclass to expose protected methods
64 < //      */
65 < //     static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
66 < //         PublicReentrantReadWriteLock() { super(); }
67 < //         PublicReentrantReadWriteLock(boolean fair) { super(fair); }
68 < //         public Thread getOwner() {
69 < //             return super.getOwner();
70 < //         }
71 < //         public Collection<Thread> getQueuedThreads() {
72 < //             return super.getQueuedThreads();
73 < //         }
74 < //         public Collection<Thread> getWaitingThreads(Condition c) {
75 < //             return super.getWaitingThreads(c);
76 < //         }
77 < //     }
78 <
79 < //     /**
80 < //      * Releases write lock, checking that it had a hold count of 1.
81 < //      */
82 < //     void releaseWriteLock(PublicReentrantReadWriteLock lock) {
83 < //         ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
84 < //         assertWriteLockedByMoi(lock);
85 < //         assertEquals(1, lock.getWriteHoldCount());
86 < //         writeLock.unlock();
87 < //         assertNotWriteLocked(lock);
88 < //     }
89 <
90 < //     /**
91 < //      * Spin-waits until lock.hasQueuedThread(t) becomes true.
92 < //      */
93 < //     void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
94 < //         long startTime = System.nanoTime();
95 < //         while (!lock.hasQueuedThread(t)) {
96 < //             if (millisElapsedSince(startTime) > LONG_DELAY_MS)
97 < //                 throw new AssertionFailedError("timed out");
98 < //             Thread.yield();
99 < //         }
100 < //         assertTrue(t.isAlive());
101 < //         assertTrue(lock.getOwner() != t);
102 < //     }
103 <
104 < //     /**
105 < //      * Checks that lock is not write-locked.
106 < //      */
107 < //     void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
108 < //         assertFalse(lock.isWriteLocked());
109 < //         assertFalse(lock.isWriteLockedByCurrentThread());
110 < //         assertFalse(lock.writeLock().isHeldByCurrentThread());
111 < //         assertEquals(0, lock.getWriteHoldCount());
112 < //         assertEquals(0, lock.writeLock().getHoldCount());
113 < //         assertNull(lock.getOwner());
114 < //     }
115 <
116 < //     /**
117 < //      * Checks that lock is write-locked by the given thread.
118 < //      */
119 < //     void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
120 < //         assertTrue(lock.isWriteLocked());
121 < //         assertSame(t, lock.getOwner());
122 < //         assertEquals(t == Thread.currentThread(),
123 < //                      lock.isWriteLockedByCurrentThread());
124 < //         assertEquals(t == Thread.currentThread(),
125 < //                      lock.writeLock().isHeldByCurrentThread());
126 < //         assertEquals(t == Thread.currentThread(),
127 < //                      lock.getWriteHoldCount() > 0);
128 < //         assertEquals(t == Thread.currentThread(),
129 < //                      lock.writeLock().getHoldCount() > 0);
130 < //         assertEquals(0, lock.getReadLockCount());
131 < //     }
132 <
133 < //     /**
134 < //      * Checks that lock is write-locked by the current thread.
135 < //      */
136 < //     void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
137 < //         assertWriteLockedBy(lock, Thread.currentThread());
138 < //     }
139 <
140 < //     /**
141 < //      * Checks that condition c has no waiters.
142 < //      */
143 < //     void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
144 < //         assertHasWaiters(lock, c, new Thread[] {});
145 < //     }
146 <
147 < //     /**
148 < //      * Checks that condition c has exactly the given waiter threads.
149 < //      */
150 < //     void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
151 < //                           Thread... threads) {
152 < //         lock.writeLock().lock();
153 < //         assertEquals(threads.length > 0, lock.hasWaiters(c));
154 < //         assertEquals(threads.length, lock.getWaitQueueLength(c));
155 < //         assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
156 < //         assertEquals(threads.length, lock.getWaitingThreads(c).size());
157 < //         assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
158 < //                      new HashSet<Thread>(Arrays.asList(threads)));
159 < //         lock.writeLock().unlock();
160 < //     }
161 <
162 < //     enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
163 <
164 < //     /**
165 < //      * Awaits condition using the specified AwaitMethod.
166 < //      */
167 < //     void await(Condition c, AwaitMethod awaitMethod)
168 < //             throws InterruptedException {
169 < //         switch (awaitMethod) {
170 < //         case await:
171 < //             c.await();
172 < //             break;
173 < //         case awaitTimed:
174 < //             assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
175 < //             break;
176 < //         case awaitNanos:
177 < //             long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
178 < //             assertTrue(nanosRemaining > 0);
179 < //             break;
180 < //         case awaitUntil:
181 < //             java.util.Date d = new java.util.Date();
182 < //             assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
183 < //             break;
184 < //         }
185 < //     }
186 <
187 < //     /**
188 < //      * Constructor sets given fairness, and is in unlocked state
189 < //      */
190 < //     public void testConstructor() {
191 < //         PublicReentrantReadWriteLock lock;
192 <
193 < //         lock = new PublicReentrantReadWriteLock();
194 < //         assertFalse(lock.isFair());
195 < //         assertNotWriteLocked(lock);
196 < //         assertEquals(0, lock.getReadLockCount());
197 <
198 < //         lock = new PublicReentrantReadWriteLock(true);
199 < //         assertTrue(lock.isFair());
200 < //         assertNotWriteLocked(lock);
201 < //         assertEquals(0, lock.getReadLockCount());
202 <
203 < //         lock = new PublicReentrantReadWriteLock(false);
204 < //         assertFalse(lock.isFair());
205 < //         assertNotWriteLocked(lock);
206 < //         assertEquals(0, lock.getReadLockCount());
207 < //     }
208 <
209 < //     /**
210 < //      * write-locking and read-locking an unlocked lock succeed
211 < //      */
212 < //     public void testLock()      { testLock(false); }
213 < //     public void testLock_fair() { testLock(true); }
214 < //     public void testLock(boolean fair) {
215 < //         PublicReentrantReadWriteLock lock =
216 < //             new PublicReentrantReadWriteLock(fair);
217 < //         assertNotWriteLocked(lock);
218 < //         lock.writeLock().lock();
219 < //         assertWriteLockedByMoi(lock);
220 < //         lock.writeLock().unlock();
221 < //         assertNotWriteLocked(lock);
222 < //         assertEquals(0, lock.getReadLockCount());
223 < //         lock.readLock().lock();
224 < //         assertNotWriteLocked(lock);
225 < //         assertEquals(1, lock.getReadLockCount());
226 < //         lock.readLock().unlock();
227 < //         assertNotWriteLocked(lock);
228 < //         assertEquals(0, lock.getReadLockCount());
229 < //     }
230 <
231 < //     /**
232 < //      * getWriteHoldCount returns number of recursive holds
233 < //      */
234 < //     public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
235 < //     public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
236 < //     public void testGetWriteHoldCount(boolean fair) {
237 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
238 < //         for (int i = 1; i <= SIZE; i++) {
239 < //             lock.writeLock().lock();
240 < //             assertEquals(i,lock.getWriteHoldCount());
241 < //         }
242 < //         for (int i = SIZE; i > 0; i--) {
243 < //             lock.writeLock().unlock();
244 < //             assertEquals(i-1,lock.getWriteHoldCount());
245 < //         }
246 < //     }
247 <
248 < //     /**
249 < //      * writelock.getHoldCount returns number of recursive holds
250 < //      */
251 < //     public void testGetHoldCount()      { testGetHoldCount(false); }
252 < //     public void testGetHoldCount_fair() { testGetHoldCount(true); }
253 < //     public void testGetHoldCount(boolean fair) {
254 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
255 < //         for (int i = 1; i <= SIZE; i++) {
256 < //             lock.writeLock().lock();
257 < //             assertEquals(i,lock.writeLock().getHoldCount());
258 < //         }
259 < //         for (int i = SIZE; i > 0; i--) {
260 < //             lock.writeLock().unlock();
261 < //             assertEquals(i-1,lock.writeLock().getHoldCount());
262 < //         }
263 < //     }
264 <
265 < //     /**
266 < //      * getReadHoldCount returns number of recursive holds
267 < //      */
268 < //     public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
269 < //     public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
270 < //     public void testGetReadHoldCount(boolean fair) {
271 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
272 < //         for (int i = 1; i <= SIZE; i++) {
273 < //             lock.readLock().lock();
274 < //             assertEquals(i,lock.getReadHoldCount());
275 < //         }
276 < //         for (int i = SIZE; i > 0; i--) {
277 < //             lock.readLock().unlock();
278 < //             assertEquals(i-1,lock.getReadHoldCount());
279 < //         }
280 < //     }
281 <
282 < //     /**
283 < //      * write-unlocking an unlocked lock throws IllegalMonitorStateException
284 < //      */
285 < //     public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
286 < //     public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
287 < //     public void testWriteUnlock_IMSE(boolean fair) {
288 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
289 < //         try {
290 < //             lock.writeLock().unlock();
291 < //             shouldThrow();
292 < //         } catch (IllegalMonitorStateException success) {}
293 < //     }
294 <
295 < //     /**
296 < //      * read-unlocking an unlocked lock throws IllegalMonitorStateException
297 < //      */
298 < //     public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
299 < //     public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
300 < //     public void testReadUnlock_IMSE(boolean fair) {
301 < //         ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
302 < //         try {
303 < //             lock.readLock().unlock();
304 < //             shouldThrow();
305 < //         } catch (IllegalMonitorStateException success) {}
306 < //     }
307 <
308 < //     /**
309 < //      * write-lockInterruptibly is interruptible
310 < //      */
311 < //     public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
312 < //     public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
313 < //     public void testWriteLockInterruptibly_Interruptible(boolean fair) {
314 < //         final PublicReentrantReadWriteLock lock =
315 < //             new PublicReentrantReadWriteLock(fair);
316 < //         lock.writeLock().lock();
317 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
318 < //             public void realRun() throws InterruptedException {
319 < //                 lock.writeLock().lockInterruptibly();
320 < //             }});
321 <
322 < //         waitForQueuedThread(lock, t);
323 < //         t.interrupt();
324 < //         awaitTermination(t);
325 < //         releaseWriteLock(lock);
326 < //     }
327 <
328 < //     /**
329 < //      * timed write-tryLock is interruptible
330 < //      */
331 < //     public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
332 < //     public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
333 < //     public void testWriteTryLock_Interruptible(boolean fair) {
334 < //         final PublicReentrantReadWriteLock lock =
335 < //             new PublicReentrantReadWriteLock(fair);
336 < //         lock.writeLock().lock();
337 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
338 < //             public void realRun() throws InterruptedException {
339 < //                 lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
340 < //             }});
341 <
342 < //         waitForQueuedThread(lock, t);
343 < //         t.interrupt();
344 < //         awaitTermination(t);
345 < //         releaseWriteLock(lock);
346 < //     }
347 <
348 < //     /**
349 < //      * read-lockInterruptibly is interruptible
350 < //      */
351 < //     public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
352 < //     public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
353 < //     public void testReadLockInterruptibly_Interruptible(boolean fair) {
354 < //         final PublicReentrantReadWriteLock lock =
355 < //             new PublicReentrantReadWriteLock(fair);
356 < //         lock.writeLock().lock();
357 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
358 < //             public void realRun() throws InterruptedException {
359 < //                 lock.readLock().lockInterruptibly();
360 < //             }});
361 <
362 < //         waitForQueuedThread(lock, t);
363 < //         t.interrupt();
364 < //         awaitTermination(t);
365 < //         releaseWriteLock(lock);
366 < //     }
367 <
368 < //     /**
369 < //      * timed read-tryLock is interruptible
370 < //      */
371 < //     public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
372 < //     public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
373 < //     public void testReadTryLock_Interruptible(boolean fair) {
374 < //         final PublicReentrantReadWriteLock lock =
375 < //             new PublicReentrantReadWriteLock(fair);
376 < //         lock.writeLock().lock();
377 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
378 < //             public void realRun() throws InterruptedException {
379 < //                 lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
380 < //             }});
381 <
382 < //         waitForQueuedThread(lock, t);
383 < //         t.interrupt();
384 < //         awaitTermination(t);
385 < //         releaseWriteLock(lock);
386 < //     }
387 <
388 < //     /**
389 < //      * write-tryLock on an unlocked lock succeeds
390 < //      */
391 < //     public void testWriteTryLock()      { testWriteTryLock(false); }
392 < //     public void testWriteTryLock_fair() { testWriteTryLock(true); }
393 < //     public void testWriteTryLock(boolean fair) {
394 < //         final PublicReentrantReadWriteLock lock =
395 < //             new PublicReentrantReadWriteLock(fair);
396 < //         assertTrue(lock.writeLock().tryLock());
397 < //         assertWriteLockedByMoi(lock);
398 < //         assertTrue(lock.writeLock().tryLock());
399 < //         assertWriteLockedByMoi(lock);
400 < //         lock.writeLock().unlock();
401 < //         releaseWriteLock(lock);
402 < //     }
403 <
404 < //     /**
405 < //      * write-tryLock fails if locked
406 < //      */
407 < //     public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
408 < //     public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
409 < //     public void testWriteTryLockWhenLocked(boolean fair) {
410 < //         final PublicReentrantReadWriteLock lock =
411 < //             new PublicReentrantReadWriteLock(fair);
412 < //         lock.writeLock().lock();
413 < //         Thread t = newStartedThread(new CheckedRunnable() {
414 < //             public void realRun() {
415 < //                 assertFalse(lock.writeLock().tryLock());
416 < //             }});
417 <
418 < //         awaitTermination(t);
419 < //         releaseWriteLock(lock);
420 < //     }
421 <
422 < //     /**
423 < //      * read-tryLock fails if locked
424 < //      */
425 < //     public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
426 < //     public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
427 < //     public void testReadTryLockWhenLocked(boolean fair) {
428 < //         final PublicReentrantReadWriteLock lock =
429 < //             new PublicReentrantReadWriteLock(fair);
430 < //         lock.writeLock().lock();
431 < //         Thread t = newStartedThread(new CheckedRunnable() {
432 < //             public void realRun() {
433 < //                 assertFalse(lock.readLock().tryLock());
434 < //             }});
435 <
436 < //         awaitTermination(t);
437 < //         releaseWriteLock(lock);
438 < //     }
439 <
440 < //     /**
441 < //      * Multiple threads can hold a read lock when not write-locked
442 < //      */
443 < //     public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
444 < //     public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
445 < //     public void testMultipleReadLocks(boolean fair) {
446 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
447 < //         lock.readLock().lock();
448 < //         Thread t = newStartedThread(new CheckedRunnable() {
449 < //             public void realRun() throws InterruptedException {
450 < //                 assertTrue(lock.readLock().tryLock());
451 < //                 lock.readLock().unlock();
452 < //                 assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
453 < //                 lock.readLock().unlock();
454 < //                 lock.readLock().lock();
455 < //                 lock.readLock().unlock();
456 < //             }});
457 <
458 < //         awaitTermination(t);
459 < //         lock.readLock().unlock();
460 < //     }
461 <
462 < //     /**
463 < //      * A writelock succeeds only after a reading thread unlocks
464 < //      */
465 < //     public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
466 < //     public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
467 < //     public void testWriteAfterReadLock(boolean fair) {
468 < //         final PublicReentrantReadWriteLock lock =
469 < //             new PublicReentrantReadWriteLock(fair);
470 < //         lock.readLock().lock();
471 < //         Thread t = newStartedThread(new CheckedRunnable() {
472 < //             public void realRun() {
473 < //                 assertEquals(1, lock.getReadLockCount());
474 < //                 lock.writeLock().lock();
475 < //                 assertEquals(0, lock.getReadLockCount());
476 < //                 lock.writeLock().unlock();
477 < //             }});
478 < //         waitForQueuedThread(lock, t);
479 < //         assertNotWriteLocked(lock);
480 < //         assertEquals(1, lock.getReadLockCount());
481 < //         lock.readLock().unlock();
482 < //         assertEquals(0, lock.getReadLockCount());
483 < //         awaitTermination(t);
484 < //         assertNotWriteLocked(lock);
485 < //     }
486 <
487 < //     /**
488 < //      * A writelock succeeds only after reading threads unlock
489 < //      */
490 < //     public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
491 < //     public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
492 < //     public void testWriteAfterMultipleReadLocks(boolean fair) {
493 < //         final PublicReentrantReadWriteLock lock =
494 < //             new PublicReentrantReadWriteLock(fair);
495 < //         lock.readLock().lock();
496 < //         lock.readLock().lock();
497 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
498 < //             public void realRun() {
499 < //                 lock.readLock().lock();
500 < //                 assertEquals(3, lock.getReadLockCount());
501 < //                 lock.readLock().unlock();
502 < //             }});
503 < //         awaitTermination(t1);
504 <
505 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
506 < //             public void realRun() {
507 < //                 assertEquals(2, lock.getReadLockCount());
508 < //                 lock.writeLock().lock();
509 < //                 assertEquals(0, lock.getReadLockCount());
510 < //                 lock.writeLock().unlock();
511 < //             }});
512 < //         waitForQueuedThread(lock, t2);
513 < //         assertNotWriteLocked(lock);
514 < //         assertEquals(2, lock.getReadLockCount());
515 < //         lock.readLock().unlock();
516 < //         lock.readLock().unlock();
517 < //         assertEquals(0, lock.getReadLockCount());
518 < //         awaitTermination(t2);
519 < //         assertNotWriteLocked(lock);
520 < //     }
521 <
522 < //     /**
523 < //      * A thread that tries to acquire a fair read lock (non-reentrantly)
524 < //      * will block if there is a waiting writer thread
525 < //      */
526 < //     public void testReaderWriterReaderFairFifo() {
527 < //         final PublicReentrantReadWriteLock lock =
528 < //             new PublicReentrantReadWriteLock(true);
529 < //         final AtomicBoolean t1GotLock = new AtomicBoolean(false);
530 <
531 < //         lock.readLock().lock();
532 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
533 < //             public void realRun() {
534 < //                 assertEquals(1, lock.getReadLockCount());
535 < //                 lock.writeLock().lock();
536 < //                 assertEquals(0, lock.getReadLockCount());
537 < //                 t1GotLock.set(true);
538 < //                 lock.writeLock().unlock();
539 < //             }});
540 < //         waitForQueuedThread(lock, t1);
541 <
542 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
543 < //             public void realRun() {
544 < //                 assertEquals(1, lock.getReadLockCount());
545 < //                 lock.readLock().lock();
546 < //                 assertEquals(1, lock.getReadLockCount());
547 < //                 assertTrue(t1GotLock.get());
548 < //                 lock.readLock().unlock();
549 < //             }});
550 < //         waitForQueuedThread(lock, t2);
551 < //         assertTrue(t1.isAlive());
552 < //         assertNotWriteLocked(lock);
553 < //         assertEquals(1, lock.getReadLockCount());
554 < //         lock.readLock().unlock();
555 < //         awaitTermination(t1);
556 < //         awaitTermination(t2);
557 < //         assertNotWriteLocked(lock);
558 < //     }
559 <
560 < //     /**
561 < //      * Readlocks succeed only after a writing thread unlocks
562 < //      */
563 < //     public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
564 < //     public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
565 < //     public void testReadAfterWriteLock(boolean fair) {
566 < //         final PublicReentrantReadWriteLock lock =
567 < //             new PublicReentrantReadWriteLock(fair);
568 < //         lock.writeLock().lock();
569 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
570 < //             public void realRun() {
571 < //                 lock.readLock().lock();
572 < //                 lock.readLock().unlock();
573 < //             }});
574 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
575 < //             public void realRun() {
576 < //                 lock.readLock().lock();
577 < //                 lock.readLock().unlock();
578 < //             }});
579 <
580 < //         waitForQueuedThread(lock, t1);
581 < //         waitForQueuedThread(lock, t2);
582 < //         releaseWriteLock(lock);
583 < //         awaitTermination(t1);
584 < //         awaitTermination(t2);
585 < //     }
586 <
587 < //     /**
588 < //      * Read trylock succeeds if write locked by current thread
589 < //      */
590 < //     public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
591 < //     public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
592 < //     public void testReadHoldingWriteLock(boolean fair) {
593 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
594 < //         lock.writeLock().lock();
595 < //         assertTrue(lock.readLock().tryLock());
596 < //         lock.readLock().unlock();
597 < //         lock.writeLock().unlock();
598 < //     }
599 <
600 < //     /**
601 < //      * Read trylock succeeds (barging) even in the presence of waiting
602 < //      * readers and/or writers
603 < //      */
604 < //     public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
605 < //     public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
606 < //     public void testReadTryLockBarging(boolean fair) {
607 < //         final PublicReentrantReadWriteLock lock =
608 < //             new PublicReentrantReadWriteLock(fair);
609 < //         lock.readLock().lock();
610 <
611 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
612 < //             public void realRun() {
613 < //                 lock.writeLock().lock();
614 < //                 lock.writeLock().unlock();
615 < //             }});
616 <
617 < //         waitForQueuedThread(lock, t1);
618 <
619 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
620 < //             public void realRun() {
621 < //                 lock.readLock().lock();
622 < //                 lock.readLock().unlock();
623 < //             }});
624 <
625 < //         if (fair)
626 < //             waitForQueuedThread(lock, t2);
627 <
628 < //         Thread t3 = newStartedThread(new CheckedRunnable() {
629 < //             public void realRun() {
630 < //                 lock.readLock().tryLock();
631 < //                 lock.readLock().unlock();
632 < //             }});
633 <
634 < //         assertTrue(lock.getReadLockCount() > 0);
635 < //         awaitTermination(t3);
636 < //         assertTrue(t1.isAlive());
637 < //         if (fair) assertTrue(t2.isAlive());
638 < //         lock.readLock().unlock();
639 < //         awaitTermination(t1);
640 < //         awaitTermination(t2);
641 < //     }
642 <
643 < //     /**
644 < //      * Read lock succeeds if write locked by current thread even if
645 < //      * other threads are waiting for readlock
646 < //      */
647 < //     public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
648 < //     public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
649 < //     public void testReadHoldingWriteLock2(boolean fair) {
650 < //         final PublicReentrantReadWriteLock lock =
651 < //             new PublicReentrantReadWriteLock(fair);
652 < //         lock.writeLock().lock();
653 < //         lock.readLock().lock();
654 < //         lock.readLock().unlock();
655 <
656 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
657 < //             public void realRun() {
658 < //                 lock.readLock().lock();
659 < //                 lock.readLock().unlock();
660 < //             }});
661 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
662 < //             public void realRun() {
663 < //                 lock.readLock().lock();
664 < //                 lock.readLock().unlock();
665 < //             }});
666 <
667 < //         waitForQueuedThread(lock, t1);
668 < //         waitForQueuedThread(lock, t2);
669 < //         assertWriteLockedByMoi(lock);
670 < //         lock.readLock().lock();
671 < //         lock.readLock().unlock();
672 < //         releaseWriteLock(lock);
673 < //         awaitTermination(t1);
674 < //         awaitTermination(t2);
675 < //     }
676 <
677 < //     /**
678 < //      * Read lock succeeds if write locked by current thread even if
679 < //      * other threads are waiting for writelock
680 < //      */
681 < //     public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
682 < //     public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
683 < //     public void testReadHoldingWriteLock3(boolean fair) {
684 < //         final PublicReentrantReadWriteLock lock =
685 < //             new PublicReentrantReadWriteLock(fair);
686 < //         lock.writeLock().lock();
687 < //         lock.readLock().lock();
688 < //         lock.readLock().unlock();
689 <
690 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
691 < //             public void realRun() {
692 < //                 lock.writeLock().lock();
693 < //                 lock.writeLock().unlock();
694 < //             }});
695 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
696 < //             public void realRun() {
697 < //                 lock.writeLock().lock();
698 < //                 lock.writeLock().unlock();
699 < //             }});
700 <
701 < //         waitForQueuedThread(lock, t1);
702 < //         waitForQueuedThread(lock, t2);
703 < //         assertWriteLockedByMoi(lock);
704 < //         lock.readLock().lock();
705 < //         lock.readLock().unlock();
706 < //         assertWriteLockedByMoi(lock);
707 < //         lock.writeLock().unlock();
708 < //         awaitTermination(t1);
709 < //         awaitTermination(t2);
710 < //     }
711 <
712 < //     /**
713 < //      * Write lock succeeds if write locked by current thread even if
714 < //      * other threads are waiting for writelock
715 < //      */
716 < //     public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
717 < //     public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
718 < //     public void testWriteHoldingWriteLock4(boolean fair) {
719 < //         final PublicReentrantReadWriteLock lock =
720 < //             new PublicReentrantReadWriteLock(fair);
721 < //         lock.writeLock().lock();
722 < //         lock.writeLock().lock();
723 < //         lock.writeLock().unlock();
724 <
725 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
726 < //             public void realRun() {
727 < //                 lock.writeLock().lock();
728 < //                 lock.writeLock().unlock();
729 < //             }});
730 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
731 < //             public void realRun() {
732 < //                 lock.writeLock().lock();
733 < //                 lock.writeLock().unlock();
734 < //             }});
735 <
736 < //         waitForQueuedThread(lock, t1);
737 < //         waitForQueuedThread(lock, t2);
738 < //         assertWriteLockedByMoi(lock);
739 < //         assertEquals(1, lock.getWriteHoldCount());
740 < //         lock.writeLock().lock();
741 < //         assertWriteLockedByMoi(lock);
742 < //         assertEquals(2, lock.getWriteHoldCount());
743 < //         lock.writeLock().unlock();
744 < //         assertWriteLockedByMoi(lock);
745 < //         assertEquals(1, lock.getWriteHoldCount());
746 < //         lock.writeLock().unlock();
747 < //         awaitTermination(t1);
748 < //         awaitTermination(t2);
749 < //     }
750 <
751 < //     /**
752 < //      * Read tryLock succeeds if readlocked but not writelocked
753 < //      */
754 < //     public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
755 < //     public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
756 < //     public void testTryLockWhenReadLocked(boolean fair) {
757 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
758 < //         lock.readLock().lock();
759 < //         Thread t = newStartedThread(new CheckedRunnable() {
760 < //             public void realRun() {
761 < //                 assertTrue(lock.readLock().tryLock());
762 < //                 lock.readLock().unlock();
763 < //             }});
764 <
765 < //         awaitTermination(t);
766 < //         lock.readLock().unlock();
767 < //     }
768 <
769 < //     /**
770 < //      * write tryLock fails when readlocked
771 < //      */
772 < //     public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
773 < //     public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
774 < //     public void testWriteTryLockWhenReadLocked(boolean fair) {
775 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
776 < //         lock.readLock().lock();
777 < //         Thread t = newStartedThread(new CheckedRunnable() {
778 < //             public void realRun() {
779 < //                 assertFalse(lock.writeLock().tryLock());
780 < //             }});
781 <
782 < //         awaitTermination(t);
783 < //         lock.readLock().unlock();
784 < //     }
785 <
786 < //     /**
787 < //      * write timed tryLock times out if locked
788 < //      */
789 < //     public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
790 < //     public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
791 < //     public void testWriteTryLock_Timeout(boolean fair) {
792 < //         final PublicReentrantReadWriteLock lock =
793 < //             new PublicReentrantReadWriteLock(fair);
794 < //         lock.writeLock().lock();
795 < //         Thread t = newStartedThread(new CheckedRunnable() {
796 < //             public void realRun() throws InterruptedException {
797 < //                 long startTime = System.nanoTime();
798 < //                 long timeoutMillis = 10;
799 < //                 assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
800 < //                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
801 < //             }});
802 <
803 < //         awaitTermination(t);
804 < //         releaseWriteLock(lock);
805 < //     }
806 <
807 < //     /**
808 < //      * read timed tryLock times out if write-locked
809 < //      */
810 < //     public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
811 < //     public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
812 < //     public void testReadTryLock_Timeout(boolean fair) {
813 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
814 < //         lock.writeLock().lock();
815 < //         Thread t = newStartedThread(new CheckedRunnable() {
816 < //             public void realRun() throws InterruptedException {
817 < //                 long startTime = System.nanoTime();
818 < //                 long timeoutMillis = 10;
819 < //                 assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
820 < //                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
821 < //             }});
822 <
823 < //         awaitTermination(t);
824 < //         assertTrue(lock.writeLock().isHeldByCurrentThread());
825 < //         lock.writeLock().unlock();
826 < //     }
827 <
828 < //     /**
829 < //      * write lockInterruptibly succeeds if unlocked, else is interruptible
830 < //      */
831 < //     public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
832 < //     public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
833 < //     public void testWriteLockInterruptibly(boolean fair) {
834 < //         final PublicReentrantReadWriteLock lock =
835 < //             new PublicReentrantReadWriteLock(fair);
836 < //         try {
837 < //             lock.writeLock().lockInterruptibly();
838 < //         } catch (InterruptedException ie) {
839 < //             threadUnexpectedException(ie);
840 < //         }
841 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
842 < //             public void realRun() throws InterruptedException {
843 < //                 lock.writeLock().lockInterruptibly();
844 < //             }});
845 <
846 < //         waitForQueuedThread(lock, t);
847 < //         t.interrupt();
848 < //         assertTrue(lock.writeLock().isHeldByCurrentThread());
849 < //         awaitTermination(t);
850 < //         releaseWriteLock(lock);
851 < //     }
852 <
853 < //     /**
854 < //      * read lockInterruptibly succeeds if lock free else is interruptible
855 < //      */
856 < //     public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
857 < //     public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
858 < //     public void testReadLockInterruptibly(boolean fair) {
859 < //         final PublicReentrantReadWriteLock lock =
860 < //             new PublicReentrantReadWriteLock(fair);
861 < //         try {
862 < //             lock.readLock().lockInterruptibly();
863 < //             lock.readLock().unlock();
864 < //             lock.writeLock().lockInterruptibly();
865 < //         } catch (InterruptedException ie) {
866 < //             threadUnexpectedException(ie);
867 < //         }
868 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
869 < //             public void realRun() throws InterruptedException {
870 < //                 lock.readLock().lockInterruptibly();
871 < //             }});
872 <
873 < //         waitForQueuedThread(lock, t);
874 < //         t.interrupt();
875 < //         awaitTermination(t);
876 < //         releaseWriteLock(lock);
877 < //     }
878 <
879 < //     /**
880 < //      * Calling await without holding lock throws IllegalMonitorStateException
881 < //      */
882 < //     public void testAwait_IMSE()      { testAwait_IMSE(false); }
883 < //     public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
884 < //     public void testAwait_IMSE(boolean fair) {
885 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
886 < //         final Condition c = lock.writeLock().newCondition();
887 < //         for (AwaitMethod awaitMethod : AwaitMethod.values()) {
888 < //             long startTime = System.nanoTime();
889 < //             try {
890 < //                 await(c, awaitMethod);
891 < //                 shouldThrow();
892 < //             } catch (IllegalMonitorStateException success) {
893 < //             } catch (InterruptedException e) { threadUnexpectedException(e); }
894 < //             assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
895 < //         }
896 < //     }
897 <
898 < //     /**
899 < //      * Calling signal without holding lock throws IllegalMonitorStateException
900 < //      */
901 < //     public void testSignal_IMSE()      { testSignal_IMSE(false); }
902 < //     public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
903 < //     public void testSignal_IMSE(boolean fair) {
904 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
905 < //         final Condition c = lock.writeLock().newCondition();
906 < //         try {
907 < //             c.signal();
908 < //             shouldThrow();
909 < //         } catch (IllegalMonitorStateException success) {}
910 < //     }
911 <
912 < //     /**
913 < //      * Calling signalAll without holding lock throws IllegalMonitorStateException
914 < //      */
915 < //     public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
916 < //     public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
917 < //     public void testSignalAll_IMSE(boolean fair) {
918 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
919 < //         final Condition c = lock.writeLock().newCondition();
920 < //         try {
921 < //             c.signalAll();
922 < //             shouldThrow();
923 < //         } catch (IllegalMonitorStateException success) {}
924 < //     }
925 <
926 < //     /**
927 < //      * awaitNanos without a signal times out
928 < //      */
929 < //     public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
930 < //     public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
931 < //     public void testAwaitNanos_Timeout(boolean fair) {
932 < //         try {
933 < //             final ReentrantReadWriteLock lock =
934 < //                 new ReentrantReadWriteLock(fair);
935 < //             final Condition c = lock.writeLock().newCondition();
936 < //             lock.writeLock().lock();
937 < //             long startTime = System.nanoTime();
938 < //             long timeoutMillis = 10;
939 < //             long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
940 < //             long nanosRemaining = c.awaitNanos(timeoutNanos);
941 < //             assertTrue(nanosRemaining <= 0);
942 < //             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
943 < //             lock.writeLock().unlock();
944 < //         } catch (InterruptedException e) {
945 < //             threadUnexpectedException(e);
946 < //         }
947 < //     }
948 <
949 < //     /**
950 < //      * timed await without a signal times out
951 < //      */
952 < //     public void testAwait_Timeout()      { testAwait_Timeout(false); }
953 < //     public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
954 < //     public void testAwait_Timeout(boolean fair) {
955 < //         try {
956 < //             final ReentrantReadWriteLock lock =
957 < //                 new ReentrantReadWriteLock(fair);
958 < //             final Condition c = lock.writeLock().newCondition();
959 < //             lock.writeLock().lock();
960 < //             long startTime = System.nanoTime();
961 < //             long timeoutMillis = 10;
962 < //             assertFalse(c.await(timeoutMillis, MILLISECONDS));
963 < //             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
964 < //             lock.writeLock().unlock();
965 < //         } catch (InterruptedException e) {
966 < //             threadUnexpectedException(e);
967 < //         }
968 < //     }
969 <
970 < //     /**
971 < //      * awaitUntil without a signal times out
972 < //      */
973 < //     public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
974 < //     public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
975 < //     public void testAwaitUntil_Timeout(boolean fair) {
976 < //         try {
977 < //             final ReentrantReadWriteLock lock =
978 < //                 new ReentrantReadWriteLock(fair);
979 < //             final Condition c = lock.writeLock().newCondition();
980 < //             lock.writeLock().lock();
981 < //             long startTime = System.nanoTime();
982 < //             long timeoutMillis = 10;
983 < //             java.util.Date d = new java.util.Date();
984 < //             assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
985 < //             assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
986 < //             lock.writeLock().unlock();
987 < //         } catch (InterruptedException e) {
988 < //             threadUnexpectedException(e);
989 < //         }
990 < //     }
991 <
992 < //     /**
993 < //      * await returns when signalled
994 < //      */
995 < //     public void testAwait()      { testAwait(false); }
996 < //     public void testAwait_fair() { testAwait(true); }
997 < //     public void testAwait(boolean fair) {
998 < //         final PublicReentrantReadWriteLock lock =
999 < //             new PublicReentrantReadWriteLock(fair);
1000 < //         final Condition c = lock.writeLock().newCondition();
1001 < //         final CountDownLatch locked = new CountDownLatch(1);
1002 < //         Thread t = newStartedThread(new CheckedRunnable() {
1003 < //             public void realRun() throws InterruptedException {
1004 < //                 lock.writeLock().lock();
1005 < //                 locked.countDown();
1006 < //                 c.await();
1007 < //                 lock.writeLock().unlock();
1008 < //             }});
1009 <
1010 < //         await(locked);
1011 < //         lock.writeLock().lock();
1012 < //         assertHasWaiters(lock, c, t);
1013 < //         c.signal();
1014 < //         assertHasNoWaiters(lock, c);
1015 < //         assertTrue(t.isAlive());
1016 < //         lock.writeLock().unlock();
1017 < //         awaitTermination(t);
1018 < //     }
1019 <
1020 < //     /**
1021 < //      * awaitUninterruptibly is uninterruptible
1022 < //      */
1023 < //     public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1024 < //     public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1025 < //     public void testAwaitUninterruptibly(boolean fair) {
1026 < //         final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1027 < //         final Condition c = lock.writeLock().newCondition();
1028 < //         final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1029 <
1030 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
1031 < //             public void realRun() {
1032 < //                 // Interrupt before awaitUninterruptibly
1033 < //                 lock.writeLock().lock();
1034 < //                 pleaseInterrupt.countDown();
1035 < //                 Thread.currentThread().interrupt();
1036 < //                 c.awaitUninterruptibly();
1037 < //                 assertTrue(Thread.interrupted());
1038 < //                 lock.writeLock().unlock();
1039 < //             }});
1040 <
1041 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
1042 < //             public void realRun() {
1043 < //                 // Interrupt during awaitUninterruptibly
1044 < //                 lock.writeLock().lock();
1045 < //                 pleaseInterrupt.countDown();
1046 < //                 c.awaitUninterruptibly();
1047 < //                 assertTrue(Thread.interrupted());
1048 < //                 lock.writeLock().unlock();
1049 < //             }});
1050 <
1051 < //         await(pleaseInterrupt);
1052 < //         lock.writeLock().lock();
1053 < //         lock.writeLock().unlock();
1054 < //         t2.interrupt();
1055 <
1056 < //         assertThreadStaysAlive(t1);
1057 < //         assertTrue(t2.isAlive());
1058 <
1059 < //         lock.writeLock().lock();
1060 < //         c.signalAll();
1061 < //         lock.writeLock().unlock();
1062 <
1063 < //         awaitTermination(t1);
1064 < //         awaitTermination(t2);
1065 < //     }
1066 <
1067 < //     /**
1068 < //      * await/awaitNanos/awaitUntil is interruptible
1069 < //      */
1070 < //     public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1071 < //     public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1072 < //     public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1073 < //     public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1074 < //     public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1075 < //     public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1076 < //     public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
1077 < //     public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
1078 < //     public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1079 < //         final PublicReentrantReadWriteLock lock =
1080 < //             new PublicReentrantReadWriteLock(fair);
1081 < //         final Condition c = lock.writeLock().newCondition();
1082 < //         final CountDownLatch locked = new CountDownLatch(1);
1083 < //         Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1084 < //             public void realRun() throws InterruptedException {
1085 < //                 lock.writeLock().lock();
1086 < //                 assertWriteLockedByMoi(lock);
1087 < //                 assertHasNoWaiters(lock, c);
1088 < //                 locked.countDown();
1089 < //                 try {
1090 < //                     await(c, awaitMethod);
1091 < //                 } finally {
1092 < //                     assertWriteLockedByMoi(lock);
1093 < //                     assertHasNoWaiters(lock, c);
1094 < //                     lock.writeLock().unlock();
1095 < //                     assertFalse(Thread.interrupted());
1096 < //                 }
1097 < //             }});
1098 <
1099 < //         await(locked);
1100 < //         assertHasWaiters(lock, c, t);
1101 < //         t.interrupt();
1102 < //         awaitTermination(t);
1103 < //         assertNotWriteLocked(lock);
1104 < //     }
1105 <
1106 < //     /**
1107 < //      * signalAll wakes up all threads
1108 < //      */
1109 < //     public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1110 < //     public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1111 < //     public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1112 < //     public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1113 < //     public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1114 < //     public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1115 < //     public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
1116 < //     public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
1117 < //     public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1118 < //         final PublicReentrantReadWriteLock lock =
1119 < //             new PublicReentrantReadWriteLock(fair);
1120 < //         final Condition c = lock.writeLock().newCondition();
1121 < //         final CountDownLatch locked = new CountDownLatch(2);
1122 < //         final Lock writeLock = lock.writeLock();
1123 < //         class Awaiter extends CheckedRunnable {
1124 < //             public void realRun() throws InterruptedException {
1125 < //                 writeLock.lock();
1126 < //                 locked.countDown();
1127 < //                 await(c, awaitMethod);
1128 < //                 writeLock.unlock();
1129 < //             }
1130 < //         }
1131 <
1132 < //         Thread t1 = newStartedThread(new Awaiter());
1133 < //         Thread t2 = newStartedThread(new Awaiter());
1134 <
1135 < //         await(locked);
1136 < //         writeLock.lock();
1137 < //         assertHasWaiters(lock, c, t1, t2);
1138 < //         c.signalAll();
1139 < //         assertHasNoWaiters(lock, c);
1140 < //         writeLock.unlock();
1141 < //         awaitTermination(t1);
1142 < //         awaitTermination(t2);
1143 < //     }
1144 <
1145 < //     /**
1146 < //      * signal wakes up waiting threads in FIFO order
1147 < //      */
1148 < //     public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1149 < //     public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1150 < //     public void testSignalWakesFifo(boolean fair) {
1151 < //         final PublicReentrantReadWriteLock lock =
1152 < //             new PublicReentrantReadWriteLock(fair);
1153 < //         final Condition c = lock.writeLock().newCondition();
1154 < //         final CountDownLatch locked1 = new CountDownLatch(1);
1155 < //         final CountDownLatch locked2 = new CountDownLatch(1);
1156 < //         final Lock writeLock = lock.writeLock();
1157 < //         Thread t1 = newStartedThread(new CheckedRunnable() {
1158 < //             public void realRun() throws InterruptedException {
1159 < //                 writeLock.lock();
1160 < //                 locked1.countDown();
1161 < //                 c.await();
1162 < //                 writeLock.unlock();
1163 < //             }});
1164 <
1165 < //         await(locked1);
1166 <
1167 < //         Thread t2 = newStartedThread(new CheckedRunnable() {
1168 < //             public void realRun() throws InterruptedException {
1169 < //                 writeLock.lock();
1170 < //                 locked2.countDown();
1171 < //                 c.await();
1172 < //                 writeLock.unlock();
1173 < //             }});
1174 <
1175 < //         await(locked2);
1176 <
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 < //     }
32 >    /**
33 >     * A runnable calling writeLockInterruptibly
34 >     */
35 >    class InterruptibleLockRunnable extends CheckedRunnable {
36 >        final StampedLock lock;
37 >        InterruptibleLockRunnable(StampedLock l) { lock = l; }
38 >        public void realRun() throws InterruptedException {
39 >            lock.writeLockInterruptibly();
40 >        }
41 >    }
42 >
43 >    /**
44 >     * A runnable calling writeLockInterruptibly that expects to be
45 >     * interrupted
46 >     */
47 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
48 >        final StampedLock lock;
49 >        InterruptedLockRunnable(StampedLock l) { lock = l; }
50 >        public void realRun() throws InterruptedException {
51 >            lock.writeLockInterruptibly();
52 >        }
53 >    }
54 >
55 >    /**
56 >     * Releases write lock, checking isWriteLocked before and after
57 >     */
58 >    void releaseWriteLock(StampedLock lock, long s) {
59 >        assertTrue(lock.isWriteLocked());
60 >        lock.unlockWrite(s);
61 >        assertFalse(lock.isWriteLocked());
62 >    }
63 >
64 >    /**
65 >     * Constructed StampedLock is in unlocked state
66 >     */
67 >    public void testConstructor() {
68 >        StampedLock lock;
69 >        lock = new StampedLock();
70 >        assertFalse(lock.isWriteLocked());
71 >        assertFalse(lock.isReadLocked());
72 >        assertEquals(lock.getReadLockCount(), 0);
73 >    }
74 >
75 >    /**
76 >     * write-locking and read-locking an unlocked lock succeed
77 >     */
78 >    public void testLock() {
79 >        StampedLock lock = new StampedLock();
80 >        assertFalse(lock.isWriteLocked());
81 >        assertFalse(lock.isReadLocked());
82 >        assertEquals(lock.getReadLockCount(), 0);
83 >        long s = lock.writeLock();
84 >        assertTrue(lock.isWriteLocked());
85 >        assertFalse(lock.isReadLocked());
86 >        assertEquals(lock.getReadLockCount(), 0);
87 >        lock.unlockWrite(s);
88 >        assertFalse(lock.isWriteLocked());
89 >        assertFalse(lock.isReadLocked());
90 >        assertEquals(lock.getReadLockCount(), 0);
91 >        long rs = lock.readLock();
92 >        assertFalse(lock.isWriteLocked());
93 >        assertTrue(lock.isReadLocked());
94 >        assertEquals(lock.getReadLockCount(), 1);
95 >        lock.unlockRead(rs);
96 >        assertFalse(lock.isWriteLocked());
97 >        assertFalse(lock.isReadLocked());
98 >        assertEquals(lock.getReadLockCount(), 0);
99 >    }
100 >
101 >    /**
102 >     * unlock releases either a read or write lock
103 >     */
104 >    public void testUnlock() {
105 >        StampedLock lock = new StampedLock();
106 >        assertFalse(lock.isWriteLocked());
107 >        assertFalse(lock.isReadLocked());
108 >        assertEquals(lock.getReadLockCount(), 0);
109 >        long s = lock.writeLock();
110 >        assertTrue(lock.isWriteLocked());
111 >        assertFalse(lock.isReadLocked());
112 >        assertEquals(lock.getReadLockCount(), 0);
113 >        lock.unlock(s);
114 >        assertFalse(lock.isWriteLocked());
115 >        assertFalse(lock.isReadLocked());
116 >        assertEquals(lock.getReadLockCount(), 0);
117 >        long rs = lock.readLock();
118 >        assertFalse(lock.isWriteLocked());
119 >        assertTrue(lock.isReadLocked());
120 >        assertEquals(lock.getReadLockCount(), 1);
121 >        lock.unlock(rs);
122 >        assertFalse(lock.isWriteLocked());
123 >        assertFalse(lock.isReadLocked());
124 >        assertEquals(lock.getReadLockCount(), 0);
125 >    }
126 >
127 >    /**
128 >     * tryUnlockRead/Write succeeds if locked in associated mode else
129 >     * returns false
130 >     */
131 >    public void testTryUnlock() {
132 >        StampedLock lock = new StampedLock();
133 >        assertFalse(lock.isWriteLocked());
134 >        assertFalse(lock.isReadLocked());
135 >        assertEquals(lock.getReadLockCount(), 0);
136 >        long s = lock.writeLock();
137 >        assertTrue(lock.isWriteLocked());
138 >        assertFalse(lock.isReadLocked());
139 >        assertEquals(lock.getReadLockCount(), 0);
140 >        assertFalse(lock.tryUnlockRead());
141 >        assertTrue(lock.tryUnlockWrite());
142 >        assertFalse(lock.tryUnlockWrite());
143 >        assertFalse(lock.tryUnlockRead());
144 >        assertFalse(lock.isWriteLocked());
145 >        assertFalse(lock.isReadLocked());
146 >        assertEquals(lock.getReadLockCount(), 0);
147 >        long rs = lock.readLock();
148 >        assertFalse(lock.isWriteLocked());
149 >        assertTrue(lock.isReadLocked());
150 >        assertEquals(lock.getReadLockCount(), 1);
151 >        assertFalse(lock.tryUnlockWrite());
152 >        assertTrue(lock.tryUnlockRead());
153 >        assertFalse(lock.tryUnlockRead());
154 >        assertFalse(lock.tryUnlockWrite());
155 >        assertFalse(lock.isWriteLocked());
156 >        assertFalse(lock.isReadLocked());
157 >        assertEquals(lock.getReadLockCount(), 0);
158 >    }
159 >
160 >    /**
161 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
162 >     */
163 >    public void testWriteUnlock_IMSE() {
164 >        StampedLock lock = new StampedLock();
165 >        try {
166 >            lock.unlockWrite(0L);
167 >            shouldThrow();
168 >        } catch (IllegalMonitorStateException success) {}
169 >    }
170 >
171 >    /**
172 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
173 >     */
174 >    public void testWriteUnlock_IMSE2() {
175 >        StampedLock lock = new StampedLock();
176 >        long s = lock.writeLock();
177 >        lock.unlockWrite(s);
178 >        try {
179 >            lock.unlockWrite(s);
180 >            shouldThrow();
181 >        } catch (IllegalMonitorStateException success) {}
182 >    }
183 >
184 >    /**
185 >     * write-unlocking after readlock throws IllegalMonitorStateException
186 >     */
187 >    public void testWriteUnlock_IMSE3() {
188 >        StampedLock lock = new StampedLock();
189 >        long s = lock.readLock();
190 >        try {
191 >            lock.unlockWrite(s);
192 >            shouldThrow();
193 >        } catch (IllegalMonitorStateException success) {}
194 >    }
195 >
196 >    /**
197 >     * read-unlocking an unlocked lock throws IllegalMonitorStateException
198 >     */
199 >    public void testReadUnlock_IMSE() {
200 >        StampedLock lock = new StampedLock();
201 >        long s = lock.readLock();
202 >        lock.unlockRead(s);
203 >        try {
204 >            lock.unlockRead(s);
205 >            shouldThrow();
206 >        } catch (IllegalMonitorStateException success) {}
207 >    }
208 >
209 >    /**
210 >     * read-unlocking an unlocked lock throws IllegalMonitorStateException
211 >     */
212 >    public void testReadUnlock_IMSE2() {
213 >        StampedLock lock = new StampedLock();
214 >        try {
215 >            lock.unlockRead(0L);
216 >            shouldThrow();
217 >        } catch (IllegalMonitorStateException success) {}
218 >    }
219 >
220 >    /**
221 >     * read-unlocking after writeLock throws IllegalMonitorStateException
222 >     */
223 >    public void testReadUnlock_IMSE3() {
224 >        StampedLock lock = new StampedLock();
225 >        long s = lock.writeLock();
226 >        try {
227 >            lock.unlockRead(s);
228 >            shouldThrow();
229 >        } catch (IllegalMonitorStateException success) {}
230 >    }
231 >
232 >    /**
233 >     * validate(0) fails
234 >     */
235 >    public void testValidate0() {
236 >        StampedLock lock = new StampedLock();
237 >        assertFalse(lock.validate(0L));
238 >    }
239 >
240 >    /**
241 >     * A stamp obtained from a successful lock operation validates
242 >     */
243 >    public void testValidate() throws InterruptedException {
244 >        StampedLock lock = new StampedLock();
245 >        long s = lock.writeLock();
246 >        assertTrue(lock.validate(s));
247 >        lock.unlockWrite(s);
248 >        s = lock.readLock();
249 >        assertTrue(lock.validate(s));
250 >        lock.unlockRead(s);
251 >        assertTrue((s = lock.tryWriteLock()) != 0L);
252 >        assertTrue(lock.validate(s));
253 >        lock.unlockWrite(s);
254 >        assertTrue((s = lock.tryReadLock()) != 0L);
255 >        assertTrue(lock.validate(s));
256 >        lock.unlockRead(s);
257 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
258 >        assertTrue(lock.validate(s));
259 >        lock.unlockWrite(s);
260 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
261 >        assertTrue(lock.validate(s));
262 >        lock.unlockRead(s);
263 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
264 >    }
265 >
266 >    /**
267 >     * A stamp obtained from an unsuccessful lock operation does not validate
268 >     */
269 >    public void testValidate2() throws InterruptedException {
270 >        StampedLock lock = new StampedLock();
271 >        long s;
272 >        assertTrue((s = lock.writeLock()) != 0L);
273 >        assertTrue(lock.validate(s));
274 >        assertFalse(lock.validate(lock.tryWriteLock()));
275 >        assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
276 >        assertFalse(lock.validate(lock.tryReadLock()));
277 >        assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
278 >        assertFalse(lock.validate(lock.tryOptimisticRead()));
279 >        lock.unlockWrite(s);
280 >    }
281 >
282 >    /**
283 >     * writeLockInterruptibly is interruptible
284 >     */
285 >    public void testWriteLockInterruptibly_Interruptible()
286 >            throws InterruptedException {
287 >        final CountDownLatch running = new CountDownLatch(1);
288 >        final StampedLock lock = new StampedLock();
289 >        long s = lock.writeLock();
290 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
291 >            public void realRun() throws InterruptedException {
292 >                running.countDown();
293 >                lock.writeLockInterruptibly();
294 >            }});
295 >
296 >        running.await();
297 >        waitForThreadToEnterWaitState(t, 100);
298 >        t.interrupt();
299 >        awaitTermination(t);
300 >        releaseWriteLock(lock, s);
301 >    }
302 >
303 >    /**
304 >     * timed tryWriteLock is interruptible
305 >     */
306 >    public void testWriteTryLock_Interruptible() throws InterruptedException {
307 >        final CountDownLatch running = new CountDownLatch(1);
308 >        final StampedLock lock = new StampedLock();
309 >        long s = lock.writeLock();
310 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
311 >            public void realRun() throws InterruptedException {
312 >                running.countDown();
313 >                lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
314 >            }});
315 >
316 >        running.await();
317 >        waitForThreadToEnterWaitState(t, 100);
318 >        t.interrupt();
319 >        awaitTermination(t);
320 >        releaseWriteLock(lock, s);
321 >    }
322 >
323 >    /**
324 >     * readLockInterruptibly is interruptible
325 >     */
326 >    public void testReadLockInterruptibly_Interruptible()
327 >            throws InterruptedException {
328 >        final CountDownLatch running = new CountDownLatch(1);
329 >        final StampedLock lock = new StampedLock();
330 >        long s = lock.writeLock();
331 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
332 >            public void realRun() throws InterruptedException {
333 >                running.countDown();
334 >                lock.readLockInterruptibly();
335 >            }});
336 >
337 >        running.await();
338 >        waitForThreadToEnterWaitState(t, 100);
339 >        t.interrupt();
340 >        awaitTermination(t);
341 >        releaseWriteLock(lock, s);
342 >    }
343 >
344 >    /**
345 >     * timed tryReadLock is interruptible
346 >     */
347 >    public void testReadTryLock_Interruptible() throws InterruptedException {
348 >        final CountDownLatch running = new CountDownLatch(1);
349 >        final StampedLock lock = new StampedLock();
350 >        long s = lock.writeLock();
351 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
352 >            public void realRun() throws InterruptedException {
353 >                running.countDown();
354 >                lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
355 >            }});
356 >
357 >        running.await();
358 >        waitForThreadToEnterWaitState(t, 100);
359 >        t.interrupt();
360 >        awaitTermination(t);
361 >        releaseWriteLock(lock, s);
362 >    }
363 >
364 >    /**
365 >     * tryWriteLock on an unlocked lock succeeds
366 >     */
367 >    public void testWriteTryLock() {
368 >        final StampedLock lock = new StampedLock();
369 >        long s = lock.tryWriteLock();
370 >        assertTrue(s != 0L);
371 >        assertTrue(lock.isWriteLocked());
372 >        long s2 = lock.tryWriteLock();
373 >        assertEquals(s2, 0L);
374 >        releaseWriteLock(lock, s);
375 >    }
376 >
377 >    /**
378 >     * tryWriteLock fails if locked
379 >     */
380 >    public void testWriteTryLockWhenLocked() {
381 >        final StampedLock lock = new StampedLock();
382 >        long s = lock.writeLock();
383 >        Thread t = newStartedThread(new CheckedRunnable() {
384 >            public void realRun() {
385 >                long ws = lock.tryWriteLock();
386 >                assertTrue(ws == 0L);
387 >            }});
388 >
389 >        awaitTermination(t);
390 >        releaseWriteLock(lock, s);
391 >    }
392 >
393 >    /**
394 >     * tryReadLock fails if write-locked
395 >     */
396 >    public void testReadTryLockWhenLocked() {
397 >        final StampedLock lock = new StampedLock();
398 >        long s = lock.writeLock();
399 >        Thread t = newStartedThread(new CheckedRunnable() {
400 >            public void realRun() {
401 >                long rs = lock.tryReadLock();
402 >                assertEquals(rs, 0L);
403 >            }});
404 >
405 >        awaitTermination(t);
406 >        releaseWriteLock(lock, s);
407 >    }
408 >
409 >    /**
410 >     * Multiple threads can hold a read lock when not write-locked
411 >     */
412 >    public void testMultipleReadLocks() {
413 >        final StampedLock lock = new StampedLock();
414 >        final long s = lock.readLock();
415 >        Thread t = newStartedThread(new CheckedRunnable() {
416 >            public void realRun() throws InterruptedException {
417 >                long s2 = lock.tryReadLock();
418 >                assertTrue(s2 != 0L);
419 >                lock.unlockRead(s2);
420 >                long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
421 >                assertTrue(s3 != 0L);
422 >                lock.unlockRead(s3);
423 >                long s4 = lock.readLock();
424 >                lock.unlockRead(s4);
425 >            }});
426 >
427 >        awaitTermination(t);
428 >        lock.unlockRead(s);
429 >    }
430 >
431 >    /**
432 >     * A writelock succeeds only after a reading thread unlocks
433 >     */
434 >    public void testWriteAfterReadLock() throws InterruptedException {
435 >        final CountDownLatch running = new CountDownLatch(1);
436 >        final StampedLock lock = new StampedLock();
437 >        long rs = lock.readLock();
438 >        Thread t = newStartedThread(new CheckedRunnable() {
439 >            public void realRun() {
440 >                running.countDown();
441 >                long s = lock.writeLock();
442 >                lock.unlockWrite(s);
443 >            }});
444 >
445 >        running.await();
446 >        waitForThreadToEnterWaitState(t, 100);
447 >        assertFalse(lock.isWriteLocked());
448 >        lock.unlockRead(rs);
449 >        awaitTermination(t);
450 >        assertFalse(lock.isWriteLocked());
451 >    }
452 >
453 >    /**
454 >     * A writelock succeeds only after reading threads unlock
455 >     */
456 >    public void testWriteAfterMultipleReadLocks() {
457 >        final StampedLock lock = new StampedLock();
458 >        long s = lock.readLock();
459 >        Thread t1 = newStartedThread(new CheckedRunnable() {
460 >            public void realRun() {
461 >                long rs = lock.readLock();
462 >                lock.unlockRead(rs);
463 >            }});
464 >
465 >        awaitTermination(t1);
466 >
467 >        Thread t2 = newStartedThread(new CheckedRunnable() {
468 >            public void realRun() {
469 >                long ws = lock.writeLock();
470 >                lock.unlockWrite(ws);
471 >            }});
472 >
473 >        assertFalse(lock.isWriteLocked());
474 >        lock.unlockRead(s);
475 >        awaitTermination(t2);
476 >        assertFalse(lock.isWriteLocked());
477 >    }
478 >
479 >    /**
480 >     * Readlocks succeed only after a writing thread unlocks
481 >     */
482 >    public void testReadAfterWriteLock() {
483 >        final StampedLock lock = new StampedLock();
484 >        final long s = lock.writeLock();
485 >        Thread t1 = newStartedThread(new CheckedRunnable() {
486 >            public void realRun() {
487 >                long rs = lock.readLock();
488 >                lock.unlockRead(rs);
489 >            }});
490 >        Thread t2 = newStartedThread(new CheckedRunnable() {
491 >            public void realRun() {
492 >                long rs = lock.readLock();
493 >                lock.unlockRead(rs);
494 >            }});
495 >
496 >        releaseWriteLock(lock, s);
497 >        awaitTermination(t1);
498 >        awaitTermination(t2);
499 >    }
500 >
501 >    /**
502 >     * tryReadLock succeeds if readlocked but not writelocked
503 >     */
504 >    public void testTryLockWhenReadLocked() {
505 >        final StampedLock lock = new StampedLock();
506 >        long s = lock.readLock();
507 >        Thread t = newStartedThread(new CheckedRunnable() {
508 >            public void realRun() {
509 >                long rs = lock.tryReadLock();
510 >                threadAssertTrue(rs != 0L);
511 >                lock.unlockRead(rs);
512 >            }});
513 >
514 >        awaitTermination(t);
515 >        lock.unlockRead(s);
516 >    }
517 >
518 >    /**
519 >     * tryWriteLock fails when readlocked
520 >     */
521 >    public void testWriteTryLockWhenReadLocked() {
522 >        final StampedLock lock = new StampedLock();
523 >        long s = lock.readLock();
524 >        Thread t = newStartedThread(new CheckedRunnable() {
525 >            public void realRun() {
526 >                long ws = lock.tryWriteLock();
527 >                threadAssertEquals(ws, 0L);
528 >            }});
529 >
530 >        awaitTermination(t);
531 >        lock.unlockRead(s);
532 >    }
533 >
534 >    /**
535 >     * timed tryWriteLock times out if locked
536 >     */
537 >    public void testWriteTryLock_Timeout() {
538 >        final StampedLock lock = new StampedLock();
539 >        long s = lock.writeLock();
540 >        Thread t = newStartedThread(new CheckedRunnable() {
541 >            public void realRun() throws InterruptedException {
542 >                long startTime = System.nanoTime();
543 >                long timeoutMillis = 10;
544 >                long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
545 >                assertEquals(ws, 0L);
546 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
547 >            }});
548 >
549 >        awaitTermination(t);
550 >        releaseWriteLock(lock, s);
551 >    }
552 >
553 >    /**
554 >     * timed tryReadLock times out if write-locked
555 >     */
556 >    public void testReadTryLock_Timeout() {
557 >        final StampedLock lock = new StampedLock();
558 >        long s = lock.writeLock();
559 >        Thread t = newStartedThread(new CheckedRunnable() {
560 >            public void realRun() throws InterruptedException {
561 >                long startTime = System.nanoTime();
562 >                long timeoutMillis = 10;
563 >                long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
564 >                assertEquals(rs, 0L);
565 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
566 >            }});
567 >
568 >        awaitTermination(t);
569 >        assertTrue(lock.isWriteLocked());
570 >        lock.unlockWrite(s);
571 >    }
572 >
573 >    /**
574 >     * writeLockInterruptibly succeeds if unlocked, else is interruptible
575 >     */
576 >    public void testWriteLockInterruptibly() throws InterruptedException {
577 >        final CountDownLatch running = new CountDownLatch(1);
578 >        final StampedLock lock = new StampedLock();
579 >        long s = lock.writeLockInterruptibly();
580 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
581 >            public void realRun() throws InterruptedException {
582 >                running.countDown();
583 >                lock.writeLockInterruptibly();
584 >            }});
585 >
586 >        running.await();
587 >        waitForThreadToEnterWaitState(t, 100);
588 >        t.interrupt();
589 >        assertTrue(lock.isWriteLocked());
590 >        awaitTermination(t);
591 >        releaseWriteLock(lock, s);
592 >    }
593 >
594 >    /**
595 >     * readLockInterruptibly succeeds if lock free else is interruptible
596 >     */
597 >    public void testReadLockInterruptibly() throws InterruptedException {
598 >        final CountDownLatch running = new CountDownLatch(1);
599 >        final StampedLock lock = new StampedLock();
600 >        long s;
601 >        s = lock.readLockInterruptibly();
602 >        lock.unlockRead(s);
603 >        s = lock.writeLockInterruptibly();
604 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
605 >            public void realRun() throws InterruptedException {
606 >                running.countDown();
607 >                lock.readLockInterruptibly();
608 >            }});
609 >
610 >        running.await();
611 >        waitForThreadToEnterWaitState(t, 100);
612 >        t.interrupt();
613 >        awaitTermination(t);
614 >        releaseWriteLock(lock, s);
615 >    }
616 >
617 >    /**
618 >     * A serialized lock deserializes as unlocked
619 >     */
620 >    public void testSerialization() {
621 >        StampedLock lock = new StampedLock();
622 >        lock.writeLock();
623 >        StampedLock clone = serialClone(lock);
624 >        assertTrue(lock.isWriteLocked());
625 >        assertFalse(clone.isWriteLocked());
626 >        long s = clone.writeLock();
627 >        assertTrue(clone.isWriteLocked());
628 >        clone.unlockWrite(s);
629 >        assertFalse(clone.isWriteLocked());
630 >    }
631 >
632 >    /**
633 >     * toString indicates current lock state
634 >     */
635 >    public void testToString() {
636 >        StampedLock lock = new StampedLock();
637 >        assertTrue(lock.toString().contains("Unlocked"));
638 >        long s = lock.writeLock();
639 >        assertTrue(lock.toString().contains("Write-locked"));
640 >        lock.unlockWrite(s);
641 >        s = lock.readLock();
642 >        assertTrue(lock.toString().contains("Read-locks"));
643 >    }
644 >
645 >    /**
646 >     * tryOptimisticRead succeeds and validates if unlocked, fails if locked
647 >     */
648 >    public void testValidateOptimistic() throws InterruptedException {
649 >        StampedLock lock = new StampedLock();
650 >        long s, p;
651 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
652 >        assertTrue(lock.validate(p));
653 >        assertTrue((s = lock.writeLock()) != 0L);
654 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
655 >        assertTrue(lock.validate(s));
656 >        lock.unlockWrite(s);
657 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
658 >        assertTrue(lock.validate(p));
659 >        assertTrue((s = lock.readLock()) != 0L);
660 >        assertTrue(lock.validate(s));
661 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
662 >        assertTrue(lock.validate(p));
663 >        lock.unlockRead(s);
664 >        assertTrue((s = lock.tryWriteLock()) != 0L);
665 >        assertTrue(lock.validate(s));
666 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
667 >        lock.unlockWrite(s);
668 >        assertTrue((s = lock.tryReadLock()) != 0L);
669 >        assertTrue(lock.validate(s));
670 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
671 >        lock.unlockRead(s);
672 >        assertTrue(lock.validate(p));
673 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
674 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
675 >        assertTrue(lock.validate(s));
676 >        lock.unlockWrite(s);
677 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
678 >        assertTrue(lock.validate(s));
679 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
680 >        lock.unlockRead(s);
681 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
682 >    }
683 >
684 >    /**
685 >     * tryOptimisticRead stamp does not validate if a write lock intervenes
686 >     */
687 >    public void testValidateOptimisticWriteLocked() {
688 >        StampedLock lock = new StampedLock();
689 >        long s, p;
690 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
691 >        assertTrue((s = lock.writeLock()) != 0L);
692 >        assertFalse(lock.validate(p));
693 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
694 >        assertTrue(lock.validate(s));
695 >        lock.unlockWrite(s);
696 >    }
697 >
698 >    /**
699 >     * tryOptimisticRead stamp does not validate if a write lock
700 >     * intervenes in another thread
701 >     */
702 >    public void testValidateOptimisticWriteLocked2()
703 >            throws InterruptedException {
704 >        final CountDownLatch running = new CountDownLatch(1);
705 >        final StampedLock lock = new StampedLock();
706 >        long s, p;
707 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
708 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
709 >            public void realRun() throws InterruptedException {
710 >                lock.writeLockInterruptibly();
711 >                running.countDown();
712 >                lock.writeLockInterruptibly();
713 >            }});
714 >
715 >        running.await();
716 >        assertFalse(lock.validate(p));
717 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
718 >        t.interrupt();
719 >        awaitTermination(t);
720 >    }
721 >
722 >    /**
723 >     * tryConvertToOptimisticRead succeeds and validates if successfully locked,
724 >     */
725 >    public void testTryConvertToOptimisticRead() throws InterruptedException {
726 >        StampedLock lock = new StampedLock();
727 >        long s, p;
728 >        assertEquals(0L, lock.tryConvertToOptimisticRead(0L));
729 >
730 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
731 >        assertEquals(s, lock.tryConvertToOptimisticRead(s));
732 >        assertTrue(lock.validate(s));
733 >
734 >        assertTrue((p = lock.readLock()) != 0L);
735 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
736 >        assertEquals(s, lock.tryConvertToOptimisticRead(s));
737 >        assertTrue(lock.validate(s));
738 >        lock.unlockRead(p);
739 >
740 >        assertTrue((s = lock.writeLock()) != 0L);
741 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
742 >        assertTrue(lock.validate(p));
743 >
744 >        assertTrue((s = lock.readLock()) != 0L);
745 >        assertTrue(lock.validate(s));
746 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
747 >        assertTrue(lock.validate(p));
748 >
749 >        assertTrue((s = lock.tryWriteLock()) != 0L);
750 >        assertTrue(lock.validate(s));
751 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
752 >        assertTrue(lock.validate(p));
753 >
754 >        assertTrue((s = lock.tryReadLock()) != 0L);
755 >        assertTrue(lock.validate(s));
756 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
757 >        assertTrue(lock.validate(p));
758 >
759 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
760 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
761 >        assertTrue(lock.validate(p));
762 >
763 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
764 >        assertTrue(lock.validate(s));
765 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
766 >        assertTrue(lock.validate(p));
767 >    }
768 >
769 >    /**
770 >     * tryConvertToReadLock succeeds and validates if successfully locked
771 >     * or lock free;
772 >     */
773 >    public void testTryConvertToReadLock() throws InterruptedException {
774 >        StampedLock lock = new StampedLock();
775 >        long s, p;
776 >
777 >        assertFalse((p = lock.tryConvertToReadLock(0L)) != 0L);
778 >
779 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
780 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
781 >        assertTrue(lock.isReadLocked());
782 >        assertEquals(1, lock.getReadLockCount());
783 >        lock.unlockRead(p);
784 >
785 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
786 >        lock.readLock();
787 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
788 >        assertTrue(lock.isReadLocked());
789 >        assertEquals(2, lock.getReadLockCount());
790 >        lock.unlockRead(p);
791 >        lock.unlockRead(p);
792 >
793 >        assertTrue((s = lock.writeLock()) != 0L);
794 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
795 >        assertTrue(lock.validate(p));
796 >        assertTrue(lock.isReadLocked());
797 >        assertEquals(1, lock.getReadLockCount());
798 >        lock.unlockRead(p);
799 >
800 >        assertTrue((s = lock.readLock()) != 0L);
801 >        assertTrue(lock.validate(s));
802 >        assertEquals(s, lock.tryConvertToReadLock(s));
803 >        assertTrue(lock.validate(s));
804 >        assertTrue(lock.isReadLocked());
805 >        assertEquals(1, lock.getReadLockCount());
806 >        lock.unlockRead(s);
807 >
808 >        assertTrue((s = lock.tryWriteLock()) != 0L);
809 >        assertTrue(lock.validate(s));
810 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
811 >        assertTrue(lock.validate(p));
812 >        assertEquals(1, lock.getReadLockCount());
813 >        lock.unlockRead(p);
814 >
815 >        assertTrue((s = lock.tryReadLock()) != 0L);
816 >        assertTrue(lock.validate(s));
817 >        assertEquals(s, lock.tryConvertToReadLock(s));
818 >        assertTrue(lock.validate(s));
819 >        assertTrue(lock.isReadLocked());
820 >        assertEquals(1, lock.getReadLockCount());
821 >        lock.unlockRead(s);
822 >
823 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
824 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
825 >        assertTrue(lock.validate(p));
826 >        assertTrue(lock.isReadLocked());
827 >        assertEquals(1, lock.getReadLockCount());
828 >        lock.unlockRead(p);
829 >
830 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
831 >        assertTrue(lock.validate(s));
832 >        assertEquals(s, lock.tryConvertToReadLock(s));
833 >        assertTrue(lock.validate(s));
834 >        assertTrue(lock.isReadLocked());
835 >        assertEquals(1, lock.getReadLockCount());
836 >        lock.unlockRead(s);
837 >    }
838 >
839 >    /**
840 >     * tryConvertToWriteLock succeeds and validates if successfully locked
841 >     * or lock free;
842 >     */
843 >    public void testTryConvertToWriteLock() throws InterruptedException {
844 >        StampedLock lock = new StampedLock();
845 >        long s, p;
846 >
847 >        assertFalse((p = lock.tryConvertToWriteLock(0L)) != 0L);
848 >
849 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
850 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
851 >        assertTrue(lock.isWriteLocked());
852 >        lock.unlockWrite(p);
853 >
854 >        assertTrue((s = lock.writeLock()) != 0L);
855 >        assertEquals(s, lock.tryConvertToWriteLock(s));
856 >        assertTrue(lock.validate(s));
857 >        assertTrue(lock.isWriteLocked());
858 >        lock.unlockWrite(s);
859 >
860 >        assertTrue((s = lock.readLock()) != 0L);
861 >        assertTrue(lock.validate(s));
862 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
863 >        assertTrue(lock.validate(p));
864 >        assertTrue(lock.isWriteLocked());
865 >        lock.unlockWrite(p);
866 >
867 >        assertTrue((s = lock.tryWriteLock()) != 0L);
868 >        assertTrue(lock.validate(s));
869 >        assertEquals(s, lock.tryConvertToWriteLock(s));
870 >        assertTrue(lock.validate(s));
871 >        assertTrue(lock.isWriteLocked());
872 >        lock.unlockWrite(s);
873 >
874 >        assertTrue((s = lock.tryReadLock()) != 0L);
875 >        assertTrue(lock.validate(s));
876 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
877 >        assertTrue(lock.validate(p));
878 >        assertTrue(lock.isWriteLocked());
879 >        lock.unlockWrite(p);
880 >
881 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
882 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
883 >        assertTrue(lock.validate(p));
884 >        assertTrue(lock.isWriteLocked());
885 >        lock.unlockWrite(p);
886 >
887 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
888 >        assertTrue(lock.validate(s));
889 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
890 >        assertTrue(lock.validate(p));
891 >        assertTrue(lock.isWriteLocked());
892 >        lock.unlockWrite(p);
893 >    }
894 >
895 >    /**
896 >     * asWriteLock can be locked and unlocked
897 >     */
898 >    public void testAsWriteLock() {
899 >        StampedLock sl = new StampedLock();
900 >        Lock lock = sl.asWriteLock();
901 >        lock.lock();
902 >        assertFalse(lock.tryLock());
903 >        lock.unlock();
904 >        assertTrue(lock.tryLock());
905 >    }
906 >
907 >    /**
908 >     * asReadLock can be locked and unlocked
909 >     */
910 >    public void testAsReadLock() {
911 >        StampedLock sl = new StampedLock();
912 >        Lock lock = sl.asReadLock();
913 >        lock.lock();
914 >        lock.unlock();
915 >        assertTrue(lock.tryLock());
916 >    }
917 >
918 >    /**
919 >     * asReadWriteLock.writeLock can be locked and unlocked
920 >     */
921 >    public void testAsReadWriteLockWriteLock() {
922 >        StampedLock sl = new StampedLock();
923 >        Lock lock = sl.asReadWriteLock().writeLock();
924 >        lock.lock();
925 >        assertFalse(lock.tryLock());
926 >        lock.unlock();
927 >        assertTrue(lock.tryLock());
928 >    }
929 >
930 >    /**
931 >     * asReadWriteLock.readLock can be locked and unlocked
932 >     */
933 >    public void testAsReadWriteLockReadLock() {
934 >        StampedLock sl = new StampedLock();
935 >        Lock lock = sl.asReadWriteLock().readLock();
936 >        lock.lock();
937 >        lock.unlock();
938 >        assertTrue(lock.tryLock());
939 >    }
940 >
941 >    /**
942 >     * Lock.newCondition throws UnsupportedOperationException
943 >     */
944 >    public void testLockViewsDoNotSupportConditions() {
945 >        StampedLock sl = new StampedLock();
946 >        assertThrows(UnsupportedOperationException.class,
947 >                     () -> sl.asWriteLock().newCondition(),
948 >                     () -> sl.asReadLock().newCondition(),
949 >                     () -> sl.asReadWriteLock().writeLock().newCondition(),
950 >                     () -> sl.asReadWriteLock().readLock().newCondition());
951 >    }
952 >
953 >    /**
954 >     * Passing optimistic read stamps to unlock operations result in
955 >     * IllegalMonitorStateException
956 >     */
957 >    public void testCannotUnlockOptimisticReadStamps() {
958 >        Runnable[] actions = {
959 >            () -> {
960 >                StampedLock sl = new StampedLock();
961 >                long stamp = sl.tryOptimisticRead();
962 >                assertTrue(stamp != 0);
963 >                sl.unlockRead(stamp);
964 >            },
965 >            () -> {
966 >                StampedLock sl = new StampedLock();
967 >                long stamp = sl.tryOptimisticRead();
968 >                sl.unlock(stamp);
969 >            },
970 >
971 >            () -> {
972 >                StampedLock sl = new StampedLock();
973 >                long stamp = sl.tryOptimisticRead();
974 >                sl.writeLock();
975 >                sl.unlock(stamp);
976 >            },
977 >            () -> {
978 >                StampedLock sl = new StampedLock();
979 >                long stamp = sl.tryOptimisticRead();
980 >                sl.readLock();
981 >                sl.unlockRead(stamp);
982 >            },
983 >            () -> {
984 >                StampedLock sl = new StampedLock();
985 >                long stamp = sl.tryOptimisticRead();
986 >                sl.readLock();
987 >                sl.unlock(stamp);
988 >            },
989 >
990 >            () -> {
991 >                StampedLock sl = new StampedLock();
992 >                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
993 >                assertTrue(stamp != 0);
994 >                sl.writeLock();
995 >                sl.unlockWrite(stamp);
996 >            },
997 >            () -> {
998 >                StampedLock sl = new StampedLock();
999 >                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1000 >                sl.writeLock();
1001 >                sl.unlock(stamp);
1002 >            },
1003 >            () -> {
1004 >                StampedLock sl = new StampedLock();
1005 >                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1006 >                sl.readLock();
1007 >                sl.unlockRead(stamp);
1008 >            },
1009 >            () -> {
1010 >                StampedLock sl = new StampedLock();
1011 >                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1012 >                sl.readLock();
1013 >                sl.unlock(stamp);
1014 >            },
1015 >
1016 >            () -> {
1017 >                StampedLock sl = new StampedLock();
1018 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1019 >                assertTrue(stamp != 0);
1020 >                sl.writeLock();
1021 >                sl.unlockWrite(stamp);
1022 >            },
1023 >            () -> {
1024 >                StampedLock sl = new StampedLock();
1025 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1026 >                sl.writeLock();
1027 >                sl.unlock(stamp);
1028 >            },
1029 >            () -> {
1030 >                StampedLock sl = new StampedLock();
1031 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1032 >                sl.readLock();
1033 >                sl.unlockRead(stamp);
1034 >            },
1035 >            () -> {
1036 >                StampedLock sl = new StampedLock();
1037 >                sl.readLock();
1038 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1039 >                assertTrue(stamp != 0);
1040 >                sl.readLock();
1041 >                sl.unlockRead(stamp);
1042 >            },
1043 >            () -> {
1044 >                StampedLock sl = new StampedLock();
1045 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1046 >                sl.readLock();
1047 >                sl.unlock(stamp);
1048 >            },
1049 >            () -> {
1050 >                StampedLock sl = new StampedLock();
1051 >                sl.readLock();
1052 >                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1053 >                sl.readLock();
1054 >                sl.unlock(stamp);
1055 >            },
1056 >        };
1057 >
1058 >        assertThrows(IllegalMonitorStateException.class, actions);
1059 >    }
1060 >
1061 >    static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
1062 >        try { return sl.writeLockInterruptibly(); }
1063 >        catch (InterruptedException ex) { throw new AssertionError(ex); }
1064 >    }
1065 >
1066 >    static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
1067 >        try { return sl.tryWriteLock(time, unit); }
1068 >        catch (InterruptedException ex) { throw new AssertionError(ex); }
1069 >    }
1070 >
1071 >    static long readLockInterruptiblyUninterrupted(StampedLock sl) {
1072 >        try { return sl.readLockInterruptibly(); }
1073 >        catch (InterruptedException ex) { throw new AssertionError(ex); }
1074 >    }
1075 >
1076 >    static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
1077 >        try { return sl.tryReadLock(time, unit); }
1078 >        catch (InterruptedException ex) { throw new AssertionError(ex); }
1079 >    }
1080 >
1081 >    /**
1082 >     * Invalid write stamps result in IllegalMonitorStateException
1083 >     */
1084 >    public void testInvalidWriteStampsThrowIllegalMonitorStateException() {
1085 >        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1086 >        writeLockers.add((sl) -> sl.writeLock());
1087 >        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1088 >        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1089 >        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1090 >
1091 >        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1092 >        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1093 >        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1094 >        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1095 >        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1096 >
1097 >        List<Consumer<StampedLock>> mutaters = new ArrayList<>();
1098 >        mutaters.add((sl) -> {});
1099 >        mutaters.add((sl) -> sl.readLock());
1100 >        for (Function<StampedLock, Long> writeLocker : writeLockers)
1101 >            mutaters.add((sl) -> writeLocker.apply(sl));
1102 >
1103 >        for (Function<StampedLock, Long> writeLocker : writeLockers)
1104 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1105 >        for (Consumer<StampedLock> mutater : mutaters) {
1106 >            final StampedLock sl = new StampedLock();
1107 >            final long stamp = writeLocker.apply(sl);
1108 >            assertTrue(stamp != 0L);
1109 >            assertThrows(IllegalMonitorStateException.class,
1110 >                         () -> sl.unlockRead(stamp));
1111 >            writeUnlocker.accept(sl, stamp);
1112 >            mutater.accept(sl);
1113 >            assertThrows(IllegalMonitorStateException.class,
1114 >                         () -> sl.unlock(stamp),
1115 >                         () -> sl.unlockRead(stamp),
1116 >                         () -> sl.unlockWrite(stamp));
1117 >        }
1118 >    }
1119 >
1120 >    /**
1121 >     * Invalid read stamps result in IllegalMonitorStateException
1122 >     */
1123 >    public void testInvalidReadStampsThrowIllegalMonitorStateException() {
1124 >        List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
1125 >        readLockers.add((sl) -> sl.readLock());
1126 >        readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl));
1127 >        readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1128 >        readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0, DAYS));
1129 >
1130 >        List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>();
1131 >        readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp));
1132 >        readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead()));
1133 >        readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock());
1134 >        readUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1135 >
1136 >        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1137 >        writeLockers.add((sl) -> sl.writeLock());
1138 >        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1139 >        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1140 >        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1141 >
1142 >        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1143 >        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1144 >        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1145 >        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1146 >        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1147 >
1148 >
1149 >        for (Function<StampedLock, Long> readLocker : readLockers)
1150 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers)
1151 >        for (Function<StampedLock, Long> writeLocker : writeLockers)
1152 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers) {
1153 >            final StampedLock sl = new StampedLock();
1154 >            final long stamp = readLocker.apply(sl);
1155 >            assertTrue(stamp != 0L);
1156 >            assertThrows(IllegalMonitorStateException.class,
1157 >                         () -> sl.unlockWrite(stamp));
1158 >            readUnlocker.accept(sl, stamp);
1159 >            assertThrows(IllegalMonitorStateException.class,
1160 >                         () -> sl.unlock(stamp),
1161 >                         () -> sl.unlockRead(stamp),
1162 >                         () -> sl.unlockWrite(stamp));
1163 >            final long writeStamp = writeLocker.apply(sl);
1164 >            assertTrue(writeStamp != 0L);
1165 >            assertTrue(writeStamp != stamp);
1166 >            assertThrows(IllegalMonitorStateException.class,
1167 >                         () -> sl.unlock(stamp),
1168 >                         () -> sl.unlockRead(stamp),
1169 >                         () -> sl.unlockWrite(stamp));
1170 >            writeUnlocker.accept(sl, writeStamp);
1171 >            assertThrows(IllegalMonitorStateException.class,
1172 >                         () -> sl.unlock(stamp),
1173 >                         () -> sl.unlockRead(stamp),
1174 >                         () -> sl.unlockWrite(stamp));
1175 >        }
1176 >    }
1177  
1178   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines