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.6 by dl, Mon Nov 10 13:32:08 2003 UTC vs.
Revision 1.38 by jsr166, Sat Nov 21 21:59:50 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14   import java.util.*;
15  
16   public class ReentrantReadWriteLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run (suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ReentrantReadWriteLockTest.class);
21 >        return new TestSuite(ReentrantReadWriteLockTest.class);
22      }
23  
24      /**
25       * A runnable calling lockInterruptibly
26       */
27 <    class InterruptibleLockRunnable implements Runnable {
27 >    class InterruptibleLockRunnable extends CheckedRunnable {
28          final ReentrantReadWriteLock lock;
29          InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
30 <        public void run() {
31 <            try {
30 <                lock.writeLock().lockInterruptibly();
31 <            } catch(InterruptedException success){}
30 >        public void realRun() throws InterruptedException {
31 >            lock.writeLock().lockInterruptibly();
32          }
33      }
34  
# Line 37 | Line 37 | public class ReentrantReadWriteLockTest
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 {
45 <                lock.writeLock().lockInterruptibly();
46 <                threadShouldThrow();
47 <            } catch(InterruptedException success){}
43 >        public void realRun() throws InterruptedException {
44 >            lock.writeLock().lockInterruptibly();
45          }
46      }
47  
# Line 53 | 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 >        public Collection<Thread> getQueuedThreads() {
54 >            return super.getQueuedThreads();
55          }
56 <        public WriterConditionObject newCondition() {
57 <            return new PublicCondition(this);
56 >        public Collection<Thread> getWaitingThreads(Condition c) {
57 >            return super.getWaitingThreads(c);
58          }
62
63        static class PublicCondition extends ReentrantReadWriteLock.WriterConditionObject {
64            PublicCondition(PublicReentrantReadWriteLock l) { super(l); }
65            public Collection<Thread> getWaitingThreads() {
66                return super.getWaitingThreads();
67            }
68        }
69
59      }
60  
61      /**
62       * Constructor sets given fairness, and is in unlocked state
63       */
64 <    public void testConstructor() {
65 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
64 >    public void testConstructor() {
65 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
66          assertFalse(rl.isFair());
67          assertFalse(rl.isWriteLocked());
68 <        assertEquals(0, rl.getReadLocks());
69 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
68 >        assertEquals(0, rl.getReadLockCount());
69 >        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
70          assertTrue(r2.isFair());
71          assertFalse(r2.isWriteLocked());
72 <        assertEquals(0, r2.getReadLocks());
72 >        assertEquals(0, r2.getReadLockCount());
73 >        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
74 >        assertFalse(r3.isFair());
75 >        assertFalse(r3.isWriteLocked());
76 >        assertEquals(0, r3.getReadLockCount());
77      }
78  
79      /**
80       * write-locking and read-locking an unlocked lock succeed
81       */
82 <    public void testLock() {
83 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
82 >    public void testLock() {
83 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
87 <        assertEquals(0, rl.getReadLocks());
87 >        assertTrue(rl.writeLock().isHeldByCurrentThread());
88 >        assertEquals(0, rl.getReadLockCount());
89          rl.writeLock().unlock();
90          assertFalse(rl.isWriteLocked());
91          assertFalse(rl.isWriteLockedByCurrentThread());
92 <        assertEquals(0, rl.getReadLocks());
92 >        assertFalse(rl.writeLock().isHeldByCurrentThread());
93 >        assertEquals(0, rl.getReadLockCount());
94          rl.readLock().lock();
95          assertFalse(rl.isWriteLocked());
96          assertFalse(rl.isWriteLockedByCurrentThread());
97 <        assertEquals(1, rl.getReadLocks());
97 >        assertEquals(1, rl.getReadLockCount());
98          rl.readLock().unlock();
99          assertFalse(rl.isWriteLocked());
100          assertFalse(rl.isWriteLockedByCurrentThread());
101 <        assertEquals(0, rl.getReadLocks());
101 >        assertEquals(0, rl.getReadLockCount());
102      }
103  
104  
105      /**
106       * locking an unlocked fair lock succeeds
107       */
108 <    public void testFairLock() {
109 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
108 >    public void testFairLock() {
109 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
110          rl.writeLock().lock();
111          assertTrue(rl.isWriteLocked());
112          assertTrue(rl.isWriteLockedByCurrentThread());
113 <        assertEquals(0, rl.getReadLocks());
113 >        assertTrue(rl.writeLock().isHeldByCurrentThread());
114 >        assertEquals(0, rl.getReadLockCount());
115          rl.writeLock().unlock();
116          assertFalse(rl.isWriteLocked());
117          assertFalse(rl.isWriteLockedByCurrentThread());
118 <        assertEquals(0, rl.getReadLocks());
118 >        assertFalse(rl.writeLock().isHeldByCurrentThread());
119 >        assertEquals(0, rl.getReadLockCount());
120          rl.readLock().lock();
121          assertFalse(rl.isWriteLocked());
122          assertFalse(rl.isWriteLockedByCurrentThread());
123 <        assertEquals(1, rl.getReadLocks());
123 >        assertEquals(1, rl.getReadLockCount());
124          rl.readLock().unlock();
125          assertFalse(rl.isWriteLocked());
126          assertFalse(rl.isWriteLockedByCurrentThread());
127 <        assertEquals(0, rl.getReadLocks());
127 >        assertEquals(0, rl.getReadLockCount());
128      }
129  
130      /**
131       * getWriteHoldCount returns number of recursive holds
132       */
133 <    public void testGetHoldCount() {
134 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 <        for(int i = 1; i <= SIZE; i++) {
136 <            lock.writeLock().lock();
137 <            assertEquals(i,lock.getWriteHoldCount());
141 <        }
142 <        for(int i = SIZE; i > 0; i--) {
143 <            lock.writeLock().unlock();
144 <            assertEquals(i-1,lock.getWriteHoldCount());
145 <        }
146 <    }
147 <    
148 <
149 <    /**
150 <     * write-unlocking an unlocked lock throws IllegalMonitorStateException
151 <     */
152 <    public void testUnlock_IllegalMonitorStateException() {
153 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
154 <        try {
155 <            rl.writeLock().unlock();
156 <            shouldThrow();
157 <        } catch(IllegalMonitorStateException success){}
158 <    }
159 <
160 <
161 <    /**
162 <     * write-lockInterruptibly is interruptible
163 <     */
164 <    public void testWriteLockInterruptibly_Interrupted() {
165 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
166 <        lock.writeLock().lock();
167 <        Thread t = new Thread(new Runnable() {
168 <                public void run() {
169 <                    try {
170 <                        lock.writeLock().lockInterruptibly();
171 <                        threadShouldThrow();
172 <                    } catch(InterruptedException success){}
173 <                }
174 <            });
175 <        try {
176 <            t.start();
177 <            t.interrupt();
178 <            lock.writeLock().unlock();
179 <            t.join();
180 <        } catch(Exception e){
181 <            unexpectedException();
133 >    public void testGetWriteHoldCount() {
134 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 >        for (int i = 1; i <= SIZE; i++) {
136 >            lock.writeLock().lock();
137 >            assertEquals(i,lock.getWriteHoldCount());
138          }
139 <    }
184 <
185 <    /**
186 <     * timed write-trylock is interruptible
187 <     */
188 <    public void testWriteTryLock_Interrupted() {
189 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
190 <        lock.writeLock().lock();
191 <        Thread t = new Thread(new Runnable() {
192 <                public void run() {
193 <                    try {
194 <                        lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
195 <                        threadShouldThrow();
196 <                    } catch(InterruptedException success){}
197 <                }
198 <            });
199 <        try {
200 <            t.start();
201 <            t.interrupt();
139 >        for (int i = SIZE; i > 0; i--) {
140              lock.writeLock().unlock();
141 <            t.join();
204 <        } catch(Exception e){
205 <            unexpectedException();
141 >            assertEquals(i-1,lock.getWriteHoldCount());
142          }
143      }
144  
145      /**
146 <     * read-lockInterruptibly is interruptible
146 >     * WriteLock.getHoldCount returns number of recursive holds
147       */
148 <    public void testReadLockInterruptibly_Interrupted() {
149 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 <        lock.writeLock().lock();
151 <        Thread t = new Thread(new Runnable() {
152 <                public void run() {
153 <                    try {
154 <                        lock.readLock().lockInterruptibly();
219 <                        threadShouldThrow();
220 <                    } catch(InterruptedException success){}
221 <                }
222 <            });
223 <        try {
224 <            t.start();
225 <            t.interrupt();
148 >    public void testGetHoldCount() {
149 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 >        for (int i = 1; i <= SIZE; i++) {
151 >            lock.writeLock().lock();
152 >            assertEquals(i,lock.writeLock().getHoldCount());
153 >        }
154 >        for (int i = SIZE; i > 0; i--) {
155              lock.writeLock().unlock();
156 <            t.join();
228 <        } catch(Exception e){
229 <            unexpectedException();
156 >            assertEquals(i-1,lock.writeLock().getHoldCount());
157          }
158 <    }
158 >    }
159  
160      /**
161 <     * timed read-trylock is interruptible
161 >     * getReadHoldCount returns number of recursive holds
162       */
163 <    public void testReadTryLock_Interrupted() {
164 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 <        lock.writeLock().lock();
166 <        Thread t = new Thread(new Runnable() {
167 <                public void run() {
168 <                    try {
169 <                        lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
170 <                        threadShouldThrow();
171 <                    } catch(InterruptedException success){}
245 <                }
246 <            });
247 <        try {
248 <            t.start();
249 <            t.interrupt();
250 <            t.join();
251 <        } catch(Exception e){
252 <            unexpectedException();
163 >    public void testGetReadHoldCount() {
164 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 >        for (int i = 1; i <= SIZE; i++) {
166 >            lock.readLock().lock();
167 >            assertEquals(i,lock.getReadHoldCount());
168 >        }
169 >        for (int i = SIZE; i > 0; i--) {
170 >            lock.readLock().unlock();
171 >            assertEquals(i-1,lock.getReadHoldCount());
172          }
173      }
174  
175 <    
175 >
176      /**
177 <     * write-trylock fails if locked
177 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
178       */
179 <    public void testWriteTryLockWhenLocked() {
180 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
262 <        lock.writeLock().lock();
263 <        Thread t = new Thread(new Runnable() {
264 <                public void run() {
265 <                    threadAssertFalse(lock.writeLock().tryLock());
266 <                }
267 <            });
179 >    public void testUnlock_IllegalMonitorStateException() {
180 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181          try {
182 <            t.start();
183 <            t.join();
184 <            lock.writeLock().unlock();
185 <        } catch(Exception e){
186 <            unexpectedException();
274 <        }
275 <    }
182 >            rl.writeLock().unlock();
183 >            shouldThrow();
184 >        } catch (IllegalMonitorStateException success) {}
185 >    }
186 >
187  
188      /**
189 <     * read-trylock fails if locked
189 >     * write-lockInterruptibly is interruptible
190       */
191 <    public void testReadTryLockWhenLocked() {
192 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 <        lock.writeLock().lock();
194 <        Thread t = new Thread(new Runnable() {
195 <                public void run() {
196 <                    threadAssertFalse(lock.readLock().tryLock());
197 <                }
198 <            });
199 <        try {
200 <            t.start();
201 <            t.join();
202 <            lock.writeLock().unlock();
203 <        } catch(Exception e){
204 <            unexpectedException();
205 <        }
206 <    }
191 >    public void testWriteLockInterruptibly_Interrupted() throws Exception {
192 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
194 >            public void realRun() throws InterruptedException {
195 >                lock.writeLock().lockInterruptibly();
196 >                lock.writeLock().unlock();
197 >                lock.writeLock().lockInterruptibly();
198 >                lock.writeLock().unlock();
199 >            }});
200 >
201 >        lock.writeLock().lock();
202 >        t.start();
203 >        Thread.sleep(SHORT_DELAY_MS);
204 >        t.interrupt();
205 >        Thread.sleep(SHORT_DELAY_MS);
206 >        lock.writeLock().unlock();
207 >        t.join();
208 >    }
209  
210      /**
211 <     * Multiple threads can hold a read lock when not write-locked
212 <     */
213 <    public void testMultipleReadLocks() {
214 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 <        lock.readLock().lock();
216 <        Thread t = new Thread(new Runnable() {
217 <                public void run() {
218 <                    threadAssertTrue(lock.readLock().tryLock());
219 <                    lock.readLock().unlock();
220 <                }
221 <            });
222 <        try {
223 <            t.start();
224 <            t.join();
225 <            lock.readLock().unlock();
226 <        } catch(Exception e){
314 <            unexpectedException();
315 <        }
316 <    }
211 >     * timed write-tryLock is interruptible
212 >     */
213 >    public void testWriteTryLock_Interrupted() throws InterruptedException {
214 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 >        lock.writeLock().lock();
216 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
217 >            public void realRun() throws InterruptedException {
218 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
219 >            }});
220 >
221 >        t.start();
222 >        Thread.sleep(SHORT_DELAY_MS);
223 >        t.interrupt();
224 >        lock.writeLock().unlock();
225 >        t.join();
226 >    }
227  
228      /**
229 <     * A writelock succeeds after reading threads unlock
229 >     * read-lockInterruptibly is interruptible
230       */
231 <    public void testWriteAfterMultipleReadLocks() {
232 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 <        lock.readLock().lock();
234 <        Thread t1 = new Thread(new Runnable() {
235 <                public void run() {
236 <                    lock.readLock().lock();
237 <                    lock.readLock().unlock();
328 <                }
329 <            });
330 <        Thread t2 = new Thread(new Runnable() {
331 <                public void run() {
332 <                    lock.writeLock().lock();
333 <                    lock.writeLock().unlock();
334 <                }
335 <            });
231 >    public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
232 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 >        lock.writeLock().lock();
234 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
235 >            public void realRun() throws InterruptedException {
236 >                lock.readLock().lockInterruptibly();
237 >            }});
238  
239 <        try {
240 <            t1.start();
241 <            t2.start();
242 <            Thread.sleep(SHORT_DELAY_MS);
243 <            lock.readLock().unlock();
244 <            t1.join(MEDIUM_DELAY_MS);
245 <            t2.join(MEDIUM_DELAY_MS);
344 <            assertTrue(!t1.isAlive());
345 <            assertTrue(!t2.isAlive());
346 <          
347 <        } catch(Exception e){
348 <            unexpectedException();
349 <        }
350 <    }
239 >        t.start();
240 >        Thread.sleep(SHORT_DELAY_MS);
241 >        t.interrupt();
242 >        Thread.sleep(SHORT_DELAY_MS);
243 >        lock.writeLock().unlock();
244 >        t.join();
245 >    }
246  
247      /**
248 <     * Readlocks succeed after a writing thread unlocks
248 >     * timed read-tryLock is interruptible
249       */
250 <    public void testReadAfterWriteLock() {
251 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 <        lock.writeLock().lock();
253 <        Thread t1 = new Thread(new Runnable() {
254 <                public void run() {
255 <                    lock.readLock().lock();
256 <                    lock.readLock().unlock();
362 <                }
363 <            });
364 <        Thread t2 = new Thread(new Runnable() {
365 <                public void run() {
366 <                    lock.readLock().lock();
367 <                    lock.readLock().unlock();
368 <                }
369 <            });
250 >    public void testReadTryLock_Interrupted() throws InterruptedException {
251 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 >        lock.writeLock().lock();
253 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 >            public void realRun() throws InterruptedException {
255 >                lock.readLock().tryLock(1000,MILLISECONDS);
256 >            }});
257  
258 <        try {
259 <            t1.start();
260 <            t2.start();
261 <            Thread.sleep(SHORT_DELAY_MS);
375 <            lock.writeLock().unlock();
376 <            t1.join(MEDIUM_DELAY_MS);
377 <            t2.join(MEDIUM_DELAY_MS);
378 <            assertTrue(!t1.isAlive());
379 <            assertTrue(!t2.isAlive());
380 <          
381 <        } catch(Exception e){
382 <            unexpectedException();
383 <        }
384 <    }
258 >        t.start();
259 >        t.interrupt();
260 >        t.join();
261 >    }
262  
263  
264      /**
265 <     * Read trylock succeeds if readlocked but not writelocked
265 >     * write-tryLock fails if locked
266       */
267 <    public void testTryLockWhenReadLocked() {
268 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269 <        lock.readLock().lock();
270 <        Thread t = new Thread(new Runnable() {
271 <                public void run() {
272 <                    threadAssertTrue(lock.readLock().tryLock());
273 <                    lock.readLock().unlock();
397 <                }
398 <            });
399 <        try {
400 <            t.start();
401 <            t.join();
402 <            lock.readLock().unlock();
403 <        } catch(Exception e){
404 <            unexpectedException();
405 <        }
406 <    }
267 >    public void testWriteTryLockWhenLocked() throws InterruptedException {
268 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269 >        lock.writeLock().lock();
270 >        Thread t = new Thread(new CheckedRunnable() {
271 >            public void realRun() {
272 >                threadAssertFalse(lock.writeLock().tryLock());
273 >            }});
274  
275 <    
275 >        t.start();
276 >        t.join();
277 >        lock.writeLock().unlock();
278 >    }
279  
280      /**
281 <     * write trylock fails when readlocked
281 >     * read-tryLock fails if locked
282       */
283 <    public void testWriteTryLockWhenReadLocked() {
284 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 <        lock.readLock().lock();
286 <        Thread t = new Thread(new Runnable() {
287 <                public void run() {
288 <                    threadAssertFalse(lock.writeLock().tryLock());
289 <                }
420 <            });
421 <        try {
422 <            t.start();
423 <            t.join();
424 <            lock.readLock().unlock();
425 <        } catch(Exception e){
426 <            unexpectedException();
427 <        }
428 <    }
283 >    public void testReadTryLockWhenLocked() throws InterruptedException {
284 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 >        lock.writeLock().lock();
286 >        Thread t = new Thread(new CheckedRunnable() {
287 >            public void realRun() {
288 >                threadAssertFalse(lock.readLock().tryLock());
289 >            }});
290  
291 <    
291 >        t.start();
292 >        t.join();
293 >        lock.writeLock().unlock();
294 >    }
295  
296      /**
297 <     * write timed trylock times out if locked
297 >     * Multiple threads can hold a read lock when not write-locked
298       */
299 <    public void testWriteTryLock_Timeout() {
300 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
301 <        lock.writeLock().lock();
302 <        Thread t = new Thread(new Runnable() {
303 <                public void run() {
304 <                    try {
305 <                        threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
306 <                    } catch (Exception ex) {
307 <                        threadUnexpectedException();
308 <                    }
309 <                }
310 <            });
311 <        try {
448 <            t.start();
449 <            t.join();
450 <            lock.writeLock().unlock();
451 <        } catch(Exception e){
452 <            unexpectedException();
453 <        }
454 <    }
299 >    public void testMultipleReadLocks() throws InterruptedException {
300 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
301 >        lock.readLock().lock();
302 >        Thread t = new Thread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                threadAssertTrue(lock.readLock().tryLock());
305 >                lock.readLock().unlock();
306 >            }});
307 >
308 >        t.start();
309 >        t.join();
310 >        lock.readLock().unlock();
311 >    }
312  
313      /**
314 <     * read timed trylock times out if write-locked
314 >     * A writelock succeeds after reading threads unlock
315       */
316 <    public void testReadTryLock_Timeout() {
317 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
318 <        lock.writeLock().lock();
319 <        Thread t = new Thread(new Runnable() {
320 <                public void run() {
321 <                    try {
322 <                        threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
323 <                    } catch (Exception ex) {
324 <                        threadUnexpectedException();
325 <                    }
326 <                }
327 <            });
328 <        try {
329 <            t.start();
330 <            t.join();
331 <            lock.writeLock().unlock();
332 <        } catch(Exception e){
333 <            unexpectedException();
334 <        }
335 <    }
316 >    public void testWriteAfterMultipleReadLocks() throws InterruptedException {
317 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
318 >        lock.readLock().lock();
319 >        Thread t1 = new Thread(new CheckedRunnable() {
320 >            public void realRun() {
321 >                lock.readLock().lock();
322 >                lock.readLock().unlock();
323 >            }});
324 >        Thread t2 = new Thread(new CheckedRunnable() {
325 >            public void realRun() {
326 >                lock.writeLock().lock();
327 >                lock.writeLock().unlock();
328 >            }});
329 >
330 >        t1.start();
331 >        t2.start();
332 >        Thread.sleep(SHORT_DELAY_MS);
333 >        lock.readLock().unlock();
334 >        t1.join(MEDIUM_DELAY_MS);
335 >        t2.join(MEDIUM_DELAY_MS);
336 >        assertTrue(!t1.isAlive());
337 >        assertTrue(!t2.isAlive());
338 >    }
339 >
340 >    /**
341 >     * Readlocks succeed after a writing thread unlocks
342 >     */
343 >    public void testReadAfterWriteLock() throws InterruptedException {
344 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
345 >        lock.writeLock().lock();
346 >        Thread t1 = new Thread(new CheckedRunnable() {
347 >            public void realRun() {
348 >                lock.readLock().lock();
349 >                lock.readLock().unlock();
350 >            }});
351 >        Thread t2 = new Thread(new CheckedRunnable() {
352 >            public void realRun() {
353 >                lock.readLock().lock();
354 >                lock.readLock().unlock();
355 >            }});
356 >
357 >        t1.start();
358 >        t2.start();
359 >        Thread.sleep(SHORT_DELAY_MS);
360 >        lock.writeLock().unlock();
361 >        t1.join(MEDIUM_DELAY_MS);
362 >        t2.join(MEDIUM_DELAY_MS);
363 >        assertTrue(!t1.isAlive());
364 >        assertTrue(!t2.isAlive());
365 >    }
366 >
367 >    /**
368 >     * Read trylock succeeds if write locked by current thread
369 >     */
370 >    public void testReadHoldingWriteLock() {
371 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
372 >        lock.writeLock().lock();
373 >        assertTrue(lock.readLock().tryLock());
374 >        lock.readLock().unlock();
375 >        lock.writeLock().unlock();
376 >    }
377 >
378 >    /**
379 >     * Read lock succeeds if write locked by current thread even if
380 >     * other threads are waiting for readlock
381 >     */
382 >    public void testReadHoldingWriteLock2() throws InterruptedException {
383 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 >        lock.writeLock().lock();
385 >        Thread t1 = new Thread(new CheckedRunnable() {
386 >            public void realRun() {
387 >                lock.readLock().lock();
388 >                lock.readLock().unlock();
389 >            }});
390 >        Thread t2 = new Thread(new CheckedRunnable() {
391 >            public void realRun() {
392 >                lock.readLock().lock();
393 >                lock.readLock().unlock();
394 >            }});
395 >
396 >        t1.start();
397 >        t2.start();
398 >        lock.readLock().lock();
399 >        lock.readLock().unlock();
400 >        Thread.sleep(SHORT_DELAY_MS);
401 >        lock.readLock().lock();
402 >        lock.readLock().unlock();
403 >        lock.writeLock().unlock();
404 >        t1.join(MEDIUM_DELAY_MS);
405 >        t2.join(MEDIUM_DELAY_MS);
406 >        assertTrue(!t1.isAlive());
407 >        assertTrue(!t2.isAlive());
408 >    }
409 >
410 >    /**
411 >     *  Read lock succeeds if write locked by current thread even if
412 >     * other threads are waiting for writelock
413 >     */
414 >    public void testReadHoldingWriteLock3() throws InterruptedException {
415 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
416 >        lock.writeLock().lock();
417 >        Thread t1 = new Thread(new CheckedRunnable() {
418 >            public void realRun() {
419 >                lock.writeLock().lock();
420 >                lock.writeLock().unlock();
421 >            }});
422 >        Thread t2 = new Thread(new CheckedRunnable() {
423 >            public void realRun() {
424 >                lock.writeLock().lock();
425 >                lock.writeLock().unlock();
426 >            }});
427 >
428 >        t1.start();
429 >        t2.start();
430 >        lock.readLock().lock();
431 >        lock.readLock().unlock();
432 >        Thread.sleep(SHORT_DELAY_MS);
433 >        lock.readLock().lock();
434 >        lock.readLock().unlock();
435 >        lock.writeLock().unlock();
436 >        t1.join(MEDIUM_DELAY_MS);
437 >        t2.join(MEDIUM_DELAY_MS);
438 >        assertTrue(!t1.isAlive());
439 >        assertTrue(!t2.isAlive());
440 >    }
441 >
442 >
443 >    /**
444 >     *  Write lock succeeds if write locked by current thread even if
445 >     * other threads are waiting for writelock
446 >     */
447 >    public void testWriteHoldingWriteLock4() throws InterruptedException {
448 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
449 >        lock.writeLock().lock();
450 >        Thread t1 = new Thread(new CheckedRunnable() {
451 >            public void realRun() {
452 >                lock.writeLock().lock();
453 >                lock.writeLock().unlock();
454 >            }});
455 >        Thread t2 = new Thread(new CheckedRunnable() {
456 >            public void realRun() {
457 >                lock.writeLock().lock();
458 >                lock.writeLock().unlock();
459 >            }});
460 >
461 >        t1.start();
462 >        t2.start();
463 >        lock.writeLock().lock();
464 >        lock.writeLock().unlock();
465 >        Thread.sleep(SHORT_DELAY_MS);
466 >        lock.writeLock().lock();
467 >        lock.writeLock().unlock();
468 >        lock.writeLock().unlock();
469 >        t1.join(MEDIUM_DELAY_MS);
470 >        t2.join(MEDIUM_DELAY_MS);
471 >        assertTrue(!t1.isAlive());
472 >        assertTrue(!t2.isAlive());
473 >    }
474 >
475 >
476 >    /**
477 >     * Fair Read trylock succeeds if write locked by current thread
478 >     */
479 >    public void testReadHoldingWriteLockFair() {
480 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
481 >        lock.writeLock().lock();
482 >        assertTrue(lock.readLock().tryLock());
483 >        lock.readLock().unlock();
484 >        lock.writeLock().unlock();
485 >    }
486 >
487 >    /**
488 >     * Fair Read lock succeeds if write locked by current thread even if
489 >     * other threads are waiting for readlock
490 >     */
491 >    public void testReadHoldingWriteLockFair2() throws InterruptedException {
492 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
493 >        lock.writeLock().lock();
494 >        Thread t1 = new Thread(new CheckedRunnable() {
495 >            public void realRun() {
496 >                lock.readLock().lock();
497 >                lock.readLock().unlock();
498 >            }});
499 >        Thread t2 = new Thread(new CheckedRunnable() {
500 >            public void realRun() {
501 >                lock.readLock().lock();
502 >                lock.readLock().unlock();
503 >            }});
504 >
505 >        t1.start();
506 >        t2.start();
507 >        lock.readLock().lock();
508 >        lock.readLock().unlock();
509 >        Thread.sleep(SHORT_DELAY_MS);
510 >        lock.readLock().lock();
511 >        lock.readLock().unlock();
512 >        lock.writeLock().unlock();
513 >        t1.join(MEDIUM_DELAY_MS);
514 >        t2.join(MEDIUM_DELAY_MS);
515 >        assertTrue(!t1.isAlive());
516 >        assertTrue(!t2.isAlive());
517 >    }
518 >
519 >
520 >    /**
521 >     * Fair Read lock succeeds if write locked by current thread even if
522 >     * other threads are waiting for writelock
523 >     */
524 >    public void testReadHoldingWriteLockFair3() throws InterruptedException {
525 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
526 >        lock.writeLock().lock();
527 >        Thread t1 = new Thread(new CheckedRunnable() {
528 >            public void realRun() {
529 >                lock.writeLock().lock();
530 >                lock.writeLock().unlock();
531 >            }});
532 >        Thread t2 = new Thread(new CheckedRunnable() {
533 >            public void realRun() {
534 >                lock.writeLock().lock();
535 >                lock.writeLock().unlock();
536 >            }});
537 >
538 >        t1.start();
539 >        t2.start();
540 >        lock.readLock().lock();
541 >        lock.readLock().unlock();
542 >        Thread.sleep(SHORT_DELAY_MS);
543 >        lock.readLock().lock();
544 >        lock.readLock().unlock();
545 >        lock.writeLock().unlock();
546 >        t1.join(MEDIUM_DELAY_MS);
547 >        t2.join(MEDIUM_DELAY_MS);
548 >        assertTrue(!t1.isAlive());
549 >        assertTrue(!t2.isAlive());
550 >    }
551 >
552 >
553 >    /**
554 >     * Fair Write lock succeeds if write locked by current thread even if
555 >     * other threads are waiting for writelock
556 >     */
557 >    public void testWriteHoldingWriteLockFair4() throws InterruptedException {
558 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
559 >        lock.writeLock().lock();
560 >        Thread t1 = new Thread(new CheckedRunnable() {
561 >            public void realRun() {
562 >                lock.writeLock().lock();
563 >                lock.writeLock().unlock();
564 >            }});
565 >        Thread t2 = new Thread(new CheckedRunnable() {
566 >            public void realRun() {
567 >                lock.writeLock().lock();
568 >                lock.writeLock().unlock();
569 >            }});
570 >
571 >        t1.start();
572 >        t2.start();
573 >        Thread.sleep(SHORT_DELAY_MS);
574 >        assertTrue(lock.isWriteLockedByCurrentThread());
575 >        assertTrue(lock.getWriteHoldCount() == 1);
576 >        lock.writeLock().lock();
577 >        assertTrue(lock.getWriteHoldCount() == 2);
578 >        lock.writeLock().unlock();
579 >        lock.writeLock().lock();
580 >        lock.writeLock().unlock();
581 >        lock.writeLock().unlock();
582 >        t1.join(MEDIUM_DELAY_MS);
583 >        t2.join(MEDIUM_DELAY_MS);
584 >        assertTrue(!t1.isAlive());
585 >        assertTrue(!t2.isAlive());
586 >    }
587 >
588 >
589 >    /**
590 >     * Read tryLock succeeds if readlocked but not writelocked
591 >     */
592 >    public void testTryLockWhenReadLocked() throws InterruptedException {
593 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
594 >        lock.readLock().lock();
595 >        Thread t = new Thread(new CheckedRunnable() {
596 >            public void realRun() {
597 >                threadAssertTrue(lock.readLock().tryLock());
598 >                lock.readLock().unlock();
599 >            }});
600 >
601 >        t.start();
602 >        t.join();
603 >        lock.readLock().unlock();
604 >    }
605 >
606 >
607 >
608 >    /**
609 >     * write tryLock fails when readlocked
610 >     */
611 >    public void testWriteTryLockWhenReadLocked() throws InterruptedException {
612 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
613 >        lock.readLock().lock();
614 >        Thread t = new Thread(new CheckedRunnable() {
615 >            public void realRun() {
616 >                threadAssertFalse(lock.writeLock().tryLock());
617 >            }});
618 >
619 >        t.start();
620 >        t.join();
621 >        lock.readLock().unlock();
622 >    }
623 >
624 >
625 >    /**
626 >     * Fair Read tryLock succeeds if readlocked but not writelocked
627 >     */
628 >    public void testTryLockWhenReadLockedFair() throws InterruptedException {
629 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
630 >        lock.readLock().lock();
631 >        Thread t = new Thread(new CheckedRunnable() {
632 >            public void realRun() {
633 >                threadAssertTrue(lock.readLock().tryLock());
634 >                lock.readLock().unlock();
635 >            }});
636 >
637 >        t.start();
638 >        t.join();
639 >        lock.readLock().unlock();
640 >    }
641 >
642 >
643 >
644 >    /**
645 >     * Fair write tryLock fails when readlocked
646 >     */
647 >    public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
648 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 >        lock.readLock().lock();
650 >        Thread t = new Thread(new CheckedRunnable() {
651 >            public void realRun() {
652 >                threadAssertFalse(lock.writeLock().tryLock());
653 >            }});
654 >
655 >        t.start();
656 >        t.join();
657 >        lock.readLock().unlock();
658 >    }
659 >
660 >
661 >
662 >    /**
663 >     * write timed tryLock times out if locked
664 >     */
665 >    public void testWriteTryLock_Timeout() throws InterruptedException {
666 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
667 >        lock.writeLock().lock();
668 >        Thread t = new Thread(new CheckedRunnable() {
669 >            public void realRun() throws InterruptedException {
670 >                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
671 >            }});
672 >
673 >        t.start();
674 >        t.join();
675 >        assertTrue(lock.writeLock().isHeldByCurrentThread());
676 >        lock.writeLock().unlock();
677 >    }
678 >
679 >    /**
680 >     * read timed tryLock times out if write-locked
681 >     */
682 >    public void testReadTryLock_Timeout() throws InterruptedException {
683 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
684 >        lock.writeLock().lock();
685 >        Thread t = new Thread(new CheckedRunnable() {
686 >            public void realRun() throws InterruptedException {
687 >                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
688 >            }});
689 >
690 >        t.start();
691 >        t.join();
692 >        assertTrue(lock.writeLock().isHeldByCurrentThread());
693 >        lock.writeLock().unlock();
694 >    }
695  
696  
697      /**
698       * write lockInterruptibly succeeds if lock free else is interruptible
699       */
700 <    public void testWriteLockInterruptibly() {
701 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
702 <        try {
703 <            lock.writeLock().lockInterruptibly();
704 <        } catch(Exception e) {
705 <            unexpectedException();
706 <        }
707 <        Thread t = new Thread(new Runnable() {
708 <                public void run() {
709 <                    try {
710 <                        lock.writeLock().lockInterruptibly();
711 <                        threadShouldThrow();
712 <                    }
713 <                    catch(InterruptedException success) {
498 <                    }
499 <                }
500 <            });
501 <        try {
502 <            t.start();
503 <            t.interrupt();
504 <            t.join();
505 <            lock.writeLock().unlock();
506 <        } catch(Exception e){
507 <            unexpectedException();
508 <        }
700 >    public void testWriteLockInterruptibly() throws InterruptedException {
701 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
702 >        lock.writeLock().lockInterruptibly();
703 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
704 >            public void realRun() throws InterruptedException {
705 >                lock.writeLock().lockInterruptibly();
706 >            }});
707 >
708 >        t.start();
709 >        Thread.sleep(SHORT_DELAY_MS);
710 >        t.interrupt();
711 >        Thread.sleep(SHORT_DELAY_MS);
712 >        t.join();
713 >        lock.writeLock().unlock();
714      }
715  
716      /**
717       *  read lockInterruptibly succeeds if lock free else is interruptible
718       */
719 <    public void testReadLockInterruptibly() {
720 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
721 <        try {
722 <            lock.writeLock().lockInterruptibly();
723 <        } catch(Exception e) {
724 <            unexpectedException();
725 <        }
726 <        Thread t = new Thread(new Runnable() {
727 <                public void run() {
728 <                    try {
729 <                        lock.readLock().lockInterruptibly();
730 <                        threadShouldThrow();
731 <                    }
527 <                    catch(InterruptedException success) {
528 <                    }
529 <                }
530 <            });
531 <        try {
532 <            t.start();
533 <            t.interrupt();
534 <            t.join();
535 <            lock.writeLock().unlock();
536 <        } catch(Exception e){
537 <            unexpectedException();
538 <        }
719 >    public void testReadLockInterruptibly() throws InterruptedException {
720 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
721 >        lock.writeLock().lockInterruptibly();
722 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
723 >            public void realRun() throws InterruptedException {
724 >                lock.readLock().lockInterruptibly();
725 >            }});
726 >
727 >        t.start();
728 >        Thread.sleep(SHORT_DELAY_MS);
729 >        t.interrupt();
730 >        t.join();
731 >        lock.writeLock().unlock();
732      }
733  
734      /**
735       * Calling await without holding lock throws IllegalMonitorStateException
736       */
737 <    public void testAwait_IllegalMonitor() {
738 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
737 >    public void testAwait_IllegalMonitor() throws InterruptedException {
738 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739          final Condition c = lock.writeLock().newCondition();
740          try {
741              c.await();
742              shouldThrow();
743 <        }
551 <        catch (IllegalMonitorStateException success) {
552 <        }
553 <        catch (Exception ex) {
554 <            shouldThrow();
555 <        }
743 >        } catch (IllegalMonitorStateException success) {}
744      }
745  
746      /**
747       * Calling signal without holding lock throws IllegalMonitorStateException
748       */
749      public void testSignal_IllegalMonitor() {
750 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
750 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
751          final Condition c = lock.writeLock().newCondition();
752          try {
753              c.signal();
754              shouldThrow();
755 <        }
568 <        catch (IllegalMonitorStateException success) {
569 <        }
570 <        catch (Exception ex) {
571 <            unexpectedException();
572 <        }
755 >        } catch (IllegalMonitorStateException success) {}
756      }
757  
758      /**
759       * awaitNanos without a signal times out
760       */
761 <    public void testAwaitNanos_Timeout() {
762 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
761 >    public void testAwaitNanos_Timeout() throws InterruptedException {
762 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
763          final Condition c = lock.writeLock().newCondition();
764 <        try {
765 <            lock.writeLock().lock();
766 <            long t = c.awaitNanos(100);
767 <            assertTrue(t <= 0);
768 <            lock.writeLock().unlock();
586 <        }
587 <        catch (Exception ex) {
588 <            unexpectedException();
589 <        }
764 >
765 >        lock.writeLock().lock();
766 >        long t = c.awaitNanos(100);
767 >        assertTrue(t <= 0);
768 >        lock.writeLock().unlock();
769      }
770  
771  
772      /**
773       *  timed await without a signal times out
774       */
775 <    public void testAwait_Timeout() {
776 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
775 >    public void testAwait_Timeout() throws InterruptedException {
776 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
777          final Condition c = lock.writeLock().newCondition();
778 <        try {
779 <            lock.writeLock().lock();
780 <            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
602 <            lock.writeLock().unlock();
603 <        }
604 <        catch (Exception ex) {
605 <            unexpectedException();
606 <        }
778 >        lock.writeLock().lock();
779 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
780 >        lock.writeLock().unlock();
781      }
782  
783      /**
784       * awaitUntil without a signal times out
785       */
786 <    public void testAwaitUntil_Timeout() {
787 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
786 >    public void testAwaitUntil_Timeout() throws InterruptedException {
787 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
788          final Condition c = lock.writeLock().newCondition();
789 <        try {
790 <            lock.writeLock().lock();
791 <            java.util.Date d = new java.util.Date();
792 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
619 <            lock.writeLock().unlock();
620 <        }
621 <        catch (Exception ex) {
622 <            unexpectedException();
623 <        }
789 >        lock.writeLock().lock();
790 >        java.util.Date d = new java.util.Date();
791 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
792 >        lock.writeLock().unlock();
793      }
794  
795      /**
796       * await returns when signalled
797       */
798 <    public void testAwait() {
799 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
798 >    public void testAwait() throws InterruptedException {
799 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
800          final Condition c = lock.writeLock().newCondition();
801 <        Thread t = new Thread(new Runnable() {
802 <                public void run() {
803 <                    try {
804 <                        lock.writeLock().lock();
805 <                        c.await();
806 <                        lock.writeLock().unlock();
807 <                    }
808 <                    catch(InterruptedException e) {
809 <                        threadUnexpectedException();
810 <                    }
811 <                }
812 <            });
801 >        Thread t = new Thread(new CheckedRunnable() {
802 >            public void realRun() throws InterruptedException {
803 >                lock.writeLock().lock();
804 >                c.await();
805 >                lock.writeLock().unlock();
806 >            }});
807 >
808 >        t.start();
809 >        Thread.sleep(SHORT_DELAY_MS);
810 >        lock.writeLock().lock();
811 >        c.signal();
812 >        lock.writeLock().unlock();
813 >        t.join(SHORT_DELAY_MS);
814 >        assertFalse(t.isAlive());
815 >    }
816 >
817 >    /** A helper class for uninterruptible wait tests */
818 >    class UninterruptableThread extends Thread {
819 >        private Lock lock;
820 >        private Condition c;
821 >
822 >        public volatile boolean canAwake = false;
823 >        public volatile boolean interrupted = false;
824 >        public volatile boolean lockStarted = false;
825 >
826 >        public UninterruptableThread(Lock lock, Condition c) {
827 >            this.lock = lock;
828 >            this.c = c;
829 >        }
830 >
831 >        public synchronized void run() {
832 >            lock.lock();
833 >            lockStarted = true;
834  
835 <        try {
836 <            t.start();
837 <            Thread.sleep(SHORT_DELAY_MS);
838 <            lock.writeLock().lock();
839 <            c.signal();
840 <            lock.writeLock().unlock();
651 <            t.join(SHORT_DELAY_MS);
652 <            assertFalse(t.isAlive());
653 <        }
654 <        catch (Exception ex) {
655 <            unexpectedException();
835 >            while (!canAwake) {
836 >                c.awaitUninterruptibly();
837 >            }
838 >
839 >            interrupted = isInterrupted();
840 >            lock.unlock();
841          }
842      }
843  
844      /**
845       * awaitUninterruptibly doesn't abort on interrupt
846       */
847 <    public void testAwaitUninterruptibly() {
848 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
847 >    public void testAwaitUninterruptibly() throws InterruptedException {
848 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
849          final Condition c = lock.writeLock().newCondition();
850 <        Thread t = new Thread(new Runnable() {
851 <                public void run() {
852 <                    lock.writeLock().lock();
853 <                    c.awaitUninterruptibly();
854 <                    lock.writeLock().unlock();
855 <                }
856 <            });
850 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
851 >
852 >        thread.start();
853 >
854 >        while (!thread.lockStarted) {
855 >            Thread.sleep(100);
856 >        }
857  
858 +        lock.writeLock().lock();
859          try {
860 <            t.start();
861 <            Thread.sleep(SHORT_DELAY_MS);
676 <            t.interrupt();
677 <            lock.writeLock().lock();
860 >            thread.interrupt();
861 >            thread.canAwake = true;
862              c.signal();
863 +        } finally {
864              lock.writeLock().unlock();
680            assert(t.isInterrupted());
681            t.join(SHORT_DELAY_MS);
682            assertFalse(t.isAlive());
683        }
684        catch (Exception ex) {
685            unexpectedException();
865          }
866 +
867 +        thread.join();
868 +        assertTrue(thread.interrupted);
869 +        assertFalse(thread.isAlive());
870      }
871  
872      /**
873       * await is interruptible
874       */
875 <    public void testAwait_Interrupt() {
876 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
875 >    public void testAwait_Interrupt() throws InterruptedException {
876 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
877          final Condition c = lock.writeLock().newCondition();
878 <        Thread t = new Thread(new Runnable() {
879 <                public void run() {
880 <                    try {
881 <                        lock.writeLock().lock();
882 <                        c.await();
883 <                        lock.writeLock().unlock();
884 <                        threadShouldThrow();
885 <                    }
886 <                    catch(InterruptedException success) {
887 <                    }
888 <                }
889 <            });
707 <
708 <        try {
709 <            t.start();
710 <            Thread.sleep(SHORT_DELAY_MS);
711 <            t.interrupt();
712 <            t.join(SHORT_DELAY_MS);
713 <            assertFalse(t.isAlive());
714 <        }
715 <        catch (Exception ex) {
716 <            unexpectedException();
717 <        }
878 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
879 >            public void realRun() throws InterruptedException {
880 >                lock.writeLock().lock();
881 >                c.await();
882 >                lock.writeLock().unlock();
883 >            }});
884 >
885 >        t.start();
886 >        Thread.sleep(SHORT_DELAY_MS);
887 >        t.interrupt();
888 >        t.join(SHORT_DELAY_MS);
889 >        assertFalse(t.isAlive());
890      }
891  
892      /**
893       * awaitNanos is interruptible
894       */
895 <    public void testAwaitNanos_Interrupt() {
896 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
895 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
896 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
897          final Condition c = lock.writeLock().newCondition();
898 <        Thread t = new Thread(new Runnable() {
899 <                public void run() {
900 <                    try {
901 <                        lock.writeLock().lock();
902 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
903 <                        lock.writeLock().unlock();
904 <                        threadShouldThrow();
905 <                    }
906 <                    catch(InterruptedException success) {
907 <                    }
908 <                }
909 <            });
738 <
739 <        try {
740 <            t.start();
741 <            Thread.sleep(SHORT_DELAY_MS);
742 <            t.interrupt();
743 <            t.join(SHORT_DELAY_MS);
744 <            assertFalse(t.isAlive());
745 <        }
746 <        catch (Exception ex) {
747 <            unexpectedException();
748 <        }
898 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
899 >            public void realRun() throws InterruptedException {
900 >                lock.writeLock().lock();
901 >                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
902 >                lock.writeLock().unlock();
903 >            }});
904 >
905 >        t.start();
906 >        Thread.sleep(SHORT_DELAY_MS);
907 >        t.interrupt();
908 >        t.join(SHORT_DELAY_MS);
909 >        assertFalse(t.isAlive());
910      }
911  
912      /**
913       * awaitUntil is interruptible
914       */
915 <    public void testAwaitUntil_Interrupt() {
916 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
915 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
916 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
917          final Condition c = lock.writeLock().newCondition();
918 <        Thread t = new Thread(new Runnable() {
919 <                public void run() {
920 <                    try {
921 <                        lock.writeLock().lock();
922 <                        java.util.Date d = new java.util.Date();
923 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
924 <                        lock.writeLock().unlock();
925 <                        threadShouldThrow();
926 <                    }
927 <                    catch(InterruptedException success) {
928 <                    }
929 <                }
930 <            });
770 <
771 <        try {
772 <            t.start();
773 <            Thread.sleep(SHORT_DELAY_MS);
774 <            t.interrupt();
775 <            t.join(SHORT_DELAY_MS);
776 <            assertFalse(t.isAlive());
777 <        }
778 <        catch (Exception ex) {
779 <            unexpectedException();
780 <        }
918 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
919 >            public void realRun() throws InterruptedException {
920 >                lock.writeLock().lock();
921 >                java.util.Date d = new java.util.Date();
922 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
923 >                lock.writeLock().unlock();
924 >            }});
925 >
926 >        t.start();
927 >        Thread.sleep(SHORT_DELAY_MS);
928 >        t.interrupt();
929 >        t.join(SHORT_DELAY_MS);
930 >        assertFalse(t.isAlive());
931      }
932  
933      /**
934       * signalAll wakes up all threads
935       */
936 <    public void testSignalAll() {
937 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
936 >    public void testSignalAll() throws InterruptedException {
937 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938          final Condition c = lock.writeLock().newCondition();
939 <        Thread t1 = new Thread(new Runnable() {
940 <                public void run() {
941 <                    try {
942 <                        lock.writeLock().lock();
943 <                        c.await();
944 <                        lock.writeLock().unlock();
945 <                    }
946 <                    catch(InterruptedException e) {
947 <                        threadUnexpectedException();
948 <                    }
949 <                }
950 <            });
951 <
952 <        Thread t2 = new Thread(new Runnable() {
953 <                public void run() {
954 <                    try {
955 <                        lock.writeLock().lock();
956 <                        c.await();
957 <                        lock.writeLock().unlock();
958 <                    }
959 <                    catch(InterruptedException e) {
960 <                        threadUnexpectedException();
961 <                    }
962 <                }
813 <            });
814 <
815 <        try {
816 <            t1.start();
817 <            t2.start();
818 <            Thread.sleep(SHORT_DELAY_MS);
819 <            lock.writeLock().lock();
820 <            c.signalAll();
821 <            lock.writeLock().unlock();
822 <            t1.join(SHORT_DELAY_MS);
823 <            t2.join(SHORT_DELAY_MS);
824 <            assertFalse(t1.isAlive());
825 <            assertFalse(t2.isAlive());
826 <        }
827 <        catch (Exception ex) {
828 <            unexpectedException();
829 <        }
939 >        Thread t1 = new Thread(new CheckedRunnable() {
940 >            public void realRun() throws InterruptedException {
941 >                lock.writeLock().lock();
942 >                c.await();
943 >                lock.writeLock().unlock();
944 >            }});
945 >
946 >        Thread t2 = new Thread(new CheckedRunnable() {
947 >            public void realRun() throws InterruptedException {
948 >                lock.writeLock().lock();
949 >                c.await();
950 >                lock.writeLock().unlock();
951 >            }});
952 >
953 >        t1.start();
954 >        t2.start();
955 >        Thread.sleep(SHORT_DELAY_MS);
956 >        lock.writeLock().lock();
957 >        c.signalAll();
958 >        lock.writeLock().unlock();
959 >        t1.join(SHORT_DELAY_MS);
960 >        t2.join(SHORT_DELAY_MS);
961 >        assertFalse(t1.isAlive());
962 >        assertFalse(t2.isAlive());
963      }
964  
965      /**
966       * A serialized lock deserializes as unlocked
967       */
968 <    public void testSerialization() {
968 >    public void testSerialization() throws Exception {
969          ReentrantReadWriteLock l = new ReentrantReadWriteLock();
970          l.readLock().lock();
971          l.readLock().unlock();
972  
973 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
974 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
975 +        out.writeObject(l);
976 +        out.close();
977 +
978 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
979 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
980 +        ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
981 +        r.readLock().lock();
982 +        r.readLock().unlock();
983 +    }
984 +
985 +    /**
986 +     * hasQueuedThreads reports whether there are waiting threads
987 +     */
988 +    public void testhasQueuedThreads() throws InterruptedException {
989 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
990 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
991 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
992 +        assertFalse(lock.hasQueuedThreads());
993 +        lock.writeLock().lock();
994 +        t1.start();
995 +        Thread.sleep(SHORT_DELAY_MS);
996 +        assertTrue(lock.hasQueuedThreads());
997 +        t2.start();
998 +        Thread.sleep(SHORT_DELAY_MS);
999 +        assertTrue(lock.hasQueuedThreads());
1000 +        t1.interrupt();
1001 +        Thread.sleep(SHORT_DELAY_MS);
1002 +        assertTrue(lock.hasQueuedThreads());
1003 +        lock.writeLock().unlock();
1004 +        Thread.sleep(SHORT_DELAY_MS);
1005 +        assertFalse(lock.hasQueuedThreads());
1006 +        t1.join();
1007 +        t2.join();
1008 +    }
1009 +
1010 +    /**
1011 +     * hasQueuedThread(null) throws NPE
1012 +     */
1013 +    public void testHasQueuedThreadNPE() {
1014 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1015          try {
1016 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1017 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1018 <            out.writeObject(l);
844 <            out.close();
845 <
846 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
847 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
848 <            ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
849 <            r.readLock().lock();
850 <            r.readLock().unlock();
851 <        } catch(Exception e){
852 <            e.printStackTrace();
853 <            unexpectedException();
854 <        }
1016 >            sync.hasQueuedThread(null);
1017 >            shouldThrow();
1018 >        } catch (NullPointerException success) {}
1019      }
1020  
1021      /**
1022 +     * hasQueuedThread reports whether a thread is queued.
1023 +     */
1024 +    public void testHasQueuedThread() throws InterruptedException {
1025 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1026 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1027 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1028 +        assertFalse(sync.hasQueuedThread(t1));
1029 +        assertFalse(sync.hasQueuedThread(t2));
1030 +        sync.writeLock().lock();
1031 +        t1.start();
1032 +        Thread.sleep(SHORT_DELAY_MS);
1033 +        assertTrue(sync.hasQueuedThread(t1));
1034 +        t2.start();
1035 +        Thread.sleep(SHORT_DELAY_MS);
1036 +        assertTrue(sync.hasQueuedThread(t1));
1037 +        assertTrue(sync.hasQueuedThread(t2));
1038 +        t1.interrupt();
1039 +        Thread.sleep(SHORT_DELAY_MS);
1040 +        assertFalse(sync.hasQueuedThread(t1));
1041 +        assertTrue(sync.hasQueuedThread(t2));
1042 +        sync.writeLock().unlock();
1043 +        Thread.sleep(SHORT_DELAY_MS);
1044 +        assertFalse(sync.hasQueuedThread(t1));
1045 +        Thread.sleep(SHORT_DELAY_MS);
1046 +        assertFalse(sync.hasQueuedThread(t2));
1047 +        t1.join();
1048 +        t2.join();
1049 +    }
1050 +
1051 +
1052 +    /**
1053       * getQueueLength reports number of waiting threads
1054       */
1055 <    public void testGetQueueLength() {
1056 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1055 >    public void testGetQueueLength() throws InterruptedException {
1056 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1057          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1058          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1059 <        try {
1060 <            assertEquals(0, lock.getQueueLength());
1061 <            lock.writeLock().lock();
1062 <            t1.start();
1063 <            Thread.sleep(SHORT_DELAY_MS);
1064 <            assertEquals(1, lock.getQueueLength());
1065 <            t2.start();
1066 <            Thread.sleep(SHORT_DELAY_MS);
1067 <            assertEquals(2, lock.getQueueLength());
1068 <            t1.interrupt();
1069 <            Thread.sleep(SHORT_DELAY_MS);
1070 <            assertEquals(1, lock.getQueueLength());
1071 <            lock.writeLock().unlock();
1072 <            Thread.sleep(SHORT_DELAY_MS);
1073 <            assertEquals(0, lock.getQueueLength());
1074 <            t1.join();
1075 <            t2.join();
881 <        } catch(Exception e){
882 <            unexpectedException();
883 <        }
884 <    }
1059 >        assertEquals(0, lock.getQueueLength());
1060 >        lock.writeLock().lock();
1061 >        t1.start();
1062 >        Thread.sleep(SHORT_DELAY_MS);
1063 >        assertEquals(1, lock.getQueueLength());
1064 >        t2.start();
1065 >        Thread.sleep(SHORT_DELAY_MS);
1066 >        assertEquals(2, lock.getQueueLength());
1067 >        t1.interrupt();
1068 >        Thread.sleep(SHORT_DELAY_MS);
1069 >        assertEquals(1, lock.getQueueLength());
1070 >        lock.writeLock().unlock();
1071 >        Thread.sleep(SHORT_DELAY_MS);
1072 >        assertEquals(0, lock.getQueueLength());
1073 >        t1.join();
1074 >        t2.join();
1075 >    }
1076  
1077      /**
1078       * getQueuedThreads includes waiting threads
1079       */
1080 <    public void testGetQueuedThreads() {
1081 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1080 >    public void testGetQueuedThreads() throws InterruptedException {
1081 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1082          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1083          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1084 +        assertTrue(lock.getQueuedThreads().isEmpty());
1085 +        lock.writeLock().lock();
1086 +        assertTrue(lock.getQueuedThreads().isEmpty());
1087 +        t1.start();
1088 +        Thread.sleep(SHORT_DELAY_MS);
1089 +        assertTrue(lock.getQueuedThreads().contains(t1));
1090 +        t2.start();
1091 +        Thread.sleep(SHORT_DELAY_MS);
1092 +        assertTrue(lock.getQueuedThreads().contains(t1));
1093 +        assertTrue(lock.getQueuedThreads().contains(t2));
1094 +        t1.interrupt();
1095 +        Thread.sleep(SHORT_DELAY_MS);
1096 +        assertFalse(lock.getQueuedThreads().contains(t1));
1097 +        assertTrue(lock.getQueuedThreads().contains(t2));
1098 +        lock.writeLock().unlock();
1099 +        Thread.sleep(SHORT_DELAY_MS);
1100 +        assertTrue(lock.getQueuedThreads().isEmpty());
1101 +        t1.join();
1102 +        t2.join();
1103 +    }
1104 +
1105 +    /**
1106 +     * hasWaiters throws NPE if null
1107 +     */
1108 +    public void testHasWaitersNPE() {
1109 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1110          try {
1111 <            assertTrue(lock.getQueuedThreads().isEmpty());
1112 <            lock.writeLock().lock();
1113 <            assertTrue(lock.getQueuedThreads().isEmpty());
1114 <            t1.start();
898 <            Thread.sleep(SHORT_DELAY_MS);
899 <            assertTrue(lock.getQueuedThreads().contains(t1));
900 <            t2.start();
901 <            Thread.sleep(SHORT_DELAY_MS);
902 <            assertTrue(lock.getQueuedThreads().contains(t1));
903 <            assertTrue(lock.getQueuedThreads().contains(t2));
904 <            t1.interrupt();
905 <            Thread.sleep(SHORT_DELAY_MS);
906 <            assertFalse(lock.getQueuedThreads().contains(t1));
907 <            assertTrue(lock.getQueuedThreads().contains(t2));
908 <            lock.writeLock().unlock();
909 <            Thread.sleep(SHORT_DELAY_MS);
910 <            assertTrue(lock.getQueuedThreads().isEmpty());
911 <            t1.join();
912 <            t2.join();
913 <        } catch(Exception e){
914 <            unexpectedException();
915 <        }
916 <    }
1111 >            lock.hasWaiters(null);
1112 >            shouldThrow();
1113 >        } catch (NullPointerException success) {}
1114 >    }
1115  
1116      /**
1117 <     * hasWaiters returns true when a thread is waiting, else false
1117 >     * getWaitQueueLength throws NPE if null
1118       */
1119 <    public void testHasWaiters() {
1120 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1121 <        final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
1122 <        Thread t = new Thread(new Runnable() {
1123 <                public void run() {
1124 <                    try {
1125 <                        lock.writeLock().lock();
1126 <                        threadAssertFalse(c.hasWaiters());
929 <                        threadAssertEquals(0, c.getWaitQueueLength());
930 <                        c.await();
931 <                        lock.writeLock().unlock();
932 <                    }
933 <                    catch(InterruptedException e) {
934 <                        threadUnexpectedException();
935 <                    }
936 <                }
937 <            });
1119 >    public void testGetWaitQueueLengthNPE() {
1120 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1121 >        try {
1122 >            lock.getWaitQueueLength(null);
1123 >            shouldThrow();
1124 >        } catch (NullPointerException success) {}
1125 >    }
1126 >
1127  
1128 +    /**
1129 +     * getWaitingThreads throws NPE if null
1130 +     */
1131 +    public void testGetWaitingThreadsNPE() {
1132 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1133          try {
1134 <            t.start();
1135 <            Thread.sleep(SHORT_DELAY_MS);
1136 <            lock.writeLock().lock();
943 <            assertTrue(c.hasWaiters());
944 <            assertEquals(1, c.getWaitQueueLength());
945 <            c.signal();
946 <            lock.writeLock().unlock();
947 <            Thread.sleep(SHORT_DELAY_MS);
948 <            lock.writeLock().lock();
949 <            assertFalse(c.hasWaiters());
950 <            assertEquals(0, c.getWaitQueueLength());
951 <            lock.writeLock().unlock();
952 <            t.join(SHORT_DELAY_MS);
953 <            assertFalse(t.isAlive());
954 <        }
955 <        catch (Exception ex) {
956 <            unexpectedException();
957 <        }
1134 >            lock.getWaitingThreads(null);
1135 >            shouldThrow();
1136 >        } catch (NullPointerException success) {}
1137      }
1138  
1139      /**
1140 <     * getWaitQueueLength returns number of waiting threads
1140 >     * hasWaiters throws IAE if not owned
1141       */
1142 <    public void testGetWaitQueueLength() {
1143 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1144 <        final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
1145 <        Thread t1 = new Thread(new Runnable() {
1146 <                public void run() {
1147 <                    try {
1148 <                        lock.writeLock().lock();
1149 <                        threadAssertFalse(c.hasWaiters());
1150 <                        threadAssertEquals(0, c.getWaitQueueLength());
972 <                        c.await();
973 <                        lock.writeLock().unlock();
974 <                    }
975 <                    catch(InterruptedException e) {
976 <                        threadUnexpectedException();
977 <                    }
978 <                }
979 <            });
980 <
981 <        Thread t2 = new Thread(new Runnable() {
982 <                public void run() {
983 <                    try {
984 <                        lock.writeLock().lock();
985 <                        threadAssertTrue(c.hasWaiters());
986 <                        threadAssertEquals(1, c.getWaitQueueLength());
987 <                        c.await();
988 <                        lock.writeLock().unlock();
989 <                    }
990 <                    catch(InterruptedException e) {
991 <                        threadUnexpectedException();
992 <                    }
993 <                }
994 <            });
1142 >    public void testHasWaitersIAE() {
1143 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1144 >        final Condition c = lock.writeLock().newCondition();
1145 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1146 >        try {
1147 >            lock2.hasWaiters(c);
1148 >            shouldThrow();
1149 >        } catch (IllegalArgumentException success) {}
1150 >    }
1151  
1152 +    /**
1153 +     * hasWaiters throws IMSE if not locked
1154 +     */
1155 +    public void testHasWaitersIMSE() {
1156 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1157 +        final Condition c = lock.writeLock().newCondition();
1158          try {
1159 <            t1.start();
1160 <            Thread.sleep(SHORT_DELAY_MS);
1161 <            t2.start();
1000 <            Thread.sleep(SHORT_DELAY_MS);
1001 <            lock.writeLock().lock();
1002 <            assertTrue(c.hasWaiters());
1003 <            assertEquals(2, c.getWaitQueueLength());
1004 <            c.signalAll();
1005 <            lock.writeLock().unlock();
1006 <            Thread.sleep(SHORT_DELAY_MS);
1007 <            lock.writeLock().lock();
1008 <            assertFalse(c.hasWaiters());
1009 <            assertEquals(0, c.getWaitQueueLength());
1010 <            lock.writeLock().unlock();
1011 <            t1.join(SHORT_DELAY_MS);
1012 <            t2.join(SHORT_DELAY_MS);
1013 <            assertFalse(t1.isAlive());
1014 <            assertFalse(t2.isAlive());
1015 <        }
1016 <        catch (Exception ex) {
1017 <            unexpectedException();
1018 <        }
1159 >            lock.hasWaiters(c);
1160 >            shouldThrow();
1161 >        } catch (IllegalMonitorStateException success) {}
1162      }
1163  
1164 +
1165      /**
1166 <     * getWaitingThreads returns only and all waiting threads
1166 >     * getWaitQueueLength throws IAE if not owned
1167       */
1168 <    public void testGetWaitingThreads() {
1169 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1170 <        final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition();
1171 <        Thread t1 = new Thread(new Runnable() {
1172 <                public void run() {
1173 <                    try {
1174 <                        lock.writeLock().lock();
1175 <                        threadAssertTrue(c.getWaitingThreads().isEmpty());
1176 <                        c.await();
1033 <                        lock.writeLock().unlock();
1034 <                    }
1035 <                    catch(InterruptedException e) {
1036 <                        threadUnexpectedException();
1037 <                    }
1038 <                }
1039 <            });
1040 <
1041 <        Thread t2 = new Thread(new Runnable() {
1042 <                public void run() {
1043 <                    try {
1044 <                        lock.writeLock().lock();
1045 <                        threadAssertFalse(c.getWaitingThreads().isEmpty());
1046 <                        c.await();
1047 <                        lock.writeLock().unlock();
1048 <                    }
1049 <                    catch(InterruptedException e) {
1050 <                        threadUnexpectedException();
1051 <                    }
1052 <                }
1053 <            });
1168 >    public void testGetWaitQueueLengthIAE() {
1169 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1170 >        final Condition c = lock.writeLock().newCondition();
1171 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1172 >        try {
1173 >            lock2.getWaitQueueLength(c);
1174 >            shouldThrow();
1175 >        } catch (IllegalArgumentException success) {}
1176 >    }
1177  
1178 +    /**
1179 +     * getWaitQueueLength throws IMSE if not locked
1180 +     */
1181 +    public void testGetWaitQueueLengthIMSE() {
1182 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1183 +        final Condition c = lock.writeLock().newCondition();
1184          try {
1185 <            lock.writeLock().lock();
1186 <            assertTrue(c.getWaitingThreads().isEmpty());
1187 <            lock.writeLock().unlock();
1188 <            t1.start();
1189 <            Thread.sleep(SHORT_DELAY_MS);
1190 <            t2.start();
1191 <            Thread.sleep(SHORT_DELAY_MS);
1192 <            lock.writeLock().lock();
1193 <            assertTrue(c.hasWaiters());
1194 <            assertTrue(c.getWaitingThreads().contains(t1));
1195 <            assertTrue(c.getWaitingThreads().contains(t2));
1196 <            c.signalAll();
1197 <            lock.writeLock().unlock();
1198 <            Thread.sleep(SHORT_DELAY_MS);
1199 <            lock.writeLock().lock();
1200 <            assertFalse(c.hasWaiters());
1201 <            assertTrue(c.getWaitingThreads().isEmpty());
1202 <            lock.writeLock().unlock();
1203 <            t1.join(SHORT_DELAY_MS);
1204 <            t2.join(SHORT_DELAY_MS);
1205 <            assertFalse(t1.isAlive());
1206 <            assertFalse(t2.isAlive());
1207 <        }
1208 <        catch (Exception ex) {
1209 <            unexpectedException();
1210 <        }
1185 >            lock.getWaitQueueLength(c);
1186 >            shouldThrow();
1187 >        } catch (IllegalMonitorStateException success) {}
1188 >    }
1189 >
1190 >
1191 >    /**
1192 >     * getWaitingThreads throws IAE if not owned
1193 >     */
1194 >    public void testGetWaitingThreadsIAE() {
1195 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1196 >        final Condition c = lock.writeLock().newCondition();
1197 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1198 >        try {
1199 >            lock2.getWaitingThreads(c);
1200 >            shouldThrow();
1201 >        } catch (IllegalArgumentException success) {}
1202 >    }
1203 >
1204 >    /**
1205 >     * getWaitingThreads throws IMSE if not locked
1206 >     */
1207 >    public void testGetWaitingThreadsIMSE() {
1208 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1209 >        final Condition c = lock.writeLock().newCondition();
1210 >        try {
1211 >            lock.getWaitingThreads(c);
1212 >            shouldThrow();
1213 >        } catch (IllegalMonitorStateException success) {}
1214 >    }
1215 >
1216 >
1217 >    /**
1218 >     * hasWaiters returns true when a thread is waiting, else false
1219 >     */
1220 >    public void testHasWaiters() throws InterruptedException {
1221 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1222 >        final Condition c = lock.writeLock().newCondition();
1223 >        Thread t = new Thread(new CheckedRunnable() {
1224 >            public void realRun() throws InterruptedException {
1225 >                lock.writeLock().lock();
1226 >                threadAssertFalse(lock.hasWaiters(c));
1227 >                threadAssertEquals(0, lock.getWaitQueueLength(c));
1228 >                c.await();
1229 >                lock.writeLock().unlock();
1230 >            }});
1231 >
1232 >        t.start();
1233 >        Thread.sleep(SHORT_DELAY_MS);
1234 >        lock.writeLock().lock();
1235 >        assertTrue(lock.hasWaiters(c));
1236 >        assertEquals(1, lock.getWaitQueueLength(c));
1237 >        c.signal();
1238 >        lock.writeLock().unlock();
1239 >        Thread.sleep(SHORT_DELAY_MS);
1240 >        lock.writeLock().lock();
1241 >        assertFalse(lock.hasWaiters(c));
1242 >        assertEquals(0, lock.getWaitQueueLength(c));
1243 >        lock.writeLock().unlock();
1244 >        t.join(SHORT_DELAY_MS);
1245 >        assertFalse(t.isAlive());
1246 >    }
1247 >
1248 >    /**
1249 >     * getWaitQueueLength returns number of waiting threads
1250 >     */
1251 >    public void testGetWaitQueueLength() throws InterruptedException {
1252 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1253 >        final Condition c = lock.writeLock().newCondition();
1254 >        Thread t = new Thread(new CheckedRunnable() {
1255 >            public void realRun() throws InterruptedException {
1256 >                lock.writeLock().lock();
1257 >                threadAssertFalse(lock.hasWaiters(c));
1258 >                threadAssertEquals(0, lock.getWaitQueueLength(c));
1259 >                c.await();
1260 >                lock.writeLock().unlock();
1261 >            }});
1262 >
1263 >        t.start();
1264 >        Thread.sleep(SHORT_DELAY_MS);
1265 >        lock.writeLock().lock();
1266 >        assertTrue(lock.hasWaiters(c));
1267 >        assertEquals(1, lock.getWaitQueueLength(c));
1268 >        c.signal();
1269 >        lock.writeLock().unlock();
1270 >        Thread.sleep(SHORT_DELAY_MS);
1271 >        lock.writeLock().lock();
1272 >        assertFalse(lock.hasWaiters(c));
1273 >        assertEquals(0, lock.getWaitQueueLength(c));
1274 >        lock.writeLock().unlock();
1275 >        t.join(SHORT_DELAY_MS);
1276 >        assertFalse(t.isAlive());
1277 >    }
1278 >
1279 >
1280 >    /**
1281 >     * getWaitingThreads returns only and all waiting threads
1282 >     */
1283 >    public void testGetWaitingThreads() throws InterruptedException {
1284 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1285 >        final Condition c = lock.writeLock().newCondition();
1286 >        Thread t1 = new Thread(new CheckedRunnable() {
1287 >            public void realRun() throws InterruptedException {
1288 >                lock.writeLock().lock();
1289 >                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1290 >                c.await();
1291 >                lock.writeLock().unlock();
1292 >            }});
1293 >
1294 >        Thread t2 = new Thread(new CheckedRunnable() {
1295 >            public void realRun() throws InterruptedException {
1296 >                lock.writeLock().lock();
1297 >                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1298 >                c.await();
1299 >                lock.writeLock().unlock();
1300 >            }});
1301 >
1302 >        lock.writeLock().lock();
1303 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
1304 >        lock.writeLock().unlock();
1305 >        t1.start();
1306 >        Thread.sleep(SHORT_DELAY_MS);
1307 >        t2.start();
1308 >        Thread.sleep(SHORT_DELAY_MS);
1309 >        lock.writeLock().lock();
1310 >        assertTrue(lock.hasWaiters(c));
1311 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
1312 >        assertTrue(lock.getWaitingThreads(c).contains(t2));
1313 >        c.signalAll();
1314 >        lock.writeLock().unlock();
1315 >        Thread.sleep(SHORT_DELAY_MS);
1316 >        lock.writeLock().lock();
1317 >        assertFalse(lock.hasWaiters(c));
1318 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
1319 >        lock.writeLock().unlock();
1320 >        t1.join(SHORT_DELAY_MS);
1321 >        t2.join(SHORT_DELAY_MS);
1322 >        assertFalse(t1.isAlive());
1323 >        assertFalse(t2.isAlive());
1324 >    }
1325 >
1326 >    /**
1327 >     * toString indicates current lock state
1328 >     */
1329 >    public void testToString() {
1330 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1331 >        String us = lock.toString();
1332 >        assertTrue(us.indexOf("Write locks = 0") >= 0);
1333 >        assertTrue(us.indexOf("Read locks = 0") >= 0);
1334 >        lock.writeLock().lock();
1335 >        String ws = lock.toString();
1336 >        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1337 >        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1338 >        lock.writeLock().unlock();
1339 >        lock.readLock().lock();
1340 >        lock.readLock().lock();
1341 >        String rs = lock.toString();
1342 >        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1343 >        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1344 >    }
1345 >
1346 >    /**
1347 >     * readLock.toString indicates current lock state
1348 >     */
1349 >    public void testReadLockToString() {
1350 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1351 >        String us = lock.readLock().toString();
1352 >        assertTrue(us.indexOf("Read locks = 0") >= 0);
1353 >        lock.readLock().lock();
1354 >        lock.readLock().lock();
1355 >        String rs = lock.readLock().toString();
1356 >        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1357 >    }
1358 >
1359 >    /**
1360 >     * writeLock.toString indicates current lock state
1361 >     */
1362 >    public void testWriteLockToString() {
1363 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1364 >        String us = lock.writeLock().toString();
1365 >        assertTrue(us.indexOf("Unlocked") >= 0);
1366 >        lock.writeLock().lock();
1367 >        String ls = lock.writeLock().toString();
1368 >        assertTrue(ls.indexOf("Locked") >= 0);
1369      }
1370  
1371   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines