6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.locks.Condition; |
11 |
< |
import java.util.concurrent.locks.ReentrantLock; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.HashSet; |
15 |
|
import java.util.concurrent.CountDownLatch; |
16 |
|
import java.util.concurrent.CyclicBarrier; |
17 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
< |
import java.util.*; |
17 |
> |
import java.util.concurrent.ThreadLocalRandom; |
18 |
> |
import java.util.concurrent.locks.Condition; |
19 |
> |
import java.util.concurrent.locks.ReentrantLock; |
20 |
|
|
21 |
+ |
import junit.framework.Test; |
22 |
+ |
import junit.framework.TestSuite; |
23 |
+ |
|
24 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
25 |
|
public class ReentrantLockTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(ReentrantLockTest.class); |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
< |
* A runnable calling lockInterruptibly |
34 |
> |
* A checked runnable calling lockInterruptibly |
35 |
|
*/ |
36 |
|
class InterruptibleLockRunnable extends CheckedRunnable { |
37 |
|
final ReentrantLock lock; |
38 |
< |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
38 |
> |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } |
39 |
|
public void realRun() throws InterruptedException { |
40 |
|
lock.lockInterruptibly(); |
41 |
|
} |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* A runnable calling lockInterruptibly that expects to be |
45 |
> |
* A checked runnable calling lockInterruptibly that expects to be |
46 |
|
* interrupted |
47 |
|
*/ |
48 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
49 |
|
final ReentrantLock lock; |
50 |
< |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
50 |
> |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } |
51 |
|
public void realRun() throws InterruptedException { |
52 |
|
lock.lockInterruptibly(); |
53 |
|
} |
87 |
|
long startTime = System.nanoTime(); |
88 |
|
while (!lock.hasQueuedThread(t)) { |
89 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
90 |
< |
throw new AssertionFailedError("timed out"); |
90 |
> |
throw new AssertionError("timed out"); |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
94 |
< |
assertTrue(lock.getOwner() != t); |
94 |
> |
assertNotSame(t, lock.getOwner()); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
145 |
|
lock.unlock(); |
146 |
|
} |
147 |
|
|
148 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
148 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
149 |
> |
|
150 |
> |
static AwaitMethod randomAwaitMethod() { |
151 |
> |
AwaitMethod[] awaitMethods = AwaitMethod.values(); |
152 |
> |
return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)]; |
153 |
> |
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* Awaits condition using the specified AwaitMethod. |
156 |
> |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
157 |
|
*/ |
158 |
|
void await(Condition c, AwaitMethod awaitMethod) |
159 |
|
throws InterruptedException { |
166 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
167 |
|
break; |
168 |
|
case awaitNanos: |
169 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
170 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
171 |
< |
assertTrue(nanosRemaining > 0); |
169 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
170 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
171 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
172 |
> |
assertTrue(nanosRemaining <= timeoutNanos); |
173 |
|
break; |
174 |
|
case awaitUntil: |
175 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
176 |
|
break; |
177 |
+ |
default: |
178 |
+ |
throw new AssertionError(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
216 |
|
public void testUnlock_IMSE() { testUnlock_IMSE(false); } |
217 |
|
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } |
218 |
|
public void testUnlock_IMSE(boolean fair) { |
219 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
219 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
220 |
|
try { |
221 |
|
lock.unlock(); |
222 |
|
shouldThrow(); |
406 |
|
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } |
407 |
|
public void testTryLock_Timeout(boolean fair) { |
408 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
409 |
+ |
final long timeoutMillis = timeoutMillis(); |
410 |
|
lock.lock(); |
411 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
412 |
|
public void realRun() throws InterruptedException { |
413 |
|
long startTime = System.nanoTime(); |
397 |
– |
long timeoutMillis = 10; |
414 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
415 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
416 |
|
}}); |
425 |
|
public void testGetHoldCount() { testGetHoldCount(false); } |
426 |
|
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
427 |
|
public void testGetHoldCount(boolean fair) { |
428 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
428 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
429 |
|
for (int i = 1; i <= SIZE; i++) { |
430 |
|
lock.lock(); |
431 |
|
assertEquals(i, lock.getHoldCount()); |
432 |
|
} |
433 |
|
for (int i = SIZE; i > 0; i--) { |
434 |
|
lock.unlock(); |
435 |
< |
assertEquals(i-1, lock.getHoldCount()); |
435 |
> |
assertEquals(i - 1, lock.getHoldCount()); |
436 |
|
} |
437 |
|
} |
438 |
|
|
442 |
|
public void testIsLocked() { testIsLocked(false); } |
443 |
|
public void testIsLocked_fair() { testIsLocked(true); } |
444 |
|
public void testIsLocked(boolean fair) { |
445 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
446 |
|
try { |
430 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
447 |
|
assertFalse(lock.isLocked()); |
448 |
|
lock.lock(); |
449 |
|
assertTrue(lock.isLocked()); |
468 |
|
barrier.await(); |
469 |
|
awaitTermination(t); |
470 |
|
assertFalse(lock.isLocked()); |
471 |
< |
} catch (Exception e) { |
456 |
< |
threadUnexpectedException(e); |
457 |
< |
} |
471 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
472 |
|
} |
473 |
|
|
474 |
|
/** |
480 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
481 |
|
try { |
482 |
|
lock.lockInterruptibly(); |
483 |
< |
} catch (InterruptedException ie) { |
470 |
< |
threadUnexpectedException(ie); |
471 |
< |
} |
483 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
484 |
|
assertLockedByMoi(lock); |
485 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
486 |
|
waitForQueuedThread(lock, t); |
530 |
|
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
531 |
|
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
532 |
|
public void testAwaitNanos_Timeout(boolean fair) { |
533 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
534 |
+ |
final Condition c = lock.newCondition(); |
535 |
+ |
final long timeoutMillis = timeoutMillis(); |
536 |
+ |
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
537 |
+ |
lock.lock(); |
538 |
+ |
final long startTime = System.nanoTime(); |
539 |
|
try { |
522 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
523 |
– |
final Condition c = lock.newCondition(); |
524 |
– |
lock.lock(); |
525 |
– |
long startTime = System.nanoTime(); |
526 |
– |
long timeoutMillis = 10; |
527 |
– |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
540 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
541 |
|
assertTrue(nanosRemaining <= 0); |
542 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
543 |
< |
lock.unlock(); |
544 |
< |
} catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(e); |
534 |
< |
} |
542 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
543 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
544 |
> |
lock.unlock(); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
550 |
|
public void testAwait_Timeout() { testAwait_Timeout(false); } |
551 |
|
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
552 |
|
public void testAwait_Timeout(boolean fair) { |
553 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
554 |
+ |
final Condition c = lock.newCondition(); |
555 |
+ |
final long timeoutMillis = timeoutMillis(); |
556 |
+ |
lock.lock(); |
557 |
+ |
final long startTime = System.nanoTime(); |
558 |
|
try { |
544 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
545 |
– |
final Condition c = lock.newCondition(); |
546 |
– |
lock.lock(); |
547 |
– |
long startTime = System.nanoTime(); |
548 |
– |
long timeoutMillis = 10; |
559 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
560 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
561 |
< |
lock.unlock(); |
562 |
< |
} catch (InterruptedException e) { |
553 |
< |
threadUnexpectedException(e); |
554 |
< |
} |
560 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
561 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
562 |
> |
lock.unlock(); |
563 |
|
} |
564 |
|
|
565 |
|
/** |
568 |
|
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
569 |
|
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
570 |
|
public void testAwaitUntil_Timeout(boolean fair) { |
571 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
572 |
+ |
final Condition c = lock.newCondition(); |
573 |
+ |
lock.lock(); |
574 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
575 |
+ |
// use the same time source, so don't use nanoTime here. |
576 |
+ |
final java.util.Date delayedDate = delayedDate(timeoutMillis()); |
577 |
|
try { |
578 |
< |
final ReentrantLock lock = new ReentrantLock(fair); |
579 |
< |
final Condition c = lock.newCondition(); |
580 |
< |
lock.lock(); |
581 |
< |
long startTime = System.nanoTime(); |
568 |
< |
long timeoutMillis = 10; |
569 |
< |
java.util.Date d = new java.util.Date(); |
570 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
571 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
572 |
< |
lock.unlock(); |
573 |
< |
} catch (InterruptedException e) { |
574 |
< |
threadUnexpectedException(e); |
575 |
< |
} |
578 |
> |
assertFalse(c.awaitUntil(delayedDate)); |
579 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
580 |
> |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
581 |
> |
lock.unlock(); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
892 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
893 |
|
public void testAwaitUninterruptibly(boolean fair) { |
894 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
895 |
< |
final Condition c = lock.newCondition(); |
895 |
> |
final Condition condition = lock.newCondition(); |
896 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
897 |
|
|
898 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
901 |
|
lock.lock(); |
902 |
|
pleaseInterrupt.countDown(); |
903 |
|
Thread.currentThread().interrupt(); |
904 |
< |
c.awaitUninterruptibly(); |
904 |
> |
condition.awaitUninterruptibly(); |
905 |
|
assertTrue(Thread.interrupted()); |
906 |
|
lock.unlock(); |
907 |
|
}}); |
911 |
|
// Interrupt during awaitUninterruptibly |
912 |
|
lock.lock(); |
913 |
|
pleaseInterrupt.countDown(); |
914 |
< |
c.awaitUninterruptibly(); |
914 |
> |
condition.awaitUninterruptibly(); |
915 |
|
assertTrue(Thread.interrupted()); |
916 |
|
lock.unlock(); |
917 |
|
}}); |
918 |
|
|
919 |
|
await(pleaseInterrupt); |
920 |
+ |
t2.interrupt(); |
921 |
|
lock.lock(); |
922 |
|
lock.unlock(); |
923 |
< |
t2.interrupt(); |
924 |
< |
|
918 |
< |
assertThreadStaysAlive(t1); |
919 |
< |
assertTrue(t2.isAlive()); |
923 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
924 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
925 |
|
|
926 |
|
lock.lock(); |
927 |
< |
c.signalAll(); |
927 |
> |
condition.signalAll(); |
928 |
|
lock.unlock(); |
929 |
|
|
930 |
|
awaitTermination(t1); |
1106 |
|
public void testSerialization() { testSerialization(false); } |
1107 |
|
public void testSerialization_fair() { testSerialization(true); } |
1108 |
|
public void testSerialization(boolean fair) { |
1109 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1109 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1110 |
|
lock.lock(); |
1111 |
|
|
1112 |
|
ReentrantLock clone = serialClone(lock); |
1132 |
|
public void testToString() { testToString(false); } |
1133 |
|
public void testToString_fair() { testToString(true); } |
1134 |
|
public void testToString(boolean fair) { |
1135 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1135 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1136 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1137 |
|
lock.lock(); |
1138 |
< |
assertTrue(lock.toString().contains("Locked")); |
1138 |
> |
assertTrue(lock.toString().contains("Locked by")); |
1139 |
|
lock.unlock(); |
1140 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1141 |
|
} |
1142 |
+ |
|
1143 |
+ |
/** |
1144 |
+ |
* Tests scenario for JDK-8187408 |
1145 |
+ |
* AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock |
1146 |
+ |
*/ |
1147 |
+ |
public void testBug8187408() throws InterruptedException { |
1148 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1149 |
+ |
final AwaitMethod awaitMethod = randomAwaitMethod(); |
1150 |
+ |
final int nThreads = rnd.nextInt(2, 10); |
1151 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1152 |
+ |
final Condition cond = lock.newCondition(); |
1153 |
+ |
final CountDownLatch done = new CountDownLatch(nThreads); |
1154 |
+ |
final ArrayList<Thread> threads = new ArrayList<>(); |
1155 |
+ |
|
1156 |
+ |
Runnable rogue = () -> { |
1157 |
+ |
while (done.getCount() > 0) { |
1158 |
+ |
try { |
1159 |
+ |
// call await without holding lock?! |
1160 |
+ |
await(cond, awaitMethod); |
1161 |
+ |
throw new AssertionError("should throw"); |
1162 |
+ |
} |
1163 |
+ |
catch (IllegalMonitorStateException expected) {} |
1164 |
+ |
catch (Throwable fail) { threadUnexpectedException(fail); }}}; |
1165 |
+ |
Thread rogueThread = new Thread(rogue, "rogue"); |
1166 |
+ |
threads.add(rogueThread); |
1167 |
+ |
rogueThread.start(); |
1168 |
+ |
|
1169 |
+ |
Runnable waiter = () -> { |
1170 |
+ |
lock.lock(); |
1171 |
+ |
try { |
1172 |
+ |
done.countDown(); |
1173 |
+ |
cond.await(); |
1174 |
+ |
} catch (Throwable fail) { |
1175 |
+ |
threadUnexpectedException(fail); |
1176 |
+ |
} finally { |
1177 |
+ |
lock.unlock(); |
1178 |
+ |
}}; |
1179 |
+ |
for (int i = 0; i < nThreads; i++) { |
1180 |
+ |
Thread thread = new Thread(waiter, "waiter"); |
1181 |
+ |
threads.add(thread); |
1182 |
+ |
thread.start(); |
1183 |
+ |
} |
1184 |
+ |
|
1185 |
+ |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1186 |
+ |
lock.lock(); |
1187 |
+ |
try { |
1188 |
+ |
assertEquals(nThreads, lock.getWaitQueueLength(cond)); |
1189 |
+ |
} finally { |
1190 |
+ |
cond.signalAll(); |
1191 |
+ |
lock.unlock(); |
1192 |
+ |
} |
1193 |
+ |
for (Thread thread : threads) { |
1194 |
+ |
thread.join(LONG_DELAY_MS); |
1195 |
+ |
assertFalse(thread.isAlive()); |
1196 |
+ |
} |
1197 |
+ |
} |
1198 |
|
} |