15 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
17 |
|
|
18 |
– |
import junit.framework.AssertionFailedError; |
18 |
|
import junit.framework.Test; |
19 |
|
import junit.framework.TestSuite; |
20 |
|
|
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()); |
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: |
251 |
|
break; |
252 |
|
case awaitNanos: |
253 |
|
startTime = System.nanoTime(); |
254 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
255 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
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); |
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 |
+ |
final RuntimeException ex = new RuntimeException(); |
1265 |
+ |
|
1266 |
+ |
// A synchronizer only offering a choice of failure modes |
1267 |
+ |
class Sync extends AbstractQueuedLongSynchronizer { |
1268 |
+ |
volatile boolean pleaseThrow; |
1269 |
+ |
@Override protected boolean tryAcquire(long ignored) { |
1270 |
+ |
if (pleaseThrow) throw ex; |
1271 |
+ |
return false; |
1272 |
+ |
} |
1273 |
+ |
@Override protected long tryAcquireShared(long ignored) { |
1274 |
+ |
if (pleaseThrow) throw ex; |
1275 |
+ |
return -1; |
1276 |
+ |
} |
1277 |
+ |
@Override protected boolean tryRelease(long ignored) { |
1278 |
+ |
return true; |
1279 |
+ |
} |
1280 |
+ |
@Override protected boolean tryReleaseShared(long ignored) { |
1281 |
+ |
return true; |
1282 |
+ |
} |
1283 |
+ |
} |
1284 |
+ |
|
1285 |
+ |
final Sync s = new Sync(); |
1286 |
+ |
final Action[] uninterruptibleAcquireMethods = { |
1287 |
+ |
() -> s.acquire(1), |
1288 |
+ |
() -> s.acquireShared(1), |
1289 |
+ |
// TODO: test interruptible acquire methods |
1290 |
+ |
}; |
1291 |
+ |
final Action[] releaseMethods = { |
1292 |
+ |
() -> s.release(1), |
1293 |
+ |
() -> s.releaseShared(1), |
1294 |
+ |
}; |
1295 |
+ |
final Action acquireMethod |
1296 |
+ |
= chooseRandomly(uninterruptibleAcquireMethods); |
1297 |
+ |
final Action releaseMethod |
1298 |
+ |
= chooseRandomly(releaseMethods); |
1299 |
+ |
|
1300 |
+ |
// From os_posix.cpp: |
1301 |
+ |
// |
1302 |
+ |
// NOTE that since there is no "lock" around the interrupt and |
1303 |
+ |
// is_interrupted operations, there is the possibility that the |
1304 |
+ |
// interrupted flag (in osThread) will be "false" but that the |
1305 |
+ |
// low-level events will be in the signaled state. This is |
1306 |
+ |
// intentional. The effect of this is that Object.wait() and |
1307 |
+ |
// LockSupport.park() will appear to have a spurious wakeup, which |
1308 |
+ |
// is allowed and not harmful, and the possibility is so rare that |
1309 |
+ |
// it is not worth the added complexity to add yet another lock. |
1310 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1311 |
+ |
public void realRun() { |
1312 |
+ |
try { |
1313 |
+ |
acquireMethod.run(); |
1314 |
+ |
shouldThrow(); |
1315 |
+ |
} catch (Throwable t) { |
1316 |
+ |
assertSame(ex, t); |
1317 |
+ |
awaitInterrupted(); |
1318 |
+ |
} |
1319 |
+ |
}}); |
1320 |
+ |
for (long startTime = 0L;; ) { |
1321 |
+ |
waitForThreadToEnterWaitState(thread); |
1322 |
+ |
if (s.getFirstQueuedThread() == thread |
1323 |
+ |
&& s.hasQueuedPredecessors() |
1324 |
+ |
&& s.hasQueuedThreads() |
1325 |
+ |
&& s.getQueueLength() == 1) |
1326 |
+ |
break; |
1327 |
+ |
if (startTime == 0L) |
1328 |
+ |
startTime = System.nanoTime(); |
1329 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1330 |
+ |
fail("timed out waiting for AQS state: " |
1331 |
+ |
+ "thread state=" + thread.getState() |
1332 |
+ |
+ ", queued threads=" + s.getQueuedThreads()); |
1333 |
+ |
Thread.yield(); |
1334 |
+ |
} |
1335 |
+ |
|
1336 |
+ |
s.pleaseThrow = true; |
1337 |
+ |
// release and interrupt, in random order |
1338 |
+ |
if (randomBoolean()) { |
1339 |
+ |
thread.interrupt(); |
1340 |
+ |
releaseMethod.run(); |
1341 |
+ |
} else { |
1342 |
+ |
releaseMethod.run(); |
1343 |
+ |
thread.interrupt(); |
1344 |
+ |
} |
1345 |
+ |
awaitTermination(thread); |
1346 |
+ |
} |
1347 |
+ |
|
1348 |
|
} |