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.28 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.69 by jsr166, Wed Dec 31 19:05:43 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines