12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
|
|
20 |
|
import junit.framework.Test; |
21 |
|
import junit.framework.TestSuite; |
22 |
|
|
23 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
24 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
26 |
|
main(suite(), args); |
32 |
|
/** |
33 |
|
* A simple mutex class, adapted from the class javadoc. Exclusive |
34 |
|
* acquire tests exercise this as a sample user extension. |
35 |
+ |
* |
36 |
+ |
* Unlike the javadoc sample, we don't track owner thread via |
37 |
+ |
* AbstractOwnableSynchronizer methods. |
38 |
|
*/ |
39 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
40 |
|
/** An eccentric value > 32 bits for locked synchronizer state. */ |
42 |
|
|
43 |
|
static final long UNLOCKED = 0; |
44 |
|
|
45 |
< |
public boolean isHeldExclusively() { |
45 |
> |
/** Owner thread is untracked, so this is really just isLocked(). */ |
46 |
> |
@Override public boolean isHeldExclusively() { |
47 |
|
long state = getState(); |
48 |
|
assertTrue(state == UNLOCKED || state == LOCKED); |
49 |
|
return state == LOCKED; |
50 |
|
} |
51 |
|
|
52 |
< |
public boolean tryAcquire(long acquires) { |
52 |
> |
@Override protected boolean tryAcquire(long acquires) { |
53 |
|
assertEquals(LOCKED, acquires); |
54 |
|
return compareAndSetState(UNLOCKED, LOCKED); |
55 |
|
} |
56 |
|
|
57 |
< |
public boolean tryRelease(long releases) { |
57 |
> |
@Override protected boolean tryRelease(long releases) { |
58 |
|
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
59 |
|
setState(UNLOCKED); |
60 |
|
return true; |
84 |
|
release(LOCKED); |
85 |
|
} |
86 |
|
|
87 |
+ |
/** Faux-Implements Lock.newCondition(). */ |
88 |
|
public ConditionObject newCondition() { |
89 |
|
return new ConditionObject(); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
< |
* A simple latch class, to test shared mode. |
94 |
> |
* A minimal latch class, to test shared mode. |
95 |
|
*/ |
96 |
|
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
97 |
|
public boolean isSignalled() { return getState() != 0; } |
1138 |
|
|
1139 |
|
waitForQueuedThread(l, t); |
1140 |
|
assertFalse(l.isSignalled()); |
1141 |
< |
assertThreadStaysAlive(t); |
1141 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1142 |
|
assertHasSharedQueuedThreads(l, t); |
1143 |
|
assertTrue(l.releaseShared(0)); |
1144 |
|
assertTrue(l.isSignalled()); |
1163 |
|
|
1164 |
|
waitForQueuedThread(l, t); |
1165 |
|
assertFalse(l.isSignalled()); |
1166 |
< |
assertThreadStaysAlive(t); |
1166 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1167 |
|
assertTrue(l.releaseShared(0)); |
1168 |
|
assertTrue(l.isSignalled()); |
1169 |
|
awaitTermination(t); |
1257 |
|
sync.release(); |
1258 |
|
} |
1259 |
|
|
1260 |
+ |
/** |
1261 |
+ |
* Tests scenario for |
1262 |
+ |
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1263 |
+ |
*/ |
1264 |
+ |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1265 |
+ |
final RuntimeException ex = new RuntimeException(); |
1266 |
+ |
|
1267 |
+ |
// A synchronizer only offering a choice of failure modes |
1268 |
+ |
class Sync extends AbstractQueuedLongSynchronizer { |
1269 |
+ |
boolean pleaseThrow; |
1270 |
+ |
@Override protected boolean tryAcquire(long ignored) { |
1271 |
+ |
if (pleaseThrow) throw ex; |
1272 |
+ |
return false; |
1273 |
+ |
} |
1274 |
+ |
@Override protected long tryAcquireShared(long ignored) { |
1275 |
+ |
if (pleaseThrow) throw ex; |
1276 |
+ |
return -1; |
1277 |
+ |
} |
1278 |
+ |
@Override protected boolean tryRelease(long ignored) { |
1279 |
+ |
return true; |
1280 |
+ |
} |
1281 |
+ |
@Override protected boolean tryReleaseShared(long ignored) { |
1282 |
+ |
return true; |
1283 |
+ |
} |
1284 |
+ |
} |
1285 |
+ |
|
1286 |
+ |
final Sync s = new Sync(); |
1287 |
+ |
|
1288 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1289 |
+ |
public void realRun() { |
1290 |
+ |
try { |
1291 |
+ |
if (ThreadLocalRandom.current().nextBoolean()) |
1292 |
+ |
s.acquire(1); |
1293 |
+ |
else |
1294 |
+ |
s.acquireShared(1); |
1295 |
+ |
shouldThrow(); |
1296 |
+ |
} catch (Throwable t) { |
1297 |
+ |
assertSame(ex, t); |
1298 |
+ |
assertTrue(Thread.interrupted()); |
1299 |
+ |
} |
1300 |
+ |
}}); |
1301 |
+ |
waitForThreadToEnterWaitState(thread); |
1302 |
+ |
assertSame(thread, s.getFirstQueuedThread()); |
1303 |
+ |
assertTrue(s.hasQueuedPredecessors()); |
1304 |
+ |
assertTrue(s.hasQueuedThreads()); |
1305 |
+ |
assertEquals(1, s.getQueueLength()); |
1306 |
+ |
|
1307 |
+ |
s.pleaseThrow = true; |
1308 |
+ |
thread.interrupt(); |
1309 |
+ |
s.release(1); |
1310 |
+ |
awaitTermination(thread); |
1311 |
+ |
} |
1312 |
+ |
|
1313 |
|
} |