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