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