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