35394
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation.
|
|
7 |
*
|
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
12 |
* accompanied this code).
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License version
|
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 |
*
|
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
19 |
* or visit www.oracle.com if you need additional information or have any
|
|
20 |
* questions.
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* This file is available under and governed by the GNU General Public
|
|
25 |
* License version 2 only, as published by the Free Software Foundation.
|
|
26 |
* However, the following notice accompanied the original version of this
|
|
27 |
* file:
|
|
28 |
*
|
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
|
30 |
* Expert Group and released to the public domain, as explained at
|
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes,
|
|
33 |
* Pat Fisher, Mike Judd.
|
|
34 |
*/
|
|
35 |
|
|
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
37 |
|
|
38 |
import java.util.Arrays;
|
|
39 |
import java.util.Collection;
|
|
40 |
import java.util.HashSet;
|
|
41 |
import java.util.concurrent.CountDownLatch;
|
|
42 |
import java.util.concurrent.CyclicBarrier;
|
|
43 |
import java.util.concurrent.locks.Condition;
|
|
44 |
import java.util.concurrent.locks.ReentrantLock;
|
|
45 |
|
|
46 |
import junit.framework.AssertionFailedError;
|
|
47 |
import junit.framework.Test;
|
|
48 |
import junit.framework.TestSuite;
|
|
49 |
|
|
50 |
public class ReentrantLockTest extends JSR166TestCase {
|
|
51 |
public static void main(String[] args) {
|
|
52 |
main(suite(), args);
|
|
53 |
}
|
|
54 |
public static Test suite() {
|
|
55 |
return new TestSuite(ReentrantLockTest.class);
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* A checked runnable calling lockInterruptibly
|
|
60 |
*/
|
|
61 |
class InterruptibleLockRunnable extends CheckedRunnable {
|
|
62 |
final ReentrantLock lock;
|
|
63 |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
|
|
64 |
public void realRun() throws InterruptedException {
|
|
65 |
lock.lockInterruptibly();
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* A checked runnable calling lockInterruptibly that expects to be
|
|
71 |
* interrupted
|
|
72 |
*/
|
|
73 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable {
|
|
74 |
final ReentrantLock lock;
|
|
75 |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
|
|
76 |
public void realRun() throws InterruptedException {
|
|
77 |
lock.lockInterruptibly();
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Subclass to expose protected methods
|
|
83 |
*/
|
|
84 |
static class PublicReentrantLock extends ReentrantLock {
|
|
85 |
PublicReentrantLock() { super(); }
|
|
86 |
PublicReentrantLock(boolean fair) { super(fair); }
|
|
87 |
public Thread getOwner() {
|
|
88 |
return super.getOwner();
|
|
89 |
}
|
|
90 |
public Collection<Thread> getQueuedThreads() {
|
|
91 |
return super.getQueuedThreads();
|
|
92 |
}
|
|
93 |
public Collection<Thread> getWaitingThreads(Condition c) {
|
|
94 |
return super.getWaitingThreads(c);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Releases write lock, checking that it had a hold count of 1.
|
|
100 |
*/
|
|
101 |
void releaseLock(PublicReentrantLock lock) {
|
|
102 |
assertLockedByMoi(lock);
|
|
103 |
lock.unlock();
|
|
104 |
assertFalse(lock.isHeldByCurrentThread());
|
|
105 |
assertNotLocked(lock);
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Spin-waits until lock.hasQueuedThread(t) becomes true.
|
|
110 |
*/
|
|
111 |
void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
|
|
112 |
long startTime = System.nanoTime();
|
|
113 |
while (!lock.hasQueuedThread(t)) {
|
|
114 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS)
|
|
115 |
throw new AssertionFailedError("timed out");
|
|
116 |
Thread.yield();
|
|
117 |
}
|
|
118 |
assertTrue(t.isAlive());
|
|
119 |
assertNotSame(t, lock.getOwner());
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Checks that lock is not locked.
|
|
124 |
*/
|
|
125 |
void assertNotLocked(PublicReentrantLock lock) {
|
|
126 |
assertFalse(lock.isLocked());
|
|
127 |
assertFalse(lock.isHeldByCurrentThread());
|
|
128 |
assertNull(lock.getOwner());
|
|
129 |
assertEquals(0, lock.getHoldCount());
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Checks that lock is locked by the given thread.
|
|
134 |
*/
|
|
135 |
void assertLockedBy(PublicReentrantLock lock, Thread t) {
|
|
136 |
assertTrue(lock.isLocked());
|
|
137 |
assertSame(t, lock.getOwner());
|
|
138 |
assertEquals(t == Thread.currentThread(),
|
|
139 |
lock.isHeldByCurrentThread());
|
|
140 |
assertEquals(t == Thread.currentThread(),
|
|
141 |
lock.getHoldCount() > 0);
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Checks that lock is locked by the current thread.
|
|
146 |
*/
|
|
147 |
void assertLockedByMoi(PublicReentrantLock lock) {
|
|
148 |
assertLockedBy(lock, Thread.currentThread());
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Checks that condition c has no waiters.
|
|
153 |
*/
|
|
154 |
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
|
|
155 |
assertHasWaiters(lock, c, new Thread[] {});
|
|
156 |
}
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Checks that condition c has exactly the given waiter threads.
|
|
160 |
*/
|
|
161 |
void assertHasWaiters(PublicReentrantLock lock, Condition c,
|
|
162 |
Thread... threads) {
|
|
163 |
lock.lock();
|
|
164 |
assertEquals(threads.length > 0, lock.hasWaiters(c));
|
|
165 |
assertEquals(threads.length, lock.getWaitQueueLength(c));
|
|
166 |
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
|
|
167 |
assertEquals(threads.length, lock.getWaitingThreads(c).size());
|
|
168 |
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
|
|
169 |
new HashSet<Thread>(Arrays.asList(threads)));
|
|
170 |
lock.unlock();
|
|
171 |
}
|
|
172 |
|
|
173 |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Awaits condition "indefinitely" using the specified AwaitMethod.
|
|
177 |
*/
|
|
178 |
void await(Condition c, AwaitMethod awaitMethod)
|
|
179 |
throws InterruptedException {
|
|
180 |
long timeoutMillis = 2 * LONG_DELAY_MS;
|
|
181 |
switch (awaitMethod) {
|
|
182 |
case await:
|
|
183 |
c.await();
|
|
184 |
break;
|
|
185 |
case awaitTimed:
|
|
186 |
assertTrue(c.await(timeoutMillis, MILLISECONDS));
|
|
187 |
break;
|
|
188 |
case awaitNanos:
|
|
189 |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
|
|
190 |
long nanosRemaining = c.awaitNanos(timeoutNanos);
|
|
191 |
assertTrue(nanosRemaining > timeoutNanos / 2);
|
|
192 |
assertTrue(nanosRemaining <= timeoutNanos);
|
|
193 |
break;
|
|
194 |
case awaitUntil:
|
|
195 |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
|
|
196 |
break;
|
|
197 |
default:
|
|
198 |
throw new AssertionError();
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Constructor sets given fairness, and is in unlocked state
|
|
204 |
*/
|
|
205 |
public void testConstructor() {
|
|
206 |
PublicReentrantLock lock;
|
|
207 |
|
|
208 |
lock = new PublicReentrantLock();
|
|
209 |
assertFalse(lock.isFair());
|
|
210 |
assertNotLocked(lock);
|
|
211 |
|
|
212 |
lock = new PublicReentrantLock(true);
|
|
213 |
assertTrue(lock.isFair());
|
|
214 |
assertNotLocked(lock);
|
|
215 |
|
|
216 |
lock = new PublicReentrantLock(false);
|
|
217 |
assertFalse(lock.isFair());
|
|
218 |
assertNotLocked(lock);
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* locking an unlocked lock succeeds
|
|
223 |
*/
|
|
224 |
public void testLock() { testLock(false); }
|
|
225 |
public void testLock_fair() { testLock(true); }
|
|
226 |
public void testLock(boolean fair) {
|
|
227 |
PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
228 |
lock.lock();
|
|
229 |
assertLockedByMoi(lock);
|
|
230 |
releaseLock(lock);
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* Unlocking an unlocked lock throws IllegalMonitorStateException
|
|
235 |
*/
|
|
236 |
public void testUnlock_IMSE() { testUnlock_IMSE(false); }
|
|
237 |
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
|
|
238 |
public void testUnlock_IMSE(boolean fair) {
|
|
239 |
ReentrantLock lock = new ReentrantLock(fair);
|
|
240 |
try {
|
|
241 |
lock.unlock();
|
|
242 |
shouldThrow();
|
|
243 |
} catch (IllegalMonitorStateException success) {}
|
|
244 |
}
|
|
245 |
|
|
246 |
/**
|
|
247 |
* tryLock on an unlocked lock succeeds
|
|
248 |
*/
|
|
249 |
public void testTryLock() { testTryLock(false); }
|
|
250 |
public void testTryLock_fair() { testTryLock(true); }
|
|
251 |
public void testTryLock(boolean fair) {
|
|
252 |
PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
253 |
assertTrue(lock.tryLock());
|
|
254 |
assertLockedByMoi(lock);
|
|
255 |
assertTrue(lock.tryLock());
|
|
256 |
assertLockedByMoi(lock);
|
|
257 |
lock.unlock();
|
|
258 |
releaseLock(lock);
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* hasQueuedThreads reports whether there are waiting threads
|
|
263 |
*/
|
|
264 |
public void testHasQueuedThreads() { testHasQueuedThreads(false); }
|
|
265 |
public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
|
|
266 |
public void testHasQueuedThreads(boolean fair) {
|
|
267 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
268 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
|
|
269 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
|
|
270 |
assertFalse(lock.hasQueuedThreads());
|
|
271 |
lock.lock();
|
|
272 |
assertFalse(lock.hasQueuedThreads());
|
|
273 |
t1.start();
|
|
274 |
waitForQueuedThread(lock, t1);
|
|
275 |
assertTrue(lock.hasQueuedThreads());
|
|
276 |
t2.start();
|
|
277 |
waitForQueuedThread(lock, t2);
|
|
278 |
assertTrue(lock.hasQueuedThreads());
|
|
279 |
t1.interrupt();
|
|
280 |
awaitTermination(t1);
|
|
281 |
assertTrue(lock.hasQueuedThreads());
|
|
282 |
lock.unlock();
|
|
283 |
awaitTermination(t2);
|
|
284 |
assertFalse(lock.hasQueuedThreads());
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* getQueueLength reports number of waiting threads
|
|
289 |
*/
|
|
290 |
public void testGetQueueLength() { testGetQueueLength(false); }
|
|
291 |
public void testGetQueueLength_fair() { testGetQueueLength(true); }
|
|
292 |
public void testGetQueueLength(boolean fair) {
|
|
293 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
294 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
|
|
295 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
|
|
296 |
assertEquals(0, lock.getQueueLength());
|
|
297 |
lock.lock();
|
|
298 |
t1.start();
|
|
299 |
waitForQueuedThread(lock, t1);
|
|
300 |
assertEquals(1, lock.getQueueLength());
|
|
301 |
t2.start();
|
|
302 |
waitForQueuedThread(lock, t2);
|
|
303 |
assertEquals(2, lock.getQueueLength());
|
|
304 |
t1.interrupt();
|
|
305 |
awaitTermination(t1);
|
|
306 |
assertEquals(1, lock.getQueueLength());
|
|
307 |
lock.unlock();
|
|
308 |
awaitTermination(t2);
|
|
309 |
assertEquals(0, lock.getQueueLength());
|
|
310 |
}
|
|
311 |
|
|
312 |
/**
|
|
313 |
* hasQueuedThread(null) throws NPE
|
|
314 |
*/
|
|
315 |
public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
|
|
316 |
public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
|
|
317 |
public void testHasQueuedThreadNPE(boolean fair) {
|
|
318 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
319 |
try {
|
|
320 |
lock.hasQueuedThread(null);
|
|
321 |
shouldThrow();
|
|
322 |
} catch (NullPointerException success) {}
|
|
323 |
}
|
|
324 |
|
|
325 |
/**
|
|
326 |
* hasQueuedThread reports whether a thread is queued
|
|
327 |
*/
|
|
328 |
public void testHasQueuedThread() { testHasQueuedThread(false); }
|
|
329 |
public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
|
|
330 |
public void testHasQueuedThread(boolean fair) {
|
|
331 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
332 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
|
|
333 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
|
|
334 |
assertFalse(lock.hasQueuedThread(t1));
|
|
335 |
assertFalse(lock.hasQueuedThread(t2));
|
|
336 |
lock.lock();
|
|
337 |
t1.start();
|
|
338 |
waitForQueuedThread(lock, t1);
|
|
339 |
assertTrue(lock.hasQueuedThread(t1));
|
|
340 |
assertFalse(lock.hasQueuedThread(t2));
|
|
341 |
t2.start();
|
|
342 |
waitForQueuedThread(lock, t2);
|
|
343 |
assertTrue(lock.hasQueuedThread(t1));
|
|
344 |
assertTrue(lock.hasQueuedThread(t2));
|
|
345 |
t1.interrupt();
|
|
346 |
awaitTermination(t1);
|
|
347 |
assertFalse(lock.hasQueuedThread(t1));
|
|
348 |
assertTrue(lock.hasQueuedThread(t2));
|
|
349 |
lock.unlock();
|
|
350 |
awaitTermination(t2);
|
|
351 |
assertFalse(lock.hasQueuedThread(t1));
|
|
352 |
assertFalse(lock.hasQueuedThread(t2));
|
|
353 |
}
|
|
354 |
|
|
355 |
/**
|
|
356 |
* getQueuedThreads includes waiting threads
|
|
357 |
*/
|
|
358 |
public void testGetQueuedThreads() { testGetQueuedThreads(false); }
|
|
359 |
public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
|
|
360 |
public void testGetQueuedThreads(boolean fair) {
|
|
361 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
362 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
|
|
363 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
|
|
364 |
assertTrue(lock.getQueuedThreads().isEmpty());
|
|
365 |
lock.lock();
|
|
366 |
assertTrue(lock.getQueuedThreads().isEmpty());
|
|
367 |
t1.start();
|
|
368 |
waitForQueuedThread(lock, t1);
|
|
369 |
assertEquals(1, lock.getQueuedThreads().size());
|
|
370 |
assertTrue(lock.getQueuedThreads().contains(t1));
|
|
371 |
t2.start();
|
|
372 |
waitForQueuedThread(lock, t2);
|
|
373 |
assertEquals(2, lock.getQueuedThreads().size());
|
|
374 |
assertTrue(lock.getQueuedThreads().contains(t1));
|
|
375 |
assertTrue(lock.getQueuedThreads().contains(t2));
|
|
376 |
t1.interrupt();
|
|
377 |
awaitTermination(t1);
|
|
378 |
assertFalse(lock.getQueuedThreads().contains(t1));
|
|
379 |
assertTrue(lock.getQueuedThreads().contains(t2));
|
|
380 |
assertEquals(1, lock.getQueuedThreads().size());
|
|
381 |
lock.unlock();
|
|
382 |
awaitTermination(t2);
|
|
383 |
assertTrue(lock.getQueuedThreads().isEmpty());
|
|
384 |
}
|
|
385 |
|
|
386 |
/**
|
|
387 |
* timed tryLock is interruptible
|
|
388 |
*/
|
|
389 |
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); }
|
|
390 |
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
|
|
391 |
public void testTryLock_Interruptible(boolean fair) {
|
|
392 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
393 |
lock.lock();
|
|
394 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
|
|
395 |
public void realRun() throws InterruptedException {
|
|
396 |
lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
|
|
397 |
}});
|
|
398 |
|
|
399 |
waitForQueuedThread(lock, t);
|
|
400 |
t.interrupt();
|
|
401 |
awaitTermination(t);
|
|
402 |
releaseLock(lock);
|
|
403 |
}
|
|
404 |
|
|
405 |
/**
|
|
406 |
* tryLock on a locked lock fails
|
|
407 |
*/
|
|
408 |
public void testTryLockWhenLocked() { testTryLockWhenLocked(false); }
|
|
409 |
public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
|
|
410 |
public void testTryLockWhenLocked(boolean fair) {
|
|
411 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
412 |
lock.lock();
|
|
413 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
414 |
public void realRun() {
|
|
415 |
assertFalse(lock.tryLock());
|
|
416 |
}});
|
|
417 |
|
|
418 |
awaitTermination(t);
|
|
419 |
releaseLock(lock);
|
|
420 |
}
|
|
421 |
|
|
422 |
/**
|
|
423 |
* Timed tryLock on a locked lock times out
|
|
424 |
*/
|
|
425 |
public void testTryLock_Timeout() { testTryLock_Timeout(false); }
|
|
426 |
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
|
|
427 |
public void testTryLock_Timeout(boolean fair) {
|
|
428 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
429 |
lock.lock();
|
|
430 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
431 |
public void realRun() throws InterruptedException {
|
|
432 |
long startTime = System.nanoTime();
|
|
433 |
long timeoutMillis = 10;
|
|
434 |
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
|
|
435 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
|
|
436 |
}});
|
|
437 |
|
|
438 |
awaitTermination(t);
|
|
439 |
releaseLock(lock);
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
|
443 |
* getHoldCount returns number of recursive holds
|
|
444 |
*/
|
|
445 |
public void testGetHoldCount() { testGetHoldCount(false); }
|
|
446 |
public void testGetHoldCount_fair() { testGetHoldCount(true); }
|
|
447 |
public void testGetHoldCount(boolean fair) {
|
|
448 |
ReentrantLock lock = new ReentrantLock(fair);
|
|
449 |
for (int i = 1; i <= SIZE; i++) {
|
|
450 |
lock.lock();
|
|
451 |
assertEquals(i, lock.getHoldCount());
|
|
452 |
}
|
|
453 |
for (int i = SIZE; i > 0; i--) {
|
|
454 |
lock.unlock();
|
|
455 |
assertEquals(i - 1, lock.getHoldCount());
|
|
456 |
}
|
|
457 |
}
|
|
458 |
|
|
459 |
/**
|
|
460 |
* isLocked is true when locked and false when not
|
|
461 |
*/
|
|
462 |
public void testIsLocked() { testIsLocked(false); }
|
|
463 |
public void testIsLocked_fair() { testIsLocked(true); }
|
|
464 |
public void testIsLocked(boolean fair) {
|
|
465 |
try {
|
|
466 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
467 |
assertFalse(lock.isLocked());
|
|
468 |
lock.lock();
|
|
469 |
assertTrue(lock.isLocked());
|
|
470 |
lock.lock();
|
|
471 |
assertTrue(lock.isLocked());
|
|
472 |
lock.unlock();
|
|
473 |
assertTrue(lock.isLocked());
|
|
474 |
lock.unlock();
|
|
475 |
assertFalse(lock.isLocked());
|
|
476 |
final CyclicBarrier barrier = new CyclicBarrier(2);
|
|
477 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
478 |
public void realRun() throws Exception {
|
|
479 |
lock.lock();
|
|
480 |
assertTrue(lock.isLocked());
|
|
481 |
barrier.await();
|
|
482 |
barrier.await();
|
|
483 |
lock.unlock();
|
|
484 |
}});
|
|
485 |
|
|
486 |
barrier.await();
|
|
487 |
assertTrue(lock.isLocked());
|
|
488 |
barrier.await();
|
|
489 |
awaitTermination(t);
|
|
490 |
assertFalse(lock.isLocked());
|
|
491 |
} catch (Exception fail) { threadUnexpectedException(fail); }
|
|
492 |
}
|
|
493 |
|
|
494 |
/**
|
|
495 |
* lockInterruptibly succeeds when unlocked, else is interruptible
|
|
496 |
*/
|
|
497 |
public void testLockInterruptibly() { testLockInterruptibly(false); }
|
|
498 |
public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
|
|
499 |
public void testLockInterruptibly(boolean fair) {
|
|
500 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
501 |
try {
|
|
502 |
lock.lockInterruptibly();
|
|
503 |
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
|
|
504 |
assertLockedByMoi(lock);
|
|
505 |
Thread t = newStartedThread(new InterruptedLockRunnable(lock));
|
|
506 |
waitForQueuedThread(lock, t);
|
|
507 |
t.interrupt();
|
|
508 |
assertTrue(lock.isLocked());
|
|
509 |
assertTrue(lock.isHeldByCurrentThread());
|
|
510 |
awaitTermination(t);
|
|
511 |
releaseLock(lock);
|
|
512 |
}
|
|
513 |
|
|
514 |
/**
|
|
515 |
* Calling await without holding lock throws IllegalMonitorStateException
|
|
516 |
*/
|
|
517 |
public void testAwait_IMSE() { testAwait_IMSE(false); }
|
|
518 |
public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
|
|
519 |
public void testAwait_IMSE(boolean fair) {
|
|
520 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
521 |
final Condition c = lock.newCondition();
|
|
522 |
for (AwaitMethod awaitMethod : AwaitMethod.values()) {
|
|
523 |
long startTime = System.nanoTime();
|
|
524 |
try {
|
|
525 |
await(c, awaitMethod);
|
|
526 |
shouldThrow();
|
|
527 |
} catch (IllegalMonitorStateException success) {
|
|
528 |
} catch (InterruptedException e) { threadUnexpectedException(e); }
|
|
529 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
|
|
530 |
}
|
|
531 |
}
|
|
532 |
|
|
533 |
/**
|
|
534 |
* Calling signal without holding lock throws IllegalMonitorStateException
|
|
535 |
*/
|
|
536 |
public void testSignal_IMSE() { testSignal_IMSE(false); }
|
|
537 |
public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
|
|
538 |
public void testSignal_IMSE(boolean fair) {
|
|
539 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
540 |
final Condition c = lock.newCondition();
|
|
541 |
try {
|
|
542 |
c.signal();
|
|
543 |
shouldThrow();
|
|
544 |
} catch (IllegalMonitorStateException success) {}
|
|
545 |
}
|
|
546 |
|
|
547 |
/**
|
|
548 |
* awaitNanos without a signal times out
|
|
549 |
*/
|
|
550 |
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); }
|
|
551 |
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
|
|
552 |
public void testAwaitNanos_Timeout(boolean fair) {
|
|
553 |
try {
|
|
554 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
555 |
final Condition c = lock.newCondition();
|
|
556 |
lock.lock();
|
|
557 |
long startTime = System.nanoTime();
|
|
558 |
long timeoutMillis = 10;
|
|
559 |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
|
|
560 |
long nanosRemaining = c.awaitNanos(timeoutNanos);
|
|
561 |
assertTrue(nanosRemaining <= 0);
|
|
562 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
|
|
563 |
lock.unlock();
|
|
564 |
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
|
|
565 |
}
|
|
566 |
|
|
567 |
/**
|
|
568 |
* timed await without a signal times out
|
|
569 |
*/
|
|
570 |
public void testAwait_Timeout() { testAwait_Timeout(false); }
|
|
571 |
public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
|
|
572 |
public void testAwait_Timeout(boolean fair) {
|
|
573 |
try {
|
|
574 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
575 |
final Condition c = lock.newCondition();
|
|
576 |
lock.lock();
|
|
577 |
long startTime = System.nanoTime();
|
|
578 |
long timeoutMillis = 10;
|
|
579 |
assertFalse(c.await(timeoutMillis, MILLISECONDS));
|
|
580 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
|
|
581 |
lock.unlock();
|
|
582 |
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
|
|
583 |
}
|
|
584 |
|
|
585 |
/**
|
|
586 |
* awaitUntil without a signal times out
|
|
587 |
*/
|
|
588 |
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
|
|
589 |
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
|
|
590 |
public void testAwaitUntil_Timeout(boolean fair) {
|
|
591 |
try {
|
|
592 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
593 |
final Condition c = lock.newCondition();
|
|
594 |
lock.lock();
|
|
595 |
// We shouldn't assume that nanoTime and currentTimeMillis
|
|
596 |
// use the same time source, so don't use nanoTime here.
|
|
597 |
java.util.Date delayedDate = delayedDate(timeoutMillis());
|
|
598 |
assertFalse(c.awaitUntil(delayedDate));
|
|
599 |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
|
|
600 |
lock.unlock();
|
|
601 |
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
|
|
602 |
}
|
|
603 |
|
|
604 |
/**
|
|
605 |
* await returns when signalled
|
|
606 |
*/
|
|
607 |
public void testAwait() { testAwait(false); }
|
|
608 |
public void testAwait_fair() { testAwait(true); }
|
|
609 |
public void testAwait(boolean fair) {
|
|
610 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
611 |
final Condition c = lock.newCondition();
|
|
612 |
final CountDownLatch locked = new CountDownLatch(1);
|
|
613 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
614 |
public void realRun() throws InterruptedException {
|
|
615 |
lock.lock();
|
|
616 |
locked.countDown();
|
|
617 |
c.await();
|
|
618 |
lock.unlock();
|
|
619 |
}});
|
|
620 |
|
|
621 |
await(locked);
|
|
622 |
lock.lock();
|
|
623 |
assertHasWaiters(lock, c, t);
|
|
624 |
c.signal();
|
|
625 |
assertHasNoWaiters(lock, c);
|
|
626 |
assertTrue(t.isAlive());
|
|
627 |
lock.unlock();
|
|
628 |
awaitTermination(t);
|
|
629 |
}
|
|
630 |
|
|
631 |
/**
|
|
632 |
* hasWaiters throws NPE if null
|
|
633 |
*/
|
|
634 |
public void testHasWaitersNPE() { testHasWaitersNPE(false); }
|
|
635 |
public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
|
|
636 |
public void testHasWaitersNPE(boolean fair) {
|
|
637 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
638 |
try {
|
|
639 |
lock.hasWaiters(null);
|
|
640 |
shouldThrow();
|
|
641 |
} catch (NullPointerException success) {}
|
|
642 |
}
|
|
643 |
|
|
644 |
/**
|
|
645 |
* getWaitQueueLength throws NPE if null
|
|
646 |
*/
|
|
647 |
public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
|
|
648 |
public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
|
|
649 |
public void testGetWaitQueueLengthNPE(boolean fair) {
|
|
650 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
651 |
try {
|
|
652 |
lock.getWaitQueueLength(null);
|
|
653 |
shouldThrow();
|
|
654 |
} catch (NullPointerException success) {}
|
|
655 |
}
|
|
656 |
|
|
657 |
/**
|
|
658 |
* getWaitingThreads throws NPE if null
|
|
659 |
*/
|
|
660 |
public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
|
|
661 |
public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
|
|
662 |
public void testGetWaitingThreadsNPE(boolean fair) {
|
|
663 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
664 |
try {
|
|
665 |
lock.getWaitingThreads(null);
|
|
666 |
shouldThrow();
|
|
667 |
} catch (NullPointerException success) {}
|
|
668 |
}
|
|
669 |
|
|
670 |
/**
|
|
671 |
* hasWaiters throws IllegalArgumentException if not owned
|
|
672 |
*/
|
|
673 |
public void testHasWaitersIAE() { testHasWaitersIAE(false); }
|
|
674 |
public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
|
|
675 |
public void testHasWaitersIAE(boolean fair) {
|
|
676 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
677 |
final Condition c = lock.newCondition();
|
|
678 |
final ReentrantLock lock2 = new ReentrantLock(fair);
|
|
679 |
try {
|
|
680 |
lock2.hasWaiters(c);
|
|
681 |
shouldThrow();
|
|
682 |
} catch (IllegalArgumentException success) {}
|
|
683 |
}
|
|
684 |
|
|
685 |
/**
|
|
686 |
* hasWaiters throws IllegalMonitorStateException if not locked
|
|
687 |
*/
|
|
688 |
public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
|
|
689 |
public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
|
|
690 |
public void testHasWaitersIMSE(boolean fair) {
|
|
691 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
692 |
final Condition c = lock.newCondition();
|
|
693 |
try {
|
|
694 |
lock.hasWaiters(c);
|
|
695 |
shouldThrow();
|
|
696 |
} catch (IllegalMonitorStateException success) {}
|
|
697 |
}
|
|
698 |
|
|
699 |
/**
|
|
700 |
* getWaitQueueLength throws IllegalArgumentException if not owned
|
|
701 |
*/
|
|
702 |
public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
|
|
703 |
public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
|
|
704 |
public void testGetWaitQueueLengthIAE(boolean fair) {
|
|
705 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
706 |
final Condition c = lock.newCondition();
|
|
707 |
final ReentrantLock lock2 = new ReentrantLock(fair);
|
|
708 |
try {
|
|
709 |
lock2.getWaitQueueLength(c);
|
|
710 |
shouldThrow();
|
|
711 |
} catch (IllegalArgumentException success) {}
|
|
712 |
}
|
|
713 |
|
|
714 |
/**
|
|
715 |
* getWaitQueueLength throws IllegalMonitorStateException if not locked
|
|
716 |
*/
|
|
717 |
public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
|
|
718 |
public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
|
|
719 |
public void testGetWaitQueueLengthIMSE(boolean fair) {
|
|
720 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
721 |
final Condition c = lock.newCondition();
|
|
722 |
try {
|
|
723 |
lock.getWaitQueueLength(c);
|
|
724 |
shouldThrow();
|
|
725 |
} catch (IllegalMonitorStateException success) {}
|
|
726 |
}
|
|
727 |
|
|
728 |
/**
|
|
729 |
* getWaitingThreads throws IllegalArgumentException if not owned
|
|
730 |
*/
|
|
731 |
public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
|
|
732 |
public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
|
|
733 |
public void testGetWaitingThreadsIAE(boolean fair) {
|
|
734 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
735 |
final Condition c = lock.newCondition();
|
|
736 |
final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
|
|
737 |
try {
|
|
738 |
lock2.getWaitingThreads(c);
|
|
739 |
shouldThrow();
|
|
740 |
} catch (IllegalArgumentException success) {}
|
|
741 |
}
|
|
742 |
|
|
743 |
/**
|
|
744 |
* getWaitingThreads throws IllegalMonitorStateException if not locked
|
|
745 |
*/
|
|
746 |
public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
|
|
747 |
public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
|
|
748 |
public void testGetWaitingThreadsIMSE(boolean fair) {
|
|
749 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
750 |
final Condition c = lock.newCondition();
|
|
751 |
try {
|
|
752 |
lock.getWaitingThreads(c);
|
|
753 |
shouldThrow();
|
|
754 |
} catch (IllegalMonitorStateException success) {}
|
|
755 |
}
|
|
756 |
|
|
757 |
/**
|
|
758 |
* hasWaiters returns true when a thread is waiting, else false
|
|
759 |
*/
|
|
760 |
public void testHasWaiters() { testHasWaiters(false); }
|
|
761 |
public void testHasWaiters_fair() { testHasWaiters(true); }
|
|
762 |
public void testHasWaiters(boolean fair) {
|
|
763 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
764 |
final Condition c = lock.newCondition();
|
|
765 |
final CountDownLatch pleaseSignal = new CountDownLatch(1);
|
|
766 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
767 |
public void realRun() throws InterruptedException {
|
|
768 |
lock.lock();
|
|
769 |
assertHasNoWaiters(lock, c);
|
|
770 |
assertFalse(lock.hasWaiters(c));
|
|
771 |
pleaseSignal.countDown();
|
|
772 |
c.await();
|
|
773 |
assertHasNoWaiters(lock, c);
|
|
774 |
assertFalse(lock.hasWaiters(c));
|
|
775 |
lock.unlock();
|
|
776 |
}});
|
|
777 |
|
|
778 |
await(pleaseSignal);
|
|
779 |
lock.lock();
|
|
780 |
assertHasWaiters(lock, c, t);
|
|
781 |
assertTrue(lock.hasWaiters(c));
|
|
782 |
c.signal();
|
|
783 |
assertHasNoWaiters(lock, c);
|
|
784 |
assertFalse(lock.hasWaiters(c));
|
|
785 |
lock.unlock();
|
|
786 |
awaitTermination(t);
|
|
787 |
assertHasNoWaiters(lock, c);
|
|
788 |
}
|
|
789 |
|
|
790 |
/**
|
|
791 |
* getWaitQueueLength returns number of waiting threads
|
|
792 |
*/
|
|
793 |
public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
|
|
794 |
public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
|
|
795 |
public void testGetWaitQueueLength(boolean fair) {
|
|
796 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
797 |
final Condition c = lock.newCondition();
|
|
798 |
final CountDownLatch locked1 = new CountDownLatch(1);
|
|
799 |
final CountDownLatch locked2 = new CountDownLatch(1);
|
|
800 |
Thread t1 = new Thread(new CheckedRunnable() {
|
|
801 |
public void realRun() throws InterruptedException {
|
|
802 |
lock.lock();
|
|
803 |
assertFalse(lock.hasWaiters(c));
|
|
804 |
assertEquals(0, lock.getWaitQueueLength(c));
|
|
805 |
locked1.countDown();
|
|
806 |
c.await();
|
|
807 |
lock.unlock();
|
|
808 |
}});
|
|
809 |
|
|
810 |
Thread t2 = new Thread(new CheckedRunnable() {
|
|
811 |
public void realRun() throws InterruptedException {
|
|
812 |
lock.lock();
|
|
813 |
assertTrue(lock.hasWaiters(c));
|
|
814 |
assertEquals(1, lock.getWaitQueueLength(c));
|
|
815 |
locked2.countDown();
|
|
816 |
c.await();
|
|
817 |
lock.unlock();
|
|
818 |
}});
|
|
819 |
|
|
820 |
lock.lock();
|
|
821 |
assertEquals(0, lock.getWaitQueueLength(c));
|
|
822 |
lock.unlock();
|
|
823 |
|
|
824 |
t1.start();
|
|
825 |
await(locked1);
|
|
826 |
|
|
827 |
lock.lock();
|
|
828 |
assertHasWaiters(lock, c, t1);
|
|
829 |
assertEquals(1, lock.getWaitQueueLength(c));
|
|
830 |
lock.unlock();
|
|
831 |
|
|
832 |
t2.start();
|
|
833 |
await(locked2);
|
|
834 |
|
|
835 |
lock.lock();
|
|
836 |
assertHasWaiters(lock, c, t1, t2);
|
|
837 |
assertEquals(2, lock.getWaitQueueLength(c));
|
|
838 |
c.signalAll();
|
|
839 |
assertHasNoWaiters(lock, c);
|
|
840 |
lock.unlock();
|
|
841 |
|
|
842 |
awaitTermination(t1);
|
|
843 |
awaitTermination(t2);
|
|
844 |
|
|
845 |
assertHasNoWaiters(lock, c);
|
|
846 |
}
|
|
847 |
|
|
848 |
/**
|
|
849 |
* getWaitingThreads returns only and all waiting threads
|
|
850 |
*/
|
|
851 |
public void testGetWaitingThreads() { testGetWaitingThreads(false); }
|
|
852 |
public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
|
|
853 |
public void testGetWaitingThreads(boolean fair) {
|
|
854 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
855 |
final Condition c = lock.newCondition();
|
|
856 |
final CountDownLatch locked1 = new CountDownLatch(1);
|
|
857 |
final CountDownLatch locked2 = new CountDownLatch(1);
|
|
858 |
Thread t1 = new Thread(new CheckedRunnable() {
|
|
859 |
public void realRun() throws InterruptedException {
|
|
860 |
lock.lock();
|
|
861 |
assertTrue(lock.getWaitingThreads(c).isEmpty());
|
|
862 |
locked1.countDown();
|
|
863 |
c.await();
|
|
864 |
lock.unlock();
|
|
865 |
}});
|
|
866 |
|
|
867 |
Thread t2 = new Thread(new CheckedRunnable() {
|
|
868 |
public void realRun() throws InterruptedException {
|
|
869 |
lock.lock();
|
|
870 |
assertFalse(lock.getWaitingThreads(c).isEmpty());
|
|
871 |
locked2.countDown();
|
|
872 |
c.await();
|
|
873 |
lock.unlock();
|
|
874 |
}});
|
|
875 |
|
|
876 |
lock.lock();
|
|
877 |
assertTrue(lock.getWaitingThreads(c).isEmpty());
|
|
878 |
lock.unlock();
|
|
879 |
|
|
880 |
t1.start();
|
|
881 |
await(locked1);
|
|
882 |
|
|
883 |
lock.lock();
|
|
884 |
assertHasWaiters(lock, c, t1);
|
|
885 |
assertTrue(lock.getWaitingThreads(c).contains(t1));
|
|
886 |
assertFalse(lock.getWaitingThreads(c).contains(t2));
|
|
887 |
assertEquals(1, lock.getWaitingThreads(c).size());
|
|
888 |
lock.unlock();
|
|
889 |
|
|
890 |
t2.start();
|
|
891 |
await(locked2);
|
|
892 |
|
|
893 |
lock.lock();
|
|
894 |
assertHasWaiters(lock, c, t1, t2);
|
|
895 |
assertTrue(lock.getWaitingThreads(c).contains(t1));
|
|
896 |
assertTrue(lock.getWaitingThreads(c).contains(t2));
|
|
897 |
assertEquals(2, lock.getWaitingThreads(c).size());
|
|
898 |
c.signalAll();
|
|
899 |
assertHasNoWaiters(lock, c);
|
|
900 |
lock.unlock();
|
|
901 |
|
|
902 |
awaitTermination(t1);
|
|
903 |
awaitTermination(t2);
|
|
904 |
|
|
905 |
assertHasNoWaiters(lock, c);
|
|
906 |
}
|
|
907 |
|
|
908 |
/**
|
|
909 |
* awaitUninterruptibly is uninterruptible
|
|
910 |
*/
|
|
911 |
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
|
|
912 |
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
|
|
913 |
public void testAwaitUninterruptibly(boolean fair) {
|
|
914 |
final ReentrantLock lock = new ReentrantLock(fair);
|
|
915 |
final Condition c = lock.newCondition();
|
|
916 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
|
|
917 |
|
|
918 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
919 |
public void realRun() {
|
|
920 |
// Interrupt before awaitUninterruptibly
|
|
921 |
lock.lock();
|
|
922 |
pleaseInterrupt.countDown();
|
|
923 |
Thread.currentThread().interrupt();
|
|
924 |
c.awaitUninterruptibly();
|
|
925 |
assertTrue(Thread.interrupted());
|
|
926 |
lock.unlock();
|
|
927 |
}});
|
|
928 |
|
|
929 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
930 |
public void realRun() {
|
|
931 |
// Interrupt during awaitUninterruptibly
|
|
932 |
lock.lock();
|
|
933 |
pleaseInterrupt.countDown();
|
|
934 |
c.awaitUninterruptibly();
|
|
935 |
assertTrue(Thread.interrupted());
|
|
936 |
lock.unlock();
|
|
937 |
}});
|
|
938 |
|
|
939 |
await(pleaseInterrupt);
|
|
940 |
lock.lock();
|
|
941 |
lock.unlock();
|
|
942 |
t2.interrupt();
|
|
943 |
|
|
944 |
assertThreadStaysAlive(t1);
|
|
945 |
assertTrue(t2.isAlive());
|
|
946 |
|
|
947 |
lock.lock();
|
|
948 |
c.signalAll();
|
|
949 |
lock.unlock();
|
|
950 |
|
|
951 |
awaitTermination(t1);
|
|
952 |
awaitTermination(t2);
|
|
953 |
}
|
|
954 |
|
|
955 |
/**
|
|
956 |
* await/awaitNanos/awaitUntil is interruptible
|
|
957 |
*/
|
|
958 |
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
|
|
959 |
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
|
|
960 |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
|
|
961 |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
|
|
962 |
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
|
|
963 |
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
|
|
964 |
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
|
|
965 |
public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
|
|
966 |
public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
|
|
967 |
final PublicReentrantLock lock =
|
|
968 |
new PublicReentrantLock(fair);
|
|
969 |
final Condition c = lock.newCondition();
|
|
970 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
|
|
971 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
|
|
972 |
public void realRun() throws InterruptedException {
|
|
973 |
lock.lock();
|
|
974 |
assertLockedByMoi(lock);
|
|
975 |
assertHasNoWaiters(lock, c);
|
|
976 |
pleaseInterrupt.countDown();
|
|
977 |
try {
|
|
978 |
await(c, awaitMethod);
|
|
979 |
} finally {
|
|
980 |
assertLockedByMoi(lock);
|
|
981 |
assertHasNoWaiters(lock, c);
|
|
982 |
lock.unlock();
|
|
983 |
assertFalse(Thread.interrupted());
|
|
984 |
}
|
|
985 |
}});
|
|
986 |
|
|
987 |
await(pleaseInterrupt);
|
|
988 |
assertHasWaiters(lock, c, t);
|
|
989 |
t.interrupt();
|
|
990 |
awaitTermination(t);
|
|
991 |
assertNotLocked(lock);
|
|
992 |
}
|
|
993 |
|
|
994 |
/**
|
|
995 |
* signalAll wakes up all threads
|
|
996 |
*/
|
|
997 |
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
|
|
998 |
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
|
|
999 |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
|
|
1000 |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
|
|
1001 |
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
|
|
1002 |
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
|
|
1003 |
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
|
|
1004 |
public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
|
|
1005 |
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
|
|
1006 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
1007 |
final Condition c = lock.newCondition();
|
|
1008 |
final CountDownLatch pleaseSignal = new CountDownLatch(2);
|
|
1009 |
class Awaiter extends CheckedRunnable {
|
|
1010 |
public void realRun() throws InterruptedException {
|
|
1011 |
lock.lock();
|
|
1012 |
pleaseSignal.countDown();
|
|
1013 |
await(c, awaitMethod);
|
|
1014 |
lock.unlock();
|
|
1015 |
}
|
|
1016 |
}
|
|
1017 |
|
|
1018 |
Thread t1 = newStartedThread(new Awaiter());
|
|
1019 |
Thread t2 = newStartedThread(new Awaiter());
|
|
1020 |
|
|
1021 |
await(pleaseSignal);
|
|
1022 |
lock.lock();
|
|
1023 |
assertHasWaiters(lock, c, t1, t2);
|
|
1024 |
c.signalAll();
|
|
1025 |
assertHasNoWaiters(lock, c);
|
|
1026 |
lock.unlock();
|
|
1027 |
awaitTermination(t1);
|
|
1028 |
awaitTermination(t2);
|
|
1029 |
}
|
|
1030 |
|
|
1031 |
/**
|
|
1032 |
* signal wakes up waiting threads in FIFO order
|
|
1033 |
*/
|
|
1034 |
public void testSignalWakesFifo() { testSignalWakesFifo(false); }
|
|
1035 |
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
|
|
1036 |
public void testSignalWakesFifo(boolean fair) {
|
|
1037 |
final PublicReentrantLock lock =
|
|
1038 |
new PublicReentrantLock(fair);
|
|
1039 |
final Condition c = lock.newCondition();
|
|
1040 |
final CountDownLatch locked1 = new CountDownLatch(1);
|
|
1041 |
final CountDownLatch locked2 = new CountDownLatch(1);
|
|
1042 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
1043 |
public void realRun() throws InterruptedException {
|
|
1044 |
lock.lock();
|
|
1045 |
locked1.countDown();
|
|
1046 |
c.await();
|
|
1047 |
lock.unlock();
|
|
1048 |
}});
|
|
1049 |
|
|
1050 |
await(locked1);
|
|
1051 |
|
|
1052 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
1053 |
public void realRun() throws InterruptedException {
|
|
1054 |
lock.lock();
|
|
1055 |
locked2.countDown();
|
|
1056 |
c.await();
|
|
1057 |
lock.unlock();
|
|
1058 |
}});
|
|
1059 |
|
|
1060 |
await(locked2);
|
|
1061 |
|
|
1062 |
lock.lock();
|
|
1063 |
assertHasWaiters(lock, c, t1, t2);
|
|
1064 |
assertFalse(lock.hasQueuedThreads());
|
|
1065 |
c.signal();
|
|
1066 |
assertHasWaiters(lock, c, t2);
|
|
1067 |
assertTrue(lock.hasQueuedThread(t1));
|
|
1068 |
assertFalse(lock.hasQueuedThread(t2));
|
|
1069 |
c.signal();
|
|
1070 |
assertHasNoWaiters(lock, c);
|
|
1071 |
assertTrue(lock.hasQueuedThread(t1));
|
|
1072 |
assertTrue(lock.hasQueuedThread(t2));
|
|
1073 |
lock.unlock();
|
|
1074 |
awaitTermination(t1);
|
|
1075 |
awaitTermination(t2);
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
/**
|
|
1079 |
* await after multiple reentrant locking preserves lock count
|
|
1080 |
*/
|
|
1081 |
public void testAwaitLockCount() { testAwaitLockCount(false); }
|
|
1082 |
public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
|
|
1083 |
public void testAwaitLockCount(boolean fair) {
|
|
1084 |
final PublicReentrantLock lock = new PublicReentrantLock(fair);
|
|
1085 |
final Condition c = lock.newCondition();
|
|
1086 |
final CountDownLatch pleaseSignal = new CountDownLatch(2);
|
|
1087 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
1088 |
public void realRun() throws InterruptedException {
|
|
1089 |
lock.lock();
|
|
1090 |
assertLockedByMoi(lock);
|
|
1091 |
assertEquals(1, lock.getHoldCount());
|
|
1092 |
pleaseSignal.countDown();
|
|
1093 |
c.await();
|
|
1094 |
assertLockedByMoi(lock);
|
|
1095 |
assertEquals(1, lock.getHoldCount());
|
|
1096 |
lock.unlock();
|
|
1097 |
}});
|
|
1098 |
|
|
1099 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
1100 |
public void realRun() throws InterruptedException {
|
|
1101 |
lock.lock();
|
|
1102 |
lock.lock();
|
|
1103 |
assertLockedByMoi(lock);
|
|
1104 |
assertEquals(2, lock.getHoldCount());
|
|
1105 |
pleaseSignal.countDown();
|
|
1106 |
c.await();
|
|
1107 |
assertLockedByMoi(lock);
|
|
1108 |
assertEquals(2, lock.getHoldCount());
|
|
1109 |
lock.unlock();
|
|
1110 |
lock.unlock();
|
|
1111 |
}});
|
|
1112 |
|
|
1113 |
await(pleaseSignal);
|
|
1114 |
lock.lock();
|
|
1115 |
assertHasWaiters(lock, c, t1, t2);
|
|
1116 |
assertEquals(1, lock.getHoldCount());
|
|
1117 |
c.signalAll();
|
|
1118 |
assertHasNoWaiters(lock, c);
|
|
1119 |
lock.unlock();
|
|
1120 |
awaitTermination(t1);
|
|
1121 |
awaitTermination(t2);
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
/**
|
|
1125 |
* A serialized lock deserializes as unlocked
|
|
1126 |
*/
|
|
1127 |
public void testSerialization() { testSerialization(false); }
|
|
1128 |
public void testSerialization_fair() { testSerialization(true); }
|
|
1129 |
public void testSerialization(boolean fair) {
|
|
1130 |
ReentrantLock lock = new ReentrantLock(fair);
|
|
1131 |
lock.lock();
|
|
1132 |
|
|
1133 |
ReentrantLock clone = serialClone(lock);
|
|
1134 |
assertEquals(lock.isFair(), clone.isFair());
|
|
1135 |
assertTrue(lock.isLocked());
|
|
1136 |
assertFalse(clone.isLocked());
|
|
1137 |
assertEquals(1, lock.getHoldCount());
|
|
1138 |
assertEquals(0, clone.getHoldCount());
|
|
1139 |
clone.lock();
|
|
1140 |
clone.lock();
|
|
1141 |
assertTrue(clone.isLocked());
|
|
1142 |
assertEquals(2, clone.getHoldCount());
|
|
1143 |
assertEquals(1, lock.getHoldCount());
|
|
1144 |
clone.unlock();
|
|
1145 |
clone.unlock();
|
|
1146 |
assertTrue(lock.isLocked());
|
|
1147 |
assertFalse(clone.isLocked());
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
/**
|
|
1151 |
* toString indicates current lock state
|
|
1152 |
*/
|
|
1153 |
public void testToString() { testToString(false); }
|
|
1154 |
public void testToString_fair() { testToString(true); }
|
|
1155 |
public void testToString(boolean fair) {
|
|
1156 |
ReentrantLock lock = new ReentrantLock(fair);
|
|
1157 |
assertTrue(lock.toString().contains("Unlocked"));
|
|
1158 |
lock.lock();
|
|
1159 |
assertTrue(lock.toString().contains("Locked"));
|
|
1160 |
lock.unlock();
|
|
1161 |
assertTrue(lock.toString().contains("Unlocked"));
|
|
1162 |
}
|
|
1163 |
}
|