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.25 by jsr166, Sun Jul 17 02:30:53 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines