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