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.locks.AbstractQueuedSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
18 |
|
|
18 |
– |
import junit.framework.AssertionFailedError; |
19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
21 |
|
|
22 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
|
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
|
main(suite(), args); |
34 |
|
* methods/features of AbstractQueuedSynchronizer are tested via |
35 |
|
* other test classes, including those for ReentrantLock, |
36 |
|
* ReentrantReadWriteLock, and Semaphore. |
37 |
+ |
* |
38 |
+ |
* Unlike the javadoc sample, we don't track owner thread via |
39 |
+ |
* AbstractOwnableSynchronizer methods. |
40 |
|
*/ |
41 |
|
static class Mutex extends AbstractQueuedSynchronizer { |
42 |
|
/** An eccentric value for locked synchronizer state. */ |
44 |
|
|
45 |
|
static final int UNLOCKED = 0; |
46 |
|
|
47 |
+ |
/** Owner thread is untracked, so this is really just isLocked(). */ |
48 |
|
@Override public boolean isHeldExclusively() { |
49 |
|
int state = getState(); |
50 |
|
assertTrue(state == UNLOCKED || state == LOCKED); |
51 |
|
return state == LOCKED; |
52 |
|
} |
53 |
|
|
54 |
< |
@Override public boolean tryAcquire(int acquires) { |
54 |
> |
@Override protected boolean tryAcquire(int acquires) { |
55 |
|
assertEquals(LOCKED, acquires); |
56 |
|
return compareAndSetState(UNLOCKED, LOCKED); |
57 |
|
} |
58 |
|
|
59 |
< |
@Override public boolean tryRelease(int releases) { |
59 |
> |
@Override protected boolean tryRelease(int releases) { |
60 |
|
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
61 |
|
assertEquals(LOCKED, releases); |
62 |
|
setState(UNLOCKED); |
87 |
|
release(LOCKED); |
88 |
|
} |
89 |
|
|
90 |
+ |
/** Faux-Implements Lock.newCondition(). */ |
91 |
|
public ConditionObject newCondition() { |
92 |
|
return new ConditionObject(); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
< |
* A simple latch class, to test shared mode. |
97 |
> |
* A minimal latch class, to test shared mode. |
98 |
|
*/ |
99 |
|
static class BooleanLatch extends AbstractQueuedSynchronizer { |
100 |
|
public boolean isSignalled() { return getState() != 0; } |
143 |
|
long startTime = System.nanoTime(); |
144 |
|
while (!sync.isQueued(t)) { |
145 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
146 |
< |
throw new AssertionFailedError("timed out"); |
146 |
> |
throw new AssertionError("timed out"); |
147 |
|
Thread.yield(); |
148 |
|
} |
149 |
|
assertTrue(t.isAlive()); |
227 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
228 |
|
break; |
229 |
|
case awaitNanos: |
230 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
231 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
230 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
231 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
232 |
|
assertTrue(nanosRemaining > 0); |
233 |
|
break; |
234 |
|
case awaitUntil: |
255 |
|
break; |
256 |
|
case awaitNanos: |
257 |
|
startTime = System.nanoTime(); |
258 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
259 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
258 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
259 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
260 |
|
assertTrue(nanosRemaining <= 0); |
261 |
|
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
262 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1140 |
|
|
1141 |
|
waitForQueuedThread(l, t); |
1142 |
|
assertFalse(l.isSignalled()); |
1143 |
< |
assertThreadStaysAlive(t); |
1143 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1144 |
|
assertHasSharedQueuedThreads(l, t); |
1145 |
|
assertTrue(l.releaseShared(0)); |
1146 |
|
assertTrue(l.isSignalled()); |
1165 |
|
|
1166 |
|
waitForQueuedThread(l, t); |
1167 |
|
assertFalse(l.isSignalled()); |
1168 |
< |
assertThreadStaysAlive(t); |
1168 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1169 |
|
assertTrue(l.releaseShared(0)); |
1170 |
|
assertTrue(l.isSignalled()); |
1171 |
|
awaitTermination(t); |
1259 |
|
sync.release(); |
1260 |
|
} |
1261 |
|
|
1262 |
+ |
/** |
1263 |
+ |
* JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race |
1264 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck |
1265 |
+ |
*/ |
1266 |
+ |
public void testCancelCancelRace() throws InterruptedException { |
1267 |
+ |
class Sync extends AbstractQueuedSynchronizer { |
1268 |
+ |
protected boolean tryAcquire(int acquires) { |
1269 |
+ |
return !hasQueuedPredecessors() && compareAndSetState(0, 1); |
1270 |
+ |
} |
1271 |
+ |
protected boolean tryRelease(int releases) { |
1272 |
+ |
return compareAndSetState(1, 0); |
1273 |
+ |
} |
1274 |
+ |
} |
1275 |
+ |
|
1276 |
+ |
Sync s = new Sync(); |
1277 |
+ |
s.acquire(1); // acquire to force other threads to enqueue |
1278 |
+ |
|
1279 |
+ |
// try to trigger double cancel race with two background threads |
1280 |
+ |
ArrayList<Thread> threads = new ArrayList<>(); |
1281 |
+ |
Runnable failedAcquire = () -> { |
1282 |
+ |
try { |
1283 |
+ |
s.acquireInterruptibly(1); |
1284 |
+ |
shouldThrow(); |
1285 |
+ |
} catch (InterruptedException success) {} |
1286 |
+ |
}; |
1287 |
+ |
for (int i = 0; i < 2; i++) { |
1288 |
+ |
Thread thread = new Thread(failedAcquire); |
1289 |
+ |
thread.start(); |
1290 |
+ |
threads.add(thread); |
1291 |
+ |
} |
1292 |
+ |
Thread.sleep(100); |
1293 |
+ |
for (Thread thread : threads) thread.interrupt(); |
1294 |
+ |
for (Thread thread : threads) awaitTermination(thread); |
1295 |
+ |
|
1296 |
+ |
s.release(1); |
1297 |
+ |
|
1298 |
+ |
// no one holds lock now, we should be able to acquire |
1299 |
+ |
if (!s.tryAcquire(1)) |
1300 |
+ |
throw new RuntimeException( |
1301 |
+ |
String.format( |
1302 |
+ |
"Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s", |
1303 |
+ |
s.hasQueuedPredecessors(), |
1304 |
+ |
s.hasQueuedThreads(), |
1305 |
+ |
s.getQueueLength(), |
1306 |
+ |
s.getFirstQueuedThread())); |
1307 |
+ |
} |
1308 |
+ |
|
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 Throwable { |
1315 |
+ |
final RuntimeException ex = new RuntimeException(); |
1316 |
+ |
|
1317 |
+ |
// A synchronizer only offering a choice of failure modes |
1318 |
+ |
class Sync extends AbstractQueuedSynchronizer { |
1319 |
+ |
volatile boolean pleaseThrow; |
1320 |
+ |
@Override protected boolean tryAcquire(int ignored) { |
1321 |
+ |
if (pleaseThrow) throw ex; |
1322 |
+ |
return false; |
1323 |
+ |
} |
1324 |
+ |
@Override protected int tryAcquireShared(int ignored) { |
1325 |
+ |
if (pleaseThrow) throw ex; |
1326 |
+ |
return -1; |
1327 |
+ |
} |
1328 |
+ |
@Override protected boolean tryRelease(int ignored) { |
1329 |
+ |
return true; |
1330 |
+ |
} |
1331 |
+ |
@Override protected boolean tryReleaseShared(int ignored) { |
1332 |
+ |
return true; |
1333 |
+ |
} |
1334 |
+ |
} |
1335 |
+ |
|
1336 |
+ |
final Sync s = new Sync(); |
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 |
+ |
acquireMethod.run(); |
1365 |
+ |
shouldThrow(); |
1366 |
+ |
} catch (Throwable t) { |
1367 |
+ |
assertSame(ex, t); |
1368 |
+ |
awaitInterrupted(); |
1369 |
+ |
} |
1370 |
+ |
}}); |
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 |
+ |
// 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 |
+ |
|
1399 |
|
} |