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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.59 by jsr166, Sat May 7 19:03:26 2011 UTC vs.
Revision 1.61 by jsr166, Fri May 13 21:48:59 2011 UTC

# Line 67 | Line 67 | public class ReentrantReadWriteLockTest
67       */
68      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
69          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
70 <        assertWriteLockedBy(lock, Thread.currentThread());
70 >        assertWriteLockedByMoi(lock);
71          assertEquals(1, lock.getWriteHoldCount());
72          writeLock.unlock();
73          assertNotWriteLocked(lock);
# Line 94 | Line 94 | public class ReentrantReadWriteLockTest
94          assertFalse(lock.isWriteLocked());
95          assertFalse(lock.isWriteLockedByCurrentThread());
96          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
97          assertEquals(0, lock.getWriteHoldCount());
98 +        assertEquals(0, lock.writeLock().getHoldCount());
99 +        assertNull(lock.getOwner());
100      }
101  
102      /**
# Line 110 | Line 111 | public class ReentrantReadWriteLockTest
111                       lock.writeLock().isHeldByCurrentThread());
112          assertEquals(t == Thread.currentThread(),
113                       lock.getWriteHoldCount() > 0);
114 +        assertEquals(t == Thread.currentThread(),
115 +                     lock.writeLock().getHoldCount() > 0);
116          assertEquals(0, lock.getReadLockCount());
117      }
118  
119      /**
120 +     * Checks that lock is write-locked by the current thread.
121 +     */
122 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
123 +        assertWriteLockedBy(lock, Thread.currentThread());
124 +    }
125 +
126 +    /**
127       * Checks that condition c has no waiters.
128       */
129      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 145 | public class ReentrantReadWriteLockTest
145          lock.writeLock().unlock();
146      }
147  
148 +    enum AwaitMethod { await, awaitNanos, awaitUntil };
149 +
150 +    /**
151 +     * Awaits condition using the specified AwaitMethod.
152 +     */
153 +    void await(Condition c, AwaitMethod awaitMethod)
154 +            throws InterruptedException {
155 +        switch (awaitMethod) {
156 +        case await:
157 +            c.await();
158 +            break;
159 +        case awaitNanos:
160 +            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
161 +            assertTrue(nanosRemaining > 0);
162 +            break;
163 +        case awaitUntil:
164 +            java.util.Date d = new java.util.Date();
165 +            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
166 +            break;
167 +        }
168 +    }
169 +
170      /**
171       * Constructor sets given fairness, and is in unlocked state
172       */
# Line 160 | Line 192 | public class ReentrantReadWriteLockTest
192      /**
193       * write-locking and read-locking an unlocked lock succeed
194       */
195 <    public void testLock() {
196 <        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
197 <        assertNotWriteLocked(lock);
198 <        lock.writeLock().lock();
199 <        assertWriteLockedBy(lock, Thread.currentThread());
168 <        lock.writeLock().unlock();
169 <        assertNotWriteLocked(lock);
170 <        assertEquals(0, lock.getReadLockCount());
171 <        lock.readLock().lock();
172 <        assertNotWriteLocked(lock);
173 <        assertEquals(1, lock.getReadLockCount());
174 <        lock.readLock().unlock();
175 <        assertNotWriteLocked(lock);
176 <        assertEquals(0, lock.getReadLockCount());
177 <    }
178 <
179 <    /**
180 <     * locking an unlocked fair lock succeeds
181 <     */
182 <    public void testFairLock() {
183 <        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
195 >    public void testLock()      { testLock(false); }
196 >    public void testLock_fair() { testLock(true); }
197 >    public void testLock(boolean fair) {
198 >        PublicReentrantReadWriteLock lock =
199 >            new PublicReentrantReadWriteLock(fair);
200          assertNotWriteLocked(lock);
201          lock.writeLock().lock();
202 <        assertWriteLockedBy(lock, Thread.currentThread());
202 >        assertWriteLockedByMoi(lock);
203          lock.writeLock().unlock();
204          assertNotWriteLocked(lock);
205          assertEquals(0, lock.getReadLockCount());
# Line 198 | Line 214 | public class ReentrantReadWriteLockTest
214      /**
215       * getWriteHoldCount returns number of recursive holds
216       */
217 <    public void testGetWriteHoldCount() {
218 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
217 >    public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
218 >    public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
219 >    public void testGetWriteHoldCount(boolean fair) {
220 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
221          for (int i = 1; i <= SIZE; i++) {
222              lock.writeLock().lock();
223              assertEquals(i,lock.getWriteHoldCount());
# Line 211 | Line 229 | public class ReentrantReadWriteLockTest
229      }
230  
231      /**
232 <     * WriteLock.getHoldCount returns number of recursive holds
232 >     * writelock.getHoldCount returns number of recursive holds
233       */
234 <    public void testGetHoldCount() {
235 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
234 >    public void testGetHoldCount()      { testGetHoldCount(false); }
235 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
236 >    public void testGetHoldCount(boolean fair) {
237 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
238          for (int i = 1; i <= SIZE; i++) {
239              lock.writeLock().lock();
240              assertEquals(i,lock.writeLock().getHoldCount());
# Line 228 | Line 248 | public class ReentrantReadWriteLockTest
248      /**
249       * getReadHoldCount returns number of recursive holds
250       */
251 <    public void testGetReadHoldCount() {
252 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
251 >    public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
252 >    public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
253 >    public void testGetReadHoldCount(boolean fair) {
254 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
255          for (int i = 1; i <= SIZE; i++) {
256              lock.readLock().lock();
257              assertEquals(i,lock.getReadHoldCount());
# Line 243 | Line 265 | public class ReentrantReadWriteLockTest
265      /**
266       * write-unlocking an unlocked lock throws IllegalMonitorStateException
267       */
268 <    public void testWriteUnlock_IllegalMonitorStateException() {
269 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
268 >    public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
269 >    public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
270 >    public void testWriteUnlock_IMSE(boolean fair) {
271 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
272          try {
273              lock.writeLock().unlock();
274              shouldThrow();
# Line 254 | Line 278 | public class ReentrantReadWriteLockTest
278      /**
279       * read-unlocking an unlocked lock throws IllegalMonitorStateException
280       */
281 <    public void testReadUnlock_IllegalMonitorStateException() {
282 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
281 >    public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
282 >    public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
283 >    public void testReadUnlock_IMSE(boolean fair) {
284 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
285          try {
286              lock.readLock().unlock();
287              shouldThrow();
# Line 265 | Line 291 | public class ReentrantReadWriteLockTest
291      /**
292       * write-lockInterruptibly is interruptible
293       */
294 <    public void testWriteLockInterruptibly_Interrupted() throws Exception {
295 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
294 >    public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
295 >    public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
296 >    public void testWriteLockInterruptibly_Interruptible(boolean fair) {
297 >        final PublicReentrantReadWriteLock lock =
298 >            new PublicReentrantReadWriteLock(fair);
299          lock.writeLock().lock();
300          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
301              public void realRun() throws InterruptedException {
# Line 282 | Line 311 | public class ReentrantReadWriteLockTest
311      /**
312       * timed write-tryLock is interruptible
313       */
314 <    public void testWriteTryLock_Interrupted() throws InterruptedException {
315 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
314 >    public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
315 >    public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
316 >    public void testWriteTryLock_Interruptible(boolean fair) {
317 >        final PublicReentrantReadWriteLock lock =
318 >            new PublicReentrantReadWriteLock(fair);
319          lock.writeLock().lock();
320          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
321              public void realRun() throws InterruptedException {
# Line 299 | Line 331 | public class ReentrantReadWriteLockTest
331      /**
332       * read-lockInterruptibly is interruptible
333       */
334 <    public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
335 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
334 >    public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
335 >    public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
336 >    public void testReadLockInterruptibly_Interruptible(boolean fair) {
337 >        final PublicReentrantReadWriteLock lock =
338 >            new PublicReentrantReadWriteLock(fair);
339          lock.writeLock().lock();
340          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
341              public void realRun() throws InterruptedException {
# Line 316 | Line 351 | public class ReentrantReadWriteLockTest
351      /**
352       * timed read-tryLock is interruptible
353       */
354 <    public void testReadTryLock_Interrupted() throws InterruptedException {
355 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
354 >    public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
355 >    public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
356 >    public void testReadTryLock_Interruptible(boolean fair) {
357 >        final PublicReentrantReadWriteLock lock =
358 >            new PublicReentrantReadWriteLock(fair);
359          lock.writeLock().lock();
360          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
361              public void realRun() throws InterruptedException {
# Line 331 | Line 369 | public class ReentrantReadWriteLockTest
369      }
370  
371      /**
372 +     * write-tryLock on an unlocked lock succeeds
373 +     */
374 +    public void testWriteTryLock()      { testWriteTryLock(false); }
375 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
376 +    public void testWriteTryLock(boolean fair) {
377 +        final PublicReentrantReadWriteLock lock =
378 +            new PublicReentrantReadWriteLock(fair);
379 +        assertTrue(lock.writeLock().tryLock());
380 +        assertWriteLockedByMoi(lock);
381 +        assertTrue(lock.writeLock().tryLock());
382 +        assertWriteLockedByMoi(lock);
383 +        lock.writeLock().unlock();
384 +        releaseWriteLock(lock);
385 +    }
386 +
387 +    /**
388       * write-tryLock fails if locked
389       */
390 <    public void testWriteTryLockWhenLocked() throws InterruptedException {
391 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
390 >    public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
391 >    public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
392 >    public void testWriteTryLockWhenLocked(boolean fair) {
393 >        final PublicReentrantReadWriteLock lock =
394 >            new PublicReentrantReadWriteLock(fair);
395          lock.writeLock().lock();
396          Thread t = newStartedThread(new CheckedRunnable() {
397              public void realRun() {
# Line 348 | Line 405 | public class ReentrantReadWriteLockTest
405      /**
406       * read-tryLock fails if locked
407       */
408 <    public void testReadTryLockWhenLocked() throws InterruptedException {
409 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
408 >    public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
409 >    public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
410 >    public void testReadTryLockWhenLocked(boolean fair) {
411 >        final PublicReentrantReadWriteLock lock =
412 >            new PublicReentrantReadWriteLock(fair);
413          lock.writeLock().lock();
414          Thread t = newStartedThread(new CheckedRunnable() {
415              public void realRun() {
# Line 363 | Line 423 | public class ReentrantReadWriteLockTest
423      /**
424       * Multiple threads can hold a read lock when not write-locked
425       */
426 <    public void testMultipleReadLocks() throws InterruptedException {
427 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
426 >    public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
427 >    public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
428 >    public void testMultipleReadLocks(boolean fair) {
429 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
430          lock.readLock().lock();
431          Thread t = newStartedThread(new CheckedRunnable() {
432 <            public void realRun() {
432 >            public void realRun() throws InterruptedException {
433                  assertTrue(lock.readLock().tryLock());
434                  lock.readLock().unlock();
435 +                assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
436 +                lock.readLock().unlock();
437 +                lock.readLock().lock();
438 +                lock.readLock().unlock();
439              }});
440  
441          awaitTermination(t);
# Line 379 | Line 445 | public class ReentrantReadWriteLockTest
445      /**
446       * A writelock succeeds only after a reading thread unlocks
447       */
448 <    public void testWriteAfterReadLock() throws InterruptedException {
449 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
448 >    public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
449 >    public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
450 >    public void testWriteAfterReadLock(boolean fair) {
451 >        final PublicReentrantReadWriteLock lock =
452 >            new PublicReentrantReadWriteLock(fair);
453          lock.readLock().lock();
385
454          Thread t = newStartedThread(new CheckedRunnable() {
455              public void realRun() {
456                  assertEquals(1, lock.getReadLockCount());
# Line 402 | Line 470 | public class ReentrantReadWriteLockTest
470      /**
471       * A writelock succeeds only after reading threads unlock
472       */
473 <    public void testWriteAfterMultipleReadLocks() throws InterruptedException {
474 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
473 >    public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
474 >    public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
475 >    public void testWriteAfterMultipleReadLocks(boolean fair) {
476 >        final PublicReentrantReadWriteLock lock =
477 >            new PublicReentrantReadWriteLock(fair);
478          lock.readLock().lock();
479          lock.readLock().lock();
480          Thread t1 = newStartedThread(new CheckedRunnable() {
# Line 435 | Line 506 | public class ReentrantReadWriteLockTest
506       * A thread that tries to acquire a fair read lock (non-reentrantly)
507       * will block if there is a waiting writer thread.
508       */
509 <    public void testReaderWriterReaderFairFifo() throws InterruptedException {
510 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
509 >    public void testReaderWriterReaderFairFifo() {
510 >        final PublicReentrantReadWriteLock lock =
511 >            new PublicReentrantReadWriteLock(true);
512          final AtomicBoolean t1GotLock = new AtomicBoolean(false);
513  
514          lock.readLock().lock();
# Line 471 | Line 543 | public class ReentrantReadWriteLockTest
543      /**
544       * Readlocks succeed only after a writing thread unlocks
545       */
546 <    public void testReadAfterWriteLock() throws InterruptedException {
547 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
546 >    public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
547 >    public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
548 >    public void testReadAfterWriteLock(boolean fair) {
549 >        final PublicReentrantReadWriteLock lock =
550 >            new PublicReentrantReadWriteLock(fair);
551          lock.writeLock().lock();
552          Thread t1 = newStartedThread(new CheckedRunnable() {
553              public void realRun() {
# Line 495 | Line 570 | public class ReentrantReadWriteLockTest
570      /**
571       * Read trylock succeeds if write locked by current thread
572       */
573 <    public void testReadHoldingWriteLock() {
574 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
573 >    public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
574 >    public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
575 >    public void testReadHoldingWriteLock(boolean fair) {
576 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
577          lock.writeLock().lock();
578          assertTrue(lock.readLock().tryLock());
579          lock.readLock().unlock();
# Line 504 | Line 581 | public class ReentrantReadWriteLockTest
581      }
582  
583      /**
584 <     * Read lock succeeds if write locked by current thread even if
508 <     * other threads are waiting for readlock
584 >     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
585       */
586 <    public void testReadHoldingWriteLock2() throws InterruptedException {
587 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
588 <        lock.writeLock().lock();
586 >    public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
587 >    public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
588 >    public void testReadTryLockBarging(boolean fair) {
589 >        final PublicReentrantReadWriteLock lock =
590 >            new PublicReentrantReadWriteLock(fair);
591          lock.readLock().lock();
514        lock.readLock().unlock();
592  
593          Thread t1 = newStartedThread(new CheckedRunnable() {
594              public void realRun() {
595 <                lock.readLock().lock();
596 <                lock.readLock().unlock();
520 <            }});
521 <        Thread t2 = newStartedThread(new CheckedRunnable() {
522 <            public void realRun() {
523 <                lock.readLock().lock();
524 <                lock.readLock().unlock();
595 >                lock.writeLock().lock();
596 >                lock.writeLock().unlock();
597              }});
598  
599          waitForQueuedThread(lock, t1);
528        waitForQueuedThread(lock, t2);
529        assertWriteLockedBy(lock, Thread.currentThread());
530        lock.readLock().lock();
531        lock.readLock().unlock();
532        releaseWriteLock(lock);
533        awaitTermination(t1);
534        awaitTermination(t2);
535    }
600  
537    /**
538     * Read lock succeeds if write locked by current thread even if
539     * other threads are waiting for writelock
540     */
541    public void testReadHoldingWriteLock3() throws InterruptedException {
542        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
543        lock.writeLock().lock();
544        lock.readLock().lock();
545        lock.readLock().unlock();
546
547        Thread t1 = newStartedThread(new CheckedRunnable() {
548            public void realRun() {
549                lock.writeLock().lock();
550                lock.writeLock().unlock();
551            }});
601          Thread t2 = newStartedThread(new CheckedRunnable() {
602              public void realRun() {
603 <                lock.writeLock().lock();
604 <                lock.writeLock().unlock();
603 >                lock.readLock().lock();
604 >                lock.readLock().unlock();
605              }});
606  
607 <        waitForQueuedThread(lock, t1);
608 <        waitForQueuedThread(lock, t2);
560 <        assertWriteLockedBy(lock, Thread.currentThread());
561 <        lock.readLock().lock();
562 <        lock.readLock().unlock();
563 <        releaseWriteLock(lock);
564 <        awaitTermination(t1);
565 <        awaitTermination(t2);
566 <    }
607 >        if (fair)
608 >            waitForQueuedThread(lock, t2);
609  
610 <    /**
569 <     * Write lock succeeds if write locked by current thread even if
570 <     * other threads are waiting for writelock
571 <     */
572 <    public void testWriteHoldingWriteLock4() throws InterruptedException {
573 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
574 <        lock.writeLock().lock();
575 <        lock.writeLock().lock();
576 <        lock.writeLock().unlock();
577 <
578 <        Thread t1 = newStartedThread(new CheckedRunnable() {
579 <            public void realRun() {
580 <                lock.writeLock().lock();
581 <                lock.writeLock().unlock();
582 <            }});
583 <        Thread t2 = newStartedThread(new CheckedRunnable() {
610 >        Thread t3 = newStartedThread(new CheckedRunnable() {
611              public void realRun() {
612 <                lock.writeLock().lock();
613 <                lock.writeLock().unlock();
612 >                lock.readLock().tryLock();
613 >                lock.readLock().unlock();
614              }});
615  
616 <        waitForQueuedThread(lock, t1);
617 <        waitForQueuedThread(lock, t2);
618 <        assertWriteLockedBy(lock, Thread.currentThread());
619 <        assertEquals(1, lock.getWriteHoldCount());
620 <        lock.writeLock().lock();
594 <        assertWriteLockedBy(lock, Thread.currentThread());
595 <        assertEquals(2, lock.getWriteHoldCount());
596 <        lock.writeLock().unlock();
597 <        releaseWriteLock(lock);
616 >        assertTrue(lock.getReadLockCount() > 0);
617 >        awaitTermination(t3);
618 >        assertTrue(t1.isAlive());
619 >        if (fair) assertTrue(t2.isAlive());
620 >        lock.readLock().unlock();
621          awaitTermination(t1);
622          awaitTermination(t2);
623      }
624  
625      /**
626 <     * Fair Read trylock succeeds if write locked by current thread
604 <     */
605 <    public void testReadHoldingWriteLockFair() {
606 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
607 <        lock.writeLock().lock();
608 <        assertTrue(lock.readLock().tryLock());
609 <        lock.readLock().unlock();
610 <        lock.writeLock().unlock();
611 <    }
612 <
613 <    /**
614 <     * Fair Read lock succeeds if write locked by current thread even if
626 >     * Read lock succeeds if write locked by current thread even if
627       * other threads are waiting for readlock
628       */
629 <    public void testReadHoldingWriteLockFair2() throws InterruptedException {
630 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
629 >    public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
630 >    public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
631 >    public void testReadHoldingWriteLock2(boolean fair) {
632 >        final PublicReentrantReadWriteLock lock =
633 >            new PublicReentrantReadWriteLock(fair);
634          lock.writeLock().lock();
635          lock.readLock().lock();
636          lock.readLock().unlock();
# Line 633 | Line 648 | public class ReentrantReadWriteLockTest
648  
649          waitForQueuedThread(lock, t1);
650          waitForQueuedThread(lock, t2);
651 <        assertWriteLockedBy(lock, Thread.currentThread());
651 >        assertWriteLockedByMoi(lock);
652          lock.readLock().lock();
653          lock.readLock().unlock();
654          releaseWriteLock(lock);
# Line 642 | Line 657 | public class ReentrantReadWriteLockTest
657      }
658  
659      /**
660 <     * Fair Read lock succeeds if write locked by current thread even if
660 >     * Read lock succeeds if write locked by current thread even if
661       * other threads are waiting for writelock
662       */
663 <    public void testReadHoldingWriteLockFair3() throws InterruptedException {
664 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
663 >    public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
664 >    public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
665 >    public void testReadHoldingWriteLock3(boolean fair) {
666 >        final PublicReentrantReadWriteLock lock =
667 >            new PublicReentrantReadWriteLock(fair);
668          lock.writeLock().lock();
669          lock.readLock().lock();
670          lock.readLock().unlock();
# Line 664 | Line 682 | public class ReentrantReadWriteLockTest
682  
683          waitForQueuedThread(lock, t1);
684          waitForQueuedThread(lock, t2);
685 <        assertWriteLockedBy(lock, Thread.currentThread());
685 >        assertWriteLockedByMoi(lock);
686          lock.readLock().lock();
687          lock.readLock().unlock();
688 <        releaseWriteLock(lock);
688 >        assertWriteLockedByMoi(lock);
689 >        lock.writeLock().unlock();
690          awaitTermination(t1);
691          awaitTermination(t2);
692      }
693  
694      /**
695 <     * Fair Write lock succeeds if write locked by current thread even if
695 >     * Write lock succeeds if write locked by current thread even if
696       * other threads are waiting for writelock
697       */
698 <    public void testWriteHoldingWriteLockFair4() throws InterruptedException {
699 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
698 >    public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
699 >    public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
700 >    public void testWriteHoldingWriteLock4(boolean fair) {
701 >        final PublicReentrantReadWriteLock lock =
702 >            new PublicReentrantReadWriteLock(fair);
703          lock.writeLock().lock();
704 +        lock.writeLock().lock();
705 +        lock.writeLock().unlock();
706 +
707          Thread t1 = newStartedThread(new CheckedRunnable() {
708              public void realRun() {
709                  lock.writeLock().lock();
# Line 692 | Line 717 | public class ReentrantReadWriteLockTest
717  
718          waitForQueuedThread(lock, t1);
719          waitForQueuedThread(lock, t2);
720 <        assertWriteLockedBy(lock, Thread.currentThread());
720 >        assertWriteLockedByMoi(lock);
721          assertEquals(1, lock.getWriteHoldCount());
722          lock.writeLock().lock();
723 +        assertWriteLockedByMoi(lock);
724          assertEquals(2, lock.getWriteHoldCount());
725          lock.writeLock().unlock();
726 <        lock.writeLock().lock();
726 >        assertWriteLockedByMoi(lock);
727 >        assertEquals(1, lock.getWriteHoldCount());
728          lock.writeLock().unlock();
702        releaseWriteLock(lock);
729          awaitTermination(t1);
730          awaitTermination(t2);
731      }
# Line 707 | Line 733 | public class ReentrantReadWriteLockTest
733      /**
734       * Read tryLock succeeds if readlocked but not writelocked
735       */
736 <    public void testTryLockWhenReadLocked() throws InterruptedException {
737 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
736 >    public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
737 >    public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
738 >    public void testTryLockWhenReadLocked(boolean fair) {
739 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
740          lock.readLock().lock();
741          Thread t = newStartedThread(new CheckedRunnable() {
742              public void realRun() {
# Line 723 | Line 751 | public class ReentrantReadWriteLockTest
751      /**
752       * write tryLock fails when readlocked
753       */
754 <    public void testWriteTryLockWhenReadLocked() throws InterruptedException {
755 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
756 <        lock.readLock().lock();
757 <        Thread t = newStartedThread(new CheckedRunnable() {
730 <            public void realRun() {
731 <                assertFalse(lock.writeLock().tryLock());
732 <            }});
733 <
734 <        awaitTermination(t);
735 <        lock.readLock().unlock();
736 <    }
737 <
738 <    /**
739 <     * Fair Read tryLock succeeds if readlocked but not writelocked
740 <     */
741 <    public void testTryLockWhenReadLockedFair() throws InterruptedException {
742 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
743 <        lock.readLock().lock();
744 <        Thread t = newStartedThread(new CheckedRunnable() {
745 <            public void realRun() {
746 <                assertTrue(lock.readLock().tryLock());
747 <                lock.readLock().unlock();
748 <            }});
749 <
750 <        awaitTermination(t);
751 <        lock.readLock().unlock();
752 <    }
753 <
754 <    /**
755 <     * Fair write tryLock fails when readlocked
756 <     */
757 <    public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
758 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
754 >    public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
755 >    public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
756 >    public void testWriteTryLockWhenReadLocked(boolean fair) {
757 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
758          lock.readLock().lock();
759          Thread t = newStartedThread(new CheckedRunnable() {
760              public void realRun() {
# Line 769 | Line 768 | public class ReentrantReadWriteLockTest
768      /**
769       * write timed tryLock times out if locked
770       */
771 <    public void testWriteTryLock_Timeout() throws InterruptedException {
772 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
771 >    public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
772 >    public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
773 >    public void testWriteTryLock_Timeout(boolean fair) {
774 >        final PublicReentrantReadWriteLock lock =
775 >            new PublicReentrantReadWriteLock(fair);
776          lock.writeLock().lock();
777          Thread t = newStartedThread(new CheckedRunnable() {
778              public void realRun() throws InterruptedException {
# Line 781 | Line 783 | public class ReentrantReadWriteLockTest
783              }});
784  
785          awaitTermination(t);
786 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
785 <        lock.writeLock().unlock();
786 >        releaseWriteLock(lock);
787      }
788  
789      /**
790       * read timed tryLock times out if write-locked
791       */
792 <    public void testReadTryLock_Timeout() throws InterruptedException {
793 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
792 >    public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
793 >    public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
794 >    public void testReadTryLock_Timeout(boolean fair) {
795 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
796          lock.writeLock().lock();
797          Thread t = newStartedThread(new CheckedRunnable() {
798              public void realRun() throws InterruptedException {
# Line 805 | Line 808 | public class ReentrantReadWriteLockTest
808      }
809  
810      /**
811 <     * write lockInterruptibly succeeds if lock free else is interruptible
811 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
812       */
813 <    public void testWriteLockInterruptibly() throws InterruptedException {
814 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
815 <        lock.writeLock().lockInterruptibly();
813 >    public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
814 >    public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
815 >    public void testWriteLockInterruptibly(boolean fair) {
816 >        final PublicReentrantReadWriteLock lock =
817 >            new PublicReentrantReadWriteLock(fair);
818 >        try {
819 >            lock.writeLock().lockInterruptibly();
820 >        } catch (InterruptedException ie) {
821 >            threadUnexpectedException(ie);
822 >        }
823          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
824              public void realRun() throws InterruptedException {
825                  lock.writeLock().lockInterruptibly();
# Line 817 | Line 827 | public class ReentrantReadWriteLockTest
827  
828          waitForQueuedThread(lock, t);
829          t.interrupt();
830 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
831          awaitTermination(t);
832          releaseWriteLock(lock);
833      }
# Line 824 | Line 835 | public class ReentrantReadWriteLockTest
835      /**
836       * read lockInterruptibly succeeds if lock free else is interruptible
837       */
838 <    public void testReadLockInterruptibly() throws InterruptedException {
839 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
840 <        lock.writeLock().lockInterruptibly();
838 >    public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
839 >    public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
840 >    public void testReadLockInterruptibly(boolean fair) {
841 >        final PublicReentrantReadWriteLock lock =
842 >            new PublicReentrantReadWriteLock(fair);
843 >        try {
844 >            lock.readLock().lockInterruptibly();
845 >            lock.readLock().unlock();
846 >            lock.writeLock().lockInterruptibly();
847 >        } catch (InterruptedException ie) {
848 >            threadUnexpectedException(ie);
849 >        }
850          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
851              public void realRun() throws InterruptedException {
852                  lock.readLock().lockInterruptibly();
# Line 841 | Line 861 | public class ReentrantReadWriteLockTest
861      /**
862       * Calling await without holding lock throws IllegalMonitorStateException
863       */
864 <    public void testAwait_IllegalMonitor() throws InterruptedException {
865 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
864 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
865 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
866 >    public void testAwait_IMSE(boolean fair) {
867 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
868          final Condition c = lock.writeLock().newCondition();
869 +        long startTime = System.nanoTime();
870          try {
871 <            c.await();
872 <            shouldThrow();
873 <        } catch (IllegalMonitorStateException success) {}
874 <        try {
875 <            c.await(LONG_DELAY_MS, MILLISECONDS);
876 <            shouldThrow();
877 <        } catch (IllegalMonitorStateException success) {}
878 <        try {
879 <            c.awaitNanos(100);
880 <            shouldThrow();
881 <        } catch (IllegalMonitorStateException success) {}
882 <        try {
883 <            c.awaitUninterruptibly();
884 <            shouldThrow();
885 <        } catch (IllegalMonitorStateException success) {}
871 >            try {
872 >                c.await();
873 >                shouldThrow();
874 >            } catch (IllegalMonitorStateException success) {}
875 >            try {
876 >                c.await(LONG_DELAY_MS, MILLISECONDS);
877 >                shouldThrow();
878 >            } catch (IllegalMonitorStateException success) {}
879 >            try {
880 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
881 >                shouldThrow();
882 >            } catch (IllegalMonitorStateException success) {}
883 >            try {
884 >                c.awaitUninterruptibly();
885 >                shouldThrow();
886 >            } catch (IllegalMonitorStateException success) {}
887 >        } catch (InterruptedException ie) {
888 >            threadUnexpectedException(ie);
889 >        }
890 >        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
891      }
892  
893      /**
894       * Calling signal without holding lock throws IllegalMonitorStateException
895       */
896 <    public void testSignal_IllegalMonitor() {
897 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
896 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
897 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
898 >    public void testSignal_IMSE(boolean fair) {
899 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
900          final Condition c = lock.writeLock().newCondition();
901          try {
902              c.signal();
# Line 877 | Line 907 | public class ReentrantReadWriteLockTest
907      /**
908       * Calling signalAll without holding lock throws IllegalMonitorStateException
909       */
910 <    public void testSignalAll_IllegalMonitor() {
911 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
910 >    public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
911 >    public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
912 >    public void testSignalAll_IMSE(boolean fair) {
913 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
914          final Condition c = lock.writeLock().newCondition();
915          try {
916              c.signalAll();
# Line 889 | Line 921 | public class ReentrantReadWriteLockTest
921      /**
922       * awaitNanos without a signal times out
923       */
924 <    public void testAwaitNanos_Timeout() throws InterruptedException {
925 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
926 <        final Condition c = lock.writeLock().newCondition();
927 <        lock.writeLock().lock();
928 <        long startTime = System.nanoTime();
929 <        long timeoutMillis = 10;
930 <        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
931 <        long nanosRemaining = c.awaitNanos(timeoutNanos);
932 <        assertTrue(nanosRemaining <= 0);
933 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
934 <        lock.writeLock().unlock();
924 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
925 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
926 >    public void testAwaitNanos_Timeout(boolean fair) {
927 >        try {
928 >            final ReentrantReadWriteLock lock =
929 >                new ReentrantReadWriteLock(fair);
930 >            final Condition c = lock.writeLock().newCondition();
931 >            lock.writeLock().lock();
932 >            long startTime = System.nanoTime();
933 >            long timeoutMillis = 10;
934 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
935 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
936 >            assertTrue(nanosRemaining <= 0);
937 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
938 >            lock.writeLock().unlock();
939 >        } catch (InterruptedException e) {
940 >            threadUnexpectedException(e);
941 >        }
942      }
943  
944      /**
945       * timed await without a signal times out
946       */
947 <    public void testAwait_Timeout() throws InterruptedException {
948 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
949 <        final Condition c = lock.writeLock().newCondition();
950 <        lock.writeLock().lock();
951 <        long startTime = System.nanoTime();
952 <        long timeoutMillis = 10;
953 <        assertFalse(c.await(timeoutMillis, MILLISECONDS));
954 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
955 <        lock.writeLock().unlock();
947 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
948 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
949 >    public void testAwait_Timeout(boolean fair) {
950 >        try {
951 >            final ReentrantReadWriteLock lock =
952 >                new ReentrantReadWriteLock(fair);
953 >            final Condition c = lock.writeLock().newCondition();
954 >            lock.writeLock().lock();
955 >            long startTime = System.nanoTime();
956 >            long timeoutMillis = 10;
957 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
958 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
959 >            lock.writeLock().unlock();
960 >        } catch (InterruptedException e) {
961 >            threadUnexpectedException(e);
962 >        }
963      }
964  
965      /**
966       * awaitUntil without a signal times out
967       */
968 <    public void testAwaitUntil_Timeout() throws InterruptedException {
969 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
970 <        final Condition c = lock.writeLock().newCondition();
971 <        lock.writeLock().lock();
972 <        long startTime = System.nanoTime();
973 <        long timeoutMillis = 10;
974 <        java.util.Date d = new java.util.Date();
975 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
976 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
977 <        lock.writeLock().unlock();
968 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
969 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
970 >    public void testAwaitUntil_Timeout(boolean fair) {
971 >        try {
972 >            final ReentrantReadWriteLock lock =
973 >                new ReentrantReadWriteLock(fair);
974 >            final Condition c = lock.writeLock().newCondition();
975 >            lock.writeLock().lock();
976 >            long startTime = System.nanoTime();
977 >            long timeoutMillis = 10;
978 >            java.util.Date d = new java.util.Date();
979 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
980 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
981 >            lock.writeLock().unlock();
982 >        } catch (InterruptedException e) {
983 >            threadUnexpectedException(e);
984 >        }
985      }
986  
987      /**
988       * await returns when signalled
989       */
990 <    public void testAwait() throws InterruptedException {
991 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
990 >    public void testAwait()      { testAwait(false); }
991 >    public void testAwait_fair() { testAwait(true); }
992 >    public void testAwait(boolean fair) {
993 >        final PublicReentrantReadWriteLock lock =
994 >            new PublicReentrantReadWriteLock(fair);
995          final Condition c = lock.writeLock().newCondition();
996          final CountDownLatch locked = new CountDownLatch(1);
997          Thread t = newStartedThread(new CheckedRunnable() {
# Line 946 | Line 1002 | public class ReentrantReadWriteLockTest
1002                  lock.writeLock().unlock();
1003              }});
1004  
1005 <        locked.await();
1005 >        await(locked);
1006          lock.writeLock().lock();
1007          assertHasWaiters(lock, c, t);
1008          c.signal();
# Line 959 | Line 1015 | public class ReentrantReadWriteLockTest
1015      /**
1016       * awaitUninterruptibly doesn't abort on interrupt
1017       */
1018 <    public void testAwaitUninterruptibly() throws InterruptedException {
1019 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1018 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1019 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1020 >    public void testAwaitUninterruptibly(boolean fair) {
1021 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1022          final Condition c = lock.writeLock().newCondition();
1023          final CountDownLatch locked = new CountDownLatch(1);
1024          Thread t = newStartedThread(new CheckedRunnable() {
# Line 972 | Line 1030 | public class ReentrantReadWriteLockTest
1030                  lock.writeLock().unlock();
1031              }});
1032  
1033 <        locked.await();
1033 >        await(locked);
1034          lock.writeLock().lock();
1035          lock.writeLock().unlock();
1036          t.interrupt();
1037          long timeoutMillis = 10;
1038 <        assertThreadJoinTimesOut(t, timeoutMillis);
1038 >        assertThreadStaysAlive(t, timeoutMillis);
1039          lock.writeLock().lock();
1040          c.signal();
1041          lock.writeLock().unlock();
# Line 985 | Line 1043 | public class ReentrantReadWriteLockTest
1043      }
1044  
1045      /**
1046 <     * await is interruptible
989 <     */
990 <    public void testAwait_Interrupt() throws InterruptedException {
991 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
992 <        final Condition c = lock.writeLock().newCondition();
993 <        final CountDownLatch locked = new CountDownLatch(1);
994 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
995 <            public void realRun() throws InterruptedException {
996 <                lock.writeLock().lock();
997 <                assertWriteLockedBy(lock, Thread.currentThread());
998 <                assertHasNoWaiters(lock, c);
999 <                locked.countDown();
1000 <                try {
1001 <                    c.await();
1002 <                } finally {
1003 <                    assertWriteLockedBy(lock, Thread.currentThread());
1004 <                    assertHasNoWaiters(lock, c);
1005 <                    lock.writeLock().unlock();
1006 <                    assertFalse(Thread.interrupted());
1007 <                }
1008 <            }});
1009 <
1010 <        locked.await();
1011 <        assertHasWaiters(lock, c, t);
1012 <        t.interrupt();
1013 <        awaitTermination(t);
1014 <        assertNotWriteLocked(lock);
1015 <    }
1016 <
1017 <    /**
1018 <     * awaitNanos is interruptible
1019 <     */
1020 <    public void testAwaitNanos_Interrupt() throws InterruptedException {
1021 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1022 <        final Condition c = lock.writeLock().newCondition();
1023 <        final CountDownLatch locked = new CountDownLatch(1);
1024 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1025 <            public void realRun() throws InterruptedException {
1026 <                lock.writeLock().lock();
1027 <                assertWriteLockedBy(lock, Thread.currentThread());
1028 <                assertHasNoWaiters(lock, c);
1029 <                locked.countDown();
1030 <                try {
1031 <                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
1032 <                } finally {
1033 <                    assertWriteLockedBy(lock, Thread.currentThread());
1034 <                    assertHasNoWaiters(lock, c);
1035 <                    lock.writeLock().unlock();
1036 <                    assertFalse(Thread.interrupted());
1037 <                }
1038 <            }});
1039 <
1040 <        locked.await();
1041 <        assertHasWaiters(lock, c, t);
1042 <        t.interrupt();
1043 <        awaitTermination(t);
1044 <        assertNotWriteLocked(lock);
1045 <    }
1046 <
1047 <    /**
1048 <     * awaitUntil is interruptible
1046 >     * await/awaitNanos/awaitUntil is interruptible
1047       */
1048 <    public void testAwaitUntil_Interrupt() throws InterruptedException {
1049 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1048 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1049 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1050 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1051 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1052 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
1053 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
1054 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1055 >        final PublicReentrantReadWriteLock lock =
1056 >            new PublicReentrantReadWriteLock(fair);
1057          final Condition c = lock.writeLock().newCondition();
1058          final CountDownLatch locked = new CountDownLatch(1);
1059          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1060              public void realRun() throws InterruptedException {
1061                  lock.writeLock().lock();
1062 <                assertWriteLockedBy(lock, Thread.currentThread());
1062 >                assertWriteLockedByMoi(lock);
1063                  assertHasNoWaiters(lock, c);
1064                  locked.countDown();
1060                java.util.Date d = new java.util.Date();
1065                  try {
1066 <                    c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS));
1066 >                    await(c, awaitMethod);
1067                  } finally {
1068 <                    assertWriteLockedBy(lock, Thread.currentThread());
1068 >                    assertWriteLockedByMoi(lock);
1069                      assertHasNoWaiters(lock, c);
1070                      lock.writeLock().unlock();
1071                      assertFalse(Thread.interrupted());
1072                  }
1073              }});
1074  
1075 <        locked.await();
1075 >        await(locked);
1076          assertHasWaiters(lock, c, t);
1077          t.interrupt();
1078          awaitTermination(t);
# Line 1078 | Line 1082 | public class ReentrantReadWriteLockTest
1082      /**
1083       * signalAll wakes up all threads
1084       */
1085 <    public void testSignalAll() throws InterruptedException {
1086 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1085 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1086 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1087 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1088 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1089 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
1090 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
1091 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1092 >        final PublicReentrantReadWriteLock lock =
1093 >            new PublicReentrantReadWriteLock(fair);
1094          final Condition c = lock.writeLock().newCondition();
1095          final CountDownLatch locked = new CountDownLatch(2);
1096          final Lock writeLock = lock.writeLock();
1097 <        Thread t1 = newStartedThread(new CheckedRunnable() {
1097 >        class Awaiter extends CheckedRunnable {
1098              public void realRun() throws InterruptedException {
1099                  writeLock.lock();
1100                  locked.countDown();
1101 <                c.await();
1101 >                await(c, awaitMethod);
1102                  writeLock.unlock();
1103 <            }});
1103 >            }
1104 >        }
1105  
1106 <        Thread t2 = newStartedThread(new CheckedRunnable() {
1107 <            public void realRun() throws InterruptedException {
1096 <                writeLock.lock();
1097 <                locked.countDown();
1098 <                c.await();
1099 <                writeLock.unlock();
1100 <            }});
1106 >        Thread t1 = newStartedThread(new Awaiter());
1107 >        Thread t2 = newStartedThread(new Awaiter());
1108  
1109 <        locked.await();
1109 >        await(locked);
1110          writeLock.lock();
1111          assertHasWaiters(lock, c, t1, t2);
1112          c.signalAll();
# Line 1112 | Line 1119 | public class ReentrantReadWriteLockTest
1119      /**
1120       * signal wakes up waiting threads in FIFO order.
1121       */
1122 <    public void testSignalWakesFifo() throws InterruptedException {
1123 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1122 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1123 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1124 >    public void testSignalWakesFifo(boolean fair) {
1125 >        final PublicReentrantReadWriteLock lock =
1126 >            new PublicReentrantReadWriteLock(fair);
1127          final Condition c = lock.writeLock().newCondition();
1128          final CountDownLatch locked1 = new CountDownLatch(1);
1129          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 1126 | Line 1136 | public class ReentrantReadWriteLockTest
1136                  writeLock.unlock();
1137              }});
1138  
1139 <        locked1.await();
1139 >        await(locked1);
1140  
1141          Thread t2 = newStartedThread(new CheckedRunnable() {
1142              public void realRun() throws InterruptedException {
# Line 1136 | Line 1146 | public class ReentrantReadWriteLockTest
1146                  writeLock.unlock();
1147              }});
1148  
1149 <        locked2.await();
1149 >        await(locked2);
1150  
1151          writeLock.lock();
1152          assertHasWaiters(lock, c, t1, t2);
# Line 1157 | Line 1167 | public class ReentrantReadWriteLockTest
1167      /**
1168       * await after multiple reentrant locking preserves lock count
1169       */
1170 <    public void testAwaitLockCount() throws InterruptedException {
1171 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1170 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1171 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1172 >    public void testAwaitLockCount(boolean fair) {
1173 >        final PublicReentrantReadWriteLock lock =
1174 >            new PublicReentrantReadWriteLock(fair);
1175          final Condition c = lock.writeLock().newCondition();
1176          final CountDownLatch locked = new CountDownLatch(2);
1177          Thread t1 = newStartedThread(new CheckedRunnable() {
1178              public void realRun() throws InterruptedException {
1179                  lock.writeLock().lock();
1180 <                assertWriteLockedBy(lock, Thread.currentThread());
1180 >                assertWriteLockedByMoi(lock);
1181                  assertEquals(1, lock.writeLock().getHoldCount());
1182                  locked.countDown();
1183                  c.await();
1184 <                assertWriteLockedBy(lock, Thread.currentThread());
1184 >                assertWriteLockedByMoi(lock);
1185                  assertEquals(1, lock.writeLock().getHoldCount());
1186                  lock.writeLock().unlock();
1187              }});
# Line 1177 | Line 1190 | public class ReentrantReadWriteLockTest
1190              public void realRun() throws InterruptedException {
1191                  lock.writeLock().lock();
1192                  lock.writeLock().lock();
1193 <                assertWriteLockedBy(lock, Thread.currentThread());
1193 >                assertWriteLockedByMoi(lock);
1194                  assertEquals(2, lock.writeLock().getHoldCount());
1195                  locked.countDown();
1196                  c.await();
1197 <                assertWriteLockedBy(lock, Thread.currentThread());
1197 >                assertWriteLockedByMoi(lock);
1198                  assertEquals(2, lock.writeLock().getHoldCount());
1199                  lock.writeLock().unlock();
1200                  lock.writeLock().unlock();
1201              }});
1202  
1203 <        locked.await();
1203 >        await(locked);
1204          lock.writeLock().lock();
1205          assertHasWaiters(lock, c, t1, t2);
1206          c.signalAll();
# Line 1200 | Line 1213 | public class ReentrantReadWriteLockTest
1213      /**
1214       * A serialized lock deserializes as unlocked
1215       */
1216 <    public void testSerialization() throws Exception {
1217 <        ReentrantReadWriteLock l = new ReentrantReadWriteLock();
1218 <        l.readLock().lock();
1219 <        l.readLock().unlock();
1220 <
1221 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1222 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1223 <        out.writeObject(l);
1224 <        out.close();
1225 <
1226 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1227 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1228 <        ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
1229 <        r.readLock().lock();
1230 <        r.readLock().unlock();
1216 >    public void testSerialization()      { testSerialization(false); }
1217 >    public void testSerialization_fair() { testSerialization(true); }
1218 >    public void testSerialization(boolean fair) {
1219 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1220 >        lock.writeLock().lock();
1221 >        lock.readLock().lock();
1222 >
1223 >        ReentrantReadWriteLock clone = serialClone(lock);
1224 >        assertEquals(lock.isFair(), clone.isFair());
1225 >        assertTrue(lock.isWriteLocked());
1226 >        assertFalse(clone.isWriteLocked());
1227 >        assertEquals(1, lock.getReadLockCount());
1228 >        assertEquals(0, clone.getReadLockCount());
1229 >        clone.writeLock().lock();
1230 >        clone.readLock().lock();
1231 >        assertTrue(clone.isWriteLocked());
1232 >        assertEquals(1, clone.getReadLockCount());
1233 >        clone.readLock().unlock();
1234 >        clone.writeLock().unlock();
1235 >        assertFalse(clone.isWriteLocked());
1236 >        assertEquals(1, lock.getReadLockCount());
1237 >        assertEquals(0, clone.getReadLockCount());
1238      }
1239  
1240      /**
1241       * hasQueuedThreads reports whether there are waiting threads
1242       */
1243 <    public void testHasQueuedThreads() throws InterruptedException {
1244 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1243 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
1244 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
1245 >    public void testHasQueuedThreads(boolean fair) {
1246 >        final PublicReentrantReadWriteLock lock =
1247 >            new PublicReentrantReadWriteLock(fair);
1248          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1249          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1250          assertFalse(lock.hasQueuedThreads());
# Line 1244 | Line 1267 | public class ReentrantReadWriteLockTest
1267      /**
1268       * hasQueuedThread(null) throws NPE
1269       */
1270 <    public void testHasQueuedThreadNPE() {
1271 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1270 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
1271 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
1272 >    public void testHasQueuedThreadNPE(boolean fair) {
1273 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1274          try {
1275              lock.hasQueuedThread(null);
1276              shouldThrow();
# Line 1255 | Line 1280 | public class ReentrantReadWriteLockTest
1280      /**
1281       * hasQueuedThread reports whether a thread is queued.
1282       */
1283 <    public void testHasQueuedThread() throws InterruptedException {
1284 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1283 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
1284 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
1285 >    public void testHasQueuedThread(boolean fair) {
1286 >        final PublicReentrantReadWriteLock lock =
1287 >            new PublicReentrantReadWriteLock(fair);
1288          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1289          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1290          assertFalse(lock.hasQueuedThread(t1));
# Line 1283 | Line 1311 | public class ReentrantReadWriteLockTest
1311      /**
1312       * getQueueLength reports number of waiting threads
1313       */
1314 <    public void testGetQueueLength() throws InterruptedException {
1315 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1314 >    public void testGetQueueLength()      { testGetQueueLength(false); }
1315 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
1316 >    public void testGetQueueLength(boolean fair) {
1317 >        final PublicReentrantReadWriteLock lock =
1318 >            new PublicReentrantReadWriteLock(fair);
1319          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1320          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1321          assertEquals(0, lock.getQueueLength());
# Line 1306 | Line 1337 | public class ReentrantReadWriteLockTest
1337      /**
1338       * getQueuedThreads includes waiting threads
1339       */
1340 <    public void testGetQueuedThreads() throws InterruptedException {
1341 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1340 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
1341 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
1342 >    public void testGetQueuedThreads(boolean fair) {
1343 >        final PublicReentrantReadWriteLock lock =
1344 >            new PublicReentrantReadWriteLock(fair);
1345          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1346          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1347          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1335 | Line 1369 | public class ReentrantReadWriteLockTest
1369      /**
1370       * hasWaiters throws NPE if null
1371       */
1372 <    public void testHasWaitersNPE() {
1373 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1372 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
1373 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
1374 >    public void testHasWaitersNPE(boolean fair) {
1375 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1376          try {
1377              lock.hasWaiters(null);
1378              shouldThrow();
# Line 1346 | Line 1382 | public class ReentrantReadWriteLockTest
1382      /**
1383       * getWaitQueueLength throws NPE if null
1384       */
1385 <    public void testGetWaitQueueLengthNPE() {
1386 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1385 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
1386 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
1387 >    public void testGetWaitQueueLengthNPE(boolean fair) {
1388 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1389          try {
1390              lock.getWaitQueueLength(null);
1391              shouldThrow();
# Line 1357 | Line 1395 | public class ReentrantReadWriteLockTest
1395      /**
1396       * getWaitingThreads throws NPE if null
1397       */
1398 <    public void testGetWaitingThreadsNPE() {
1399 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1398 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
1399 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
1400 >    public void testGetWaitingThreadsNPE(boolean fair) {
1401 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1402          try {
1403              lock.getWaitingThreads(null);
1404              shouldThrow();
# Line 1368 | Line 1408 | public class ReentrantReadWriteLockTest
1408      /**
1409       * hasWaiters throws IllegalArgumentException if not owned
1410       */
1411 <    public void testHasWaitersIAE() {
1412 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1411 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
1412 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
1413 >    public void testHasWaitersIAE(boolean fair) {
1414 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1415          final Condition c = lock.writeLock().newCondition();
1416 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1416 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1417          try {
1418              lock2.hasWaiters(c);
1419              shouldThrow();
# Line 1381 | Line 1423 | public class ReentrantReadWriteLockTest
1423      /**
1424       * hasWaiters throws IllegalMonitorStateException if not locked
1425       */
1426 <    public void testHasWaitersIMSE() {
1427 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1426 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
1427 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
1428 >    public void testHasWaitersIMSE(boolean fair) {
1429 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1430          final Condition c = lock.writeLock().newCondition();
1431          try {
1432              lock.hasWaiters(c);
# Line 1393 | Line 1437 | public class ReentrantReadWriteLockTest
1437      /**
1438       * getWaitQueueLength throws IllegalArgumentException if not owned
1439       */
1440 <    public void testGetWaitQueueLengthIAE() {
1441 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1440 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
1441 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
1442 >    public void testGetWaitQueueLengthIAE(boolean fair) {
1443 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1444          final Condition c = lock.writeLock().newCondition();
1445 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1445 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1446          try {
1447              lock2.getWaitQueueLength(c);
1448              shouldThrow();
# Line 1406 | Line 1452 | public class ReentrantReadWriteLockTest
1452      /**
1453       * getWaitQueueLength throws IllegalMonitorStateException if not locked
1454       */
1455 <    public void testGetWaitQueueLengthIMSE() {
1456 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1455 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
1456 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
1457 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
1458 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1459          final Condition c = lock.writeLock().newCondition();
1460          try {
1461              lock.getWaitQueueLength(c);
# Line 1418 | Line 1466 | public class ReentrantReadWriteLockTest
1466      /**
1467       * getWaitingThreads throws IllegalArgumentException if not owned
1468       */
1469 <    public void testGetWaitingThreadsIAE() {
1470 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1469 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
1470 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
1471 >    public void testGetWaitingThreadsIAE(boolean fair) {
1472 >        final PublicReentrantReadWriteLock lock =
1473 >            new PublicReentrantReadWriteLock(fair);
1474          final Condition c = lock.writeLock().newCondition();
1475 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1475 >        final PublicReentrantReadWriteLock lock2 =
1476 >            new PublicReentrantReadWriteLock(fair);
1477          try {
1478              lock2.getWaitingThreads(c);
1479              shouldThrow();
# Line 1431 | Line 1483 | public class ReentrantReadWriteLockTest
1483      /**
1484       * getWaitingThreads throws IllegalMonitorStateException if not locked
1485       */
1486 <    public void testGetWaitingThreadsIMSE() {
1487 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1486 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
1487 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
1488 >    public void testGetWaitingThreadsIMSE(boolean fair) {
1489 >        final PublicReentrantReadWriteLock lock =
1490 >            new PublicReentrantReadWriteLock(fair);
1491          final Condition c = lock.writeLock().newCondition();
1492          try {
1493              lock.getWaitingThreads(c);
# Line 1443 | Line 1498 | public class ReentrantReadWriteLockTest
1498      /**
1499       * hasWaiters returns true when a thread is waiting, else false
1500       */
1501 <    public void testHasWaiters() throws InterruptedException {
1502 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1501 >    public void testHasWaiters()      { testHasWaiters(false); }
1502 >    public void testHasWaiters_fair() { testHasWaiters(true); }
1503 >    public void testHasWaiters(boolean fair) {
1504 >        final PublicReentrantReadWriteLock lock =
1505 >            new PublicReentrantReadWriteLock(fair);
1506          final Condition c = lock.writeLock().newCondition();
1507          final CountDownLatch locked = new CountDownLatch(1);
1508          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1459 | Line 1517 | public class ReentrantReadWriteLockTest
1517                  lock.writeLock().unlock();
1518              }});
1519  
1520 <        locked.await();
1520 >        await(locked);
1521          lock.writeLock().lock();
1522          assertHasWaiters(lock, c, t);
1523          assertTrue(lock.hasWaiters(c));
# Line 1474 | Line 1532 | public class ReentrantReadWriteLockTest
1532      /**
1533       * getWaitQueueLength returns number of waiting threads
1534       */
1535 <    public void testGetWaitQueueLength() throws InterruptedException {
1536 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1535 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
1536 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
1537 >    public void testGetWaitQueueLength(boolean fair) {
1538 >        final PublicReentrantReadWriteLock lock =
1539 >            new PublicReentrantReadWriteLock(fair);
1540          final Condition c = lock.writeLock().newCondition();
1541          final CountDownLatch locked = new CountDownLatch(1);
1542          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1487 | Line 1548 | public class ReentrantReadWriteLockTest
1548                  lock.writeLock().unlock();
1549              }});
1550  
1551 <        locked.await();
1551 >        await(locked);
1552          lock.writeLock().lock();
1553          assertHasWaiters(lock, c, t);
1554          assertEquals(1, lock.getWaitQueueLength(c));
# Line 1501 | Line 1562 | public class ReentrantReadWriteLockTest
1562      /**
1563       * getWaitingThreads returns only and all waiting threads
1564       */
1565 <    public void testGetWaitingThreads() throws InterruptedException {
1566 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1565 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
1566 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
1567 >    public void testGetWaitingThreads(boolean fair) {
1568 >        final PublicReentrantReadWriteLock lock =
1569 >            new PublicReentrantReadWriteLock(fair);
1570          final Condition c = lock.writeLock().newCondition();
1571          final CountDownLatch locked1 = new CountDownLatch(1);
1572          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 1529 | Line 1593 | public class ReentrantReadWriteLockTest
1593          lock.writeLock().unlock();
1594  
1595          t1.start();
1596 <        locked1.await();
1596 >        await(locked1);
1597          t2.start();
1598 <        locked2.await();
1598 >        await(locked2);
1599  
1600          lock.writeLock().lock();
1601          assertTrue(lock.hasWaiters(c));
# Line 1551 | Line 1615 | public class ReentrantReadWriteLockTest
1615      /**
1616       * toString indicates current lock state
1617       */
1618 <    public void testToString() {
1619 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1618 >    public void testToString()      { testToString(false); }
1619 >    public void testToString_fair() { testToString(true); }
1620 >    public void testToString(boolean fair) {
1621 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1622          String us = lock.toString();
1623          assertTrue(us.indexOf("Write locks = 0") >= 0);
1624          assertTrue(us.indexOf("Read locks = 0") >= 0);
# Line 1571 | Line 1637 | public class ReentrantReadWriteLockTest
1637      /**
1638       * readLock.toString indicates current lock state
1639       */
1640 <    public void testReadLockToString() {
1641 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1640 >    public void testReadLockToString()      { testReadLockToString(false); }
1641 >    public void testReadLockToString_fair() { testReadLockToString(true); }
1642 >    public void testReadLockToString(boolean fair) {
1643 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1644          String us = lock.readLock().toString();
1645          assertTrue(us.indexOf("Read locks = 0") >= 0);
1646          lock.readLock().lock();
# Line 1584 | Line 1652 | public class ReentrantReadWriteLockTest
1652      /**
1653       * writeLock.toString indicates current lock state
1654       */
1655 <    public void testWriteLockToString() {
1656 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1655 >    public void testWriteLockToString()      { testWriteLockToString(false); }
1656 >    public void testWriteLockToString_fair() { testWriteLockToString(true); }
1657 >    public void testWriteLockToString(boolean fair) {
1658 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1659          String us = lock.writeLock().toString();
1660          assertTrue(us.indexOf("Unlocked") >= 0);
1661          lock.writeLock().lock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines