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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines