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.5 by dl, Thu Mar 21 16:26:43 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();
927 < //             lock.writeLock().lock();
928 < //             long startTime = System.nanoTime();
929 < //             long timeoutMillis = 10;
930 < //             long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
931 < //             long nanosRemaining = c.awaitNanos(timeoutNanos);
932 < //             assertTrue(nanosRemaining <= 0);
933 < //             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(); Thread.sleep(SHORT_DELAY_MS);
297 >            t.interrupt();
298 >            awaitTermination(t);
299 >            releaseWriteLock(lock, s);
300 >        } catch (InterruptedException ie) {
301 >            threadUnexpectedException(ie);
302 >        }
303 >    }
304 >
305 >    /**
306 >     * timed tryWriteLock is interruptible
307 >     */
308 >    public void testWriteTryLock_Interruptible() {
309 >        final CountDownLatch running = new CountDownLatch(1);
310 >        final StampedLock lock = new StampedLock();
311 >        long s = lock.writeLock();
312 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
313 >            public void realRun() throws InterruptedException {
314 >                running.countDown();
315 >                lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
316 >            }});
317 >        try {
318 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
319 >            t.interrupt();
320 >            awaitTermination(t);
321 >            releaseWriteLock(lock, s);
322 >        } catch (InterruptedException ie) {
323 >            threadUnexpectedException(ie);
324 >        }
325 >    }
326 >
327 >    /**
328 >     * readLockInterruptibly is interruptible
329 >     */
330 >    public void testReadLockInterruptibly_Interruptible() {
331 >        final CountDownLatch running = new CountDownLatch(1);
332 >        final StampedLock lock = new StampedLock();
333 >        long s = lock.writeLock();
334 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
335 >            public void realRun() throws InterruptedException {
336 >                running.countDown();
337 >                lock.readLockInterruptibly();
338 >            }});
339 >        try {
340 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
341 >            t.interrupt();
342 >            awaitTermination(t);
343 >            releaseWriteLock(lock, s);
344 >        } catch (InterruptedException ie) {
345 >            threadUnexpectedException(ie);
346 >        }
347 >    }
348 >
349 >    /**
350 >     * timed tryReadLock is interruptible
351 >     */
352 >    public void testReadTryLock_Interruptible() {
353 >        final CountDownLatch running = new CountDownLatch(1);
354 >        final StampedLock lock = new StampedLock();
355 >        long s = lock.writeLock();
356 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
357 >            public void realRun() throws InterruptedException {
358 >                running.countDown();
359 >                lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
360 >            }});
361 >        try {
362 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
363 >            t.interrupt();
364 >            awaitTermination(t);
365 >            releaseWriteLock(lock, s);
366 >        } catch (InterruptedException ie) {
367 >            threadUnexpectedException(ie);
368 >        }
369 >    }
370 >
371 >    /**
372 >     * tryWriteLock on an unlocked lock succeeds
373 >     */
374 >    public void testWriteTryLock() {
375 >        final StampedLock lock = new StampedLock();
376 >        long s = lock.tryWriteLock();
377 >        assertTrue(s != 0L);
378 >        assertTrue(lock.isWriteLocked());
379 >        long s2 = lock.tryWriteLock();
380 >        assertEquals(s2, 0L);
381 >        releaseWriteLock(lock, s);
382 >    }
383 >
384 >    /**
385 >     * tryWriteLock fails if locked
386 >     */
387 >    public void testWriteTryLockWhenLocked() {
388 >        final StampedLock lock = new StampedLock();
389 >        long s = lock.writeLock();
390 >        Thread t = newStartedThread(new CheckedRunnable() {
391 >            public void realRun() {
392 >                long ws = lock.tryWriteLock();
393 >                assertTrue(ws == 0L);
394 >            }});
395 >
396 >        awaitTermination(t);
397 >        releaseWriteLock(lock, s);
398 >    }
399 >
400 >    /**
401 >     * tryReadLock fails if write-locked
402 >     */
403 >    public void testReadTryLockWhenLocked() {
404 >        final StampedLock lock = new StampedLock();
405 >        long s = lock.writeLock();
406 >        Thread t = newStartedThread(new CheckedRunnable() {
407 >            public void realRun() {
408 >                long rs = lock.tryReadLock();
409 >                assertEquals(rs, 0L);
410 >            }});
411 >
412 >        awaitTermination(t);
413 >        releaseWriteLock(lock, s);
414 >    }
415 >
416 >    /**
417 >     * Multiple threads can hold a read lock when not write-locked
418 >     */
419 >    public void testMultipleReadLocks() {
420 >        final StampedLock lock = new StampedLock();
421 >        final long s = lock.readLock();
422 >        Thread t = newStartedThread(new CheckedRunnable() {
423 >            public void realRun() throws InterruptedException {
424 >                long s2 = lock.tryReadLock();
425 >                assertTrue(s2 != 0L);
426 >                lock.unlockRead(s2);
427 >                long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
428 >                assertTrue(s3 != 0L);
429 >                lock.unlockRead(s3);
430 >                long s4 = lock.readLock();
431 >                lock.unlockRead(s4);
432 >            }});
433 >
434 >        awaitTermination(t);
435 >        lock.unlockRead(s);
436 >    }
437 >
438 >    /**
439 >     * A writelock succeeds only after a reading thread unlocks
440 >     */
441 >    public void testWriteAfterReadLock() {
442 >        final CountDownLatch running = new CountDownLatch(1);
443 >        final StampedLock lock = new StampedLock();
444 >        long rs = lock.readLock();
445 >        Thread t = newStartedThread(new CheckedRunnable() {
446 >            public void realRun() {
447 >                running.countDown();
448 >                long s = lock.writeLock();
449 >                lock.unlockWrite(s);
450 >            }});
451 >        try {
452 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
453 >            assertFalse(lock.isWriteLocked());
454 >            lock.unlockRead(rs);
455 >            awaitTermination(t);
456 >            assertFalse(lock.isWriteLocked());
457 >        } catch (InterruptedException ie) {
458 >            threadUnexpectedException(ie);
459 >        }
460 >    }
461 >
462 >    /**
463 >     * A writelock succeeds only after reading threads unlock
464 >     */
465 >    public void testWriteAfterMultipleReadLocks() {
466 >        final StampedLock lock = new StampedLock();
467 >        long s = lock.readLock();
468 >        Thread t1 = newStartedThread(new CheckedRunnable() {
469 >            public void realRun() {
470 >                long rs = lock.readLock();
471 >                lock.unlockRead(rs);
472 >            }});
473 >        awaitTermination(t1);
474 >
475 >        Thread t2 = newStartedThread(new CheckedRunnable() {
476 >            public void realRun() {
477 >                long ws = lock.writeLock();
478 >                lock.unlockWrite(ws);
479 >            }});
480 >        assertFalse(lock.isWriteLocked());
481 >        lock.unlockRead(s);
482 >        awaitTermination(t2);
483 >        assertFalse(lock.isWriteLocked());
484 >    }
485 >
486 >    /**
487 >     * Readlocks succeed only after a writing thread unlocks
488 >     */
489 >    public void testReadAfterWriteLock() {
490 >        final StampedLock lock = new StampedLock();
491 >        final long s = lock.writeLock();
492 >        Thread t1 = newStartedThread(new CheckedRunnable() {
493 >            public void realRun() {
494 >                long rs = lock.readLock();
495 >                lock.unlockRead(rs);
496 >            }});
497 >        Thread t2 = newStartedThread(new CheckedRunnable() {
498 >            public void realRun() {
499 >                long rs = lock.readLock();
500 >                lock.unlockRead(rs);
501 >            }});
502 >
503 >        releaseWriteLock(lock, s);
504 >        awaitTermination(t1);
505 >        awaitTermination(t2);
506 >    }
507 >
508 >    /**
509 >     * tryReadLock succeeds if readlocked but not writelocked
510 >     */
511 >    public void testTryLockWhenReadLocked() {
512 >        final StampedLock lock = new StampedLock();
513 >        long s = lock.readLock();
514 >        Thread t = newStartedThread(new CheckedRunnable() {
515 >            public void realRun() {
516 >                long rs = lock.tryReadLock();
517 >                threadAssertTrue(rs != 0L);
518 >                lock.unlockRead(rs);
519 >            }});
520 >
521 >        awaitTermination(t);
522 >        lock.unlockRead(s);
523 >    }
524 >
525 >    /**
526 >     * tryWriteLock fails when readlocked
527 >     */
528 >    public void testWriteTryLockWhenReadLocked() {
529 >        final StampedLock lock = new StampedLock();
530 >        long s = lock.readLock();
531 >        Thread t = newStartedThread(new CheckedRunnable() {
532 >            public void realRun() {
533 >                long ws = lock.tryWriteLock();
534 >                threadAssertEquals(ws, 0L);
535 >            }});
536 >
537 >        awaitTermination(t);
538 >        lock.unlockRead(s);
539 >    }
540 >
541 >    /**
542 >     * timed tryWriteLock times out if locked
543 >     */
544 >    public void testWriteTryLock_Timeout() {
545 >        final StampedLock lock = new StampedLock();
546 >        long s = lock.writeLock();
547 >        Thread t = newStartedThread(new CheckedRunnable() {
548 >            public void realRun() throws InterruptedException {
549 >                long startTime = System.nanoTime();
550 >                long timeoutMillis = 10;
551 >                long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
552 >                assertEquals(ws, 0L);
553 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
554 >            }});
555 >
556 >        awaitTermination(t);
557 >        releaseWriteLock(lock, s);
558 >    }
559 >
560 >    /**
561 >     * timed tryReadLock times out if write-locked
562 >     */
563 >    public void testReadTryLock_Timeout() {
564 >        final StampedLock lock = new StampedLock();
565 >        long s = lock.writeLock();
566 >        Thread t = newStartedThread(new CheckedRunnable() {
567 >            public void realRun() throws InterruptedException {
568 >                long startTime = System.nanoTime();
569 >                long timeoutMillis = 10;
570 >                long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
571 >                assertEquals(rs, 0L);
572 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
573 >            }});
574 >        
575 >        awaitTermination(t);
576 >        assertTrue(lock.isWriteLocked());
577 >        lock.unlockWrite(s);
578 >    }
579 >
580 >    /**
581 >     * writeLockInterruptibly succeeds if unlocked, else is interruptible
582 >     */
583 >    public void testWriteLockInterruptibly() {
584 >        final CountDownLatch running = new CountDownLatch(1);
585 >        final StampedLock lock = new StampedLock();
586 >        long s = 0L;
587 >        try {
588 >            s = lock.writeLockInterruptibly();
589 >        } catch (InterruptedException ie) {
590 >            threadUnexpectedException(ie);
591 >        }
592 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
593 >            public void realRun() throws InterruptedException {
594 >                running.countDown();
595 >                lock.writeLockInterruptibly();
596 >            }});
597 >
598 >        try {
599 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
600 >            t.interrupt();
601 >            assertTrue(lock.isWriteLocked());
602 >            awaitTermination(t);
603 >            releaseWriteLock(lock, s);
604 >        } catch (InterruptedException ie) {
605 >            threadUnexpectedException(ie);
606 >        }
607 >
608 >    }
609 >
610 >    /**
611 >     * readLockInterruptibly succeeds if lock free else is interruptible
612 >     */
613 >    public void testReadLockInterruptibly() {
614 >        final CountDownLatch running = new CountDownLatch(1);
615 >        final StampedLock lock = new StampedLock();
616 >        long s = 0L;
617 >        try {
618 >            s = lock.readLockInterruptibly();
619 >            lock.unlockRead(s);
620 >            s = lock.writeLockInterruptibly();
621 >        } catch (InterruptedException ie) {
622 >            threadUnexpectedException(ie);
623 >        }
624 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
625 >            public void realRun() throws InterruptedException {
626 >                running.countDown();
627 >                lock.readLockInterruptibly();
628 >            }});
629 >        try {
630 >            running.await(); Thread.sleep(SHORT_DELAY_MS);
631 >            t.interrupt();
632 >            awaitTermination(t);
633 >            releaseWriteLock(lock, s);
634 >        } catch (InterruptedException ie) {
635 >            threadUnexpectedException(ie);
636 >        }
637 >    }
638 >
639 >    /**
640 >     * A serialized lock deserializes as unlocked
641 >     */
642 >    public void testSerialization() {
643 >        StampedLock lock = new StampedLock();
644 >        lock.writeLock();
645 >        StampedLock clone = serialClone(lock);
646 >        assertTrue(lock.isWriteLocked());
647 >        assertFalse(clone.isWriteLocked());
648 >        long s = clone.writeLock();
649 >        assertTrue(clone.isWriteLocked());
650 >        clone.unlockWrite(s);
651 >        assertFalse(clone.isWriteLocked());
652 >    }
653 >
654 >    /**
655 >     * toString indicates current lock state
656 >     */
657 >    public void testToString() {
658 >        StampedLock lock = new StampedLock();
659 >        assertTrue(lock.toString().contains("Unlocked"));
660 >        long s = lock.writeLock();
661 >        assertTrue(lock.toString().contains("Write-locked"));
662 >        lock.unlockWrite(s);
663 >        s = lock.readLock();
664 >        assertTrue(lock.toString().contains("Read-locks"));
665 >    }
666 >
667 >    /**
668 >     * tryOptimisticRead succeeds and validates if unlocked, fails if locked
669 >     */
670 >    public void testValidateOptimistic() {
671 >        try {
672 >            StampedLock lock = new StampedLock();
673 >            long s, p;
674 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
675 >            assertTrue(lock.validate(p));
676 >            assertTrue((s = lock.writeLock()) != 0L);
677 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
678 >            assertTrue(lock.validate(s));
679 >            lock.unlockWrite(s);
680 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
681 >            assertTrue(lock.validate(p));
682 >            assertTrue((s = lock.readLock()) != 0L);
683 >            assertTrue(lock.validate(s));
684 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
685 >            assertTrue(lock.validate(p));
686 >            lock.unlockRead(s);
687 >            assertTrue((s = lock.tryWriteLock()) != 0L);
688 >            assertTrue(lock.validate(s));
689 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
690 >            lock.unlockWrite(s);
691 >            assertTrue((s = lock.tryReadLock()) != 0L);
692 >            assertTrue(lock.validate(s));
693 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
694 >            lock.unlockRead(s);
695 >            assertTrue(lock.validate(p));
696 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
697 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
698 >            assertTrue(lock.validate(s));
699 >            lock.unlockWrite(s);
700 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
701 >            assertTrue(lock.validate(s));
702 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
703 >            lock.unlockRead(s);
704 >            assertTrue((p = lock.tryOptimisticRead()) != 0L);
705 >        } catch (InterruptedException ie) {
706 >            threadUnexpectedException(ie);
707 >        }
708 >    }
709 >
710 >    /**
711 >     * tryOptimisticRead stamp does not validate if a write lock intervenes
712 >     */
713 >    public void testValidateOptimisticWriteLocked() {
714 >        StampedLock lock = new StampedLock();
715 >        long s, p;
716 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
717 >        assertTrue((s = lock.writeLock()) != 0L);
718 >        assertFalse(lock.validate(p));
719 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
720 >        assertTrue(lock.validate(s));
721 >        lock.unlockWrite(s);
722 >    }
723 >
724 >    /**
725 >     * tryOptimisticRead stamp does not validate if a write lock
726 >     * intervenes in another thread
727 >     */
728 >    public void testValidateOptimisticWriteLocked2() {
729 >        final CountDownLatch running = new CountDownLatch(1);
730 >        final StampedLock lock = new StampedLock();
731 >        long s, p;
732 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
733 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
734 >                public void realRun() throws InterruptedException {
735 >                    lock.writeLockInterruptibly();
736 >                    running.countDown();
737 >                    lock.writeLockInterruptibly();
738 >                }});
739 >        try {
740 >            running.await();
741 >            assertFalse(lock.validate(p));
742 >            assertFalse((p = lock.tryOptimisticRead()) != 0L);
743 >            t.interrupt();
744 >            awaitTermination(t);
745 >        } catch (InterruptedException ie) {
746 >            threadUnexpectedException(ie);
747 >        }
748 >    }
749 >
750 >    /**
751 >     * tryConvertToOptimisticRead succeeds and validates if successfully locked,
752 >     */
753 >    public void testTryConvertToOptimisticRead() {
754 >        try {
755 >            StampedLock lock = new StampedLock();
756 >            long s, p;
757 >            s = 0L;
758 >            assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
759 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
760 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
761 >            assertTrue((s = lock.writeLock()) != 0L);
762 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
763 >            assertTrue(lock.validate(p));
764 >            assertTrue((s = lock.readLock()) != 0L);
765 >            assertTrue(lock.validate(s));
766 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
767 >            assertTrue(lock.validate(p));
768 >            assertTrue((s = lock.tryWriteLock()) != 0L);
769 >            assertTrue(lock.validate(s));
770 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
771 >            assertTrue(lock.validate(p));
772 >            assertTrue((s = lock.tryReadLock()) != 0L);
773 >            assertTrue(lock.validate(s));
774 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
775 >            assertTrue(lock.validate(p));
776 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
777 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
778 >            assertTrue(lock.validate(p));
779 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
780 >            assertTrue(lock.validate(s));
781 >            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
782 >            assertTrue(lock.validate(p));
783 >        } catch (InterruptedException ie) {
784 >            threadUnexpectedException(ie);
785 >        }
786 >    }
787 >
788 >    /**
789 >     * tryConvertToReadLock succeeds and validates if successfully locked
790 >     * or lock free;
791 >     */
792 >    public void testTryConvertToReadLock() {
793 >        try {
794 >            StampedLock lock = new StampedLock();
795 >            long s, p;
796 >            s = 0L;
797 >            assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
798 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
799 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
800 >            lock.unlockRead(p);
801 >            assertTrue((s = lock.writeLock()) != 0L);
802 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
803 >            assertTrue(lock.validate(p));
804 >            lock.unlockRead(p);
805 >            assertTrue((s = lock.readLock()) != 0L);
806 >            assertTrue(lock.validate(s));
807 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
808 >            assertTrue(lock.validate(p));
809 >            lock.unlockRead(p);
810 >            assertTrue((s = lock.tryWriteLock()) != 0L);
811 >            assertTrue(lock.validate(s));
812 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
813 >            assertTrue(lock.validate(p));
814 >            lock.unlockRead(p);
815 >            assertTrue((s = lock.tryReadLock()) != 0L);
816 >            assertTrue(lock.validate(s));
817 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
818 >            assertTrue(lock.validate(p));
819 >            lock.unlockRead(p);
820 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
821 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
822 >            assertTrue(lock.validate(p));
823 >            lock.unlockRead(p);
824 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
825 >            assertTrue(lock.validate(s));
826 >            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
827 >            assertTrue(lock.validate(p));
828 >            lock.unlockRead(p);
829 >        } catch (InterruptedException ie) {
830 >            threadUnexpectedException(ie);
831 >        }
832 >    }
833 >
834 >    /**
835 >     * tryConvertToWriteLock succeeds and validates if successfully locked
836 >     * or lock free;
837 >     */
838 >    public void testTryConvertToWriteLock() {
839 >        try {
840 >            StampedLock lock = new StampedLock();
841 >            long s, p;
842 >            s = 0L;
843 >            assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
844 >            assertTrue((s = lock.tryOptimisticRead()) != 0L);
845 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
846 >            lock.unlockWrite(p);
847 >            assertTrue((s = lock.writeLock()) != 0L);
848 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
849 >            assertTrue(lock.validate(p));
850 >            lock.unlockWrite(p);
851 >            assertTrue((s = lock.readLock()) != 0L);
852 >            assertTrue(lock.validate(s));
853 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
854 >            assertTrue(lock.validate(p));
855 >            lock.unlockWrite(p);
856 >            assertTrue((s = lock.tryWriteLock()) != 0L);
857 >            assertTrue(lock.validate(s));
858 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
859 >            assertTrue(lock.validate(p));
860 >            lock.unlockWrite(p);
861 >            assertTrue((s = lock.tryReadLock()) != 0L);
862 >            assertTrue(lock.validate(s));
863 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
864 >            assertTrue(lock.validate(p));
865 >            lock.unlockWrite(p);
866 >            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
867 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
868 >            assertTrue(lock.validate(p));
869 >            lock.unlockWrite(p);
870 >            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
871 >            assertTrue(lock.validate(s));
872 >            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
873 >            assertTrue(lock.validate(p));
874 >            lock.unlockWrite(p);
875 >        } catch (InterruptedException ie) {
876 >            threadUnexpectedException(ie);
877 >        }
878 >    }
879 >
880 >    /**
881 >     * asWriteLock can be locked and unlocked
882 >     */
883 >    public void testAsWriteLock() {
884 >        StampedLock sl = new StampedLock();
885 >        Lock lock = sl.asWriteLock();
886 >        lock.lock();
887 >        assertFalse(lock.tryLock());
888 >        lock.unlock();
889 >        assertTrue(lock.tryLock());
890 >    }
891 >
892 >    /**
893 >     * asReadLock can be locked and unlocked
894 >     */
895 >    public void testAsReadLock() {
896 >        StampedLock sl = new StampedLock();
897 >        Lock lock = sl.asReadLock();
898 >        lock.lock();
899 >        lock.unlock();
900 >        assertTrue(lock.tryLock());
901 >    }
902 >
903 >    /**
904 >     * asReadWriteLock.writeLock can be locked and unlocked
905 >     */
906 >    public void testAsReadWriteLockWriteLock() {
907 >        StampedLock sl = new StampedLock();
908 >        Lock lock = sl.asReadWriteLock().writeLock();
909 >        lock.lock();
910 >        assertFalse(lock.tryLock());
911 >        lock.unlock();
912 >        assertTrue(lock.tryLock());
913 >    }
914 >
915 >    /**
916 >     * asReadWriteLock.readLock can be locked and unlocked
917 >     */
918 >    public void testAsReadWriteLockReadLock() {
919 >        StampedLock sl = new StampedLock();
920 >        Lock lock = sl.asReadWriteLock().readLock();
921 >        lock.lock();
922 >        lock.unlock();
923 >        assertTrue(lock.tryLock());
924 >    }
925  
926   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines