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; |
10 |
< |
import java.util.*; |
11 |
< |
import java.io.*; |
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 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 |
|
public class ReentrantLockTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* A runnable calling lockInterruptibly |
32 |
> |
* A checked runnable calling lockInterruptibly |
33 |
|
*/ |
34 |
|
class InterruptibleLockRunnable extends CheckedRunnable { |
35 |
|
final ReentrantLock lock; |
36 |
< |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
36 |
> |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } |
37 |
|
public void realRun() throws InterruptedException { |
38 |
|
lock.lockInterruptibly(); |
39 |
|
} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* A runnable calling lockInterruptibly that expects to be |
43 |
> |
* A checked runnable calling lockInterruptibly that expects to be |
44 |
|
* interrupted |
45 |
|
*/ |
46 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
47 |
|
final ReentrantLock lock; |
48 |
< |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
48 |
> |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } |
49 |
|
public void realRun() throws InterruptedException { |
50 |
|
lock.lockInterruptibly(); |
51 |
|
} |
89 |
|
Thread.yield(); |
90 |
|
} |
91 |
|
assertTrue(t.isAlive()); |
92 |
< |
assertTrue(lock.getOwner() != t); |
92 |
> |
assertNotSame(t, lock.getOwner()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
143 |
|
lock.unlock(); |
144 |
|
} |
145 |
|
|
146 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
146 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
|
|
148 |
|
/** |
149 |
|
* Awaits condition using the specified AwaitMethod. |
150 |
|
*/ |
151 |
|
void await(Condition c, AwaitMethod awaitMethod) |
152 |
|
throws InterruptedException { |
153 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
154 |
|
switch (awaitMethod) { |
155 |
|
case await: |
156 |
|
c.await(); |
157 |
|
break; |
158 |
+ |
case awaitTimed: |
159 |
+ |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
160 |
+ |
break; |
161 |
|
case awaitNanos: |
162 |
< |
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
162 |
> |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
163 |
> |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
164 |
|
assertTrue(nanosRemaining > 0); |
165 |
|
break; |
166 |
|
case awaitUntil: |
167 |
< |
java.util.Date d = new java.util.Date(); |
156 |
< |
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
167 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
168 |
|
break; |
169 |
+ |
default: |
170 |
+ |
throw new AssertionError(); |
171 |
|
} |
172 |
|
} |
173 |
|
|
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
< |
* hasQueuedThread reports whether a thread is queued. |
298 |
> |
* hasQueuedThread reports whether a thread is queued |
299 |
|
*/ |
300 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
301 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
< |
* timed tryLock is interruptible. |
359 |
> |
* timed tryLock is interruptible |
360 |
|
*/ |
361 |
|
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
362 |
|
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
460 |
|
barrier.await(); |
461 |
|
awaitTermination(t); |
462 |
|
assertFalse(lock.isLocked()); |
463 |
< |
} catch (Exception e) { |
451 |
< |
threadUnexpectedException(e); |
452 |
< |
} |
463 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
464 |
|
} |
465 |
|
|
466 |
|
/** |
472 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
473 |
|
try { |
474 |
|
lock.lockInterruptibly(); |
475 |
< |
} catch (InterruptedException ie) { |
465 |
< |
threadUnexpectedException(ie); |
466 |
< |
} |
475 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
476 |
|
assertLockedByMoi(lock); |
477 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
478 |
|
waitForQueuedThread(lock, t); |
491 |
|
public void testAwait_IMSE(boolean fair) { |
492 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
493 |
|
final Condition c = lock.newCondition(); |
494 |
< |
long startTime = System.nanoTime(); |
495 |
< |
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) {} |
494 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
495 |
> |
long startTime = System.nanoTime(); |
496 |
|
try { |
497 |
< |
c.awaitUninterruptibly(); |
497 |
> |
await(c, awaitMethod); |
498 |
|
shouldThrow(); |
499 |
< |
} catch (IllegalMonitorStateException success) {} |
500 |
< |
} catch (InterruptedException ie) { |
501 |
< |
threadUnexpectedException(ie); |
499 |
> |
} catch (IllegalMonitorStateException success) { |
500 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
501 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
502 |
|
} |
506 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
533 |
|
assertTrue(nanosRemaining <= 0); |
534 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
535 |
|
lock.unlock(); |
536 |
< |
} catch (InterruptedException e) { |
541 |
< |
threadUnexpectedException(e); |
542 |
< |
} |
536 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
537 |
|
} |
538 |
|
|
539 |
|
/** |
551 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
552 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
553 |
|
lock.unlock(); |
554 |
< |
} catch (InterruptedException e) { |
561 |
< |
threadUnexpectedException(e); |
562 |
< |
} |
554 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
555 |
|
} |
556 |
|
|
557 |
|
/** |
570 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
571 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
572 |
|
lock.unlock(); |
573 |
< |
} catch (InterruptedException e) { |
582 |
< |
threadUnexpectedException(e); |
583 |
< |
} |
573 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
574 |
|
} |
575 |
|
|
576 |
|
/** |
734 |
|
public void testHasWaiters(boolean fair) { |
735 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
736 |
|
final Condition c = lock.newCondition(); |
737 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
737 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
738 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
739 |
|
public void realRun() throws InterruptedException { |
740 |
|
lock.lock(); |
741 |
|
assertHasNoWaiters(lock, c); |
742 |
|
assertFalse(lock.hasWaiters(c)); |
743 |
< |
locked.countDown(); |
743 |
> |
pleaseSignal.countDown(); |
744 |
|
c.await(); |
745 |
|
assertHasNoWaiters(lock, c); |
746 |
|
assertFalse(lock.hasWaiters(c)); |
747 |
|
lock.unlock(); |
748 |
|
}}); |
749 |
|
|
750 |
< |
await(locked); |
750 |
> |
await(pleaseSignal); |
751 |
|
lock.lock(); |
752 |
|
assertHasWaiters(lock, c, t); |
753 |
|
assertTrue(lock.hasWaiters(c)); |
878 |
|
} |
879 |
|
|
880 |
|
/** |
881 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
881 |
> |
* awaitUninterruptibly is uninterruptible |
882 |
|
*/ |
883 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
884 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
885 |
|
public void testAwaitUninterruptibly(boolean fair) { |
886 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
887 |
|
final Condition c = lock.newCondition(); |
888 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
889 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
888 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
889 |
> |
|
890 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
891 |
|
public void realRun() { |
892 |
+ |
// Interrupt before awaitUninterruptibly |
893 |
|
lock.lock(); |
894 |
< |
locked.countDown(); |
894 |
> |
pleaseInterrupt.countDown(); |
895 |
> |
Thread.currentThread().interrupt(); |
896 |
|
c.awaitUninterruptibly(); |
897 |
|
assertTrue(Thread.interrupted()); |
898 |
|
lock.unlock(); |
899 |
|
}}); |
900 |
|
|
901 |
< |
await(locked); |
901 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
902 |
> |
public void realRun() { |
903 |
> |
// Interrupt during awaitUninterruptibly |
904 |
> |
lock.lock(); |
905 |
> |
pleaseInterrupt.countDown(); |
906 |
> |
c.awaitUninterruptibly(); |
907 |
> |
assertTrue(Thread.interrupted()); |
908 |
> |
lock.unlock(); |
909 |
> |
}}); |
910 |
> |
|
911 |
> |
await(pleaseInterrupt); |
912 |
|
lock.lock(); |
913 |
|
lock.unlock(); |
914 |
< |
t.interrupt(); |
915 |
< |
long timeoutMillis = 10; |
916 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
914 |
> |
t2.interrupt(); |
915 |
> |
|
916 |
> |
assertThreadStaysAlive(t1); |
917 |
> |
assertTrue(t2.isAlive()); |
918 |
> |
|
919 |
|
lock.lock(); |
920 |
< |
c.signal(); |
920 |
> |
c.signalAll(); |
921 |
|
lock.unlock(); |
922 |
< |
awaitTermination(t); |
922 |
> |
|
923 |
> |
awaitTermination(t1); |
924 |
> |
awaitTermination(t2); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
929 |
|
*/ |
930 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
931 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
932 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
933 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
934 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
935 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
936 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
939 |
|
final PublicReentrantLock lock = |
940 |
|
new PublicReentrantLock(fair); |
941 |
|
final Condition c = lock.newCondition(); |
942 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
942 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
943 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
944 |
|
public void realRun() throws InterruptedException { |
945 |
|
lock.lock(); |
946 |
|
assertLockedByMoi(lock); |
947 |
|
assertHasNoWaiters(lock, c); |
948 |
< |
locked.countDown(); |
948 |
> |
pleaseInterrupt.countDown(); |
949 |
|
try { |
950 |
|
await(c, awaitMethod); |
951 |
|
} finally { |
956 |
|
} |
957 |
|
}}); |
958 |
|
|
959 |
< |
await(locked); |
959 |
> |
await(pleaseInterrupt); |
960 |
|
assertHasWaiters(lock, c, t); |
961 |
|
t.interrupt(); |
962 |
|
awaitTermination(t); |
968 |
|
*/ |
969 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
970 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
971 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
972 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
973 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
974 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
975 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
977 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
978 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
979 |
|
final Condition c = lock.newCondition(); |
980 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
980 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
981 |
|
class Awaiter extends CheckedRunnable { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
|
lock.lock(); |
984 |
< |
locked.countDown(); |
984 |
> |
pleaseSignal.countDown(); |
985 |
|
await(c, awaitMethod); |
986 |
|
lock.unlock(); |
987 |
|
} |
990 |
|
Thread t1 = newStartedThread(new Awaiter()); |
991 |
|
Thread t2 = newStartedThread(new Awaiter()); |
992 |
|
|
993 |
< |
await(locked); |
993 |
> |
await(pleaseSignal); |
994 |
|
lock.lock(); |
995 |
|
assertHasWaiters(lock, c, t1, t2); |
996 |
|
c.signalAll(); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* signal wakes up waiting threads in FIFO order. |
1004 |
> |
* signal wakes up waiting threads in FIFO order |
1005 |
|
*/ |
1006 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1007 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1055 |
|
public void testAwaitLockCount(boolean fair) { |
1056 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1057 |
|
final Condition c = lock.newCondition(); |
1058 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1058 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1059 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
|
lock.lock(); |
1062 |
|
assertLockedByMoi(lock); |
1063 |
|
assertEquals(1, lock.getHoldCount()); |
1064 |
< |
locked.countDown(); |
1064 |
> |
pleaseSignal.countDown(); |
1065 |
|
c.await(); |
1066 |
|
assertLockedByMoi(lock); |
1067 |
|
assertEquals(1, lock.getHoldCount()); |
1074 |
|
lock.lock(); |
1075 |
|
assertLockedByMoi(lock); |
1076 |
|
assertEquals(2, lock.getHoldCount()); |
1077 |
< |
locked.countDown(); |
1077 |
> |
pleaseSignal.countDown(); |
1078 |
|
c.await(); |
1079 |
|
assertLockedByMoi(lock); |
1080 |
|
assertEquals(2, lock.getHoldCount()); |
1082 |
|
lock.unlock(); |
1083 |
|
}}); |
1084 |
|
|
1085 |
< |
await(locked); |
1085 |
> |
await(pleaseSignal); |
1086 |
|
lock.lock(); |
1087 |
|
assertHasWaiters(lock, c, t1, t2); |
1088 |
|
assertEquals(1, lock.getHoldCount()); |
1126 |
|
public void testToString_fair() { testToString(true); } |
1127 |
|
public void testToString(boolean fair) { |
1128 |
|
ReentrantLock lock = new ReentrantLock(fair); |
1129 |
< |
String us = lock.toString(); |
1119 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1129 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1130 |
|
lock.lock(); |
1131 |
< |
String ls = lock.toString(); |
1132 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1131 |
> |
assertTrue(lock.toString().contains("Locked")); |
1132 |
> |
lock.unlock(); |
1133 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1134 |
|
} |
1135 |
|
} |