12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
|
|
1262 |
|
* ant -Djsr166.tckTestClass=AbstractQueuedLongSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck |
1263 |
|
*/ |
1264 |
|
public void testInterruptedFailingAcquire() throws Throwable { |
1265 |
< |
final RuntimeException ex = new RuntimeException(); |
1265 |
> |
class PleaseThrow extends RuntimeException {} |
1266 |
> |
final PleaseThrow ex = new PleaseThrow(); |
1267 |
> |
final AtomicBoolean thrown = new AtomicBoolean(); |
1268 |
|
|
1269 |
|
// A synchronizer only offering a choice of failure modes |
1270 |
|
class Sync extends AbstractQueuedLongSynchronizer { |
1271 |
|
volatile boolean pleaseThrow; |
1272 |
+ |
void maybeThrow() { |
1273 |
+ |
if (pleaseThrow) { |
1274 |
+ |
// assert: tryAcquire methods can throw at most once |
1275 |
+ |
if (! thrown.compareAndSet(false, true)) |
1276 |
+ |
throw new AssertionError(); |
1277 |
+ |
throw ex; |
1278 |
+ |
} |
1279 |
+ |
} |
1280 |
+ |
|
1281 |
|
@Override protected boolean tryAcquire(long ignored) { |
1282 |
< |
if (pleaseThrow) throw ex; |
1282 |
> |
maybeThrow(); |
1283 |
|
return false; |
1284 |
|
} |
1285 |
|
@Override protected long tryAcquireShared(long ignored) { |
1286 |
< |
if (pleaseThrow) throw ex; |
1286 |
> |
maybeThrow(); |
1287 |
|
return -1; |
1288 |
|
} |
1289 |
|
@Override protected boolean tryRelease(long ignored) { |
1295 |
|
} |
1296 |
|
|
1297 |
|
final Sync s = new Sync(); |
1298 |
< |
final Action[] uninterruptibleAcquireMethods = { |
1298 |
> |
final boolean acquireInterruptibly = randomBoolean(); |
1299 |
> |
final Action[] uninterruptibleAcquireActions = { |
1300 |
|
() -> s.acquire(1), |
1301 |
|
() -> s.acquireShared(1), |
1289 |
– |
// TODO: test interruptible acquire methods |
1302 |
|
}; |
1303 |
< |
final Action[] releaseMethods = { |
1303 |
> |
final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1304 |
> |
final Action[] interruptibleAcquireActions = { |
1305 |
> |
() -> s.acquireInterruptibly(1), |
1306 |
> |
() -> s.acquireSharedInterruptibly(1), |
1307 |
> |
() -> s.tryAcquireNanos(1, nanosTimeout), |
1308 |
> |
() -> s.tryAcquireSharedNanos(1, nanosTimeout), |
1309 |
> |
}; |
1310 |
> |
final Action[] releaseActions = { |
1311 |
|
() -> s.release(1), |
1312 |
|
() -> s.releaseShared(1), |
1313 |
|
}; |
1314 |
< |
final Action acquireMethod |
1315 |
< |
= chooseRandomly(uninterruptibleAcquireMethods); |
1316 |
< |
final Action releaseMethod |
1317 |
< |
= chooseRandomly(releaseMethods); |
1314 |
> |
final Action acquireAction = acquireInterruptibly |
1315 |
> |
? chooseRandomly(interruptibleAcquireActions) |
1316 |
> |
: chooseRandomly(uninterruptibleAcquireActions); |
1317 |
> |
final Action releaseAction |
1318 |
> |
= chooseRandomly(releaseActions); |
1319 |
|
|
1320 |
|
// From os_posix.cpp: |
1321 |
|
// |
1328 |
|
// is allowed and not harmful, and the possibility is so rare that |
1329 |
|
// it is not worth the added complexity to add yet another lock. |
1330 |
|
final Thread thread = newStartedThread(new CheckedRunnable() { |
1331 |
< |
public void realRun() { |
1331 |
> |
public void realRun() throws Throwable { |
1332 |
|
try { |
1333 |
< |
acquireMethod.run(); |
1333 |
> |
acquireAction.run(); |
1334 |
|
shouldThrow(); |
1335 |
< |
} catch (Throwable t) { |
1336 |
< |
assertSame(ex, t); |
1335 |
> |
} catch (InterruptedException possible) { |
1336 |
> |
assertTrue(acquireInterruptibly); |
1337 |
> |
assertFalse(Thread.interrupted()); |
1338 |
> |
} catch (PleaseThrow possible) { |
1339 |
|
awaitInterrupted(); |
1340 |
|
} |
1341 |
|
}}); |
1344 |
|
if (s.getFirstQueuedThread() == thread |
1345 |
|
&& s.hasQueuedPredecessors() |
1346 |
|
&& s.hasQueuedThreads() |
1347 |
< |
&& s.getQueueLength() == 1) |
1347 |
> |
&& s.getQueueLength() == 1 |
1348 |
> |
&& s.hasContended()) |
1349 |
|
break; |
1350 |
|
if (startTime == 0L) |
1351 |
|
startTime = System.nanoTime(); |
1360 |
|
// release and interrupt, in random order |
1361 |
|
if (randomBoolean()) { |
1362 |
|
thread.interrupt(); |
1363 |
< |
releaseMethod.run(); |
1363 |
> |
releaseAction.run(); |
1364 |
|
} else { |
1365 |
< |
releaseMethod.run(); |
1365 |
> |
releaseAction.run(); |
1366 |
|
thread.interrupt(); |
1367 |
|
} |
1368 |
|
awaitTermination(thread); |
1369 |
|
|
1370 |
+ |
if (! acquireInterruptibly) |
1371 |
+ |
assertTrue(thrown.get()); |
1372 |
+ |
|
1373 |
|
assertNull(s.getFirstQueuedThread()); |
1374 |
|
assertFalse(s.hasQueuedPredecessors()); |
1375 |
|
assertFalse(s.hasQueuedThreads()); |
1376 |
|
assertEquals(0, s.getQueueLength()); |
1377 |
|
assertTrue(s.getQueuedThreads().isEmpty()); |
1378 |
+ |
assertTrue(s.hasContended()); |
1379 |
|
} |
1380 |
|
|
1381 |
|
} |