6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.locks.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.util.*; |
14 |
– |
import java.io.*; |
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 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 |
|
} |
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, 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 { |
160 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
161 |
|
switch (awaitMethod) { |
162 |
|
case await: |
163 |
|
c.await(); |
164 |
|
break; |
165 |
+ |
case awaitTimed: |
166 |
+ |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
167 |
+ |
break; |
168 |
|
case awaitNanos: |
169 |
< |
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
170 |
< |
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 |
< |
java.util.Date d = new java.util.Date(); |
156 |
< |
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
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(); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* hasQueuedThread reports whether a thread is queued. |
306 |
> |
* hasQueuedThread reports whether a thread is queued |
307 |
|
*/ |
308 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
309 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
< |
* timed tryLock is interruptible. |
367 |
> |
* timed tryLock is interruptible |
368 |
|
*/ |
369 |
|
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
370 |
|
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
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(); |
392 |
– |
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 { |
425 |
– |
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) { |
451 |
< |
threadUnexpectedException(e); |
452 |
< |
} |
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) { |
465 |
< |
threadUnexpectedException(ie); |
466 |
< |
} |
483 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
484 |
|
assertLockedByMoi(lock); |
485 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
486 |
|
waitForQueuedThread(lock, t); |
499 |
|
public void testAwait_IMSE(boolean fair) { |
500 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
501 |
|
final Condition c = lock.newCondition(); |
502 |
< |
long startTime = System.nanoTime(); |
503 |
< |
try { |
487 |
< |
try { |
488 |
< |
c.await(); |
489 |
< |
shouldThrow(); |
490 |
< |
} catch (IllegalMonitorStateException success) {} |
491 |
< |
try { |
492 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
493 |
< |
shouldThrow(); |
494 |
< |
} catch (IllegalMonitorStateException success) {} |
495 |
< |
try { |
496 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
497 |
< |
shouldThrow(); |
498 |
< |
} catch (IllegalMonitorStateException success) {} |
502 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
503 |
> |
long startTime = System.nanoTime(); |
504 |
|
try { |
505 |
< |
c.awaitUninterruptibly(); |
505 |
> |
await(c, awaitMethod); |
506 |
|
shouldThrow(); |
507 |
< |
} catch (IllegalMonitorStateException success) {} |
508 |
< |
} catch (InterruptedException ie) { |
509 |
< |
threadUnexpectedException(ie); |
507 |
> |
} catch (IllegalMonitorStateException success) { |
508 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
509 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
510 |
|
} |
506 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
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 { |
530 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
531 |
– |
final Condition c = lock.newCondition(); |
532 |
– |
lock.lock(); |
533 |
– |
long startTime = System.nanoTime(); |
534 |
– |
long timeoutMillis = 10; |
535 |
– |
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) { |
541 |
< |
threadUnexpectedException(e); |
542 |
< |
} |
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 { |
552 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
553 |
– |
final Condition c = lock.newCondition(); |
554 |
– |
lock.lock(); |
555 |
– |
long startTime = System.nanoTime(); |
556 |
– |
long timeoutMillis = 10; |
559 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
560 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
561 |
< |
lock.unlock(); |
562 |
< |
} catch (InterruptedException e) { |
561 |
< |
threadUnexpectedException(e); |
562 |
< |
} |
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(); |
576 |
< |
long timeoutMillis = 10; |
577 |
< |
java.util.Date d = new java.util.Date(); |
578 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
579 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
580 |
< |
lock.unlock(); |
581 |
< |
} catch (InterruptedException e) { |
582 |
< |
threadUnexpectedException(e); |
583 |
< |
} |
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 |
|
/** |
742 |
|
public void testHasWaiters(boolean fair) { |
743 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
744 |
|
final Condition c = lock.newCondition(); |
745 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
745 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
746 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
747 |
|
public void realRun() throws InterruptedException { |
748 |
|
lock.lock(); |
749 |
|
assertHasNoWaiters(lock, c); |
750 |
|
assertFalse(lock.hasWaiters(c)); |
751 |
< |
locked.countDown(); |
751 |
> |
pleaseSignal.countDown(); |
752 |
|
c.await(); |
753 |
|
assertHasNoWaiters(lock, c); |
754 |
|
assertFalse(lock.hasWaiters(c)); |
755 |
|
lock.unlock(); |
756 |
|
}}); |
757 |
|
|
758 |
< |
await(locked); |
758 |
> |
await(pleaseSignal); |
759 |
|
lock.lock(); |
760 |
|
assertHasWaiters(lock, c, t); |
761 |
|
assertTrue(lock.hasWaiters(c)); |
886 |
|
} |
887 |
|
|
888 |
|
/** |
889 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
889 |
> |
* awaitUninterruptibly is uninterruptible |
890 |
|
*/ |
891 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
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(); |
896 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
897 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
895 |
> |
final Condition condition = lock.newCondition(); |
896 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
897 |
> |
|
898 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
899 |
|
public void realRun() { |
900 |
+ |
// Interrupt before awaitUninterruptibly |
901 |
|
lock.lock(); |
902 |
< |
locked.countDown(); |
903 |
< |
c.awaitUninterruptibly(); |
902 |
> |
pleaseInterrupt.countDown(); |
903 |
> |
Thread.currentThread().interrupt(); |
904 |
> |
condition.awaitUninterruptibly(); |
905 |
|
assertTrue(Thread.interrupted()); |
906 |
|
lock.unlock(); |
907 |
|
}}); |
908 |
|
|
909 |
< |
await(locked); |
909 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
910 |
> |
public void realRun() { |
911 |
> |
// Interrupt during awaitUninterruptibly |
912 |
> |
lock.lock(); |
913 |
> |
pleaseInterrupt.countDown(); |
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 |
< |
t.interrupt(); |
924 |
< |
long timeoutMillis = 10; |
925 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
923 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
924 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
925 |
> |
|
926 |
|
lock.lock(); |
927 |
< |
c.signal(); |
927 |
> |
condition.signalAll(); |
928 |
|
lock.unlock(); |
929 |
< |
awaitTermination(t); |
929 |
> |
|
930 |
> |
awaitTermination(t1); |
931 |
> |
awaitTermination(t2); |
932 |
|
} |
933 |
|
|
934 |
|
/** |
936 |
|
*/ |
937 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
938 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
939 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
940 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
941 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
942 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
943 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
946 |
|
final PublicReentrantLock lock = |
947 |
|
new PublicReentrantLock(fair); |
948 |
|
final Condition c = lock.newCondition(); |
949 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
949 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
950 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
951 |
|
public void realRun() throws InterruptedException { |
952 |
|
lock.lock(); |
953 |
|
assertLockedByMoi(lock); |
954 |
|
assertHasNoWaiters(lock, c); |
955 |
< |
locked.countDown(); |
955 |
> |
pleaseInterrupt.countDown(); |
956 |
|
try { |
957 |
|
await(c, awaitMethod); |
958 |
|
} finally { |
963 |
|
} |
964 |
|
}}); |
965 |
|
|
966 |
< |
await(locked); |
966 |
> |
await(pleaseInterrupt); |
967 |
|
assertHasWaiters(lock, c, t); |
968 |
|
t.interrupt(); |
969 |
|
awaitTermination(t); |
975 |
|
*/ |
976 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
977 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
978 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
979 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
980 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
981 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
982 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
984 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
985 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
986 |
|
final Condition c = lock.newCondition(); |
987 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
987 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
988 |
|
class Awaiter extends CheckedRunnable { |
989 |
|
public void realRun() throws InterruptedException { |
990 |
|
lock.lock(); |
991 |
< |
locked.countDown(); |
991 |
> |
pleaseSignal.countDown(); |
992 |
|
await(c, awaitMethod); |
993 |
|
lock.unlock(); |
994 |
|
} |
997 |
|
Thread t1 = newStartedThread(new Awaiter()); |
998 |
|
Thread t2 = newStartedThread(new Awaiter()); |
999 |
|
|
1000 |
< |
await(locked); |
1000 |
> |
await(pleaseSignal); |
1001 |
|
lock.lock(); |
1002 |
|
assertHasWaiters(lock, c, t1, t2); |
1003 |
|
c.signalAll(); |
1008 |
|
} |
1009 |
|
|
1010 |
|
/** |
1011 |
< |
* signal wakes up waiting threads in FIFO order. |
1011 |
> |
* signal wakes up waiting threads in FIFO order |
1012 |
|
*/ |
1013 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1014 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1062 |
|
public void testAwaitLockCount(boolean fair) { |
1063 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1064 |
|
final Condition c = lock.newCondition(); |
1065 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1065 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1066 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1067 |
|
public void realRun() throws InterruptedException { |
1068 |
|
lock.lock(); |
1069 |
|
assertLockedByMoi(lock); |
1070 |
|
assertEquals(1, lock.getHoldCount()); |
1071 |
< |
locked.countDown(); |
1071 |
> |
pleaseSignal.countDown(); |
1072 |
|
c.await(); |
1073 |
|
assertLockedByMoi(lock); |
1074 |
|
assertEquals(1, lock.getHoldCount()); |
1081 |
|
lock.lock(); |
1082 |
|
assertLockedByMoi(lock); |
1083 |
|
assertEquals(2, lock.getHoldCount()); |
1084 |
< |
locked.countDown(); |
1084 |
> |
pleaseSignal.countDown(); |
1085 |
|
c.await(); |
1086 |
|
assertLockedByMoi(lock); |
1087 |
|
assertEquals(2, lock.getHoldCount()); |
1089 |
|
lock.unlock(); |
1090 |
|
}}); |
1091 |
|
|
1092 |
< |
await(locked); |
1092 |
> |
await(pleaseSignal); |
1093 |
|
lock.lock(); |
1094 |
|
assertHasWaiters(lock, c, t1, t2); |
1095 |
|
assertEquals(1, lock.getHoldCount()); |
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); |
1136 |
< |
String us = lock.toString(); |
1119 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1135 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1136 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1137 |
|
lock.lock(); |
1138 |
< |
String ls = lock.toString(); |
1139 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
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 |
|
} |