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.atomic.AtomicBoolean; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
18 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
19 |
|
|
1310 |
|
/** |
1311 |
|
* Tests scenario for |
1312 |
|
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1313 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck |
1314 |
|
*/ |
1315 |
< |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1316 |
< |
final RuntimeException ex = new RuntimeException(); |
1315 |
> |
public void testInterruptedFailingAcquire() throws Throwable { |
1316 |
> |
class PleaseThrow extends RuntimeException {} |
1317 |
> |
final PleaseThrow ex = new PleaseThrow(); |
1318 |
> |
final AtomicBoolean thrown = new AtomicBoolean(); |
1319 |
|
|
1320 |
|
// A synchronizer only offering a choice of failure modes |
1321 |
|
class Sync extends AbstractQueuedSynchronizer { |
1322 |
< |
boolean pleaseThrow; |
1322 |
> |
volatile boolean pleaseThrow; |
1323 |
> |
void maybeThrow() { |
1324 |
> |
if (pleaseThrow) { |
1325 |
> |
// assert: tryAcquire methods can throw at most once |
1326 |
> |
if (! thrown.compareAndSet(false, true)) |
1327 |
> |
throw new AssertionError(); |
1328 |
> |
throw ex; |
1329 |
> |
} |
1330 |
> |
} |
1331 |
> |
|
1332 |
|
@Override protected boolean tryAcquire(int ignored) { |
1333 |
< |
if (pleaseThrow) throw ex; |
1333 |
> |
maybeThrow(); |
1334 |
|
return false; |
1335 |
|
} |
1336 |
|
@Override protected int tryAcquireShared(int ignored) { |
1337 |
< |
if (pleaseThrow) throw ex; |
1337 |
> |
maybeThrow(); |
1338 |
|
return -1; |
1339 |
|
} |
1340 |
|
@Override protected boolean tryRelease(int ignored) { |
1346 |
|
} |
1347 |
|
|
1348 |
|
final Sync s = new Sync(); |
1349 |
< |
|
1349 |
> |
final boolean acquireInterruptibly = randomBoolean(); |
1350 |
> |
final Action[] uninterruptibleAcquireActions = { |
1351 |
> |
() -> s.acquire(1), |
1352 |
> |
() -> s.acquireShared(1), |
1353 |
> |
}; |
1354 |
> |
final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1355 |
> |
final Action[] interruptibleAcquireActions = { |
1356 |
> |
() -> s.acquireInterruptibly(1), |
1357 |
> |
() -> s.acquireSharedInterruptibly(1), |
1358 |
> |
() -> s.tryAcquireNanos(1, nanosTimeout), |
1359 |
> |
() -> s.tryAcquireSharedNanos(1, nanosTimeout), |
1360 |
> |
}; |
1361 |
> |
final Action[] releaseActions = { |
1362 |
> |
() -> s.release(1), |
1363 |
> |
() -> s.releaseShared(1), |
1364 |
> |
}; |
1365 |
> |
final Action acquireAction = acquireInterruptibly |
1366 |
> |
? chooseRandomly(interruptibleAcquireActions) |
1367 |
> |
: chooseRandomly(uninterruptibleAcquireActions); |
1368 |
> |
final Action releaseAction |
1369 |
> |
= chooseRandomly(releaseActions); |
1370 |
> |
|
1371 |
> |
// From os_posix.cpp: |
1372 |
> |
// |
1373 |
> |
// NOTE that since there is no "lock" around the interrupt and |
1374 |
> |
// is_interrupted operations, there is the possibility that the |
1375 |
> |
// interrupted flag (in osThread) will be "false" but that the |
1376 |
> |
// low-level events will be in the signaled state. This is |
1377 |
> |
// intentional. The effect of this is that Object.wait() and |
1378 |
> |
// LockSupport.park() will appear to have a spurious wakeup, which |
1379 |
> |
// is allowed and not harmful, and the possibility is so rare that |
1380 |
> |
// it is not worth the added complexity to add yet another lock. |
1381 |
|
final Thread thread = newStartedThread(new CheckedRunnable() { |
1382 |
< |
public void realRun() { |
1382 |
> |
public void realRun() throws Throwable { |
1383 |
|
try { |
1384 |
< |
if (ThreadLocalRandom.current().nextBoolean()) |
1342 |
< |
s.acquire(1); |
1343 |
< |
else |
1344 |
< |
s.acquireShared(1); |
1384 |
> |
acquireAction.run(); |
1385 |
|
shouldThrow(); |
1386 |
< |
} catch (Throwable t) { |
1387 |
< |
assertSame(ex, t); |
1388 |
< |
assertTrue(Thread.interrupted()); |
1386 |
> |
} catch (InterruptedException possible) { |
1387 |
> |
assertTrue(acquireInterruptibly); |
1388 |
> |
assertFalse(Thread.interrupted()); |
1389 |
> |
} catch (PleaseThrow possible) { |
1390 |
> |
awaitInterrupted(); |
1391 |
|
} |
1392 |
|
}}); |
1393 |
< |
waitForThreadToEnterWaitState(thread); |
1394 |
< |
assertSame(thread, s.getFirstQueuedThread()); |
1395 |
< |
assertTrue(s.hasQueuedPredecessors()); |
1396 |
< |
assertTrue(s.hasQueuedThreads()); |
1397 |
< |
assertEquals(1, s.getQueueLength()); |
1393 |
> |
for (long startTime = 0L;; ) { |
1394 |
> |
waitForThreadToEnterWaitState(thread); |
1395 |
> |
if (s.getFirstQueuedThread() == thread |
1396 |
> |
&& s.hasQueuedPredecessors() |
1397 |
> |
&& s.hasQueuedThreads() |
1398 |
> |
&& s.getQueueLength() == 1 |
1399 |
> |
&& s.hasContended()) |
1400 |
> |
break; |
1401 |
> |
if (startTime == 0L) |
1402 |
> |
startTime = System.nanoTime(); |
1403 |
> |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1404 |
> |
fail("timed out waiting for AQS state: " |
1405 |
> |
+ "thread state=" + thread.getState() |
1406 |
> |
+ ", queued threads=" + s.getQueuedThreads()); |
1407 |
> |
Thread.yield(); |
1408 |
> |
} |
1409 |
|
|
1410 |
|
s.pleaseThrow = true; |
1411 |
< |
thread.interrupt(); |
1412 |
< |
s.release(1); |
1411 |
> |
// release and interrupt, in random order |
1412 |
> |
if (randomBoolean()) { |
1413 |
> |
thread.interrupt(); |
1414 |
> |
releaseAction.run(); |
1415 |
> |
} else { |
1416 |
> |
releaseAction.run(); |
1417 |
> |
thread.interrupt(); |
1418 |
> |
} |
1419 |
|
awaitTermination(thread); |
1420 |
+ |
|
1421 |
+ |
if (! acquireInterruptibly) |
1422 |
+ |
assertTrue(thrown.get()); |
1423 |
+ |
|
1424 |
+ |
assertNull(s.getFirstQueuedThread()); |
1425 |
+ |
assertFalse(s.hasQueuedPredecessors()); |
1426 |
+ |
assertFalse(s.hasQueuedThreads()); |
1427 |
+ |
assertEquals(0, s.getQueueLength()); |
1428 |
+ |
assertTrue(s.getQueuedThreads().isEmpty()); |
1429 |
+ |
assertTrue(s.hasContended()); |
1430 |
|
} |
1431 |
|
|
1432 |
|
} |