19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
21 |
|
|
22 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
31 |
|
/** |
32 |
|
* A simple mutex class, adapted from the class javadoc. Exclusive |
33 |
|
* acquire tests exercise this as a sample user extension. |
34 |
+ |
* |
35 |
+ |
* Unlike the javadoc sample, we don't track owner thread via |
36 |
+ |
* AbstractOwnableSynchronizer methods. |
37 |
|
*/ |
38 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
39 |
|
/** An eccentric value > 32 bits for locked synchronizer state. */ |
41 |
|
|
42 |
|
static final long UNLOCKED = 0; |
43 |
|
|
44 |
< |
public boolean isHeldExclusively() { |
44 |
> |
/** Owner thread is untracked, so this is really just isLocked(). */ |
45 |
> |
@Override public boolean isHeldExclusively() { |
46 |
|
long state = getState(); |
47 |
|
assertTrue(state == UNLOCKED || state == LOCKED); |
48 |
|
return state == LOCKED; |
49 |
|
} |
50 |
|
|
51 |
< |
public boolean tryAcquire(long acquires) { |
51 |
> |
@Override protected boolean tryAcquire(long acquires) { |
52 |
|
assertEquals(LOCKED, acquires); |
53 |
|
return compareAndSetState(UNLOCKED, LOCKED); |
54 |
|
} |
55 |
|
|
56 |
< |
public boolean tryRelease(long releases) { |
56 |
> |
@Override protected boolean tryRelease(long releases) { |
57 |
|
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
58 |
|
setState(UNLOCKED); |
59 |
|
return true; |
83 |
|
release(LOCKED); |
84 |
|
} |
85 |
|
|
86 |
+ |
/** Faux-Implements Lock.newCondition(). */ |
87 |
|
public ConditionObject newCondition() { |
88 |
|
return new ConditionObject(); |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* A simple latch class, to test shared mode. |
93 |
> |
* A minimal latch class, to test shared mode. |
94 |
|
*/ |
95 |
|
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
96 |
|
public boolean isSignalled() { return getState() != 0; } |
100 |
|
} |
101 |
|
|
102 |
|
public boolean tryReleaseShared(long ignore) { |
103 |
< |
setState(1 << 62); |
103 |
> |
setState(1L << 62); |
104 |
|
return true; |
105 |
|
} |
106 |
|
} |
241 |
|
* default timeout duration). |
242 |
|
*/ |
243 |
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
244 |
< |
long timeoutMillis = timeoutMillis(); |
245 |
< |
long startTime = System.nanoTime(); |
244 |
> |
final long timeoutMillis = timeoutMillis(); |
245 |
> |
final long startTime; |
246 |
|
try { |
247 |
|
switch (awaitMethod) { |
248 |
|
case awaitTimed: |
249 |
+ |
startTime = System.nanoTime(); |
250 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
251 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
252 |
|
break; |
253 |
|
case awaitNanos: |
254 |
+ |
startTime = System.nanoTime(); |
255 |
|
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
256 |
|
long nanosRemaining = c.awaitNanos(nanosTimeout); |
257 |
|
assertTrue(nanosRemaining <= 0); |
258 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
259 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
260 |
|
break; |
261 |
|
case awaitUntil: |
262 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
263 |
+ |
// use the same time source, so don't use nanoTime here. |
264 |
+ |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
265 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
266 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
267 |
|
break; |
268 |
|
default: |
269 |
|
throw new UnsupportedOperationException(); |
270 |
|
} |
271 |
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
257 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
971 |
|
*/ |
972 |
|
public void testAwaitUninterruptibly() { |
973 |
|
final Mutex sync = new Mutex(); |
974 |
< |
final ConditionObject c = sync.newCondition(); |
974 |
> |
final ConditionObject condition = sync.newCondition(); |
975 |
|
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
976 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
977 |
|
public void realRun() { |
978 |
|
sync.acquire(); |
979 |
|
assertTrue(pleaseInterrupt.releaseShared(0)); |
980 |
< |
c.awaitUninterruptibly(); |
980 |
> |
condition.awaitUninterruptibly(); |
981 |
|
assertTrue(Thread.interrupted()); |
982 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
982 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
983 |
|
sync.release(); |
984 |
|
}}); |
985 |
|
|
986 |
|
pleaseInterrupt.acquireShared(0); |
987 |
|
sync.acquire(); |
988 |
< |
assertHasWaitersLocked(sync, c, t); |
988 |
> |
assertHasWaitersLocked(sync, condition, t); |
989 |
|
sync.release(); |
990 |
|
t.interrupt(); |
991 |
< |
assertHasWaitersUnlocked(sync, c, t); |
992 |
< |
assertThreadStaysAlive(t); |
991 |
> |
assertHasWaitersUnlocked(sync, condition, t); |
992 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
993 |
|
sync.acquire(); |
994 |
< |
assertHasWaitersLocked(sync, c, t); |
994 |
> |
assertHasWaitersLocked(sync, condition, t); |
995 |
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
996 |
< |
c.signal(); |
997 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
996 |
> |
condition.signal(); |
997 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
998 |
|
assertHasExclusiveQueuedThreads(sync, t); |
999 |
|
sync.release(); |
1000 |
|
awaitTermination(t); |
1137 |
|
|
1138 |
|
waitForQueuedThread(l, t); |
1139 |
|
assertFalse(l.isSignalled()); |
1140 |
< |
assertThreadStaysAlive(t); |
1140 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1141 |
|
assertHasSharedQueuedThreads(l, t); |
1142 |
|
assertTrue(l.releaseShared(0)); |
1143 |
|
assertTrue(l.isSignalled()); |
1162 |
|
|
1163 |
|
waitForQueuedThread(l, t); |
1164 |
|
assertFalse(l.isSignalled()); |
1165 |
< |
assertThreadStaysAlive(t); |
1165 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1166 |
|
assertTrue(l.releaseShared(0)); |
1167 |
|
assertTrue(l.isSignalled()); |
1168 |
|
awaitTermination(t); |