1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.22 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.12 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
jsr166 |
1.28 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
jsr166 |
1.29 |
|
12 |
|
|
import java.util.Arrays; |
13 |
|
|
import java.util.Collection; |
14 |
|
|
import java.util.HashSet; |
15 |
jsr166 |
1.43 |
import java.util.concurrent.ThreadLocalRandom; |
16 |
jsr166 |
1.24 |
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
dl |
1.1 |
|
19 |
jsr166 |
1.29 |
import junit.framework.Test; |
20 |
|
|
import junit.framework.TestSuite; |
21 |
|
|
|
22 |
jsr166 |
1.41 |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
dl |
1.1 |
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
24 |
|
|
public static void main(String[] args) { |
25 |
jsr166 |
1.34 |
main(suite(), args); |
26 |
dl |
1.1 |
} |
27 |
|
|
public static Test suite() { |
28 |
|
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
/** |
32 |
jsr166 |
1.24 |
* A simple mutex class, adapted from the class javadoc. Exclusive |
33 |
|
|
* acquire tests exercise this as a sample user extension. |
34 |
jsr166 |
1.42 |
* |
35 |
|
|
* Unlike the javadoc sample, we don't track owner thread via |
36 |
|
|
* AbstractOwnableSynchronizer methods. |
37 |
dl |
1.1 |
*/ |
38 |
|
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
39 |
jsr166 |
1.24 |
/** An eccentric value > 32 bits for locked synchronizer state. */ |
40 |
|
|
static final long LOCKED = (1L << 63) | (1L << 15); |
41 |
|
|
|
42 |
|
|
static final long UNLOCKED = 0; |
43 |
|
|
|
44 |
jsr166 |
1.42 |
/** Owner thread is untracked, so this is really just isLocked(). */ |
45 |
|
|
@Override public boolean isHeldExclusively() { |
46 |
jsr166 |
1.24 |
long state = getState(); |
47 |
|
|
assertTrue(state == UNLOCKED || state == LOCKED); |
48 |
|
|
return state == LOCKED; |
49 |
dl |
1.1 |
} |
50 |
jsr166 |
1.5 |
|
51 |
jsr166 |
1.42 |
@Override protected boolean tryAcquire(long acquires) { |
52 |
jsr166 |
1.24 |
assertEquals(LOCKED, acquires); |
53 |
|
|
return compareAndSetState(UNLOCKED, LOCKED); |
54 |
dl |
1.1 |
} |
55 |
jsr166 |
1.5 |
|
56 |
jsr166 |
1.42 |
@Override protected boolean tryRelease(long releases) { |
57 |
jsr166 |
1.24 |
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
58 |
|
|
setState(UNLOCKED); |
59 |
dl |
1.1 |
return true; |
60 |
|
|
} |
61 |
jsr166 |
1.5 |
|
62 |
jsr166 |
1.24 |
public boolean tryAcquireNanos(long nanos) throws InterruptedException { |
63 |
|
|
return tryAcquireNanos(LOCKED, nanos); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
public boolean tryAcquire() { |
67 |
|
|
return tryAcquire(LOCKED); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
public boolean tryRelease() { |
71 |
|
|
return tryRelease(LOCKED); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
public void acquire() { |
75 |
|
|
acquire(LOCKED); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
public void acquireInterruptibly() throws InterruptedException { |
79 |
|
|
acquireInterruptibly(LOCKED); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
public void release() { |
83 |
|
|
release(LOCKED); |
84 |
jsr166 |
1.17 |
} |
85 |
dl |
1.1 |
|
86 |
jsr166 |
1.42 |
/** Faux-Implements Lock.newCondition(). */ |
87 |
jsr166 |
1.24 |
public ConditionObject newCondition() { |
88 |
|
|
return new ConditionObject(); |
89 |
|
|
} |
90 |
dl |
1.1 |
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
jsr166 |
1.42 |
* A minimal latch class, to test shared mode. |
94 |
dl |
1.1 |
*/ |
95 |
jsr166 |
1.5 |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
96 |
dl |
1.1 |
public boolean isSignalled() { return getState() != 0; } |
97 |
|
|
|
98 |
|
|
public long tryAcquireShared(long ignore) { |
99 |
jsr166 |
1.21 |
return isSignalled() ? 1 : -1; |
100 |
dl |
1.1 |
} |
101 |
jsr166 |
1.5 |
|
102 |
dl |
1.1 |
public boolean tryReleaseShared(long ignore) { |
103 |
jsr166 |
1.33 |
setState(1L << 62); |
104 |
dl |
1.1 |
return true; |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
jsr166 |
1.10 |
* A runnable calling acquireInterruptibly that does not expect to |
110 |
|
|
* be interrupted. |
111 |
dl |
1.1 |
*/ |
112 |
jsr166 |
1.10 |
class InterruptibleSyncRunnable extends CheckedRunnable { |
113 |
dl |
1.1 |
final Mutex sync; |
114 |
jsr166 |
1.24 |
InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; } |
115 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
116 |
jsr166 |
1.24 |
sync.acquireInterruptibly(); |
117 |
dl |
1.1 |
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* A runnable calling acquireInterruptibly that expects to be |
122 |
jsr166 |
1.10 |
* interrupted. |
123 |
dl |
1.1 |
*/ |
124 |
jsr166 |
1.10 |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
125 |
dl |
1.1 |
final Mutex sync; |
126 |
jsr166 |
1.24 |
InterruptedSyncRunnable(Mutex sync) { this.sync = sync; } |
127 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
128 |
jsr166 |
1.24 |
sync.acquireInterruptibly(); |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
/** A constant to clarify calls to checking methods below. */ |
133 |
jsr166 |
1.26 |
static final Thread[] NO_THREADS = new Thread[0]; |
134 |
jsr166 |
1.24 |
|
135 |
|
|
/** |
136 |
|
|
* Spin-waits until sync.isQueued(t) becomes true. |
137 |
|
|
*/ |
138 |
|
|
void waitForQueuedThread(AbstractQueuedLongSynchronizer sync, |
139 |
|
|
Thread t) { |
140 |
|
|
long startTime = System.nanoTime(); |
141 |
|
|
while (!sync.isQueued(t)) { |
142 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
143 |
jsr166 |
1.44 |
throw new AssertionError("timed out"); |
144 |
jsr166 |
1.24 |
Thread.yield(); |
145 |
|
|
} |
146 |
|
|
assertTrue(t.isAlive()); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* Checks that sync has exactly the given queued threads. |
151 |
|
|
*/ |
152 |
|
|
void assertHasQueuedThreads(AbstractQueuedLongSynchronizer sync, |
153 |
|
|
Thread... expected) { |
154 |
|
|
Collection<Thread> actual = sync.getQueuedThreads(); |
155 |
|
|
assertEquals(expected.length > 0, sync.hasQueuedThreads()); |
156 |
|
|
assertEquals(expected.length, sync.getQueueLength()); |
157 |
|
|
assertEquals(expected.length, actual.size()); |
158 |
|
|
assertEquals(expected.length == 0, actual.isEmpty()); |
159 |
|
|
assertEquals(new HashSet<Thread>(actual), |
160 |
|
|
new HashSet<Thread>(Arrays.asList(expected))); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* Checks that sync has exactly the given (exclusive) queued threads. |
165 |
|
|
*/ |
166 |
|
|
void assertHasExclusiveQueuedThreads(AbstractQueuedLongSynchronizer sync, |
167 |
|
|
Thread... expected) { |
168 |
|
|
assertHasQueuedThreads(sync, expected); |
169 |
|
|
assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()), |
170 |
|
|
new HashSet<Thread>(sync.getQueuedThreads())); |
171 |
|
|
assertEquals(0, sync.getSharedQueuedThreads().size()); |
172 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
/** |
176 |
|
|
* Checks that sync has exactly the given (shared) queued threads. |
177 |
|
|
*/ |
178 |
|
|
void assertHasSharedQueuedThreads(AbstractQueuedLongSynchronizer sync, |
179 |
|
|
Thread... expected) { |
180 |
|
|
assertHasQueuedThreads(sync, expected); |
181 |
|
|
assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()), |
182 |
|
|
new HashSet<Thread>(sync.getQueuedThreads())); |
183 |
|
|
assertEquals(0, sync.getExclusiveQueuedThreads().size()); |
184 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* Checks that condition c has exactly the given waiter threads, |
189 |
|
|
* after acquiring mutex. |
190 |
|
|
*/ |
191 |
|
|
void assertHasWaitersUnlocked(Mutex sync, ConditionObject c, |
192 |
|
|
Thread... threads) { |
193 |
|
|
sync.acquire(); |
194 |
|
|
assertHasWaitersLocked(sync, c, threads); |
195 |
|
|
sync.release(); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
/** |
199 |
|
|
* Checks that condition c has exactly the given waiter threads. |
200 |
|
|
*/ |
201 |
|
|
void assertHasWaitersLocked(Mutex sync, ConditionObject c, |
202 |
|
|
Thread... threads) { |
203 |
|
|
assertEquals(threads.length > 0, sync.hasWaiters(c)); |
204 |
|
|
assertEquals(threads.length, sync.getWaitQueueLength(c)); |
205 |
|
|
assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty()); |
206 |
|
|
assertEquals(threads.length, sync.getWaitingThreads(c).size()); |
207 |
|
|
assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)), |
208 |
|
|
new HashSet<Thread>(Arrays.asList(threads))); |
209 |
|
|
} |
210 |
|
|
|
211 |
jsr166 |
1.30 |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
212 |
jsr166 |
1.24 |
|
213 |
|
|
/** |
214 |
|
|
* Awaits condition using the specified AwaitMethod. |
215 |
|
|
*/ |
216 |
|
|
void await(ConditionObject c, AwaitMethod awaitMethod) |
217 |
|
|
throws InterruptedException { |
218 |
|
|
long timeoutMillis = 2 * LONG_DELAY_MS; |
219 |
|
|
switch (awaitMethod) { |
220 |
|
|
case await: |
221 |
|
|
c.await(); |
222 |
|
|
break; |
223 |
|
|
case awaitTimed: |
224 |
|
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
225 |
|
|
break; |
226 |
|
|
case awaitNanos: |
227 |
jsr166 |
1.45 |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
228 |
|
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
229 |
jsr166 |
1.24 |
assertTrue(nanosRemaining > 0); |
230 |
|
|
break; |
231 |
|
|
case awaitUntil: |
232 |
|
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
233 |
|
|
break; |
234 |
jsr166 |
1.31 |
default: |
235 |
|
|
throw new AssertionError(); |
236 |
dl |
1.1 |
} |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
/** |
240 |
jsr166 |
1.24 |
* Checks that awaiting the given condition times out (using the |
241 |
|
|
* default timeout duration). |
242 |
|
|
*/ |
243 |
|
|
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
244 |
jsr166 |
1.37 |
final long timeoutMillis = timeoutMillis(); |
245 |
|
|
final long startTime; |
246 |
jsr166 |
1.24 |
try { |
247 |
|
|
switch (awaitMethod) { |
248 |
|
|
case awaitTimed: |
249 |
jsr166 |
1.36 |
startTime = System.nanoTime(); |
250 |
jsr166 |
1.24 |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
251 |
jsr166 |
1.36 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
252 |
jsr166 |
1.24 |
break; |
253 |
|
|
case awaitNanos: |
254 |
jsr166 |
1.36 |
startTime = System.nanoTime(); |
255 |
jsr166 |
1.45 |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
256 |
|
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
257 |
jsr166 |
1.24 |
assertTrue(nanosRemaining <= 0); |
258 |
jsr166 |
1.35 |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
259 |
jsr166 |
1.36 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
260 |
jsr166 |
1.24 |
break; |
261 |
|
|
case awaitUntil: |
262 |
jsr166 |
1.36 |
// We shouldn't assume that nanoTime and currentTimeMillis |
263 |
|
|
// use the same time source, so don't use nanoTime here. |
264 |
jsr166 |
1.37 |
java.util.Date delayedDate = delayedDate(timeoutMillis); |
265 |
jsr166 |
1.24 |
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
266 |
jsr166 |
1.36 |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
267 |
jsr166 |
1.24 |
break; |
268 |
|
|
default: |
269 |
|
|
throw new UnsupportedOperationException(); |
270 |
|
|
} |
271 |
|
|
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
/** |
275 |
dl |
1.1 |
* isHeldExclusively is false upon construction |
276 |
|
|
*/ |
277 |
jsr166 |
1.5 |
public void testIsHeldExclusively() { |
278 |
jsr166 |
1.24 |
Mutex sync = new Mutex(); |
279 |
|
|
assertFalse(sync.isHeldExclusively()); |
280 |
dl |
1.1 |
} |
281 |
jsr166 |
1.5 |
|
282 |
dl |
1.1 |
/** |
283 |
|
|
* acquiring released sync succeeds |
284 |
|
|
*/ |
285 |
jsr166 |
1.5 |
public void testAcquire() { |
286 |
jsr166 |
1.24 |
Mutex sync = new Mutex(); |
287 |
|
|
sync.acquire(); |
288 |
|
|
assertTrue(sync.isHeldExclusively()); |
289 |
|
|
sync.release(); |
290 |
|
|
assertFalse(sync.isHeldExclusively()); |
291 |
dl |
1.1 |
} |
292 |
|
|
|
293 |
|
|
/** |
294 |
jsr166 |
1.24 |
* tryAcquire on a released sync succeeds |
295 |
dl |
1.1 |
*/ |
296 |
jsr166 |
1.5 |
public void testTryAcquire() { |
297 |
jsr166 |
1.24 |
Mutex sync = new Mutex(); |
298 |
|
|
assertTrue(sync.tryAcquire()); |
299 |
|
|
assertTrue(sync.isHeldExclusively()); |
300 |
|
|
sync.release(); |
301 |
|
|
assertFalse(sync.isHeldExclusively()); |
302 |
dl |
1.1 |
} |
303 |
|
|
|
304 |
|
|
/** |
305 |
|
|
* hasQueuedThreads reports whether there are waiting threads |
306 |
|
|
*/ |
307 |
jsr166 |
1.24 |
public void testHasQueuedThreads() { |
308 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
309 |
jsr166 |
1.9 |
assertFalse(sync.hasQueuedThreads()); |
310 |
jsr166 |
1.24 |
sync.acquire(); |
311 |
|
|
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
312 |
|
|
waitForQueuedThread(sync, t1); |
313 |
jsr166 |
1.9 |
assertTrue(sync.hasQueuedThreads()); |
314 |
jsr166 |
1.24 |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
315 |
|
|
waitForQueuedThread(sync, t2); |
316 |
jsr166 |
1.9 |
assertTrue(sync.hasQueuedThreads()); |
317 |
|
|
t1.interrupt(); |
318 |
jsr166 |
1.24 |
awaitTermination(t1); |
319 |
jsr166 |
1.9 |
assertTrue(sync.hasQueuedThreads()); |
320 |
jsr166 |
1.24 |
sync.release(); |
321 |
|
|
awaitTermination(t2); |
322 |
jsr166 |
1.9 |
assertFalse(sync.hasQueuedThreads()); |
323 |
jsr166 |
1.5 |
} |
324 |
dl |
1.1 |
|
325 |
|
|
/** |
326 |
jsr166 |
1.24 |
* isQueued(null) throws NullPointerException |
327 |
dl |
1.1 |
*/ |
328 |
jsr166 |
1.5 |
public void testIsQueuedNPE() { |
329 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
330 |
dl |
1.1 |
try { |
331 |
|
|
sync.isQueued(null); |
332 |
|
|
shouldThrow(); |
333 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
334 |
dl |
1.1 |
} |
335 |
|
|
|
336 |
|
|
/** |
337 |
jsr166 |
1.24 |
* isQueued reports whether a thread is queued |
338 |
dl |
1.1 |
*/ |
339 |
jsr166 |
1.24 |
public void testIsQueued() { |
340 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
341 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
342 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
343 |
jsr166 |
1.9 |
assertFalse(sync.isQueued(t1)); |
344 |
|
|
assertFalse(sync.isQueued(t2)); |
345 |
jsr166 |
1.24 |
sync.acquire(); |
346 |
jsr166 |
1.9 |
t1.start(); |
347 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t1); |
348 |
jsr166 |
1.9 |
assertTrue(sync.isQueued(t1)); |
349 |
jsr166 |
1.24 |
assertFalse(sync.isQueued(t2)); |
350 |
jsr166 |
1.9 |
t2.start(); |
351 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t2); |
352 |
jsr166 |
1.9 |
assertTrue(sync.isQueued(t1)); |
353 |
|
|
assertTrue(sync.isQueued(t2)); |
354 |
|
|
t1.interrupt(); |
355 |
jsr166 |
1.24 |
awaitTermination(t1); |
356 |
jsr166 |
1.9 |
assertFalse(sync.isQueued(t1)); |
357 |
|
|
assertTrue(sync.isQueued(t2)); |
358 |
jsr166 |
1.24 |
sync.release(); |
359 |
|
|
awaitTermination(t2); |
360 |
jsr166 |
1.9 |
assertFalse(sync.isQueued(t1)); |
361 |
|
|
assertFalse(sync.isQueued(t2)); |
362 |
jsr166 |
1.5 |
} |
363 |
dl |
1.1 |
|
364 |
|
|
/** |
365 |
|
|
* getFirstQueuedThread returns first waiting thread or null if none |
366 |
|
|
*/ |
367 |
jsr166 |
1.24 |
public void testGetFirstQueuedThread() { |
368 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
369 |
jsr166 |
1.9 |
assertNull(sync.getFirstQueuedThread()); |
370 |
jsr166 |
1.24 |
sync.acquire(); |
371 |
|
|
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
372 |
|
|
waitForQueuedThread(sync, t1); |
373 |
jsr166 |
1.9 |
assertEquals(t1, sync.getFirstQueuedThread()); |
374 |
jsr166 |
1.24 |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
375 |
|
|
waitForQueuedThread(sync, t2); |
376 |
jsr166 |
1.9 |
assertEquals(t1, sync.getFirstQueuedThread()); |
377 |
|
|
t1.interrupt(); |
378 |
jsr166 |
1.24 |
awaitTermination(t1); |
379 |
jsr166 |
1.9 |
assertEquals(t2, sync.getFirstQueuedThread()); |
380 |
jsr166 |
1.24 |
sync.release(); |
381 |
|
|
awaitTermination(t2); |
382 |
jsr166 |
1.9 |
assertNull(sync.getFirstQueuedThread()); |
383 |
jsr166 |
1.5 |
} |
384 |
dl |
1.1 |
|
385 |
|
|
/** |
386 |
|
|
* hasContended reports false if no thread has ever blocked, else true |
387 |
|
|
*/ |
388 |
jsr166 |
1.24 |
public void testHasContended() { |
389 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
390 |
jsr166 |
1.9 |
assertFalse(sync.hasContended()); |
391 |
jsr166 |
1.24 |
sync.acquire(); |
392 |
|
|
assertFalse(sync.hasContended()); |
393 |
|
|
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
394 |
|
|
waitForQueuedThread(sync, t1); |
395 |
jsr166 |
1.9 |
assertTrue(sync.hasContended()); |
396 |
jsr166 |
1.24 |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
397 |
|
|
waitForQueuedThread(sync, t2); |
398 |
jsr166 |
1.9 |
assertTrue(sync.hasContended()); |
399 |
|
|
t1.interrupt(); |
400 |
jsr166 |
1.24 |
awaitTermination(t1); |
401 |
jsr166 |
1.9 |
assertTrue(sync.hasContended()); |
402 |
jsr166 |
1.24 |
sync.release(); |
403 |
|
|
awaitTermination(t2); |
404 |
jsr166 |
1.9 |
assertTrue(sync.hasContended()); |
405 |
jsr166 |
1.5 |
} |
406 |
dl |
1.1 |
|
407 |
|
|
/** |
408 |
jsr166 |
1.24 |
* getQueuedThreads returns all waiting threads |
409 |
dl |
1.1 |
*/ |
410 |
jsr166 |
1.24 |
public void testGetQueuedThreads() { |
411 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
412 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
413 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
414 |
jsr166 |
1.24 |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
415 |
|
|
sync.acquire(); |
416 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
417 |
jsr166 |
1.9 |
t1.start(); |
418 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t1); |
419 |
|
|
assertHasExclusiveQueuedThreads(sync, t1); |
420 |
jsr166 |
1.9 |
assertTrue(sync.getQueuedThreads().contains(t1)); |
421 |
jsr166 |
1.24 |
assertFalse(sync.getQueuedThreads().contains(t2)); |
422 |
jsr166 |
1.9 |
t2.start(); |
423 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t2); |
424 |
|
|
assertHasExclusiveQueuedThreads(sync, t1, t2); |
425 |
jsr166 |
1.9 |
assertTrue(sync.getQueuedThreads().contains(t1)); |
426 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
427 |
|
|
t1.interrupt(); |
428 |
jsr166 |
1.24 |
awaitTermination(t1); |
429 |
|
|
assertHasExclusiveQueuedThreads(sync, t2); |
430 |
|
|
sync.release(); |
431 |
|
|
awaitTermination(t2); |
432 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
433 |
jsr166 |
1.5 |
} |
434 |
dl |
1.1 |
|
435 |
|
|
/** |
436 |
jsr166 |
1.24 |
* getExclusiveQueuedThreads returns all exclusive waiting threads |
437 |
dl |
1.1 |
*/ |
438 |
jsr166 |
1.24 |
public void testGetExclusiveQueuedThreads() { |
439 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
440 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
441 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
442 |
jsr166 |
1.24 |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
443 |
|
|
sync.acquire(); |
444 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
445 |
jsr166 |
1.9 |
t1.start(); |
446 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t1); |
447 |
|
|
assertHasExclusiveQueuedThreads(sync, t1); |
448 |
jsr166 |
1.9 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
449 |
jsr166 |
1.24 |
assertFalse(sync.getExclusiveQueuedThreads().contains(t2)); |
450 |
jsr166 |
1.9 |
t2.start(); |
451 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t2); |
452 |
|
|
assertHasExclusiveQueuedThreads(sync, t1, t2); |
453 |
jsr166 |
1.9 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
454 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
455 |
|
|
t1.interrupt(); |
456 |
jsr166 |
1.24 |
awaitTermination(t1); |
457 |
|
|
assertHasExclusiveQueuedThreads(sync, t2); |
458 |
|
|
sync.release(); |
459 |
|
|
awaitTermination(t2); |
460 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
461 |
jsr166 |
1.5 |
} |
462 |
dl |
1.1 |
|
463 |
|
|
/** |
464 |
|
|
* getSharedQueuedThreads does not include exclusively waiting threads |
465 |
|
|
*/ |
466 |
jsr166 |
1.24 |
public void testGetSharedQueuedThreads_Exclusive() { |
467 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
468 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
469 |
jsr166 |
1.24 |
sync.acquire(); |
470 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
471 |
jsr166 |
1.24 |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
472 |
|
|
waitForQueuedThread(sync, t1); |
473 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
474 |
jsr166 |
1.24 |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
475 |
|
|
waitForQueuedThread(sync, t2); |
476 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
477 |
|
|
t1.interrupt(); |
478 |
jsr166 |
1.24 |
awaitTermination(t1); |
479 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
480 |
jsr166 |
1.24 |
sync.release(); |
481 |
|
|
awaitTermination(t2); |
482 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
483 |
jsr166 |
1.5 |
} |
484 |
dl |
1.1 |
|
485 |
|
|
/** |
486 |
jsr166 |
1.24 |
* getSharedQueuedThreads returns all shared waiting threads |
487 |
dl |
1.1 |
*/ |
488 |
jsr166 |
1.24 |
public void testGetSharedQueuedThreads_Shared() { |
489 |
|
|
final BooleanLatch l = new BooleanLatch(); |
490 |
|
|
assertHasSharedQueuedThreads(l, NO_THREADS); |
491 |
|
|
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
492 |
|
|
public void realRun() throws InterruptedException { |
493 |
|
|
l.acquireSharedInterruptibly(0); |
494 |
|
|
}}); |
495 |
|
|
waitForQueuedThread(l, t1); |
496 |
|
|
assertHasSharedQueuedThreads(l, t1); |
497 |
|
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
498 |
|
|
public void realRun() throws InterruptedException { |
499 |
|
|
l.acquireSharedInterruptibly(0); |
500 |
|
|
}}); |
501 |
|
|
waitForQueuedThread(l, t2); |
502 |
|
|
assertHasSharedQueuedThreads(l, t1, t2); |
503 |
|
|
t1.interrupt(); |
504 |
|
|
awaitTermination(t1); |
505 |
|
|
assertHasSharedQueuedThreads(l, t2); |
506 |
|
|
assertTrue(l.releaseShared(0)); |
507 |
|
|
awaitTermination(t2); |
508 |
|
|
assertHasSharedQueuedThreads(l, NO_THREADS); |
509 |
|
|
} |
510 |
|
|
|
511 |
|
|
/** |
512 |
|
|
* tryAcquireNanos is interruptible |
513 |
|
|
*/ |
514 |
|
|
public void testTryAcquireNanos_Interruptible() { |
515 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
516 |
jsr166 |
1.24 |
sync.acquire(); |
517 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
518 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
519 |
jsr166 |
1.24 |
sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
520 |
jsr166 |
1.10 |
}}); |
521 |
jsr166 |
1.9 |
|
522 |
jsr166 |
1.24 |
waitForQueuedThread(sync, t); |
523 |
jsr166 |
1.9 |
t.interrupt(); |
524 |
jsr166 |
1.24 |
awaitTermination(t); |
525 |
dl |
1.1 |
} |
526 |
|
|
|
527 |
|
|
/** |
528 |
jsr166 |
1.24 |
* tryAcquire on exclusively held sync fails |
529 |
dl |
1.1 |
*/ |
530 |
jsr166 |
1.24 |
public void testTryAcquireWhenSynced() { |
531 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
532 |
jsr166 |
1.24 |
sync.acquire(); |
533 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
534 |
jsr166 |
1.10 |
public void realRun() { |
535 |
jsr166 |
1.24 |
assertFalse(sync.tryAcquire()); |
536 |
jsr166 |
1.10 |
}}); |
537 |
jsr166 |
1.9 |
|
538 |
jsr166 |
1.24 |
awaitTermination(t); |
539 |
|
|
sync.release(); |
540 |
jsr166 |
1.5 |
} |
541 |
dl |
1.1 |
|
542 |
|
|
/** |
543 |
|
|
* tryAcquireNanos on an exclusively held sync times out |
544 |
|
|
*/ |
545 |
jsr166 |
1.24 |
public void testAcquireNanos_Timeout() { |
546 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
547 |
jsr166 |
1.24 |
sync.acquire(); |
548 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
549 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
550 |
jsr166 |
1.24 |
long startTime = System.nanoTime(); |
551 |
|
|
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
552 |
|
|
assertFalse(sync.tryAcquireNanos(nanos)); |
553 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
554 |
jsr166 |
1.10 |
}}); |
555 |
jsr166 |
1.9 |
|
556 |
jsr166 |
1.24 |
awaitTermination(t); |
557 |
|
|
sync.release(); |
558 |
jsr166 |
1.5 |
} |
559 |
|
|
|
560 |
dl |
1.1 |
/** |
561 |
|
|
* getState is true when acquired and false when not |
562 |
|
|
*/ |
563 |
jsr166 |
1.24 |
public void testGetState() { |
564 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
565 |
jsr166 |
1.24 |
sync.acquire(); |
566 |
jsr166 |
1.11 |
assertTrue(sync.isHeldExclusively()); |
567 |
jsr166 |
1.24 |
sync.release(); |
568 |
jsr166 |
1.11 |
assertFalse(sync.isHeldExclusively()); |
569 |
jsr166 |
1.24 |
|
570 |
|
|
final BooleanLatch acquired = new BooleanLatch(); |
571 |
|
|
final BooleanLatch done = new BooleanLatch(); |
572 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
573 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
574 |
jsr166 |
1.24 |
sync.acquire(); |
575 |
|
|
assertTrue(acquired.releaseShared(0)); |
576 |
|
|
done.acquireShared(0); |
577 |
|
|
sync.release(); |
578 |
jsr166 |
1.10 |
}}); |
579 |
jsr166 |
1.9 |
|
580 |
jsr166 |
1.24 |
acquired.acquireShared(0); |
581 |
jsr166 |
1.9 |
assertTrue(sync.isHeldExclusively()); |
582 |
jsr166 |
1.24 |
assertTrue(done.releaseShared(0)); |
583 |
|
|
awaitTermination(t); |
584 |
jsr166 |
1.9 |
assertFalse(sync.isHeldExclusively()); |
585 |
dl |
1.1 |
} |
586 |
|
|
|
587 |
|
|
/** |
588 |
jsr166 |
1.24 |
* acquireInterruptibly succeeds when released, else is interruptible |
589 |
dl |
1.1 |
*/ |
590 |
jsr166 |
1.24 |
public void testAcquireInterruptibly() throws InterruptedException { |
591 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
592 |
jsr166 |
1.24 |
final BooleanLatch threadStarted = new BooleanLatch(); |
593 |
|
|
sync.acquireInterruptibly(); |
594 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
595 |
|
|
public void realRun() throws InterruptedException { |
596 |
|
|
assertTrue(threadStarted.releaseShared(0)); |
597 |
|
|
sync.acquireInterruptibly(); |
598 |
|
|
}}); |
599 |
dl |
1.1 |
|
600 |
jsr166 |
1.24 |
threadStarted.acquireShared(0); |
601 |
|
|
waitForQueuedThread(sync, t); |
602 |
jsr166 |
1.9 |
t.interrupt(); |
603 |
jsr166 |
1.24 |
awaitTermination(t); |
604 |
jsr166 |
1.9 |
assertTrue(sync.isHeldExclusively()); |
605 |
dl |
1.1 |
} |
606 |
|
|
|
607 |
|
|
/** |
608 |
|
|
* owns is true for a condition created by sync else false |
609 |
|
|
*/ |
610 |
|
|
public void testOwns() { |
611 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
612 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
613 |
dl |
1.1 |
final Mutex sync2 = new Mutex(); |
614 |
|
|
assertTrue(sync.owns(c)); |
615 |
|
|
assertFalse(sync2.owns(c)); |
616 |
|
|
} |
617 |
|
|
|
618 |
|
|
/** |
619 |
|
|
* Calling await without holding sync throws IllegalMonitorStateException |
620 |
|
|
*/ |
621 |
jsr166 |
1.24 |
public void testAwait_IMSE() { |
622 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
623 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
624 |
|
|
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
625 |
|
|
long startTime = System.nanoTime(); |
626 |
|
|
try { |
627 |
|
|
await(c, awaitMethod); |
628 |
|
|
shouldThrow(); |
629 |
|
|
} catch (IllegalMonitorStateException success) { |
630 |
|
|
} catch (InterruptedException e) { threadUnexpectedException(e); } |
631 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
632 |
|
|
} |
633 |
dl |
1.1 |
} |
634 |
|
|
|
635 |
|
|
/** |
636 |
|
|
* Calling signal without holding sync throws IllegalMonitorStateException |
637 |
|
|
*/ |
638 |
jsr166 |
1.24 |
public void testSignal_IMSE() { |
639 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
640 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
641 |
dl |
1.1 |
try { |
642 |
|
|
c.signal(); |
643 |
|
|
shouldThrow(); |
644 |
jsr166 |
1.10 |
} catch (IllegalMonitorStateException success) {} |
645 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
646 |
dl |
1.1 |
} |
647 |
|
|
|
648 |
|
|
/** |
649 |
jsr166 |
1.24 |
* Calling signalAll without holding sync throws IllegalMonitorStateException |
650 |
dl |
1.1 |
*/ |
651 |
jsr166 |
1.24 |
public void testSignalAll_IMSE() { |
652 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
653 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
654 |
|
|
try { |
655 |
|
|
c.signalAll(); |
656 |
|
|
shouldThrow(); |
657 |
|
|
} catch (IllegalMonitorStateException success) {} |
658 |
dl |
1.1 |
} |
659 |
|
|
|
660 |
|
|
/** |
661 |
jsr166 |
1.24 |
* await/awaitNanos/awaitUntil without a signal times out |
662 |
dl |
1.1 |
*/ |
663 |
jsr166 |
1.24 |
public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); } |
664 |
|
|
public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); } |
665 |
|
|
public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); } |
666 |
|
|
public void testAwait_Timeout(AwaitMethod awaitMethod) { |
667 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
668 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
669 |
|
|
sync.acquire(); |
670 |
|
|
assertAwaitTimesOut(c, awaitMethod); |
671 |
|
|
sync.release(); |
672 |
dl |
1.1 |
} |
673 |
|
|
|
674 |
|
|
/** |
675 |
jsr166 |
1.24 |
* await/awaitNanos/awaitUntil returns when signalled |
676 |
dl |
1.1 |
*/ |
677 |
jsr166 |
1.24 |
public void testSignal_await() { testSignal(AwaitMethod.await); } |
678 |
|
|
public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); } |
679 |
|
|
public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); } |
680 |
|
|
public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); } |
681 |
|
|
public void testSignal(final AwaitMethod awaitMethod) { |
682 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
683 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
684 |
|
|
final BooleanLatch acquired = new BooleanLatch(); |
685 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
686 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
687 |
jsr166 |
1.24 |
sync.acquire(); |
688 |
|
|
assertTrue(acquired.releaseShared(0)); |
689 |
|
|
await(c, awaitMethod); |
690 |
|
|
sync.release(); |
691 |
jsr166 |
1.10 |
}}); |
692 |
dl |
1.1 |
|
693 |
jsr166 |
1.24 |
acquired.acquireShared(0); |
694 |
|
|
sync.acquire(); |
695 |
|
|
assertHasWaitersLocked(sync, c, t); |
696 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
697 |
jsr166 |
1.9 |
c.signal(); |
698 |
jsr166 |
1.24 |
assertHasWaitersLocked(sync, c, NO_THREADS); |
699 |
|
|
assertHasExclusiveQueuedThreads(sync, t); |
700 |
|
|
sync.release(); |
701 |
|
|
awaitTermination(t); |
702 |
dl |
1.1 |
} |
703 |
|
|
|
704 |
|
|
/** |
705 |
jsr166 |
1.24 |
* hasWaiters(null) throws NullPointerException |
706 |
dl |
1.1 |
*/ |
707 |
|
|
public void testHasWaitersNPE() { |
708 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
709 |
dl |
1.1 |
try { |
710 |
|
|
sync.hasWaiters(null); |
711 |
|
|
shouldThrow(); |
712 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
713 |
dl |
1.1 |
} |
714 |
|
|
|
715 |
|
|
/** |
716 |
jsr166 |
1.24 |
* getWaitQueueLength(null) throws NullPointerException |
717 |
dl |
1.1 |
*/ |
718 |
|
|
public void testGetWaitQueueLengthNPE() { |
719 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
720 |
dl |
1.1 |
try { |
721 |
|
|
sync.getWaitQueueLength(null); |
722 |
|
|
shouldThrow(); |
723 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
724 |
dl |
1.1 |
} |
725 |
|
|
|
726 |
|
|
/** |
727 |
|
|
* getWaitingThreads throws NPE if null |
728 |
|
|
*/ |
729 |
|
|
public void testGetWaitingThreadsNPE() { |
730 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
731 |
dl |
1.1 |
try { |
732 |
|
|
sync.getWaitingThreads(null); |
733 |
|
|
shouldThrow(); |
734 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
735 |
dl |
1.1 |
} |
736 |
|
|
|
737 |
|
|
/** |
738 |
jsr166 |
1.24 |
* hasWaiters throws IllegalArgumentException if not owned |
739 |
dl |
1.1 |
*/ |
740 |
|
|
public void testHasWaitersIAE() { |
741 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
742 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
743 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
744 |
dl |
1.1 |
try { |
745 |
|
|
sync2.hasWaiters(c); |
746 |
|
|
shouldThrow(); |
747 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
748 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
749 |
dl |
1.1 |
} |
750 |
|
|
|
751 |
|
|
/** |
752 |
jsr166 |
1.24 |
* hasWaiters throws IllegalMonitorStateException if not synced |
753 |
dl |
1.1 |
*/ |
754 |
|
|
public void testHasWaitersIMSE() { |
755 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
756 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
757 |
dl |
1.1 |
try { |
758 |
|
|
sync.hasWaiters(c); |
759 |
|
|
shouldThrow(); |
760 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
761 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
762 |
dl |
1.1 |
} |
763 |
|
|
|
764 |
|
|
/** |
765 |
jsr166 |
1.24 |
* getWaitQueueLength throws IllegalArgumentException if not owned |
766 |
dl |
1.1 |
*/ |
767 |
|
|
public void testGetWaitQueueLengthIAE() { |
768 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
769 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
770 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
771 |
dl |
1.1 |
try { |
772 |
|
|
sync2.getWaitQueueLength(c); |
773 |
|
|
shouldThrow(); |
774 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
775 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
776 |
dl |
1.1 |
} |
777 |
|
|
|
778 |
|
|
/** |
779 |
jsr166 |
1.24 |
* getWaitQueueLength throws IllegalMonitorStateException if not synced |
780 |
dl |
1.1 |
*/ |
781 |
|
|
public void testGetWaitQueueLengthIMSE() { |
782 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
783 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
784 |
dl |
1.1 |
try { |
785 |
|
|
sync.getWaitQueueLength(c); |
786 |
|
|
shouldThrow(); |
787 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
788 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
789 |
dl |
1.1 |
} |
790 |
|
|
|
791 |
|
|
/** |
792 |
jsr166 |
1.24 |
* getWaitingThreads throws IllegalArgumentException if not owned |
793 |
dl |
1.1 |
*/ |
794 |
|
|
public void testGetWaitingThreadsIAE() { |
795 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
796 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
797 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
798 |
dl |
1.1 |
try { |
799 |
|
|
sync2.getWaitingThreads(c); |
800 |
|
|
shouldThrow(); |
801 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
802 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
803 |
dl |
1.1 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
jsr166 |
1.24 |
* getWaitingThreads throws IllegalMonitorStateException if not synced |
807 |
dl |
1.1 |
*/ |
808 |
|
|
public void testGetWaitingThreadsIMSE() { |
809 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
810 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
811 |
dl |
1.1 |
try { |
812 |
|
|
sync.getWaitingThreads(c); |
813 |
|
|
shouldThrow(); |
814 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
815 |
jsr166 |
1.24 |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
816 |
dl |
1.1 |
} |
817 |
|
|
|
818 |
|
|
/** |
819 |
|
|
* hasWaiters returns true when a thread is waiting, else false |
820 |
|
|
*/ |
821 |
jsr166 |
1.24 |
public void testHasWaiters() { |
822 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
823 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
824 |
|
|
final BooleanLatch acquired = new BooleanLatch(); |
825 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
826 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
827 |
jsr166 |
1.24 |
sync.acquire(); |
828 |
|
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
829 |
jsr166 |
1.17 |
assertFalse(sync.hasWaiters(c)); |
830 |
jsr166 |
1.24 |
assertTrue(acquired.releaseShared(0)); |
831 |
jsr166 |
1.10 |
c.await(); |
832 |
jsr166 |
1.24 |
sync.release(); |
833 |
jsr166 |
1.10 |
}}); |
834 |
dl |
1.1 |
|
835 |
jsr166 |
1.24 |
acquired.acquireShared(0); |
836 |
|
|
sync.acquire(); |
837 |
|
|
assertHasWaitersLocked(sync, c, t); |
838 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
839 |
jsr166 |
1.9 |
assertTrue(sync.hasWaiters(c)); |
840 |
|
|
c.signal(); |
841 |
jsr166 |
1.24 |
assertHasWaitersLocked(sync, c, NO_THREADS); |
842 |
|
|
assertHasExclusiveQueuedThreads(sync, t); |
843 |
jsr166 |
1.9 |
assertFalse(sync.hasWaiters(c)); |
844 |
jsr166 |
1.24 |
sync.release(); |
845 |
|
|
|
846 |
|
|
awaitTermination(t); |
847 |
|
|
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
848 |
dl |
1.1 |
} |
849 |
|
|
|
850 |
|
|
/** |
851 |
|
|
* getWaitQueueLength returns number of waiting threads |
852 |
|
|
*/ |
853 |
jsr166 |
1.24 |
public void testGetWaitQueueLength() { |
854 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
855 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
856 |
|
|
final BooleanLatch acquired1 = new BooleanLatch(); |
857 |
|
|
final BooleanLatch acquired2 = new BooleanLatch(); |
858 |
|
|
final Thread t1 = newStartedThread(new CheckedRunnable() { |
859 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
860 |
jsr166 |
1.24 |
sync.acquire(); |
861 |
|
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
862 |
jsr166 |
1.17 |
assertEquals(0, sync.getWaitQueueLength(c)); |
863 |
jsr166 |
1.24 |
assertTrue(acquired1.releaseShared(0)); |
864 |
jsr166 |
1.10 |
c.await(); |
865 |
jsr166 |
1.24 |
sync.release(); |
866 |
jsr166 |
1.10 |
}}); |
867 |
jsr166 |
1.24 |
acquired1.acquireShared(0); |
868 |
|
|
sync.acquire(); |
869 |
|
|
assertHasWaitersLocked(sync, c, t1); |
870 |
|
|
assertEquals(1, sync.getWaitQueueLength(c)); |
871 |
|
|
sync.release(); |
872 |
jsr166 |
1.10 |
|
873 |
jsr166 |
1.24 |
final Thread t2 = newStartedThread(new CheckedRunnable() { |
874 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
875 |
jsr166 |
1.24 |
sync.acquire(); |
876 |
|
|
assertHasWaitersLocked(sync, c, t1); |
877 |
jsr166 |
1.17 |
assertEquals(1, sync.getWaitQueueLength(c)); |
878 |
jsr166 |
1.24 |
assertTrue(acquired2.releaseShared(0)); |
879 |
jsr166 |
1.10 |
c.await(); |
880 |
jsr166 |
1.24 |
sync.release(); |
881 |
jsr166 |
1.10 |
}}); |
882 |
jsr166 |
1.24 |
acquired2.acquireShared(0); |
883 |
|
|
sync.acquire(); |
884 |
|
|
assertHasWaitersLocked(sync, c, t1, t2); |
885 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
886 |
jsr166 |
1.9 |
assertEquals(2, sync.getWaitQueueLength(c)); |
887 |
|
|
c.signalAll(); |
888 |
jsr166 |
1.24 |
assertHasWaitersLocked(sync, c, NO_THREADS); |
889 |
|
|
assertHasExclusiveQueuedThreads(sync, t1, t2); |
890 |
jsr166 |
1.9 |
assertEquals(0, sync.getWaitQueueLength(c)); |
891 |
jsr166 |
1.24 |
sync.release(); |
892 |
|
|
|
893 |
|
|
awaitTermination(t1); |
894 |
|
|
awaitTermination(t2); |
895 |
|
|
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
896 |
dl |
1.1 |
} |
897 |
|
|
|
898 |
|
|
/** |
899 |
|
|
* getWaitingThreads returns only and all waiting threads |
900 |
|
|
*/ |
901 |
jsr166 |
1.24 |
public void testGetWaitingThreads() { |
902 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
903 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
904 |
|
|
final BooleanLatch acquired1 = new BooleanLatch(); |
905 |
|
|
final BooleanLatch acquired2 = new BooleanLatch(); |
906 |
|
|
final Thread t1 = new Thread(new CheckedRunnable() { |
907 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
908 |
jsr166 |
1.24 |
sync.acquire(); |
909 |
|
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
910 |
jsr166 |
1.17 |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
911 |
jsr166 |
1.24 |
assertTrue(acquired1.releaseShared(0)); |
912 |
jsr166 |
1.10 |
c.await(); |
913 |
jsr166 |
1.24 |
sync.release(); |
914 |
jsr166 |
1.10 |
}}); |
915 |
|
|
|
916 |
jsr166 |
1.24 |
final Thread t2 = new Thread(new CheckedRunnable() { |
917 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
918 |
jsr166 |
1.24 |
sync.acquire(); |
919 |
|
|
assertHasWaitersLocked(sync, c, t1); |
920 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
921 |
jsr166 |
1.17 |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
922 |
jsr166 |
1.24 |
assertEquals(1, sync.getWaitingThreads(c).size()); |
923 |
|
|
assertTrue(acquired2.releaseShared(0)); |
924 |
jsr166 |
1.10 |
c.await(); |
925 |
jsr166 |
1.24 |
sync.release(); |
926 |
jsr166 |
1.10 |
}}); |
927 |
dl |
1.1 |
|
928 |
jsr166 |
1.24 |
sync.acquire(); |
929 |
|
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
930 |
|
|
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
931 |
|
|
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
932 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
933 |
|
|
assertEquals(0, sync.getWaitingThreads(c).size()); |
934 |
|
|
sync.release(); |
935 |
dl |
1.1 |
|
936 |
jsr166 |
1.24 |
t1.start(); |
937 |
|
|
acquired1.acquireShared(0); |
938 |
|
|
sync.acquire(); |
939 |
|
|
assertHasWaitersLocked(sync, c, t1); |
940 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
941 |
|
|
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
942 |
|
|
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
943 |
|
|
assertEquals(1, sync.getWaitingThreads(c).size()); |
944 |
|
|
sync.release(); |
945 |
dl |
1.1 |
|
946 |
jsr166 |
1.24 |
t2.start(); |
947 |
|
|
acquired2.acquireShared(0); |
948 |
|
|
sync.acquire(); |
949 |
|
|
assertHasWaitersLocked(sync, c, t1, t2); |
950 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
951 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
952 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
953 |
|
|
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
954 |
|
|
assertEquals(2, sync.getWaitingThreads(c).size()); |
955 |
|
|
c.signalAll(); |
956 |
|
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
957 |
|
|
assertHasExclusiveQueuedThreads(sync, t1, t2); |
958 |
|
|
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
959 |
|
|
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
960 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
961 |
|
|
assertEquals(0, sync.getWaitingThreads(c).size()); |
962 |
|
|
sync.release(); |
963 |
|
|
|
964 |
|
|
awaitTermination(t1); |
965 |
|
|
awaitTermination(t2); |
966 |
|
|
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
967 |
|
|
} |
968 |
dl |
1.1 |
|
969 |
|
|
/** |
970 |
jsr166 |
1.25 |
* awaitUninterruptibly is uninterruptible |
971 |
dl |
1.1 |
*/ |
972 |
jsr166 |
1.24 |
public void testAwaitUninterruptibly() { |
973 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
974 |
jsr166 |
1.38 |
final ConditionObject condition = sync.newCondition(); |
975 |
jsr166 |
1.25 |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
976 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedRunnable() { |
977 |
jsr166 |
1.10 |
public void realRun() { |
978 |
jsr166 |
1.24 |
sync.acquire(); |
979 |
jsr166 |
1.25 |
assertTrue(pleaseInterrupt.releaseShared(0)); |
980 |
jsr166 |
1.38 |
condition.awaitUninterruptibly(); |
981 |
jsr166 |
1.24 |
assertTrue(Thread.interrupted()); |
982 |
jsr166 |
1.38 |
assertHasWaitersLocked(sync, condition, NO_THREADS); |
983 |
jsr166 |
1.24 |
sync.release(); |
984 |
jsr166 |
1.10 |
}}); |
985 |
dl |
1.1 |
|
986 |
jsr166 |
1.25 |
pleaseInterrupt.acquireShared(0); |
987 |
jsr166 |
1.24 |
sync.acquire(); |
988 |
jsr166 |
1.38 |
assertHasWaitersLocked(sync, condition, t); |
989 |
jsr166 |
1.24 |
sync.release(); |
990 |
jsr166 |
1.9 |
t.interrupt(); |
991 |
jsr166 |
1.38 |
assertHasWaitersUnlocked(sync, condition, t); |
992 |
|
|
assertThreadBlocks(t, Thread.State.WAITING); |
993 |
jsr166 |
1.24 |
sync.acquire(); |
994 |
jsr166 |
1.38 |
assertHasWaitersLocked(sync, condition, t); |
995 |
jsr166 |
1.24 |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
996 |
jsr166 |
1.38 |
condition.signal(); |
997 |
|
|
assertHasWaitersLocked(sync, condition, NO_THREADS); |
998 |
jsr166 |
1.24 |
assertHasExclusiveQueuedThreads(sync, t); |
999 |
|
|
sync.release(); |
1000 |
|
|
awaitTermination(t); |
1001 |
dl |
1.1 |
} |
1002 |
|
|
|
1003 |
|
|
/** |
1004 |
jsr166 |
1.24 |
* await/awaitNanos/awaitUntil is interruptible |
1005 |
dl |
1.1 |
*/ |
1006 |
jsr166 |
1.24 |
public void testInterruptible_await() { testInterruptible(AwaitMethod.await); } |
1007 |
|
|
public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); } |
1008 |
|
|
public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); } |
1009 |
|
|
public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); } |
1010 |
|
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
1011 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
1012 |
jsr166 |
1.24 |
final ConditionObject c = sync.newCondition(); |
1013 |
jsr166 |
1.25 |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
1014 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1015 |
jsr166 |
1.11 |
public void realRun() throws InterruptedException { |
1016 |
jsr166 |
1.24 |
sync.acquire(); |
1017 |
jsr166 |
1.25 |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1018 |
jsr166 |
1.24 |
await(c, awaitMethod); |
1019 |
jsr166 |
1.10 |
}}); |
1020 |
dl |
1.1 |
|
1021 |
jsr166 |
1.25 |
pleaseInterrupt.acquireShared(0); |
1022 |
jsr166 |
1.9 |
t.interrupt(); |
1023 |
jsr166 |
1.24 |
awaitTermination(t); |
1024 |
dl |
1.1 |
} |
1025 |
|
|
|
1026 |
|
|
/** |
1027 |
|
|
* signalAll wakes up all threads |
1028 |
|
|
*/ |
1029 |
jsr166 |
1.24 |
public void testSignalAll_await() { testSignalAll(AwaitMethod.await); } |
1030 |
|
|
public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); } |
1031 |
|
|
public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); } |
1032 |
|
|
public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); } |
1033 |
|
|
public void testSignalAll(final AwaitMethod awaitMethod) { |
1034 |
|
|
final Mutex sync = new Mutex(); |
1035 |
|
|
final ConditionObject c = sync.newCondition(); |
1036 |
|
|
final BooleanLatch acquired1 = new BooleanLatch(); |
1037 |
|
|
final BooleanLatch acquired2 = new BooleanLatch(); |
1038 |
|
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1039 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1040 |
jsr166 |
1.24 |
sync.acquire(); |
1041 |
|
|
acquired1.releaseShared(0); |
1042 |
|
|
await(c, awaitMethod); |
1043 |
|
|
sync.release(); |
1044 |
jsr166 |
1.10 |
}}); |
1045 |
|
|
|
1046 |
jsr166 |
1.24 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1047 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1048 |
jsr166 |
1.24 |
sync.acquire(); |
1049 |
|
|
acquired2.releaseShared(0); |
1050 |
|
|
await(c, awaitMethod); |
1051 |
|
|
sync.release(); |
1052 |
jsr166 |
1.10 |
}}); |
1053 |
dl |
1.1 |
|
1054 |
jsr166 |
1.24 |
acquired1.acquireShared(0); |
1055 |
|
|
acquired2.acquireShared(0); |
1056 |
|
|
sync.acquire(); |
1057 |
|
|
assertHasWaitersLocked(sync, c, t1, t2); |
1058 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1059 |
jsr166 |
1.9 |
c.signalAll(); |
1060 |
jsr166 |
1.24 |
assertHasWaitersLocked(sync, c, NO_THREADS); |
1061 |
|
|
assertHasExclusiveQueuedThreads(sync, t1, t2); |
1062 |
|
|
sync.release(); |
1063 |
|
|
awaitTermination(t1); |
1064 |
|
|
awaitTermination(t2); |
1065 |
dl |
1.1 |
} |
1066 |
|
|
|
1067 |
|
|
/** |
1068 |
|
|
* toString indicates current state |
1069 |
|
|
*/ |
1070 |
|
|
public void testToString() { |
1071 |
|
|
Mutex sync = new Mutex(); |
1072 |
jsr166 |
1.24 |
assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED)); |
1073 |
|
|
sync.acquire(); |
1074 |
|
|
assertTrue(sync.toString().contains("State = " + Mutex.LOCKED)); |
1075 |
dl |
1.1 |
} |
1076 |
|
|
|
1077 |
|
|
/** |
1078 |
jsr166 |
1.24 |
* A serialized AQS deserializes with current state, but no queued threads |
1079 |
|
|
*/ |
1080 |
|
|
public void testSerialization() { |
1081 |
|
|
Mutex sync = new Mutex(); |
1082 |
|
|
assertFalse(serialClone(sync).isHeldExclusively()); |
1083 |
|
|
sync.acquire(); |
1084 |
|
|
Thread t = newStartedThread(new InterruptedSyncRunnable(sync)); |
1085 |
|
|
waitForQueuedThread(sync, t); |
1086 |
|
|
assertTrue(sync.isHeldExclusively()); |
1087 |
|
|
|
1088 |
|
|
Mutex clone = serialClone(sync); |
1089 |
|
|
assertTrue(clone.isHeldExclusively()); |
1090 |
|
|
assertHasExclusiveQueuedThreads(sync, t); |
1091 |
|
|
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1092 |
|
|
t.interrupt(); |
1093 |
|
|
awaitTermination(t); |
1094 |
|
|
sync.release(); |
1095 |
|
|
assertFalse(sync.isHeldExclusively()); |
1096 |
|
|
assertTrue(clone.isHeldExclusively()); |
1097 |
|
|
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1098 |
|
|
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1099 |
dl |
1.1 |
} |
1100 |
|
|
|
1101 |
|
|
/** |
1102 |
|
|
* tryReleaseShared setting state changes getState |
1103 |
|
|
*/ |
1104 |
|
|
public void testGetStateWithReleaseShared() { |
1105 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
1106 |
|
|
assertFalse(l.isSignalled()); |
1107 |
jsr166 |
1.24 |
assertTrue(l.releaseShared(0)); |
1108 |
jsr166 |
1.11 |
assertTrue(l.isSignalled()); |
1109 |
dl |
1.1 |
} |
1110 |
|
|
|
1111 |
|
|
/** |
1112 |
|
|
* releaseShared has no effect when already signalled |
1113 |
|
|
*/ |
1114 |
|
|
public void testReleaseShared() { |
1115 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
1116 |
|
|
assertFalse(l.isSignalled()); |
1117 |
jsr166 |
1.24 |
assertTrue(l.releaseShared(0)); |
1118 |
jsr166 |
1.11 |
assertTrue(l.isSignalled()); |
1119 |
jsr166 |
1.24 |
assertTrue(l.releaseShared(0)); |
1120 |
jsr166 |
1.11 |
assertTrue(l.isSignalled()); |
1121 |
dl |
1.1 |
} |
1122 |
|
|
|
1123 |
|
|
/** |
1124 |
|
|
* acquireSharedInterruptibly returns after release, but not before |
1125 |
|
|
*/ |
1126 |
jsr166 |
1.24 |
public void testAcquireSharedInterruptibly() { |
1127 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
1128 |
dl |
1.1 |
|
1129 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedRunnable() { |
1130 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1131 |
jsr166 |
1.17 |
assertFalse(l.isSignalled()); |
1132 |
jsr166 |
1.10 |
l.acquireSharedInterruptibly(0); |
1133 |
jsr166 |
1.17 |
assertTrue(l.isSignalled()); |
1134 |
jsr166 |
1.24 |
l.acquireSharedInterruptibly(0); |
1135 |
|
|
assertTrue(l.isSignalled()); |
1136 |
jsr166 |
1.10 |
}}); |
1137 |
jsr166 |
1.9 |
|
1138 |
jsr166 |
1.24 |
waitForQueuedThread(l, t); |
1139 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
1140 |
jsr166 |
1.39 |
assertThreadBlocks(t, Thread.State.WAITING); |
1141 |
jsr166 |
1.24 |
assertHasSharedQueuedThreads(l, t); |
1142 |
|
|
assertTrue(l.releaseShared(0)); |
1143 |
jsr166 |
1.9 |
assertTrue(l.isSignalled()); |
1144 |
jsr166 |
1.24 |
awaitTermination(t); |
1145 |
dl |
1.1 |
} |
1146 |
jsr166 |
1.5 |
|
1147 |
dl |
1.1 |
/** |
1148 |
jsr166 |
1.24 |
* tryAcquireSharedNanos returns after release, but not before |
1149 |
dl |
1.1 |
*/ |
1150 |
jsr166 |
1.24 |
public void testTryAcquireSharedNanos() { |
1151 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
1152 |
dl |
1.1 |
|
1153 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedRunnable() { |
1154 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1155 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
1156 |
jsr166 |
1.24 |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1157 |
|
|
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1158 |
|
|
assertTrue(l.isSignalled()); |
1159 |
jsr166 |
1.15 |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1160 |
|
|
assertTrue(l.isSignalled()); |
1161 |
jsr166 |
1.10 |
}}); |
1162 |
jsr166 |
1.9 |
|
1163 |
jsr166 |
1.24 |
waitForQueuedThread(l, t); |
1164 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
1165 |
jsr166 |
1.40 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1166 |
jsr166 |
1.24 |
assertTrue(l.releaseShared(0)); |
1167 |
jsr166 |
1.9 |
assertTrue(l.isSignalled()); |
1168 |
jsr166 |
1.24 |
awaitTermination(t); |
1169 |
dl |
1.1 |
} |
1170 |
jsr166 |
1.5 |
|
1171 |
dl |
1.1 |
/** |
1172 |
jsr166 |
1.24 |
* acquireSharedInterruptibly is interruptible |
1173 |
dl |
1.1 |
*/ |
1174 |
jsr166 |
1.24 |
public void testAcquireSharedInterruptibly_Interruptible() { |
1175 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
1176 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1177 |
jsr166 |
1.11 |
public void realRun() throws InterruptedException { |
1178 |
jsr166 |
1.17 |
assertFalse(l.isSignalled()); |
1179 |
jsr166 |
1.10 |
l.acquireSharedInterruptibly(0); |
1180 |
|
|
}}); |
1181 |
jsr166 |
1.9 |
|
1182 |
jsr166 |
1.24 |
waitForQueuedThread(l, t); |
1183 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
1184 |
|
|
t.interrupt(); |
1185 |
jsr166 |
1.24 |
awaitTermination(t); |
1186 |
|
|
assertFalse(l.isSignalled()); |
1187 |
dl |
1.1 |
} |
1188 |
|
|
|
1189 |
|
|
/** |
1190 |
jsr166 |
1.24 |
* tryAcquireSharedNanos is interruptible |
1191 |
dl |
1.1 |
*/ |
1192 |
jsr166 |
1.24 |
public void testTryAcquireSharedNanos_Interruptible() { |
1193 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
1194 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1195 |
jsr166 |
1.11 |
public void realRun() throws InterruptedException { |
1196 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
1197 |
jsr166 |
1.24 |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1198 |
jsr166 |
1.15 |
l.tryAcquireSharedNanos(0, nanos); |
1199 |
jsr166 |
1.10 |
}}); |
1200 |
jsr166 |
1.9 |
|
1201 |
jsr166 |
1.24 |
waitForQueuedThread(l, t); |
1202 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
1203 |
|
|
t.interrupt(); |
1204 |
jsr166 |
1.24 |
awaitTermination(t); |
1205 |
|
|
assertFalse(l.isSignalled()); |
1206 |
dl |
1.1 |
} |
1207 |
|
|
|
1208 |
|
|
/** |
1209 |
jsr166 |
1.24 |
* tryAcquireSharedNanos times out if not released before timeout |
1210 |
dl |
1.1 |
*/ |
1211 |
jsr166 |
1.24 |
public void testTryAcquireSharedNanos_Timeout() { |
1212 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
1213 |
jsr166 |
1.27 |
final BooleanLatch observedQueued = new BooleanLatch(); |
1214 |
jsr166 |
1.24 |
Thread t = newStartedThread(new CheckedRunnable() { |
1215 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1216 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
1217 |
jsr166 |
1.27 |
for (long millis = timeoutMillis(); |
1218 |
|
|
!observedQueued.isSignalled(); |
1219 |
|
|
millis *= 2) { |
1220 |
|
|
long nanos = MILLISECONDS.toNanos(millis); |
1221 |
|
|
long startTime = System.nanoTime(); |
1222 |
|
|
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1223 |
|
|
assertTrue(millisElapsedSince(startTime) >= millis); |
1224 |
|
|
} |
1225 |
jsr166 |
1.24 |
assertFalse(l.isSignalled()); |
1226 |
jsr166 |
1.10 |
}}); |
1227 |
jsr166 |
1.9 |
|
1228 |
jsr166 |
1.24 |
waitForQueuedThread(l, t); |
1229 |
jsr166 |
1.27 |
observedQueued.releaseShared(0); |
1230 |
jsr166 |
1.24 |
assertFalse(l.isSignalled()); |
1231 |
|
|
awaitTermination(t); |
1232 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
1233 |
dl |
1.1 |
} |
1234 |
|
|
|
1235 |
jsr166 |
1.28 |
/** |
1236 |
|
|
* awaitNanos/timed await with 0 wait times out immediately |
1237 |
|
|
*/ |
1238 |
|
|
public void testAwait_Zero() throws InterruptedException { |
1239 |
|
|
final Mutex sync = new Mutex(); |
1240 |
|
|
final ConditionObject c = sync.newCondition(); |
1241 |
|
|
sync.acquire(); |
1242 |
|
|
assertTrue(c.awaitNanos(0L) <= 0); |
1243 |
|
|
assertFalse(c.await(0L, NANOSECONDS)); |
1244 |
|
|
sync.release(); |
1245 |
|
|
} |
1246 |
|
|
|
1247 |
|
|
/** |
1248 |
|
|
* awaitNanos/timed await with maximum negative wait times does not underflow |
1249 |
|
|
*/ |
1250 |
|
|
public void testAwait_NegativeInfinity() throws InterruptedException { |
1251 |
|
|
final Mutex sync = new Mutex(); |
1252 |
|
|
final ConditionObject c = sync.newCondition(); |
1253 |
|
|
sync.acquire(); |
1254 |
|
|
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1255 |
|
|
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1256 |
|
|
sync.release(); |
1257 |
|
|
} |
1258 |
|
|
|
1259 |
jsr166 |
1.43 |
/** |
1260 |
|
|
* Tests scenario for |
1261 |
|
|
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1262 |
|
|
*/ |
1263 |
|
|
public void testInterruptedFailingAcquire() throws InterruptedException { |
1264 |
|
|
final RuntimeException ex = new RuntimeException(); |
1265 |
|
|
|
1266 |
|
|
// A synchronizer only offering a choice of failure modes |
1267 |
|
|
class Sync extends AbstractQueuedLongSynchronizer { |
1268 |
|
|
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 |
|
|
|
1287 |
|
|
final Thread thread = newStartedThread(new CheckedRunnable() { |
1288 |
|
|
public void realRun() { |
1289 |
|
|
try { |
1290 |
jsr166 |
1.46 |
if (randomBoolean()) |
1291 |
jsr166 |
1.43 |
s.acquire(1); |
1292 |
|
|
else |
1293 |
|
|
s.acquireShared(1); |
1294 |
|
|
shouldThrow(); |
1295 |
|
|
} catch (Throwable t) { |
1296 |
|
|
assertSame(ex, t); |
1297 |
|
|
assertTrue(Thread.interrupted()); |
1298 |
|
|
} |
1299 |
|
|
}}); |
1300 |
|
|
waitForThreadToEnterWaitState(thread); |
1301 |
|
|
assertSame(thread, s.getFirstQueuedThread()); |
1302 |
|
|
assertTrue(s.hasQueuedPredecessors()); |
1303 |
|
|
assertTrue(s.hasQueuedThreads()); |
1304 |
|
|
assertEquals(1, s.getQueueLength()); |
1305 |
|
|
|
1306 |
|
|
s.pleaseThrow = true; |
1307 |
|
|
thread.interrupt(); |
1308 |
|
|
s.release(1); |
1309 |
|
|
awaitTermination(thread); |
1310 |
|
|
} |
1311 |
|
|
|
1312 |
dl |
1.1 |
} |