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.6 by dl, Thu Mar 21 19:06:54 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines