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

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