32 |
|
} |
33 |
|
} |
34 |
|
|
35 |
– |
|
35 |
|
/** |
36 |
|
* A runnable calling lockInterruptibly that expects to be |
37 |
|
* interrupted |
49 |
|
*/ |
50 |
|
static class PublicReentrantLock extends ReentrantLock { |
51 |
|
PublicReentrantLock() { super(); } |
52 |
+ |
PublicReentrantLock(boolean fair) { super(fair); } |
53 |
+ |
public Thread getOwner() { |
54 |
+ |
return super.getOwner(); |
55 |
+ |
} |
56 |
|
public Collection<Thread> getQueuedThreads() { |
57 |
|
return super.getQueuedThreads(); |
58 |
|
} |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
+ |
* Releases write lock, checking that it had a hold count of 1. |
66 |
+ |
*/ |
67 |
+ |
void releaseLock(PublicReentrantLock lock) { |
68 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
69 |
+ |
lock.unlock(); |
70 |
+ |
assertFalse(lock.isHeldByCurrentThread()); |
71 |
+ |
assertNotLocked(lock); |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
/** |
75 |
+ |
* Spin-waits until lock.hasQueuedThread(t) becomes true. |
76 |
+ |
*/ |
77 |
+ |
void waitForQueuedThread(PublicReentrantLock lock, Thread t) { |
78 |
+ |
long startTime = System.nanoTime(); |
79 |
+ |
while (!lock.hasQueuedThread(t)) { |
80 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
81 |
+ |
throw new AssertionError("timed out"); |
82 |
+ |
Thread.yield(); |
83 |
+ |
} |
84 |
+ |
assertTrue(t.isAlive()); |
85 |
+ |
assertTrue(lock.getOwner() != t); |
86 |
+ |
} |
87 |
+ |
|
88 |
+ |
/** |
89 |
+ |
* Checks that lock is not locked. |
90 |
+ |
*/ |
91 |
+ |
void assertNotLocked(PublicReentrantLock lock) { |
92 |
+ |
assertFalse(lock.isLocked()); |
93 |
+ |
assertFalse(lock.isHeldByCurrentThread()); |
94 |
+ |
assertNull(lock.getOwner()); |
95 |
+ |
assertEquals(0, lock.getHoldCount()); |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
/** |
99 |
+ |
* Checks that lock is locked by the given thread. |
100 |
+ |
*/ |
101 |
+ |
void assertLockedBy(PublicReentrantLock lock, Thread t) { |
102 |
+ |
assertTrue(lock.isLocked()); |
103 |
+ |
assertSame(t, lock.getOwner()); |
104 |
+ |
assertEquals(t == Thread.currentThread(), |
105 |
+ |
lock.isHeldByCurrentThread()); |
106 |
+ |
assertEquals(t == Thread.currentThread(), |
107 |
+ |
lock.getHoldCount() > 0); |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
/** |
111 |
|
* Checks that condition c has no waiters. |
112 |
|
*/ |
113 |
|
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Constructor sets given fairness |
133 |
> |
* Constructor sets given fairness, and is in unlocked state |
134 |
|
*/ |
135 |
|
public void testConstructor() { |
136 |
< |
assertFalse(new ReentrantLock().isFair()); |
137 |
< |
assertFalse(new ReentrantLock(false).isFair()); |
138 |
< |
assertTrue(new ReentrantLock(true).isFair()); |
136 |
> |
PublicReentrantLock lock; |
137 |
> |
|
138 |
> |
lock = new PublicReentrantLock(); |
139 |
> |
assertFalse(lock.isFair()); |
140 |
> |
assertNotLocked(lock); |
141 |
> |
|
142 |
> |
lock = new PublicReentrantLock(true); |
143 |
> |
assertTrue(lock.isFair()); |
144 |
> |
assertNotLocked(lock); |
145 |
> |
|
146 |
> |
lock = new PublicReentrantLock(false); |
147 |
> |
assertFalse(lock.isFair()); |
148 |
> |
assertNotLocked(lock); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
|
* locking an unlocked lock succeeds |
153 |
|
*/ |
154 |
|
public void testLock() { |
155 |
< |
ReentrantLock rl = new ReentrantLock(); |
156 |
< |
rl.lock(); |
157 |
< |
assertTrue(rl.isLocked()); |
158 |
< |
rl.unlock(); |
100 |
< |
assertFalse(rl.isLocked()); |
155 |
> |
PublicReentrantLock lock = new PublicReentrantLock(); |
156 |
> |
lock.lock(); |
157 |
> |
assertLockedBy(lock, Thread.currentThread()); |
158 |
> |
releaseLock(lock); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* locking an unlocked fair lock succeeds |
163 |
|
*/ |
164 |
|
public void testFairLock() { |
165 |
< |
ReentrantLock rl = new ReentrantLock(true); |
166 |
< |
rl.lock(); |
167 |
< |
assertTrue(rl.isLocked()); |
168 |
< |
rl.unlock(); |
165 |
> |
PublicReentrantLock lock = new PublicReentrantLock(true); |
166 |
> |
lock.lock(); |
167 |
> |
assertLockedBy(lock, Thread.currentThread()); |
168 |
> |
releaseLock(lock); |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
173 |
|
*/ |
174 |
|
public void testUnlock_IllegalMonitorStateException() { |
175 |
< |
ReentrantLock rl = new ReentrantLock(); |
175 |
> |
ReentrantLock lock = new ReentrantLock(); |
176 |
|
try { |
177 |
< |
rl.unlock(); |
177 |
> |
lock.unlock(); |
178 |
|
shouldThrow(); |
179 |
|
} catch (IllegalMonitorStateException success) {} |
180 |
|
} |
183 |
|
* tryLock on an unlocked lock succeeds |
184 |
|
*/ |
185 |
|
public void testTryLock() { |
186 |
< |
ReentrantLock rl = new ReentrantLock(); |
187 |
< |
assertTrue(rl.tryLock()); |
188 |
< |
assertTrue(rl.isLocked()); |
189 |
< |
rl.unlock(); |
186 |
> |
PublicReentrantLock lock = new PublicReentrantLock(); |
187 |
> |
assertTrue(lock.tryLock()); |
188 |
> |
assertLockedBy(lock, Thread.currentThread()); |
189 |
> |
releaseLock(lock); |
190 |
|
} |
191 |
|
|
134 |
– |
|
192 |
|
/** |
193 |
|
* hasQueuedThreads reports whether there are waiting threads |
194 |
|
*/ |
195 |
< |
public void testhasQueuedThreads() throws InterruptedException { |
196 |
< |
final ReentrantLock lock = new ReentrantLock(); |
195 |
> |
public void testHasQueuedThreads() throws InterruptedException { |
196 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
197 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
198 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
199 |
|
assertFalse(lock.hasQueuedThreads()); |
200 |
|
lock.lock(); |
201 |
+ |
assertFalse(lock.hasQueuedThreads()); |
202 |
|
t1.start(); |
203 |
< |
delay(SHORT_DELAY_MS); |
203 |
> |
waitForQueuedThread(lock, t1); |
204 |
|
assertTrue(lock.hasQueuedThreads()); |
205 |
|
t2.start(); |
206 |
< |
delay(SHORT_DELAY_MS); |
206 |
> |
waitForQueuedThread(lock, t2); |
207 |
|
assertTrue(lock.hasQueuedThreads()); |
208 |
|
t1.interrupt(); |
209 |
< |
delay(SHORT_DELAY_MS); |
209 |
> |
awaitTermination(t1); |
210 |
|
assertTrue(lock.hasQueuedThreads()); |
211 |
|
lock.unlock(); |
154 |
– |
delay(SHORT_DELAY_MS); |
155 |
– |
assertFalse(lock.hasQueuedThreads()); |
156 |
– |
awaitTermination(t1); |
212 |
|
awaitTermination(t2); |
213 |
+ |
assertFalse(lock.hasQueuedThreads()); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* getQueueLength reports number of waiting threads |
218 |
|
*/ |
219 |
|
public void testGetQueueLength() throws InterruptedException { |
220 |
< |
final ReentrantLock lock = new ReentrantLock(); |
220 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
221 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
222 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
223 |
|
assertEquals(0, lock.getQueueLength()); |
224 |
|
lock.lock(); |
225 |
|
t1.start(); |
226 |
< |
delay(SHORT_DELAY_MS); |
226 |
> |
waitForQueuedThread(lock, t1); |
227 |
|
assertEquals(1, lock.getQueueLength()); |
228 |
|
t2.start(); |
229 |
< |
delay(SHORT_DELAY_MS); |
229 |
> |
waitForQueuedThread(lock, t2); |
230 |
|
assertEquals(2, lock.getQueueLength()); |
231 |
|
t1.interrupt(); |
232 |
< |
delay(SHORT_DELAY_MS); |
232 |
> |
awaitTermination(t1); |
233 |
|
assertEquals(1, lock.getQueueLength()); |
234 |
|
lock.unlock(); |
179 |
– |
delay(SHORT_DELAY_MS); |
180 |
– |
assertEquals(0, lock.getQueueLength()); |
181 |
– |
awaitTermination(t1); |
235 |
|
awaitTermination(t2); |
236 |
+ |
assertEquals(0, lock.getQueueLength()); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* getQueueLength reports number of waiting threads |
241 |
|
*/ |
242 |
|
public void testGetQueueLength_fair() throws InterruptedException { |
243 |
< |
final ReentrantLock lock = new ReentrantLock(true); |
243 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(true); |
244 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
245 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
246 |
|
assertEquals(0, lock.getQueueLength()); |
247 |
|
lock.lock(); |
248 |
|
t1.start(); |
249 |
< |
delay(SHORT_DELAY_MS); |
249 |
> |
waitForQueuedThread(lock, t1); |
250 |
|
assertEquals(1, lock.getQueueLength()); |
251 |
|
t2.start(); |
252 |
< |
delay(SHORT_DELAY_MS); |
252 |
> |
waitForQueuedThread(lock, t2); |
253 |
|
assertEquals(2, lock.getQueueLength()); |
254 |
|
t1.interrupt(); |
255 |
< |
delay(SHORT_DELAY_MS); |
255 |
> |
awaitTermination(t1); |
256 |
|
assertEquals(1, lock.getQueueLength()); |
257 |
|
lock.unlock(); |
204 |
– |
delay(SHORT_DELAY_MS); |
205 |
– |
assertEquals(0, lock.getQueueLength()); |
206 |
– |
awaitTermination(t1); |
258 |
|
awaitTermination(t2); |
259 |
+ |
assertEquals(0, lock.getQueueLength()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* hasQueuedThread(null) throws NPE |
264 |
|
*/ |
265 |
|
public void testHasQueuedThreadNPE() { |
266 |
< |
final ReentrantLock sync = new ReentrantLock(); |
266 |
> |
final ReentrantLock lock = new ReentrantLock(); |
267 |
|
try { |
268 |
< |
sync.hasQueuedThread(null); |
268 |
> |
lock.hasQueuedThread(null); |
269 |
|
shouldThrow(); |
270 |
|
} catch (NullPointerException success) {} |
271 |
|
} |
274 |
|
* hasQueuedThread reports whether a thread is queued. |
275 |
|
*/ |
276 |
|
public void testHasQueuedThread() throws InterruptedException { |
277 |
< |
final ReentrantLock sync = new ReentrantLock(); |
278 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
279 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
280 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
281 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
282 |
< |
sync.lock(); |
277 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
278 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
279 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
280 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
281 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
282 |
> |
lock.lock(); |
283 |
|
t1.start(); |
284 |
< |
delay(SHORT_DELAY_MS); |
285 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
284 |
> |
waitForQueuedThread(lock, t1); |
285 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
286 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
287 |
|
t2.start(); |
288 |
< |
delay(SHORT_DELAY_MS); |
289 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
290 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
288 |
> |
waitForQueuedThread(lock, t2); |
289 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
290 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
291 |
|
t1.interrupt(); |
239 |
– |
delay(SHORT_DELAY_MS); |
240 |
– |
assertFalse(sync.hasQueuedThread(t1)); |
241 |
– |
assertTrue(sync.hasQueuedThread(t2)); |
242 |
– |
sync.unlock(); |
243 |
– |
delay(SHORT_DELAY_MS); |
244 |
– |
assertFalse(sync.hasQueuedThread(t1)); |
245 |
– |
delay(SHORT_DELAY_MS); |
246 |
– |
assertFalse(sync.hasQueuedThread(t2)); |
292 |
|
awaitTermination(t1); |
293 |
+ |
assertFalse(lock.hasQueuedThread(t1)); |
294 |
+ |
assertTrue(lock.hasQueuedThread(t2)); |
295 |
+ |
lock.unlock(); |
296 |
|
awaitTermination(t2); |
297 |
+ |
assertFalse(lock.hasQueuedThread(t1)); |
298 |
+ |
assertFalse(lock.hasQueuedThread(t2)); |
299 |
|
} |
300 |
|
|
251 |
– |
|
301 |
|
/** |
302 |
|
* getQueuedThreads includes waiting threads |
303 |
|
*/ |
309 |
|
lock.lock(); |
310 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
311 |
|
t1.start(); |
312 |
< |
delay(SHORT_DELAY_MS); |
312 |
> |
waitForQueuedThread(lock, t1); |
313 |
> |
assertEquals(1, lock.getQueuedThreads().size()); |
314 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
315 |
|
t2.start(); |
316 |
< |
delay(SHORT_DELAY_MS); |
316 |
> |
waitForQueuedThread(lock, t2); |
317 |
> |
assertEquals(2, lock.getQueuedThreads().size()); |
318 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
319 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
320 |
|
t1.interrupt(); |
321 |
< |
delay(SHORT_DELAY_MS); |
321 |
> |
awaitTermination(t1); |
322 |
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
323 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
324 |
+ |
assertEquals(1, lock.getQueuedThreads().size()); |
325 |
|
lock.unlock(); |
274 |
– |
delay(SHORT_DELAY_MS); |
275 |
– |
assertTrue(lock.getQueuedThreads().isEmpty()); |
276 |
– |
awaitTermination(t1); |
326 |
|
awaitTermination(t2); |
327 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
328 |
|
} |
329 |
|
|
280 |
– |
|
330 |
|
/** |
331 |
|
* timed tryLock is interruptible. |
332 |
|
*/ |
333 |
< |
public void testInterruptedException2() throws InterruptedException { |
334 |
< |
final ReentrantLock lock = new ReentrantLock(); |
333 |
> |
public void testTryLock_Interrupted() throws InterruptedException { |
334 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
335 |
|
lock.lock(); |
336 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
337 |
|
public void realRun() throws InterruptedException { |
338 |
< |
lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); |
338 |
> |
lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
339 |
|
}}); |
340 |
|
|
341 |
< |
delay(SHORT_DELAY_MS); |
341 |
> |
waitForQueuedThread(lock, t); |
342 |
|
t.interrupt(); |
343 |
|
awaitTermination(t); |
344 |
+ |
releaseLock(lock); |
345 |
|
} |
346 |
|
|
297 |
– |
|
347 |
|
/** |
348 |
< |
* TryLock on a locked lock fails |
348 |
> |
* tryLock on a locked lock fails |
349 |
|
*/ |
350 |
|
public void testTryLockWhenLocked() throws InterruptedException { |
351 |
< |
final ReentrantLock lock = new ReentrantLock(); |
351 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
352 |
|
lock.lock(); |
353 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
354 |
|
public void realRun() { |
356 |
|
}}); |
357 |
|
|
358 |
|
awaitTermination(t); |
359 |
< |
lock.unlock(); |
359 |
> |
releaseLock(lock); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* Timed tryLock on a locked lock times out |
364 |
|
*/ |
365 |
|
public void testTryLock_Timeout() throws InterruptedException { |
366 |
< |
final ReentrantLock lock = new ReentrantLock(); |
366 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
367 |
|
lock.lock(); |
368 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
369 |
|
public void realRun() throws InterruptedException { |
370 |
< |
assertFalse(lock.tryLock(1, MILLISECONDS)); |
370 |
> |
long startTime = System.nanoTime(); |
371 |
> |
long timeoutMillis = 10; |
372 |
> |
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
373 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
374 |
|
}}); |
375 |
|
|
376 |
|
awaitTermination(t); |
377 |
< |
lock.unlock(); |
377 |
> |
releaseLock(lock); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
392 |
|
} |
393 |
|
} |
394 |
|
|
343 |
– |
|
395 |
|
/** |
396 |
|
* isLocked is true when locked and false when not |
397 |
|
*/ |
398 |
< |
public void testIsLocked() throws InterruptedException { |
398 |
> |
public void testIsLocked() throws Exception { |
399 |
|
final ReentrantLock lock = new ReentrantLock(); |
400 |
+ |
assertFalse(lock.isLocked()); |
401 |
|
lock.lock(); |
402 |
|
assertTrue(lock.isLocked()); |
403 |
+ |
lock.lock(); |
404 |
+ |
assertTrue(lock.isLocked()); |
405 |
+ |
lock.unlock(); |
406 |
+ |
assertTrue(lock.isLocked()); |
407 |
|
lock.unlock(); |
408 |
|
assertFalse(lock.isLocked()); |
409 |
+ |
final CyclicBarrier barrier = new CyclicBarrier(2); |
410 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
411 |
< |
public void realRun() throws InterruptedException { |
411 |
> |
public void realRun() throws Exception { |
412 |
|
lock.lock(); |
413 |
< |
delay(SMALL_DELAY_MS); |
413 |
> |
assertTrue(lock.isLocked()); |
414 |
> |
barrier.await(); |
415 |
> |
barrier.await(); |
416 |
|
lock.unlock(); |
417 |
|
}}); |
418 |
|
|
419 |
< |
delay(SHORT_DELAY_MS); |
419 |
> |
barrier.await(); |
420 |
|
assertTrue(lock.isLocked()); |
421 |
+ |
barrier.await(); |
422 |
|
awaitTermination(t); |
423 |
|
assertFalse(lock.isLocked()); |
424 |
|
} |
425 |
|
|
366 |
– |
|
426 |
|
/** |
427 |
|
* lockInterruptibly is interruptible. |
428 |
|
*/ |
429 |
< |
public void testLockInterruptibly1() throws InterruptedException { |
430 |
< |
final ReentrantLock lock = new ReentrantLock(); |
429 |
> |
public void testLockInterruptibly_Interrupted() throws InterruptedException { |
430 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
431 |
|
lock.lock(); |
432 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
433 |
< |
delay(SHORT_DELAY_MS); |
433 |
> |
waitForQueuedThread(lock, t); |
434 |
|
t.interrupt(); |
376 |
– |
delay(SHORT_DELAY_MS); |
377 |
– |
lock.unlock(); |
435 |
|
awaitTermination(t); |
436 |
+ |
releaseLock(lock); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
441 |
|
*/ |
442 |
< |
public void testLockInterruptibly2() throws InterruptedException { |
443 |
< |
final ReentrantLock lock = new ReentrantLock(); |
442 |
> |
public void testLockInterruptibly_Interrupted2() throws InterruptedException { |
443 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
444 |
|
lock.lockInterruptibly(); |
445 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
446 |
< |
delay(SHORT_DELAY_MS); |
446 |
> |
waitForQueuedThread(lock, t); |
447 |
|
t.interrupt(); |
448 |
|
assertTrue(lock.isLocked()); |
449 |
|
assertTrue(lock.isHeldByCurrentThread()); |
450 |
|
awaitTermination(t); |
451 |
+ |
releaseLock(lock); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
461 |
|
c.await(); |
462 |
|
shouldThrow(); |
463 |
|
} catch (IllegalMonitorStateException success) {} |
464 |
+ |
try { |
465 |
+ |
c.await(LONG_DELAY_MS, MILLISECONDS); |
466 |
+ |
shouldThrow(); |
467 |
+ |
} catch (IllegalMonitorStateException success) {} |
468 |
+ |
try { |
469 |
+ |
c.awaitNanos(100); |
470 |
+ |
shouldThrow(); |
471 |
+ |
} catch (IllegalMonitorStateException success) {} |
472 |
+ |
try { |
473 |
+ |
c.awaitUninterruptibly(); |
474 |
+ |
shouldThrow(); |
475 |
+ |
} catch (IllegalMonitorStateException success) {} |
476 |
|
} |
477 |
|
|
478 |
|
/** |
494 |
|
final ReentrantLock lock = new ReentrantLock(); |
495 |
|
final Condition c = lock.newCondition(); |
496 |
|
lock.lock(); |
497 |
< |
long t = c.awaitNanos(100); |
498 |
< |
assertTrue(t <= 0); |
497 |
> |
long startTime = System.nanoTime(); |
498 |
> |
long timeoutMillis = 10; |
499 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
500 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
501 |
> |
assertTrue(nanosRemaining <= 0); |
502 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
503 |
|
lock.unlock(); |
504 |
|
} |
505 |
|
|
510 |
|
final ReentrantLock lock = new ReentrantLock(); |
511 |
|
final Condition c = lock.newCondition(); |
512 |
|
lock.lock(); |
513 |
< |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
513 |
> |
long startTime = System.nanoTime(); |
514 |
> |
long timeoutMillis = 10; |
515 |
> |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
516 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
517 |
|
lock.unlock(); |
518 |
|
} |
519 |
|
|
524 |
|
final ReentrantLock lock = new ReentrantLock(); |
525 |
|
final Condition c = lock.newCondition(); |
526 |
|
lock.lock(); |
527 |
+ |
long startTime = System.nanoTime(); |
528 |
+ |
long timeoutMillis = 10; |
529 |
|
java.util.Date d = new java.util.Date(); |
530 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
530 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
531 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
532 |
|
lock.unlock(); |
533 |
|
} |
534 |
|
|
536 |
|
* await returns when signalled |
537 |
|
*/ |
538 |
|
public void testAwait() throws InterruptedException { |
539 |
< |
final ReentrantLock lock = new ReentrantLock(); |
539 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
540 |
|
final Condition c = lock.newCondition(); |
541 |
+ |
final CountDownLatch locked = new CountDownLatch(1); |
542 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
543 |
|
public void realRun() throws InterruptedException { |
544 |
|
lock.lock(); |
545 |
+ |
locked.countDown(); |
546 |
|
c.await(); |
547 |
|
lock.unlock(); |
548 |
|
}}); |
549 |
|
|
550 |
< |
delay(SHORT_DELAY_MS); |
550 |
> |
locked.await(); |
551 |
|
lock.lock(); |
552 |
+ |
assertHasWaiters(lock, c, t); |
553 |
|
c.signal(); |
554 |
+ |
assertHasNoWaiters(lock, c); |
555 |
+ |
assertTrue(t.isAlive()); |
556 |
|
lock.unlock(); |
557 |
|
awaitTermination(t); |
558 |
|
} |
579 |
|
} catch (NullPointerException success) {} |
580 |
|
} |
581 |
|
|
496 |
– |
|
582 |
|
/** |
583 |
|
* getWaitingThreads throws NPE if null |
584 |
|
*/ |
590 |
|
} catch (NullPointerException success) {} |
591 |
|
} |
592 |
|
|
508 |
– |
|
593 |
|
/** |
594 |
< |
* hasWaiters throws IAE if not owned |
594 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
595 |
|
*/ |
596 |
|
public void testHasWaitersIAE() { |
597 |
|
final ReentrantLock lock = new ReentrantLock(); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* hasWaiters throws IMSE if not locked |
607 |
> |
* hasWaiters throws IllegalMonitorStateException if not locked |
608 |
|
*/ |
609 |
|
public void testHasWaitersIMSE() { |
610 |
|
final ReentrantLock lock = new ReentrantLock(); |
615 |
|
} catch (IllegalMonitorStateException success) {} |
616 |
|
} |
617 |
|
|
534 |
– |
|
618 |
|
/** |
619 |
< |
* getWaitQueueLength throws IAE if not owned |
619 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
620 |
|
*/ |
621 |
|
public void testGetWaitQueueLengthIAE() { |
622 |
|
final ReentrantLock lock = new ReentrantLock(); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
< |
* getWaitQueueLength throws IMSE if not locked |
632 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not locked |
633 |
|
*/ |
634 |
|
public void testGetWaitQueueLengthIMSE() { |
635 |
|
final ReentrantLock lock = new ReentrantLock(); |
640 |
|
} catch (IllegalMonitorStateException success) {} |
641 |
|
} |
642 |
|
|
560 |
– |
|
643 |
|
/** |
644 |
< |
* getWaitingThreads throws IAE if not owned |
644 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
645 |
|
*/ |
646 |
|
public void testGetWaitingThreadsIAE() { |
647 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
< |
* getWaitingThreads throws IMSE if not locked |
657 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not locked |
658 |
|
*/ |
659 |
|
public void testGetWaitingThreadsIMSE() { |
660 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
665 |
|
} catch (IllegalMonitorStateException success) {} |
666 |
|
} |
667 |
|
|
586 |
– |
|
668 |
|
/** |
669 |
|
* hasWaiters returns true when a thread is waiting, else false |
670 |
|
*/ |
671 |
|
public void testHasWaiters() throws InterruptedException { |
672 |
< |
final ReentrantLock lock = new ReentrantLock(); |
672 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
673 |
|
final Condition c = lock.newCondition(); |
674 |
+ |
final CountDownLatch locked = new CountDownLatch(1); |
675 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
676 |
|
public void realRun() throws InterruptedException { |
677 |
|
lock.lock(); |
678 |
+ |
assertHasNoWaiters(lock, c); |
679 |
|
assertFalse(lock.hasWaiters(c)); |
680 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
680 |
> |
locked.countDown(); |
681 |
|
c.await(); |
682 |
+ |
assertHasNoWaiters(lock, c); |
683 |
+ |
assertFalse(lock.hasWaiters(c)); |
684 |
|
lock.unlock(); |
685 |
|
}}); |
686 |
|
|
687 |
< |
delay(SHORT_DELAY_MS); |
687 |
> |
locked.await(); |
688 |
|
lock.lock(); |
689 |
+ |
assertHasWaiters(lock, c, t); |
690 |
|
assertTrue(lock.hasWaiters(c)); |
605 |
– |
assertEquals(1, lock.getWaitQueueLength(c)); |
691 |
|
c.signal(); |
692 |
< |
lock.unlock(); |
608 |
< |
delay(SHORT_DELAY_MS); |
609 |
< |
lock.lock(); |
692 |
> |
assertHasNoWaiters(lock, c); |
693 |
|
assertFalse(lock.hasWaiters(c)); |
611 |
– |
assertEquals(0, lock.getWaitQueueLength(c)); |
694 |
|
lock.unlock(); |
695 |
|
awaitTermination(t); |
696 |
+ |
assertHasNoWaiters(lock, c); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
|
* getWaitQueueLength returns number of waiting threads |
701 |
|
*/ |
702 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
703 |
< |
final ReentrantLock lock = new ReentrantLock(); |
703 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
704 |
|
final Condition c = lock.newCondition(); |
705 |
< |
Thread t1 = newStartedThread(new CheckedRunnable() { |
705 |
> |
final CountDownLatch locked1 = new CountDownLatch(1); |
706 |
> |
final CountDownLatch locked2 = new CountDownLatch(1); |
707 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
708 |
|
public void realRun() throws InterruptedException { |
709 |
|
lock.lock(); |
710 |
|
assertFalse(lock.hasWaiters(c)); |
711 |
|
assertEquals(0, lock.getWaitQueueLength(c)); |
712 |
+ |
locked1.countDown(); |
713 |
|
c.await(); |
714 |
|
lock.unlock(); |
715 |
|
}}); |
716 |
|
|
717 |
< |
delay(SHORT_DELAY_MS); |
632 |
< |
|
633 |
< |
Thread t2 = newStartedThread(new CheckedRunnable() { |
717 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
718 |
|
public void realRun() throws InterruptedException { |
719 |
|
lock.lock(); |
720 |
|
assertTrue(lock.hasWaiters(c)); |
721 |
|
assertEquals(1, lock.getWaitQueueLength(c)); |
722 |
+ |
locked2.countDown(); |
723 |
|
c.await(); |
724 |
|
lock.unlock(); |
725 |
|
}}); |
726 |
|
|
642 |
– |
delay(SHORT_DELAY_MS); |
727 |
|
lock.lock(); |
728 |
< |
assertTrue(lock.hasWaiters(c)); |
645 |
< |
assertEquals(2, lock.getWaitQueueLength(c)); |
646 |
< |
c.signalAll(); |
728 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
729 |
|
lock.unlock(); |
730 |
< |
delay(SHORT_DELAY_MS); |
730 |
> |
|
731 |
> |
t1.start(); |
732 |
> |
locked1.await(); |
733 |
> |
|
734 |
|
lock.lock(); |
735 |
< |
assertFalse(lock.hasWaiters(c)); |
736 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
735 |
> |
assertHasWaiters(lock, c, t1); |
736 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
737 |
> |
lock.unlock(); |
738 |
> |
|
739 |
> |
t2.start(); |
740 |
> |
locked2.await(); |
741 |
> |
|
742 |
> |
lock.lock(); |
743 |
> |
assertHasWaiters(lock, c, t1, t2); |
744 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
745 |
> |
c.signalAll(); |
746 |
> |
assertHasNoWaiters(lock, c); |
747 |
|
lock.unlock(); |
748 |
+ |
|
749 |
|
awaitTermination(t1); |
750 |
|
awaitTermination(t2); |
751 |
+ |
|
752 |
+ |
assertHasNoWaiters(lock, c); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
758 |
|
public void testGetWaitingThreads() throws InterruptedException { |
759 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
760 |
|
final Condition c = lock.newCondition(); |
761 |
+ |
final CountDownLatch locked1 = new CountDownLatch(1); |
762 |
+ |
final CountDownLatch locked2 = new CountDownLatch(1); |
763 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
764 |
|
public void realRun() throws InterruptedException { |
765 |
|
lock.lock(); |
766 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
767 |
+ |
locked1.countDown(); |
768 |
|
c.await(); |
769 |
|
lock.unlock(); |
770 |
|
}}); |
773 |
|
public void realRun() throws InterruptedException { |
774 |
|
lock.lock(); |
775 |
|
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
776 |
+ |
locked2.countDown(); |
777 |
|
c.await(); |
778 |
|
lock.unlock(); |
779 |
|
}}); |
781 |
|
lock.lock(); |
782 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
783 |
|
lock.unlock(); |
784 |
+ |
|
785 |
|
t1.start(); |
786 |
< |
delay(SHORT_DELAY_MS); |
786 |
> |
locked1.await(); |
787 |
> |
|
788 |
> |
lock.lock(); |
789 |
> |
assertHasWaiters(lock, c, t1); |
790 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
791 |
> |
assertFalse(lock.getWaitingThreads(c).contains(t2)); |
792 |
> |
assertEquals(1, lock.getWaitingThreads(c).size()); |
793 |
> |
lock.unlock(); |
794 |
> |
|
795 |
|
t2.start(); |
796 |
< |
delay(SHORT_DELAY_MS); |
796 |
> |
locked2.await(); |
797 |
> |
|
798 |
|
lock.lock(); |
799 |
< |
assertTrue(lock.hasWaiters(c)); |
799 |
> |
assertHasWaiters(lock, c, t1, t2); |
800 |
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
801 |
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
802 |
+ |
assertEquals(2, lock.getWaitingThreads(c).size()); |
803 |
|
c.signalAll(); |
804 |
+ |
assertHasNoWaiters(lock, c); |
805 |
|
lock.unlock(); |
806 |
< |
delay(SHORT_DELAY_MS); |
693 |
< |
lock.lock(); |
694 |
< |
assertFalse(lock.hasWaiters(c)); |
695 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
696 |
< |
lock.unlock(); |
806 |
> |
|
807 |
|
awaitTermination(t1); |
808 |
|
awaitTermination(t2); |
699 |
– |
} |
700 |
– |
|
701 |
– |
/** A helper class for uninterruptible wait tests */ |
702 |
– |
class UninterruptibleThread extends Thread { |
703 |
– |
private ReentrantLock lock; |
704 |
– |
private Condition c; |
705 |
– |
|
706 |
– |
public volatile boolean canAwake = false; |
707 |
– |
public volatile boolean interrupted = false; |
708 |
– |
public volatile boolean lockStarted = false; |
709 |
– |
|
710 |
– |
public UninterruptibleThread(ReentrantLock lock, Condition c) { |
711 |
– |
this.lock = lock; |
712 |
– |
this.c = c; |
713 |
– |
} |
714 |
– |
|
715 |
– |
public synchronized void run() { |
716 |
– |
lock.lock(); |
717 |
– |
lockStarted = true; |
718 |
– |
|
719 |
– |
while (!canAwake) { |
720 |
– |
c.awaitUninterruptibly(); |
721 |
– |
} |
809 |
|
|
810 |
< |
interrupted = isInterrupted(); |
724 |
< |
lock.unlock(); |
725 |
< |
} |
810 |
> |
assertHasNoWaiters(lock, c); |
811 |
|
} |
812 |
|
|
813 |
|
/** |
816 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
817 |
|
final ReentrantLock lock = new ReentrantLock(); |
818 |
|
final Condition c = lock.newCondition(); |
819 |
< |
UninterruptibleThread thread = new UninterruptibleThread(lock, c); |
820 |
< |
|
821 |
< |
thread.start(); |
822 |
< |
|
823 |
< |
while (!thread.lockStarted) { |
824 |
< |
delay(100); |
825 |
< |
} |
819 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
820 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
821 |
> |
public void realRun() { |
822 |
> |
lock.lock(); |
823 |
> |
locked.countDown(); |
824 |
> |
c.awaitUninterruptibly(); |
825 |
> |
assertTrue(Thread.interrupted()); |
826 |
> |
lock.unlock(); |
827 |
> |
}}); |
828 |
|
|
829 |
+ |
locked.await(); |
830 |
|
lock.lock(); |
831 |
< |
try { |
832 |
< |
thread.interrupt(); |
833 |
< |
thread.canAwake = true; |
834 |
< |
c.signal(); |
835 |
< |
} finally { |
836 |
< |
lock.unlock(); |
837 |
< |
} |
838 |
< |
|
751 |
< |
thread.join(); |
752 |
< |
assertTrue(thread.interrupted); |
753 |
< |
assertFalse(thread.isAlive()); |
831 |
> |
lock.unlock(); |
832 |
> |
t.interrupt(); |
833 |
> |
long timeoutMillis = 10; |
834 |
> |
assertThreadJoinTimesOut(t, timeoutMillis); |
835 |
> |
lock.lock(); |
836 |
> |
c.signal(); |
837 |
> |
lock.unlock(); |
838 |
> |
awaitTermination(t); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
885 |
|
assertHasNoWaiters(lock, c); |
886 |
|
locked.countDown(); |
887 |
|
try { |
888 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
888 |
> |
c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
889 |
|
} finally { |
890 |
|
assertTrue(lock.isLocked()); |
891 |
|
assertTrue(lock.isHeldByCurrentThread()); |
918 |
|
locked.countDown(); |
919 |
|
java.util.Date d = new java.util.Date(); |
920 |
|
try { |
921 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
921 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)); |
922 |
|
} finally { |
923 |
|
assertTrue(lock.isLocked()); |
924 |
|
assertTrue(lock.isHeldByCurrentThread()); |
939 |
|
* signalAll wakes up all threads |
940 |
|
*/ |
941 |
|
public void testSignalAll() throws InterruptedException { |
942 |
< |
final ReentrantLock lock = new ReentrantLock(); |
942 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
943 |
|
final Condition c = lock.newCondition(); |
944 |
+ |
final CountDownLatch locked = new CountDownLatch(2); |
945 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
946 |
|
public void realRun() throws InterruptedException { |
947 |
|
lock.lock(); |
948 |
+ |
locked.countDown(); |
949 |
|
c.await(); |
950 |
|
lock.unlock(); |
951 |
|
}}); |
953 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
954 |
|
public void realRun() throws InterruptedException { |
955 |
|
lock.lock(); |
956 |
+ |
locked.countDown(); |
957 |
|
c.await(); |
958 |
|
lock.unlock(); |
959 |
|
}}); |
960 |
|
|
961 |
< |
delay(SHORT_DELAY_MS); |
961 |
> |
locked.await(); |
962 |
|
lock.lock(); |
963 |
+ |
assertHasWaiters(lock, c, t1, t2); |
964 |
|
c.signalAll(); |
965 |
+ |
assertHasNoWaiters(lock, c); |
966 |
|
lock.unlock(); |
967 |
|
awaitTermination(t1); |
968 |
|
awaitTermination(t2); |
972 |
|
* await after multiple reentrant locking preserves lock count |
973 |
|
*/ |
974 |
|
public void testAwaitLockCount() throws InterruptedException { |
975 |
< |
final ReentrantLock lock = new ReentrantLock(); |
975 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
976 |
|
final Condition c = lock.newCondition(); |
977 |
+ |
final CountDownLatch locked = new CountDownLatch(2); |
978 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
979 |
|
public void realRun() throws InterruptedException { |
980 |
|
lock.lock(); |
981 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
982 |
|
assertEquals(1, lock.getHoldCount()); |
983 |
+ |
locked.countDown(); |
984 |
|
c.await(); |
985 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
986 |
|
assertEquals(1, lock.getHoldCount()); |
987 |
|
lock.unlock(); |
988 |
|
}}); |
991 |
|
public void realRun() throws InterruptedException { |
992 |
|
lock.lock(); |
993 |
|
lock.lock(); |
994 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
995 |
|
assertEquals(2, lock.getHoldCount()); |
996 |
+ |
locked.countDown(); |
997 |
|
c.await(); |
998 |
+ |
assertLockedBy(lock, Thread.currentThread()); |
999 |
|
assertEquals(2, lock.getHoldCount()); |
1000 |
|
lock.unlock(); |
1001 |
|
lock.unlock(); |
1002 |
|
}}); |
1003 |
|
|
1004 |
< |
delay(SHORT_DELAY_MS); |
1004 |
> |
locked.await(); |
1005 |
|
lock.lock(); |
1006 |
+ |
assertHasWaiters(lock, c, t1, t2); |
1007 |
+ |
assertEquals(1, lock.getHoldCount()); |
1008 |
|
c.signalAll(); |
1009 |
+ |
assertHasNoWaiters(lock, c); |
1010 |
|
lock.unlock(); |
1011 |
|
awaitTermination(t1); |
1012 |
|
awaitTermination(t2); |
1046 |
|
String ls = lock.toString(); |
1047 |
|
assertTrue(ls.indexOf("Locked") >= 0); |
1048 |
|
} |
949 |
– |
|
1049 |
|
} |