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.2 by jsr166, Fri Feb 1 21:44:41 2013 UTC vs.
Revision 1.9 by jsr166, Wed Dec 31 16:44:02 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines