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.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
|
import java.util.concurrent.CountDownLatch; |
15 |
|
import java.util.concurrent.CyclicBarrier; |
16 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
< |
import java.util.*; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.ReentrantLock; |
18 |
> |
|
19 |
> |
import junit.framework.AssertionFailedError; |
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
24 |
|
public class ReentrantLockTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
public static Test suite() { |
29 |
|
return new TestSuite(ReentrantLockTest.class); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* A runnable calling lockInterruptibly |
33 |
> |
* A checked runnable calling lockInterruptibly |
34 |
|
*/ |
35 |
|
class InterruptibleLockRunnable extends CheckedRunnable { |
36 |
|
final ReentrantLock lock; |
37 |
< |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
37 |
> |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } |
38 |
|
public void realRun() throws InterruptedException { |
39 |
|
lock.lockInterruptibly(); |
40 |
|
} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* A runnable calling lockInterruptibly that expects to be |
44 |
> |
* A checked runnable calling lockInterruptibly that expects to be |
45 |
|
* interrupted |
46 |
|
*/ |
47 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
48 |
|
final ReentrantLock lock; |
49 |
< |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
49 |
> |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } |
50 |
|
public void realRun() throws InterruptedException { |
51 |
|
lock.lockInterruptibly(); |
52 |
|
} |
90 |
|
Thread.yield(); |
91 |
|
} |
92 |
|
assertTrue(t.isAlive()); |
93 |
< |
assertTrue(lock.getOwner() != t); |
93 |
> |
assertNotSame(t, lock.getOwner()); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
144 |
|
lock.unlock(); |
145 |
|
} |
146 |
|
|
147 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
147 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
148 |
|
|
149 |
|
/** |
150 |
< |
* Awaits condition using the specified AwaitMethod. |
150 |
> |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
151 |
|
*/ |
152 |
|
void await(Condition c, AwaitMethod awaitMethod) |
153 |
|
throws InterruptedException { |
160 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
161 |
|
break; |
162 |
|
case awaitNanos: |
163 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
164 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
165 |
< |
assertTrue(nanosRemaining > 0); |
163 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
164 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
165 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
166 |
> |
assertTrue(nanosRemaining <= timeoutNanos); |
167 |
|
break; |
168 |
|
case awaitUntil: |
169 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
170 |
|
break; |
171 |
+ |
default: |
172 |
+ |
throw new AssertionError(); |
173 |
|
} |
174 |
|
} |
175 |
|
|
210 |
|
public void testUnlock_IMSE() { testUnlock_IMSE(false); } |
211 |
|
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } |
212 |
|
public void testUnlock_IMSE(boolean fair) { |
213 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
213 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
214 |
|
try { |
215 |
|
lock.unlock(); |
216 |
|
shouldThrow(); |
400 |
|
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } |
401 |
|
public void testTryLock_Timeout(boolean fair) { |
402 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
403 |
+ |
final long timeoutMillis = timeoutMillis(); |
404 |
|
lock.lock(); |
405 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
406 |
|
public void realRun() throws InterruptedException { |
407 |
|
long startTime = System.nanoTime(); |
397 |
– |
long timeoutMillis = 10; |
408 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
409 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
410 |
|
}}); |
419 |
|
public void testGetHoldCount() { testGetHoldCount(false); } |
420 |
|
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
421 |
|
public void testGetHoldCount(boolean fair) { |
422 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
422 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
423 |
|
for (int i = 1; i <= SIZE; i++) { |
424 |
|
lock.lock(); |
425 |
|
assertEquals(i, lock.getHoldCount()); |
426 |
|
} |
427 |
|
for (int i = SIZE; i > 0; i--) { |
428 |
|
lock.unlock(); |
429 |
< |
assertEquals(i-1, lock.getHoldCount()); |
429 |
> |
assertEquals(i - 1, lock.getHoldCount()); |
430 |
|
} |
431 |
|
} |
432 |
|
|
436 |
|
public void testIsLocked() { testIsLocked(false); } |
437 |
|
public void testIsLocked_fair() { testIsLocked(true); } |
438 |
|
public void testIsLocked(boolean fair) { |
439 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
440 |
|
try { |
430 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
441 |
|
assertFalse(lock.isLocked()); |
442 |
|
lock.lock(); |
443 |
|
assertTrue(lock.isLocked()); |
462 |
|
barrier.await(); |
463 |
|
awaitTermination(t); |
464 |
|
assertFalse(lock.isLocked()); |
465 |
< |
} catch (Exception e) { |
456 |
< |
threadUnexpectedException(e); |
457 |
< |
} |
465 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
466 |
|
} |
467 |
|
|
468 |
|
/** |
474 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
475 |
|
try { |
476 |
|
lock.lockInterruptibly(); |
477 |
< |
} catch (InterruptedException ie) { |
470 |
< |
threadUnexpectedException(ie); |
471 |
< |
} |
477 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
478 |
|
assertLockedByMoi(lock); |
479 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
480 |
|
waitForQueuedThread(lock, t); |
524 |
|
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
525 |
|
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
526 |
|
public void testAwaitNanos_Timeout(boolean fair) { |
527 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
528 |
+ |
final Condition c = lock.newCondition(); |
529 |
+ |
final long timeoutMillis = timeoutMillis(); |
530 |
+ |
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
531 |
+ |
lock.lock(); |
532 |
+ |
final long startTime = System.nanoTime(); |
533 |
|
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); |
534 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
535 |
|
assertTrue(nanosRemaining <= 0); |
536 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
537 |
< |
lock.unlock(); |
538 |
< |
} catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(e); |
534 |
< |
} |
536 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
537 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
538 |
> |
lock.unlock(); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
544 |
|
public void testAwait_Timeout() { testAwait_Timeout(false); } |
545 |
|
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
546 |
|
public void testAwait_Timeout(boolean fair) { |
547 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
548 |
+ |
final Condition c = lock.newCondition(); |
549 |
+ |
final long timeoutMillis = timeoutMillis(); |
550 |
+ |
lock.lock(); |
551 |
+ |
final long startTime = System.nanoTime(); |
552 |
|
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; |
553 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
554 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
555 |
< |
lock.unlock(); |
556 |
< |
} catch (InterruptedException e) { |
553 |
< |
threadUnexpectedException(e); |
554 |
< |
} |
554 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
555 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
556 |
> |
lock.unlock(); |
557 |
|
} |
558 |
|
|
559 |
|
/** |
562 |
|
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
563 |
|
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
564 |
|
public void testAwaitUntil_Timeout(boolean fair) { |
565 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
566 |
+ |
final Condition c = lock.newCondition(); |
567 |
+ |
lock.lock(); |
568 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
569 |
+ |
// use the same time source, so don't use nanoTime here. |
570 |
+ |
final java.util.Date delayedDate = delayedDate(timeoutMillis()); |
571 |
|
try { |
572 |
< |
final ReentrantLock lock = new ReentrantLock(fair); |
573 |
< |
final Condition c = lock.newCondition(); |
574 |
< |
lock.lock(); |
575 |
< |
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 |
< |
} |
572 |
> |
assertFalse(c.awaitUntil(delayedDate)); |
573 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
574 |
> |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
575 |
> |
lock.unlock(); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
736 |
|
public void testHasWaiters(boolean fair) { |
737 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
738 |
|
final Condition c = lock.newCondition(); |
739 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
739 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
740 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
741 |
|
public void realRun() throws InterruptedException { |
742 |
|
lock.lock(); |
743 |
|
assertHasNoWaiters(lock, c); |
744 |
|
assertFalse(lock.hasWaiters(c)); |
745 |
< |
locked.countDown(); |
745 |
> |
pleaseSignal.countDown(); |
746 |
|
c.await(); |
747 |
|
assertHasNoWaiters(lock, c); |
748 |
|
assertFalse(lock.hasWaiters(c)); |
749 |
|
lock.unlock(); |
750 |
|
}}); |
751 |
|
|
752 |
< |
await(locked); |
752 |
> |
await(pleaseSignal); |
753 |
|
lock.lock(); |
754 |
|
assertHasWaiters(lock, c, t); |
755 |
|
assertTrue(lock.hasWaiters(c)); |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
883 |
> |
* awaitUninterruptibly is uninterruptible |
884 |
|
*/ |
885 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
886 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
887 |
|
public void testAwaitUninterruptibly(boolean fair) { |
888 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
889 |
< |
final Condition c = lock.newCondition(); |
890 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
891 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
889 |
> |
final Condition condition = lock.newCondition(); |
890 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
891 |
> |
|
892 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
893 |
|
public void realRun() { |
894 |
+ |
// Interrupt before awaitUninterruptibly |
895 |
|
lock.lock(); |
896 |
< |
locked.countDown(); |
897 |
< |
c.awaitUninterruptibly(); |
896 |
> |
pleaseInterrupt.countDown(); |
897 |
> |
Thread.currentThread().interrupt(); |
898 |
> |
condition.awaitUninterruptibly(); |
899 |
|
assertTrue(Thread.interrupted()); |
900 |
|
lock.unlock(); |
901 |
|
}}); |
902 |
|
|
903 |
< |
await(locked); |
903 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
904 |
> |
public void realRun() { |
905 |
> |
// Interrupt during awaitUninterruptibly |
906 |
> |
lock.lock(); |
907 |
> |
pleaseInterrupt.countDown(); |
908 |
> |
condition.awaitUninterruptibly(); |
909 |
> |
assertTrue(Thread.interrupted()); |
910 |
> |
lock.unlock(); |
911 |
> |
}}); |
912 |
> |
|
913 |
> |
await(pleaseInterrupt); |
914 |
> |
t2.interrupt(); |
915 |
|
lock.lock(); |
916 |
|
lock.unlock(); |
917 |
< |
t.interrupt(); |
918 |
< |
long timeoutMillis = 10; |
919 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
917 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
918 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
919 |
> |
|
920 |
|
lock.lock(); |
921 |
< |
c.signal(); |
921 |
> |
condition.signalAll(); |
922 |
|
lock.unlock(); |
923 |
< |
awaitTermination(t); |
923 |
> |
|
924 |
> |
awaitTermination(t1); |
925 |
> |
awaitTermination(t2); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
940 |
|
final PublicReentrantLock lock = |
941 |
|
new PublicReentrantLock(fair); |
942 |
|
final Condition c = lock.newCondition(); |
943 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
943 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
944 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
945 |
|
public void realRun() throws InterruptedException { |
946 |
|
lock.lock(); |
947 |
|
assertLockedByMoi(lock); |
948 |
|
assertHasNoWaiters(lock, c); |
949 |
< |
locked.countDown(); |
949 |
> |
pleaseInterrupt.countDown(); |
950 |
|
try { |
951 |
|
await(c, awaitMethod); |
952 |
|
} finally { |
957 |
|
} |
958 |
|
}}); |
959 |
|
|
960 |
< |
await(locked); |
960 |
> |
await(pleaseInterrupt); |
961 |
|
assertHasWaiters(lock, c, t); |
962 |
|
t.interrupt(); |
963 |
|
awaitTermination(t); |
978 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
979 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
980 |
|
final Condition c = lock.newCondition(); |
981 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
981 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
982 |
|
class Awaiter extends CheckedRunnable { |
983 |
|
public void realRun() throws InterruptedException { |
984 |
|
lock.lock(); |
985 |
< |
locked.countDown(); |
985 |
> |
pleaseSignal.countDown(); |
986 |
|
await(c, awaitMethod); |
987 |
|
lock.unlock(); |
988 |
|
} |
991 |
|
Thread t1 = newStartedThread(new Awaiter()); |
992 |
|
Thread t2 = newStartedThread(new Awaiter()); |
993 |
|
|
994 |
< |
await(locked); |
994 |
> |
await(pleaseSignal); |
995 |
|
lock.lock(); |
996 |
|
assertHasWaiters(lock, c, t1, t2); |
997 |
|
c.signalAll(); |
1056 |
|
public void testAwaitLockCount(boolean fair) { |
1057 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1058 |
|
final Condition c = lock.newCondition(); |
1059 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1059 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1060 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1061 |
|
public void realRun() throws InterruptedException { |
1062 |
|
lock.lock(); |
1063 |
|
assertLockedByMoi(lock); |
1064 |
|
assertEquals(1, lock.getHoldCount()); |
1065 |
< |
locked.countDown(); |
1065 |
> |
pleaseSignal.countDown(); |
1066 |
|
c.await(); |
1067 |
|
assertLockedByMoi(lock); |
1068 |
|
assertEquals(1, lock.getHoldCount()); |
1075 |
|
lock.lock(); |
1076 |
|
assertLockedByMoi(lock); |
1077 |
|
assertEquals(2, lock.getHoldCount()); |
1078 |
< |
locked.countDown(); |
1078 |
> |
pleaseSignal.countDown(); |
1079 |
|
c.await(); |
1080 |
|
assertLockedByMoi(lock); |
1081 |
|
assertEquals(2, lock.getHoldCount()); |
1083 |
|
lock.unlock(); |
1084 |
|
}}); |
1085 |
|
|
1086 |
< |
await(locked); |
1086 |
> |
await(pleaseSignal); |
1087 |
|
lock.lock(); |
1088 |
|
assertHasWaiters(lock, c, t1, t2); |
1089 |
|
assertEquals(1, lock.getHoldCount()); |
1100 |
|
public void testSerialization() { testSerialization(false); } |
1101 |
|
public void testSerialization_fair() { testSerialization(true); } |
1102 |
|
public void testSerialization(boolean fair) { |
1103 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1103 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1104 |
|
lock.lock(); |
1105 |
|
|
1106 |
|
ReentrantLock clone = serialClone(lock); |
1126 |
|
public void testToString() { testToString(false); } |
1127 |
|
public void testToString_fair() { testToString(true); } |
1128 |
|
public void testToString(boolean fair) { |
1129 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1129 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1130 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1131 |
|
lock.lock(); |
1132 |
< |
assertTrue(lock.toString().contains("Locked")); |
1132 |
> |
assertTrue(lock.toString().contains("Locked by")); |
1133 |
|
lock.unlock(); |
1134 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1135 |
|
} |