9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
|
12 |
+ |
import java.util.ArrayList; |
13 |
|
import java.util.Arrays; |
14 |
|
import java.util.Collection; |
15 |
|
import java.util.HashSet; |
16 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
18 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
19 |
|
|
1261 |
|
sync.release(); |
1262 |
|
} |
1263 |
|
|
1264 |
+ |
/** |
1265 |
+ |
* Disabled demo test for (unfixed as of 2017-11) |
1266 |
+ |
* JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race |
1267 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck |
1268 |
+ |
*/ |
1269 |
+ |
public void XXXXtestCancelCancelRace() throws InterruptedException { |
1270 |
+ |
class Sync extends AbstractQueuedSynchronizer { |
1271 |
+ |
protected boolean tryAcquire(int acquires) { |
1272 |
+ |
return !hasQueuedPredecessors() && compareAndSetState(0, 1); |
1273 |
+ |
} |
1274 |
+ |
protected boolean tryRelease(int releases) { |
1275 |
+ |
return compareAndSetState(1, 0); |
1276 |
+ |
} |
1277 |
+ |
} |
1278 |
+ |
|
1279 |
+ |
Sync s = new Sync(); |
1280 |
+ |
s.acquire(1); // acquire to force other threads to enqueue |
1281 |
+ |
|
1282 |
+ |
// try to trigger double cancel race with two background threads |
1283 |
+ |
ArrayList<Thread> threads = new ArrayList<>(); |
1284 |
+ |
Runnable failedAcquire = () -> { |
1285 |
+ |
try { |
1286 |
+ |
s.acquireInterruptibly(1); |
1287 |
+ |
shouldThrow(); |
1288 |
+ |
} catch (InterruptedException expected) {} |
1289 |
+ |
}; |
1290 |
+ |
for (int i = 0; i < 2; i++) { |
1291 |
+ |
Thread thread = new Thread(failedAcquire); |
1292 |
+ |
thread.start(); |
1293 |
+ |
threads.add(thread); |
1294 |
+ |
} |
1295 |
+ |
Thread.sleep(100); |
1296 |
+ |
for (Thread thread : threads) thread.interrupt(); |
1297 |
+ |
for (Thread thread : threads) awaitTermination(thread); |
1298 |
+ |
|
1299 |
+ |
s.release(1); |
1300 |
+ |
|
1301 |
+ |
// no one holds lock now, we should be able to acquire |
1302 |
+ |
if (!s.tryAcquire(1)) |
1303 |
+ |
throw new RuntimeException( |
1304 |
+ |
String.format( |
1305 |
+ |
"Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s", |
1306 |
+ |
s.hasQueuedPredecessors(), |
1307 |
+ |
s.hasQueuedThreads(), |
1308 |
+ |
s.getQueueLength(), |
1309 |
+ |
s.getFirstQueuedThread())); |
1310 |
+ |
} |
1311 |
+ |
|
1312 |
+ |
/** |
1313 |
+ |
* Tests scenario for |
1314 |
+ |
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1315 |
+ |
*/ |
1316 |
+ |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1317 |
+ |
final RuntimeException ex = new RuntimeException(); |
1318 |
+ |
|
1319 |
+ |
// A synchronizer only offering a choice of failure modes |
1320 |
+ |
class Sync extends AbstractQueuedSynchronizer { |
1321 |
+ |
boolean pleaseThrow; |
1322 |
+ |
@Override protected boolean tryAcquire(int ignored) { |
1323 |
+ |
if (pleaseThrow) throw ex; |
1324 |
+ |
return false; |
1325 |
+ |
} |
1326 |
+ |
@Override protected int tryAcquireShared(int ignored) { |
1327 |
+ |
if (pleaseThrow) throw ex; |
1328 |
+ |
return -1; |
1329 |
+ |
} |
1330 |
+ |
@Override protected boolean tryRelease(int ignored) { |
1331 |
+ |
return true; |
1332 |
+ |
} |
1333 |
+ |
@Override protected boolean tryReleaseShared(int ignored) { |
1334 |
+ |
return true; |
1335 |
+ |
} |
1336 |
+ |
} |
1337 |
+ |
|
1338 |
+ |
final Sync s = new Sync(); |
1339 |
+ |
|
1340 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1341 |
+ |
public void realRun() { |
1342 |
+ |
try { |
1343 |
+ |
if (ThreadLocalRandom.current().nextBoolean()) |
1344 |
+ |
s.acquire(1); |
1345 |
+ |
else |
1346 |
+ |
s.acquireShared(1); |
1347 |
+ |
shouldThrow(); |
1348 |
+ |
} catch (Throwable t) { |
1349 |
+ |
assertSame(ex, t); |
1350 |
+ |
assertTrue(Thread.interrupted()); |
1351 |
+ |
} |
1352 |
+ |
}}); |
1353 |
+ |
waitForThreadToEnterWaitState(thread); |
1354 |
+ |
assertSame(thread, s.getFirstQueuedThread()); |
1355 |
+ |
assertTrue(s.hasQueuedPredecessors()); |
1356 |
+ |
assertTrue(s.hasQueuedThreads()); |
1357 |
+ |
assertEquals(1, s.getQueueLength()); |
1358 |
+ |
|
1359 |
+ |
s.pleaseThrow = true; |
1360 |
+ |
thread.interrupt(); |
1361 |
+ |
s.release(1); |
1362 |
+ |
awaitTermination(thread); |
1363 |
+ |
} |
1364 |
+ |
|
1365 |
|
} |