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