6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
+ |
|
12 |
+ |
import java.util.Arrays; |
13 |
+ |
import java.util.Collection; |
14 |
+ |
import java.util.HashSet; |
15 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
17 |
|
|
18 |
+ |
import junit.framework.Test; |
19 |
+ |
import junit.framework.TestSuite; |
20 |
+ |
|
21 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
22 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
30 |
|
/** |
31 |
|
* A simple mutex class, adapted from the class javadoc. Exclusive |
32 |
|
* acquire tests exercise this as a sample user extension. |
33 |
+ |
* |
34 |
+ |
* Unlike the javadoc sample, we don't track owner thread via |
35 |
+ |
* AbstractOwnableSynchronizer methods. |
36 |
|
*/ |
37 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
38 |
|
/** An eccentric value > 32 bits for locked synchronizer state. */ |
40 |
|
|
41 |
|
static final long UNLOCKED = 0; |
42 |
|
|
43 |
< |
public boolean isHeldExclusively() { |
43 |
> |
/** Owner thread is untracked, so this is really just isLocked(). */ |
44 |
> |
@Override public boolean isHeldExclusively() { |
45 |
|
long state = getState(); |
46 |
|
assertTrue(state == UNLOCKED || state == LOCKED); |
47 |
|
return state == LOCKED; |
48 |
|
} |
49 |
|
|
50 |
< |
public boolean tryAcquire(long acquires) { |
50 |
> |
@Override protected boolean tryAcquire(long acquires) { |
51 |
|
assertEquals(LOCKED, acquires); |
52 |
|
return compareAndSetState(UNLOCKED, LOCKED); |
53 |
|
} |
54 |
|
|
55 |
< |
public boolean tryRelease(long releases) { |
55 |
> |
@Override protected boolean tryRelease(long releases) { |
56 |
|
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
57 |
|
setState(UNLOCKED); |
58 |
|
return true; |
82 |
|
release(LOCKED); |
83 |
|
} |
84 |
|
|
85 |
+ |
/** Faux-Implements Lock.newCondition(). */ |
86 |
|
public ConditionObject newCondition() { |
87 |
|
return new ConditionObject(); |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* A simple latch class, to test shared mode. |
92 |
> |
* A minimal latch class, to test shared mode. |
93 |
|
*/ |
94 |
|
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
95 |
|
public boolean isSignalled() { return getState() != 0; } |
99 |
|
} |
100 |
|
|
101 |
|
public boolean tryReleaseShared(long ignore) { |
102 |
< |
setState(1 << 62); |
102 |
> |
setState(1L << 62); |
103 |
|
return true; |
104 |
|
} |
105 |
|
} |
139 |
|
long startTime = System.nanoTime(); |
140 |
|
while (!sync.isQueued(t)) { |
141 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
142 |
< |
throw new AssertionFailedError("timed out"); |
142 |
> |
throw new AssertionError("timed out"); |
143 |
|
Thread.yield(); |
144 |
|
} |
145 |
|
assertTrue(t.isAlive()); |
207 |
|
new HashSet<Thread>(Arrays.asList(threads))); |
208 |
|
} |
209 |
|
|
210 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
210 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
211 |
|
|
212 |
|
/** |
213 |
|
* Awaits condition using the specified AwaitMethod. |
223 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
224 |
|
break; |
225 |
|
case awaitNanos: |
226 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
227 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
226 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
227 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
228 |
|
assertTrue(nanosRemaining > 0); |
229 |
|
break; |
230 |
|
case awaitUntil: |
231 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
232 |
|
break; |
233 |
+ |
default: |
234 |
+ |
throw new AssertionError(); |
235 |
|
} |
236 |
|
} |
237 |
|
|
240 |
|
* default timeout duration). |
241 |
|
*/ |
242 |
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
243 |
< |
long timeoutMillis = timeoutMillis(); |
244 |
< |
long startTime = System.nanoTime(); |
243 |
> |
final long timeoutMillis = timeoutMillis(); |
244 |
> |
final long startTime; |
245 |
|
try { |
246 |
|
switch (awaitMethod) { |
247 |
|
case awaitTimed: |
248 |
+ |
startTime = System.nanoTime(); |
249 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
250 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
251 |
|
break; |
252 |
|
case awaitNanos: |
253 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
254 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
253 |
> |
startTime = System.nanoTime(); |
254 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
255 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
256 |
|
assertTrue(nanosRemaining <= 0); |
257 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
258 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
259 |
|
break; |
260 |
|
case awaitUntil: |
261 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
262 |
+ |
// use the same time source, so don't use nanoTime here. |
263 |
+ |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
264 |
|
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
265 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
266 |
|
break; |
267 |
|
default: |
268 |
|
throw new UnsupportedOperationException(); |
269 |
|
} |
270 |
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
249 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
970 |
|
*/ |
971 |
|
public void testAwaitUninterruptibly() { |
972 |
|
final Mutex sync = new Mutex(); |
973 |
< |
final ConditionObject c = sync.newCondition(); |
973 |
> |
final ConditionObject condition = sync.newCondition(); |
974 |
|
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
975 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
976 |
|
public void realRun() { |
977 |
|
sync.acquire(); |
978 |
|
assertTrue(pleaseInterrupt.releaseShared(0)); |
979 |
< |
c.awaitUninterruptibly(); |
979 |
> |
condition.awaitUninterruptibly(); |
980 |
|
assertTrue(Thread.interrupted()); |
981 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
981 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
982 |
|
sync.release(); |
983 |
|
}}); |
984 |
|
|
985 |
|
pleaseInterrupt.acquireShared(0); |
986 |
|
sync.acquire(); |
987 |
< |
assertHasWaitersLocked(sync, c, t); |
987 |
> |
assertHasWaitersLocked(sync, condition, t); |
988 |
|
sync.release(); |
989 |
|
t.interrupt(); |
990 |
< |
assertHasWaitersUnlocked(sync, c, t); |
991 |
< |
assertThreadStaysAlive(t); |
990 |
> |
assertHasWaitersUnlocked(sync, condition, t); |
991 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
992 |
|
sync.acquire(); |
993 |
< |
assertHasWaitersLocked(sync, c, t); |
993 |
> |
assertHasWaitersLocked(sync, condition, t); |
994 |
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
995 |
< |
c.signal(); |
996 |
< |
assertHasWaitersLocked(sync, c, NO_THREADS); |
995 |
> |
condition.signal(); |
996 |
> |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
997 |
|
assertHasExclusiveQueuedThreads(sync, t); |
998 |
|
sync.release(); |
999 |
|
awaitTermination(t); |
1136 |
|
|
1137 |
|
waitForQueuedThread(l, t); |
1138 |
|
assertFalse(l.isSignalled()); |
1139 |
< |
assertThreadStaysAlive(t); |
1139 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1140 |
|
assertHasSharedQueuedThreads(l, t); |
1141 |
|
assertTrue(l.releaseShared(0)); |
1142 |
|
assertTrue(l.isSignalled()); |
1161 |
|
|
1162 |
|
waitForQueuedThread(l, t); |
1163 |
|
assertFalse(l.isSignalled()); |
1164 |
< |
assertThreadStaysAlive(t); |
1164 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1165 |
|
assertTrue(l.releaseShared(0)); |
1166 |
|
assertTrue(l.isSignalled()); |
1167 |
|
awaitTermination(t); |
1210 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1211 |
|
final BooleanLatch l = new BooleanLatch(); |
1212 |
|
final BooleanLatch observedQueued = new BooleanLatch(); |
1192 |
– |
final long timeoutMillis = timeoutMillis(); |
1213 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1214 |
|
public void realRun() throws InterruptedException { |
1215 |
|
assertFalse(l.isSignalled()); |
1255 |
|
sync.release(); |
1256 |
|
} |
1257 |
|
|
1258 |
+ |
/** |
1259 |
+ |
* Tests scenario for |
1260 |
+ |
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1261 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedLongSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck |
1262 |
+ |
*/ |
1263 |
+ |
public void testInterruptedFailingAcquire() throws Throwable { |
1264 |
+ |
class PleaseThrow extends RuntimeException {} |
1265 |
+ |
final PleaseThrow ex = new PleaseThrow(); |
1266 |
+ |
|
1267 |
+ |
// A synchronizer only offering a choice of failure modes |
1268 |
+ |
class Sync extends AbstractQueuedLongSynchronizer { |
1269 |
+ |
volatile boolean pleaseThrow; |
1270 |
+ |
@Override protected boolean tryAcquire(long ignored) { |
1271 |
+ |
if (pleaseThrow) throw ex; |
1272 |
+ |
return false; |
1273 |
+ |
} |
1274 |
+ |
@Override protected long tryAcquireShared(long ignored) { |
1275 |
+ |
if (pleaseThrow) throw ex; |
1276 |
+ |
return -1; |
1277 |
+ |
} |
1278 |
+ |
@Override protected boolean tryRelease(long ignored) { |
1279 |
+ |
return true; |
1280 |
+ |
} |
1281 |
+ |
@Override protected boolean tryReleaseShared(long ignored) { |
1282 |
+ |
return true; |
1283 |
+ |
} |
1284 |
+ |
} |
1285 |
+ |
|
1286 |
+ |
final Sync s = new Sync(); |
1287 |
+ |
final boolean acquireInterruptibly = randomBoolean(); |
1288 |
+ |
final Action[] uninterruptibleAcquireActions = { |
1289 |
+ |
() -> s.acquire(1), |
1290 |
+ |
() -> s.acquireShared(1), |
1291 |
+ |
}; |
1292 |
+ |
final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1293 |
+ |
final Action[] interruptibleAcquireActions = { |
1294 |
+ |
() -> s.acquireInterruptibly(1), |
1295 |
+ |
() -> s.acquireSharedInterruptibly(1), |
1296 |
+ |
() -> s.tryAcquireNanos(1, nanosTimeout), |
1297 |
+ |
() -> s.tryAcquireSharedNanos(1, nanosTimeout), |
1298 |
+ |
}; |
1299 |
+ |
final Action[] releaseActions = { |
1300 |
+ |
() -> s.release(1), |
1301 |
+ |
() -> s.releaseShared(1), |
1302 |
+ |
}; |
1303 |
+ |
final Action acquireAction = acquireInterruptibly |
1304 |
+ |
? chooseRandomly(interruptibleAcquireActions) |
1305 |
+ |
: chooseRandomly(uninterruptibleAcquireActions); |
1306 |
+ |
final Action releaseAction |
1307 |
+ |
= chooseRandomly(releaseActions); |
1308 |
+ |
|
1309 |
+ |
// From os_posix.cpp: |
1310 |
+ |
// |
1311 |
+ |
// NOTE that since there is no "lock" around the interrupt and |
1312 |
+ |
// is_interrupted operations, there is the possibility that the |
1313 |
+ |
// interrupted flag (in osThread) will be "false" but that the |
1314 |
+ |
// low-level events will be in the signaled state. This is |
1315 |
+ |
// intentional. The effect of this is that Object.wait() and |
1316 |
+ |
// LockSupport.park() will appear to have a spurious wakeup, which |
1317 |
+ |
// is allowed and not harmful, and the possibility is so rare that |
1318 |
+ |
// it is not worth the added complexity to add yet another lock. |
1319 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1320 |
+ |
public void realRun() throws Throwable { |
1321 |
+ |
try { |
1322 |
+ |
acquireAction.run(); |
1323 |
+ |
shouldThrow(); |
1324 |
+ |
} catch (InterruptedException possible) { |
1325 |
+ |
assertTrue(acquireInterruptibly); |
1326 |
+ |
assertFalse(Thread.interrupted()); |
1327 |
+ |
} catch (PleaseThrow possible) { |
1328 |
+ |
awaitInterrupted(); |
1329 |
+ |
} |
1330 |
+ |
}}); |
1331 |
+ |
for (long startTime = 0L;; ) { |
1332 |
+ |
waitForThreadToEnterWaitState(thread); |
1333 |
+ |
if (s.getFirstQueuedThread() == thread |
1334 |
+ |
&& s.hasQueuedPredecessors() |
1335 |
+ |
&& s.hasQueuedThreads() |
1336 |
+ |
&& s.getQueueLength() == 1 |
1337 |
+ |
&& s.hasContended()) |
1338 |
+ |
break; |
1339 |
+ |
if (startTime == 0L) |
1340 |
+ |
startTime = System.nanoTime(); |
1341 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1342 |
+ |
fail("timed out waiting for AQS state: " |
1343 |
+ |
+ "thread state=" + thread.getState() |
1344 |
+ |
+ ", queued threads=" + s.getQueuedThreads()); |
1345 |
+ |
Thread.yield(); |
1346 |
+ |
} |
1347 |
+ |
|
1348 |
+ |
s.pleaseThrow = true; |
1349 |
+ |
// release and interrupt, in random order |
1350 |
+ |
if (randomBoolean()) { |
1351 |
+ |
thread.interrupt(); |
1352 |
+ |
releaseAction.run(); |
1353 |
+ |
} else { |
1354 |
+ |
releaseAction.run(); |
1355 |
+ |
thread.interrupt(); |
1356 |
+ |
} |
1357 |
+ |
awaitTermination(thread); |
1358 |
+ |
|
1359 |
+ |
assertNull(s.getFirstQueuedThread()); |
1360 |
+ |
assertFalse(s.hasQueuedPredecessors()); |
1361 |
+ |
assertFalse(s.hasQueuedThreads()); |
1362 |
+ |
assertEquals(0, s.getQueueLength()); |
1363 |
+ |
assertTrue(s.getQueuedThreads().isEmpty()); |
1364 |
+ |
assertTrue(s.hasContended()); |
1365 |
+ |
} |
1366 |
+ |
|
1367 |
|
} |