13 |
|
import java.util.Arrays; |
14 |
|
import java.util.Collection; |
15 |
|
import java.util.HashSet; |
16 |
– |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
18 |
|
|
1309 |
|
/** |
1310 |
|
* Tests scenario for |
1311 |
|
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1312 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck |
1313 |
|
*/ |
1314 |
< |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1314 |
> |
public void testInterruptedFailingAcquire() throws Throwable { |
1315 |
|
final RuntimeException ex = new RuntimeException(); |
1316 |
|
|
1317 |
|
// A synchronizer only offering a choice of failure modes |
1334 |
|
} |
1335 |
|
|
1336 |
|
final Sync s = new Sync(); |
1337 |
< |
|
1337 |
> |
final Action[] uninterruptibleAcquireMethods = { |
1338 |
> |
() -> s.acquire(1), |
1339 |
> |
() -> s.acquireShared(1), |
1340 |
> |
// TODO: test interruptible acquire methods |
1341 |
> |
}; |
1342 |
> |
final Action[] releaseMethods = { |
1343 |
> |
() -> s.release(1), |
1344 |
> |
() -> s.releaseShared(1), |
1345 |
> |
}; |
1346 |
> |
final Action acquireMethod |
1347 |
> |
= chooseRandomly(uninterruptibleAcquireMethods); |
1348 |
> |
final Action releaseMethod |
1349 |
> |
= chooseRandomly(releaseMethods); |
1350 |
> |
|
1351 |
> |
// From os_posix.cpp: |
1352 |
> |
// |
1353 |
> |
// NOTE that since there is no "lock" around the interrupt and |
1354 |
> |
// is_interrupted operations, there is the possibility that the |
1355 |
> |
// interrupted flag (in osThread) will be "false" but that the |
1356 |
> |
// low-level events will be in the signaled state. This is |
1357 |
> |
// intentional. The effect of this is that Object.wait() and |
1358 |
> |
// LockSupport.park() will appear to have a spurious wakeup, which |
1359 |
> |
// is allowed and not harmful, and the possibility is so rare that |
1360 |
> |
// it is not worth the added complexity to add yet another lock. |
1361 |
|
final Thread thread = newStartedThread(new CheckedRunnable() { |
1362 |
|
public void realRun() { |
1363 |
|
try { |
1364 |
< |
if (randomBoolean()) |
1342 |
< |
s.acquire(1); |
1343 |
< |
else |
1344 |
< |
s.acquireShared(1); |
1364 |
> |
acquireMethod.run(); |
1365 |
|
shouldThrow(); |
1366 |
|
} catch (Throwable t) { |
1367 |
|
assertSame(ex, t); |
1368 |
< |
assertTrue(Thread.interrupted()); |
1368 |
> |
awaitInterrupted(); |
1369 |
|
} |
1370 |
|
}}); |
1371 |
< |
waitForThreadToEnterWaitState(thread); |
1372 |
< |
assertSame(thread, s.getFirstQueuedThread()); |
1373 |
< |
assertTrue(s.hasQueuedPredecessors()); |
1374 |
< |
assertTrue(s.hasQueuedThreads()); |
1375 |
< |
assertEquals(1, s.getQueueLength()); |
1371 |
> |
for (long startTime = 0L;; ) { |
1372 |
> |
waitForThreadToEnterWaitState(thread); |
1373 |
> |
if (s.getFirstQueuedThread() == thread |
1374 |
> |
&& s.hasQueuedPredecessors() |
1375 |
> |
&& s.hasQueuedThreads() |
1376 |
> |
&& s.getQueueLength() == 1) |
1377 |
> |
break; |
1378 |
> |
if (startTime == 0L) |
1379 |
> |
startTime = System.nanoTime(); |
1380 |
> |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1381 |
> |
fail("timed out waiting for AQS state: " |
1382 |
> |
+ "thread state=" + thread.getState() |
1383 |
> |
+ ", queued threads=" + s.getQueuedThreads()); |
1384 |
> |
Thread.yield(); |
1385 |
> |
} |
1386 |
|
|
1387 |
|
s.pleaseThrow = true; |
1388 |
< |
thread.interrupt(); |
1389 |
< |
s.release(1); |
1388 |
> |
// release and interrupt, in random order |
1389 |
> |
if (randomBoolean()) { |
1390 |
> |
thread.interrupt(); |
1391 |
> |
releaseMethod.run(); |
1392 |
> |
} else { |
1393 |
> |
releaseMethod.run(); |
1394 |
> |
thread.interrupt(); |
1395 |
> |
} |
1396 |
|
awaitTermination(thread); |
1397 |
+ |
|
1398 |
+ |
assertNull(s.getFirstQueuedThread()); |
1399 |
+ |
assertFalse(s.hasQueuedPredecessors()); |
1400 |
+ |
assertFalse(s.hasQueuedThreads()); |
1401 |
+ |
assertEquals(0, s.getQueueLength()); |
1402 |
+ |
assertTrue(s.getQueuedThreads().isEmpty()); |
1403 |
|
} |
1404 |
|
|
1405 |
|
} |