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 |
> |
junit.textui.TestRunner.run(suite()); |
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) { |
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 |
+ |
long timeoutMillis = timeoutMillis(); |
239 |
+ |
long startTime = System.nanoTime(); |
240 |
+ |
try { |
241 |
+ |
switch (awaitMethod) { |
242 |
+ |
case awaitTimed: |
243 |
+ |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
244 |
+ |
break; |
245 |
+ |
case awaitNanos: |
246 |
+ |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
247 |
+ |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
248 |
+ |
assertTrue(nanosRemaining <= 0); |
249 |
+ |
break; |
250 |
+ |
case awaitUntil: |
251 |
+ |
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
252 |
+ |
break; |
253 |
+ |
default: |
254 |
+ |
throw new UnsupportedOperationException(); |
255 |
+ |
} |
256 |
+ |
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
257 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
258 |
+ |
} |
259 |
+ |
|
260 |
+ |
/** |
261 |
|
* isHeldExclusively is false upon construction |
262 |
|
*/ |
263 |
|
public void testIsHeldExclusively() { |
264 |
< |
Mutex rl = new Mutex(); |
265 |
< |
assertFalse(rl.isHeldExclusively()); |
264 |
> |
Mutex sync = new Mutex(); |
265 |
> |
assertFalse(sync.isHeldExclusively()); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
|
* acquiring released sync succeeds |
270 |
|
*/ |
271 |
|
public void testAcquire() { |
272 |
< |
Mutex rl = new Mutex(); |
273 |
< |
rl.acquire(1); |
274 |
< |
assertTrue(rl.isHeldExclusively()); |
275 |
< |
rl.release(1); |
276 |
< |
assertFalse(rl.isHeldExclusively()); |
272 |
> |
Mutex sync = new Mutex(); |
273 |
> |
sync.acquire(); |
274 |
> |
assertTrue(sync.isHeldExclusively()); |
275 |
> |
sync.release(); |
276 |
> |
assertFalse(sync.isHeldExclusively()); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* tryAcquire on an released sync succeeds |
280 |
> |
* tryAcquire on a released sync succeeds |
281 |
|
*/ |
282 |
|
public void testTryAcquire() { |
283 |
< |
Mutex rl = new Mutex(); |
284 |
< |
assertTrue(rl.tryAcquire(1)); |
285 |
< |
assertTrue(rl.isHeldExclusively()); |
286 |
< |
rl.release(1); |
283 |
> |
Mutex sync = new Mutex(); |
284 |
> |
assertTrue(sync.tryAcquire()); |
285 |
> |
assertTrue(sync.isHeldExclusively()); |
286 |
> |
sync.release(); |
287 |
> |
assertFalse(sync.isHeldExclusively()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
|
* hasQueuedThreads reports whether there are waiting threads |
292 |
|
*/ |
293 |
< |
public void testhasQueuedThreads() throws InterruptedException { |
293 |
> |
public void testHasQueuedThreads() { |
294 |
|
final Mutex sync = new Mutex(); |
129 |
– |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
130 |
– |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
295 |
|
assertFalse(sync.hasQueuedThreads()); |
296 |
< |
sync.acquire(1); |
297 |
< |
t1.start(); |
298 |
< |
Thread.sleep(SHORT_DELAY_MS); |
296 |
> |
sync.acquire(); |
297 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
298 |
> |
waitForQueuedThread(sync, t1); |
299 |
|
assertTrue(sync.hasQueuedThreads()); |
300 |
< |
t2.start(); |
301 |
< |
Thread.sleep(SHORT_DELAY_MS); |
300 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
301 |
> |
waitForQueuedThread(sync, t2); |
302 |
|
assertTrue(sync.hasQueuedThreads()); |
303 |
|
t1.interrupt(); |
304 |
< |
Thread.sleep(SHORT_DELAY_MS); |
304 |
> |
awaitTermination(t1); |
305 |
|
assertTrue(sync.hasQueuedThreads()); |
306 |
< |
sync.release(1); |
307 |
< |
Thread.sleep(SHORT_DELAY_MS); |
306 |
> |
sync.release(); |
307 |
> |
awaitTermination(t2); |
308 |
|
assertFalse(sync.hasQueuedThreads()); |
145 |
– |
t1.join(); |
146 |
– |
t2.join(); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* isQueued(null) throws NPE |
312 |
> |
* isQueued(null) throws NullPointerException |
313 |
|
*/ |
314 |
|
public void testIsQueuedNPE() { |
315 |
|
final Mutex sync = new Mutex(); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* isQueued reports whether a thread is queued. |
323 |
> |
* isQueued reports whether a thread is queued |
324 |
|
*/ |
325 |
< |
public void testIsQueued() throws InterruptedException { |
325 |
> |
public void testIsQueued() { |
326 |
|
final Mutex sync = new Mutex(); |
327 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
328 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
329 |
|
assertFalse(sync.isQueued(t1)); |
330 |
|
assertFalse(sync.isQueued(t2)); |
331 |
< |
sync.acquire(1); |
331 |
> |
sync.acquire(); |
332 |
|
t1.start(); |
333 |
< |
Thread.sleep(SHORT_DELAY_MS); |
333 |
> |
waitForQueuedThread(sync, t1); |
334 |
|
assertTrue(sync.isQueued(t1)); |
335 |
+ |
assertFalse(sync.isQueued(t2)); |
336 |
|
t2.start(); |
337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
337 |
> |
waitForQueuedThread(sync, t2); |
338 |
|
assertTrue(sync.isQueued(t1)); |
339 |
|
assertTrue(sync.isQueued(t2)); |
340 |
|
t1.interrupt(); |
341 |
< |
Thread.sleep(SHORT_DELAY_MS); |
341 |
> |
awaitTermination(t1); |
342 |
|
assertFalse(sync.isQueued(t1)); |
343 |
|
assertTrue(sync.isQueued(t2)); |
344 |
< |
sync.release(1); |
345 |
< |
Thread.sleep(SHORT_DELAY_MS); |
344 |
> |
sync.release(); |
345 |
> |
awaitTermination(t2); |
346 |
|
assertFalse(sync.isQueued(t1)); |
184 |
– |
Thread.sleep(SHORT_DELAY_MS); |
347 |
|
assertFalse(sync.isQueued(t2)); |
186 |
– |
t1.join(); |
187 |
– |
t2.join(); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
352 |
|
*/ |
353 |
< |
public void testGetFirstQueuedThread() throws InterruptedException { |
353 |
> |
public void testGetFirstQueuedThread() { |
354 |
|
final Mutex sync = new Mutex(); |
195 |
– |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
196 |
– |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
355 |
|
assertNull(sync.getFirstQueuedThread()); |
356 |
< |
sync.acquire(1); |
357 |
< |
t1.start(); |
358 |
< |
Thread.sleep(SHORT_DELAY_MS); |
356 |
> |
sync.acquire(); |
357 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
358 |
> |
waitForQueuedThread(sync, t1); |
359 |
|
assertEquals(t1, sync.getFirstQueuedThread()); |
360 |
< |
t2.start(); |
361 |
< |
Thread.sleep(SHORT_DELAY_MS); |
360 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
361 |
> |
waitForQueuedThread(sync, t2); |
362 |
|
assertEquals(t1, sync.getFirstQueuedThread()); |
363 |
|
t1.interrupt(); |
364 |
< |
Thread.sleep(SHORT_DELAY_MS); |
207 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
> |
awaitTermination(t1); |
365 |
|
assertEquals(t2, sync.getFirstQueuedThread()); |
366 |
< |
sync.release(1); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
366 |
> |
sync.release(); |
367 |
> |
awaitTermination(t2); |
368 |
|
assertNull(sync.getFirstQueuedThread()); |
212 |
– |
t1.join(); |
213 |
– |
t2.join(); |
369 |
|
} |
370 |
|
|
216 |
– |
|
371 |
|
/** |
372 |
|
* hasContended reports false if no thread has ever blocked, else true |
373 |
|
*/ |
374 |
< |
public void testHasContended() throws InterruptedException { |
374 |
> |
public void testHasContended() { |
375 |
|
final Mutex sync = new Mutex(); |
222 |
– |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
223 |
– |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
376 |
|
assertFalse(sync.hasContended()); |
377 |
< |
sync.acquire(1); |
378 |
< |
t1.start(); |
379 |
< |
Thread.sleep(SHORT_DELAY_MS); |
377 |
> |
sync.acquire(); |
378 |
> |
assertFalse(sync.hasContended()); |
379 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
380 |
> |
waitForQueuedThread(sync, t1); |
381 |
|
assertTrue(sync.hasContended()); |
382 |
< |
t2.start(); |
383 |
< |
Thread.sleep(SHORT_DELAY_MS); |
382 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
383 |
> |
waitForQueuedThread(sync, t2); |
384 |
|
assertTrue(sync.hasContended()); |
385 |
|
t1.interrupt(); |
386 |
< |
Thread.sleep(SHORT_DELAY_MS); |
386 |
> |
awaitTermination(t1); |
387 |
|
assertTrue(sync.hasContended()); |
388 |
< |
sync.release(1); |
389 |
< |
Thread.sleep(SHORT_DELAY_MS); |
388 |
> |
sync.release(); |
389 |
> |
awaitTermination(t2); |
390 |
|
assertTrue(sync.hasContended()); |
238 |
– |
t1.join(); |
239 |
– |
t2.join(); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
< |
* getQueuedThreads includes waiting threads |
394 |
> |
* getQueuedThreads returns all waiting threads |
395 |
|
*/ |
396 |
< |
public void testGetQueuedThreads() throws InterruptedException { |
396 |
> |
public void testGetQueuedThreads() { |
397 |
|
final Mutex sync = new Mutex(); |
398 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
399 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
400 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
401 |
< |
sync.acquire(1); |
402 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
400 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
401 |
> |
sync.acquire(); |
402 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
403 |
|
t1.start(); |
404 |
< |
Thread.sleep(SHORT_DELAY_MS); |
404 |
> |
waitForQueuedThread(sync, t1); |
405 |
> |
assertHasExclusiveQueuedThreads(sync, t1); |
406 |
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
407 |
+ |
assertFalse(sync.getQueuedThreads().contains(t2)); |
408 |
|
t2.start(); |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
409 |
> |
waitForQueuedThread(sync, t2); |
410 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
411 |
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
412 |
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
413 |
|
t1.interrupt(); |
414 |
< |
Thread.sleep(SHORT_DELAY_MS); |
415 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
416 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
417 |
< |
sync.release(1); |
418 |
< |
Thread.sleep(SHORT_DELAY_MS); |
265 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
266 |
< |
t1.join(); |
267 |
< |
t2.join(); |
414 |
> |
awaitTermination(t1); |
415 |
> |
assertHasExclusiveQueuedThreads(sync, t2); |
416 |
> |
sync.release(); |
417 |
> |
awaitTermination(t2); |
418 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
< |
* getExclusiveQueuedThreads includes waiting threads |
422 |
> |
* getExclusiveQueuedThreads returns all exclusive waiting threads |
423 |
|
*/ |
424 |
< |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
424 |
> |
public void testGetExclusiveQueuedThreads() { |
425 |
|
final Mutex sync = new Mutex(); |
426 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
427 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
428 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
429 |
< |
sync.acquire(1); |
430 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
428 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
429 |
> |
sync.acquire(); |
430 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
431 |
|
t1.start(); |
432 |
< |
Thread.sleep(SHORT_DELAY_MS); |
432 |
> |
waitForQueuedThread(sync, t1); |
433 |
> |
assertHasExclusiveQueuedThreads(sync, t1); |
434 |
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
435 |
+ |
assertFalse(sync.getExclusiveQueuedThreads().contains(t2)); |
436 |
|
t2.start(); |
437 |
< |
Thread.sleep(SHORT_DELAY_MS); |
437 |
> |
waitForQueuedThread(sync, t2); |
438 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
439 |
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
440 |
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
441 |
|
t1.interrupt(); |
442 |
< |
Thread.sleep(SHORT_DELAY_MS); |
443 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
444 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
445 |
< |
sync.release(1); |
446 |
< |
Thread.sleep(SHORT_DELAY_MS); |
293 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
294 |
< |
t1.join(); |
295 |
< |
t2.join(); |
442 |
> |
awaitTermination(t1); |
443 |
> |
assertHasExclusiveQueuedThreads(sync, t2); |
444 |
> |
sync.release(); |
445 |
> |
awaitTermination(t2); |
446 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
451 |
|
*/ |
452 |
< |
public void testGetSharedQueuedThreads() throws InterruptedException { |
452 |
> |
public void testGetSharedQueuedThreads_Exclusive() { |
453 |
|
final Mutex sync = new Mutex(); |
303 |
– |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
304 |
– |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
454 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
455 |
< |
sync.acquire(1); |
455 |
> |
sync.acquire(); |
456 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
457 |
< |
t1.start(); |
458 |
< |
Thread.sleep(SHORT_DELAY_MS); |
457 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
458 |
> |
waitForQueuedThread(sync, t1); |
459 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
460 |
< |
t2.start(); |
461 |
< |
Thread.sleep(SHORT_DELAY_MS); |
460 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
461 |
> |
waitForQueuedThread(sync, t2); |
462 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
463 |
|
t1.interrupt(); |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
464 |
> |
awaitTermination(t1); |
465 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
466 |
< |
sync.release(1); |
467 |
< |
Thread.sleep(SHORT_DELAY_MS); |
466 |
> |
sync.release(); |
467 |
> |
awaitTermination(t2); |
468 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
320 |
– |
t1.join(); |
321 |
– |
t2.join(); |
469 |
|
} |
470 |
|
|
471 |
|
/** |
472 |
< |
* tryAcquireNanos is interruptible. |
472 |
> |
* getSharedQueuedThreads returns all shared waiting threads |
473 |
> |
*/ |
474 |
> |
public void testGetSharedQueuedThreads_Shared() { |
475 |
> |
final BooleanLatch l = new BooleanLatch(); |
476 |
> |
assertHasSharedQueuedThreads(l, NO_THREADS); |
477 |
> |
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
478 |
> |
public void realRun() throws InterruptedException { |
479 |
> |
l.acquireSharedInterruptibly(0); |
480 |
> |
}}); |
481 |
> |
waitForQueuedThread(l, t1); |
482 |
> |
assertHasSharedQueuedThreads(l, t1); |
483 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
484 |
> |
public void realRun() throws InterruptedException { |
485 |
> |
l.acquireSharedInterruptibly(0); |
486 |
> |
}}); |
487 |
> |
waitForQueuedThread(l, t2); |
488 |
> |
assertHasSharedQueuedThreads(l, t1, t2); |
489 |
> |
t1.interrupt(); |
490 |
> |
awaitTermination(t1); |
491 |
> |
assertHasSharedQueuedThreads(l, t2); |
492 |
> |
assertTrue(l.releaseShared(0)); |
493 |
> |
awaitTermination(t2); |
494 |
> |
assertHasSharedQueuedThreads(l, NO_THREADS); |
495 |
> |
} |
496 |
> |
|
497 |
> |
/** |
498 |
> |
* tryAcquireNanos is interruptible |
499 |
|
*/ |
500 |
< |
public void testInterruptedException2() throws InterruptedException { |
500 |
> |
public void testTryAcquireNanos_Interruptible() { |
501 |
|
final Mutex sync = new Mutex(); |
502 |
< |
sync.acquire(1); |
503 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
502 |
> |
sync.acquire(); |
503 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
504 |
|
public void realRun() throws InterruptedException { |
505 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
505 |
> |
sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
506 |
|
}}); |
507 |
|
|
508 |
< |
t.start(); |
508 |
> |
waitForQueuedThread(sync, t); |
509 |
|
t.interrupt(); |
510 |
< |
t.join(); |
510 |
> |
awaitTermination(t); |
511 |
|
} |
512 |
|
|
340 |
– |
|
513 |
|
/** |
514 |
< |
* TryAcquire on exclusively held sync fails |
514 |
> |
* tryAcquire on exclusively held sync fails |
515 |
|
*/ |
516 |
< |
public void testTryAcquireWhenSynced() throws InterruptedException { |
516 |
> |
public void testTryAcquireWhenSynced() { |
517 |
|
final Mutex sync = new Mutex(); |
518 |
< |
sync.acquire(1); |
519 |
< |
Thread t = new Thread(new CheckedRunnable() { |
518 |
> |
sync.acquire(); |
519 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
520 |
|
public void realRun() { |
521 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
521 |
> |
assertFalse(sync.tryAcquire()); |
522 |
|
}}); |
523 |
|
|
524 |
< |
t.start(); |
525 |
< |
t.join(); |
354 |
< |
sync.release(1); |
524 |
> |
awaitTermination(t); |
525 |
> |
sync.release(); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* tryAcquireNanos on an exclusively held sync times out |
530 |
|
*/ |
531 |
< |
public void testAcquireNanos_Timeout() throws InterruptedException { |
531 |
> |
public void testAcquireNanos_Timeout() { |
532 |
|
final Mutex sync = new Mutex(); |
533 |
< |
sync.acquire(1); |
534 |
< |
Thread t = new Thread(new CheckedRunnable() { |
533 |
> |
sync.acquire(); |
534 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
535 |
|
public void realRun() throws InterruptedException { |
536 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
536 |
> |
long startTime = System.nanoTime(); |
537 |
> |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
538 |
> |
assertFalse(sync.tryAcquireNanos(nanos)); |
539 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
540 |
|
}}); |
541 |
|
|
542 |
< |
t.start(); |
543 |
< |
t.join(); |
370 |
< |
sync.release(1); |
542 |
> |
awaitTermination(t); |
543 |
> |
sync.release(); |
544 |
|
} |
545 |
|
|
373 |
– |
|
546 |
|
/** |
547 |
|
* getState is true when acquired and false when not |
548 |
|
*/ |
549 |
< |
public void testGetState() throws InterruptedException { |
549 |
> |
public void testGetState() { |
550 |
|
final Mutex sync = new Mutex(); |
551 |
< |
sync.acquire(1); |
551 |
> |
sync.acquire(); |
552 |
|
assertTrue(sync.isHeldExclusively()); |
553 |
< |
sync.release(1); |
553 |
> |
sync.release(); |
554 |
|
assertFalse(sync.isHeldExclusively()); |
555 |
< |
Thread t = new Thread(new CheckedRunnable() { |
555 |
> |
|
556 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
557 |
> |
final BooleanLatch done = new BooleanLatch(); |
558 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
559 |
|
public void realRun() throws InterruptedException { |
560 |
< |
sync.acquire(1); |
561 |
< |
Thread.sleep(SMALL_DELAY_MS); |
562 |
< |
sync.release(1); |
560 |
> |
sync.acquire(); |
561 |
> |
assertTrue(acquired.releaseShared(0)); |
562 |
> |
done.acquireShared(0); |
563 |
> |
sync.release(); |
564 |
|
}}); |
565 |
|
|
566 |
< |
t.start(); |
391 |
< |
Thread.sleep(SHORT_DELAY_MS); |
566 |
> |
acquired.acquireShared(0); |
567 |
|
assertTrue(sync.isHeldExclusively()); |
568 |
< |
t.join(); |
568 |
> |
assertTrue(done.releaseShared(0)); |
569 |
> |
awaitTermination(t); |
570 |
|
assertFalse(sync.isHeldExclusively()); |
571 |
|
} |
572 |
|
|
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 |
– |
|
573 |
|
/** |
574 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
575 |
|
*/ |
576 |
< |
public void testAcquireInterruptibly2() throws InterruptedException { |
576 |
> |
public void testAcquireInterruptibly() throws InterruptedException { |
577 |
|
final Mutex sync = new Mutex(); |
578 |
< |
sync.acquireInterruptibly(1); |
579 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
580 |
< |
t.start(); |
578 |
> |
final BooleanLatch threadStarted = new BooleanLatch(); |
579 |
> |
sync.acquireInterruptibly(); |
580 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
581 |
> |
public void realRun() throws InterruptedException { |
582 |
> |
assertTrue(threadStarted.releaseShared(0)); |
583 |
> |
sync.acquireInterruptibly(); |
584 |
> |
}}); |
585 |
> |
|
586 |
> |
threadStarted.acquireShared(0); |
587 |
> |
waitForQueuedThread(sync, t); |
588 |
|
t.interrupt(); |
589 |
+ |
awaitTermination(t); |
590 |
|
assertTrue(sync.isHeldExclusively()); |
423 |
– |
t.join(); |
591 |
|
} |
592 |
|
|
593 |
|
/** |
595 |
|
*/ |
596 |
|
public void testOwns() { |
597 |
|
final Mutex sync = new Mutex(); |
598 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
598 |
> |
final ConditionObject c = sync.newCondition(); |
599 |
|
final Mutex sync2 = new Mutex(); |
600 |
|
assertTrue(sync.owns(c)); |
601 |
|
assertFalse(sync2.owns(c)); |
604 |
|
/** |
605 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
606 |
|
*/ |
607 |
< |
public void testAwait_IllegalMonitor() throws InterruptedException { |
607 |
> |
public void testAwait_IMSE() { |
608 |
|
final Mutex sync = new Mutex(); |
609 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
610 |
< |
try { |
611 |
< |
c.await(); |
612 |
< |
shouldThrow(); |
613 |
< |
} catch (IllegalMonitorStateException success) {} |
609 |
> |
final ConditionObject c = sync.newCondition(); |
610 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
611 |
> |
long startTime = System.nanoTime(); |
612 |
> |
try { |
613 |
> |
await(c, awaitMethod); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (IllegalMonitorStateException success) { |
616 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
617 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
618 |
> |
} |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
623 |
|
*/ |
624 |
< |
public void testSignal_IllegalMonitor() throws InterruptedException { |
624 |
> |
public void testSignal_IMSE() { |
625 |
|
final Mutex sync = new Mutex(); |
626 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
626 |
> |
final ConditionObject c = sync.newCondition(); |
627 |
|
try { |
628 |
|
c.signal(); |
629 |
|
shouldThrow(); |
630 |
|
} catch (IllegalMonitorStateException success) {} |
631 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* 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 |
635 |
> |
* Calling signalAll without holding sync throws IllegalMonitorStateException |
636 |
|
*/ |
637 |
< |
public void testAwait_Timeout() throws InterruptedException { |
637 |
> |
public void testSignalAll_IMSE() { |
638 |
|
final Mutex sync = new Mutex(); |
639 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
640 |
< |
sync.acquire(1); |
641 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
642 |
< |
sync.release(1); |
639 |
> |
final ConditionObject c = sync.newCondition(); |
640 |
> |
try { |
641 |
> |
c.signalAll(); |
642 |
> |
shouldThrow(); |
643 |
> |
} catch (IllegalMonitorStateException success) {} |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* awaitUntil without a signal times out |
647 |
> |
* await/awaitNanos/awaitUntil without a signal times out |
648 |
|
*/ |
649 |
< |
public void testAwaitUntil_Timeout() throws InterruptedException { |
649 |
> |
public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); } |
650 |
> |
public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); } |
651 |
> |
public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); } |
652 |
> |
public void testAwait_Timeout(AwaitMethod awaitMethod) { |
653 |
|
final Mutex sync = new Mutex(); |
654 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
655 |
< |
sync.acquire(1); |
656 |
< |
java.util.Date d = new java.util.Date(); |
657 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
493 |
< |
sync.release(1); |
654 |
> |
final ConditionObject c = sync.newCondition(); |
655 |
> |
sync.acquire(); |
656 |
> |
assertAwaitTimesOut(c, awaitMethod); |
657 |
> |
sync.release(); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* await returns when signalled |
661 |
> |
* await/awaitNanos/awaitUntil returns when signalled |
662 |
|
*/ |
663 |
< |
public void testAwait() throws InterruptedException { |
663 |
> |
public void testSignal_await() { testSignal(AwaitMethod.await); } |
664 |
> |
public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); } |
665 |
> |
public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); } |
666 |
> |
public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); } |
667 |
> |
public void testSignal(final AwaitMethod awaitMethod) { |
668 |
|
final Mutex sync = new Mutex(); |
669 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
670 |
< |
Thread t = new Thread(new CheckedRunnable() { |
669 |
> |
final ConditionObject c = sync.newCondition(); |
670 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
671 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
672 |
|
public void realRun() throws InterruptedException { |
673 |
< |
sync.acquire(1); |
674 |
< |
c.await(); |
675 |
< |
sync.release(1); |
673 |
> |
sync.acquire(); |
674 |
> |
assertTrue(acquired.releaseShared(0)); |
675 |
> |
await(c, awaitMethod); |
676 |
> |
sync.release(); |
677 |
|
}}); |
678 |
|
|
679 |
< |
t.start(); |
680 |
< |
Thread.sleep(SHORT_DELAY_MS); |
681 |
< |
sync.acquire(1); |
679 |
> |
acquired.acquireShared(0); |
680 |
> |
sync.acquire(); |
681 |
> |
assertHasWaitersLocked(sync, c, t); |
682 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
683 |
|
c.signal(); |
684 |
< |
sync.release(1); |
685 |
< |
t.join(SHORT_DELAY_MS); |
686 |
< |
assertFalse(t.isAlive()); |
684 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
685 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
686 |
> |
sync.release(); |
687 |
> |
awaitTermination(t); |
688 |
|
} |
689 |
|
|
518 |
– |
|
519 |
– |
|
690 |
|
/** |
691 |
< |
* hasWaiters throws NPE if null |
691 |
> |
* hasWaiters(null) throws NullPointerException |
692 |
|
*/ |
693 |
|
public void testHasWaitersNPE() { |
694 |
|
final Mutex sync = new Mutex(); |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
< |
* getWaitQueueLength throws NPE if null |
702 |
> |
* getWaitQueueLength(null) throws NullPointerException |
703 |
|
*/ |
704 |
|
public void testGetWaitQueueLengthNPE() { |
705 |
|
final Mutex sync = new Mutex(); |
709 |
|
} catch (NullPointerException success) {} |
710 |
|
} |
711 |
|
|
542 |
– |
|
712 |
|
/** |
713 |
|
* getWaitingThreads throws NPE if null |
714 |
|
*/ |
720 |
|
} catch (NullPointerException success) {} |
721 |
|
} |
722 |
|
|
554 |
– |
|
723 |
|
/** |
724 |
< |
* hasWaiters throws IAE if not owned |
724 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
725 |
|
*/ |
726 |
|
public void testHasWaitersIAE() { |
727 |
|
final Mutex sync = new Mutex(); |
728 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
728 |
> |
final ConditionObject c = sync.newCondition(); |
729 |
|
final Mutex sync2 = new Mutex(); |
730 |
|
try { |
731 |
|
sync2.hasWaiters(c); |
732 |
|
shouldThrow(); |
733 |
|
} catch (IllegalArgumentException success) {} |
734 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
738 |
< |
* hasWaiters throws IMSE if not synced |
738 |
> |
* hasWaiters throws IllegalMonitorStateException if not synced |
739 |
|
*/ |
740 |
|
public void testHasWaitersIMSE() { |
741 |
|
final Mutex sync = new Mutex(); |
742 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
742 |
> |
final ConditionObject c = sync.newCondition(); |
743 |
|
try { |
744 |
|
sync.hasWaiters(c); |
745 |
|
shouldThrow(); |
746 |
|
} catch (IllegalMonitorStateException success) {} |
747 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
748 |
|
} |
749 |
|
|
580 |
– |
|
750 |
|
/** |
751 |
< |
* getWaitQueueLength throws IAE if not owned |
751 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
752 |
|
*/ |
753 |
|
public void testGetWaitQueueLengthIAE() { |
754 |
|
final Mutex sync = new Mutex(); |
755 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
755 |
> |
final ConditionObject c = sync.newCondition(); |
756 |
|
final Mutex sync2 = new Mutex(); |
757 |
|
try { |
758 |
|
sync2.getWaitQueueLength(c); |
759 |
|
shouldThrow(); |
760 |
|
} catch (IllegalArgumentException success) {} |
761 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
< |
* getWaitQueueLength throws IMSE if not synced |
765 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not synced |
766 |
|
*/ |
767 |
|
public void testGetWaitQueueLengthIMSE() { |
768 |
|
final Mutex sync = new Mutex(); |
769 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
> |
final ConditionObject c = sync.newCondition(); |
770 |
|
try { |
771 |
|
sync.getWaitQueueLength(c); |
772 |
|
shouldThrow(); |
773 |
|
} catch (IllegalMonitorStateException success) {} |
774 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
775 |
|
} |
776 |
|
|
606 |
– |
|
777 |
|
/** |
778 |
< |
* getWaitingThreads throws IAE if not owned |
778 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
779 |
|
*/ |
780 |
|
public void testGetWaitingThreadsIAE() { |
781 |
|
final Mutex sync = new Mutex(); |
782 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
782 |
> |
final ConditionObject c = sync.newCondition(); |
783 |
|
final Mutex sync2 = new Mutex(); |
784 |
|
try { |
785 |
|
sync2.getWaitingThreads(c); |
786 |
|
shouldThrow(); |
787 |
|
} catch (IllegalArgumentException success) {} |
788 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
789 |
|
} |
790 |
|
|
791 |
|
/** |
792 |
< |
* getWaitingThreads throws IMSE if not synced |
792 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not synced |
793 |
|
*/ |
794 |
|
public void testGetWaitingThreadsIMSE() { |
795 |
|
final Mutex sync = new Mutex(); |
796 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
796 |
> |
final ConditionObject c = sync.newCondition(); |
797 |
|
try { |
798 |
|
sync.getWaitingThreads(c); |
799 |
|
shouldThrow(); |
800 |
|
} catch (IllegalMonitorStateException success) {} |
801 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
802 |
|
} |
803 |
|
|
632 |
– |
|
633 |
– |
|
804 |
|
/** |
805 |
|
* hasWaiters returns true when a thread is waiting, else false |
806 |
|
*/ |
807 |
< |
public void testHasWaiters() throws InterruptedException { |
807 |
> |
public void testHasWaiters() { |
808 |
|
final Mutex sync = new Mutex(); |
809 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
810 |
< |
Thread t = new Thread(new CheckedRunnable() { |
809 |
> |
final ConditionObject c = sync.newCondition(); |
810 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
811 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
812 |
|
public void realRun() throws InterruptedException { |
813 |
< |
sync.acquire(1); |
814 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
815 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
813 |
> |
sync.acquire(); |
814 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
815 |
> |
assertFalse(sync.hasWaiters(c)); |
816 |
> |
assertTrue(acquired.releaseShared(0)); |
817 |
|
c.await(); |
818 |
< |
sync.release(1); |
818 |
> |
sync.release(); |
819 |
|
}}); |
820 |
|
|
821 |
< |
t.start(); |
822 |
< |
Thread.sleep(SHORT_DELAY_MS); |
823 |
< |
sync.acquire(1); |
821 |
> |
acquired.acquireShared(0); |
822 |
> |
sync.acquire(); |
823 |
> |
assertHasWaitersLocked(sync, c, t); |
824 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
825 |
|
assertTrue(sync.hasWaiters(c)); |
653 |
– |
assertEquals(1, sync.getWaitQueueLength(c)); |
826 |
|
c.signal(); |
827 |
< |
sync.release(1); |
828 |
< |
Thread.sleep(SHORT_DELAY_MS); |
657 |
< |
sync.acquire(1); |
827 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
828 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
829 |
|
assertFalse(sync.hasWaiters(c)); |
830 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
831 |
< |
sync.release(1); |
832 |
< |
t.join(SHORT_DELAY_MS); |
833 |
< |
assertFalse(t.isAlive()); |
830 |
> |
sync.release(); |
831 |
> |
|
832 |
> |
awaitTermination(t); |
833 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
837 |
|
* getWaitQueueLength returns number of waiting threads |
838 |
|
*/ |
839 |
< |
public void testGetWaitQueueLength() throws InterruptedException { |
839 |
> |
public void testGetWaitQueueLength() { |
840 |
|
final Mutex sync = new Mutex(); |
841 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
842 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
841 |
> |
final ConditionObject c = sync.newCondition(); |
842 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
843 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
844 |
> |
final Thread t1 = newStartedThread(new CheckedRunnable() { |
845 |
|
public void realRun() throws InterruptedException { |
846 |
< |
sync.acquire(1); |
847 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
848 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
846 |
> |
sync.acquire(); |
847 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
848 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
849 |
> |
assertTrue(acquired1.releaseShared(0)); |
850 |
|
c.await(); |
851 |
< |
sync.release(1); |
851 |
> |
sync.release(); |
852 |
|
}}); |
853 |
+ |
acquired1.acquireShared(0); |
854 |
+ |
sync.acquire(); |
855 |
+ |
assertHasWaitersLocked(sync, c, t1); |
856 |
+ |
assertEquals(1, sync.getWaitQueueLength(c)); |
857 |
+ |
sync.release(); |
858 |
|
|
859 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
859 |
> |
final Thread t2 = newStartedThread(new CheckedRunnable() { |
860 |
|
public void realRun() throws InterruptedException { |
861 |
< |
sync.acquire(1); |
862 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
863 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
861 |
> |
sync.acquire(); |
862 |
> |
assertHasWaitersLocked(sync, c, t1); |
863 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
864 |
> |
assertTrue(acquired2.releaseShared(0)); |
865 |
|
c.await(); |
866 |
< |
sync.release(1); |
866 |
> |
sync.release(); |
867 |
|
}}); |
868 |
< |
|
869 |
< |
t1.start(); |
870 |
< |
Thread.sleep(SHORT_DELAY_MS); |
871 |
< |
t2.start(); |
692 |
< |
Thread.sleep(SHORT_DELAY_MS); |
693 |
< |
sync.acquire(1); |
694 |
< |
assertTrue(sync.hasWaiters(c)); |
868 |
> |
acquired2.acquireShared(0); |
869 |
> |
sync.acquire(); |
870 |
> |
assertHasWaitersLocked(sync, c, t1, t2); |
871 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
872 |
|
assertEquals(2, sync.getWaitQueueLength(c)); |
873 |
|
c.signalAll(); |
874 |
< |
sync.release(1); |
875 |
< |
Thread.sleep(SHORT_DELAY_MS); |
699 |
< |
sync.acquire(1); |
700 |
< |
assertFalse(sync.hasWaiters(c)); |
874 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
875 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
876 |
|
assertEquals(0, sync.getWaitQueueLength(c)); |
877 |
< |
sync.release(1); |
878 |
< |
t1.join(SHORT_DELAY_MS); |
879 |
< |
t2.join(SHORT_DELAY_MS); |
880 |
< |
assertFalse(t1.isAlive()); |
881 |
< |
assertFalse(t2.isAlive()); |
877 |
> |
sync.release(); |
878 |
> |
|
879 |
> |
awaitTermination(t1); |
880 |
> |
awaitTermination(t2); |
881 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
882 |
|
} |
883 |
|
|
884 |
|
/** |
885 |
|
* getWaitingThreads returns only and all waiting threads |
886 |
|
*/ |
887 |
< |
public void testGetWaitingThreads() throws InterruptedException { |
887 |
> |
public void testGetWaitingThreads() { |
888 |
|
final Mutex sync = new Mutex(); |
889 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
890 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
889 |
> |
final ConditionObject c = sync.newCondition(); |
890 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
891 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
892 |
> |
final Thread t1 = new Thread(new CheckedRunnable() { |
893 |
|
public void realRun() throws InterruptedException { |
894 |
< |
sync.acquire(1); |
895 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
894 |
> |
sync.acquire(); |
895 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
896 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
897 |
> |
assertTrue(acquired1.releaseShared(0)); |
898 |
|
c.await(); |
899 |
< |
sync.release(1); |
899 |
> |
sync.release(); |
900 |
|
}}); |
901 |
|
|
902 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
902 |
> |
final Thread t2 = new Thread(new CheckedRunnable() { |
903 |
|
public void realRun() throws InterruptedException { |
904 |
< |
sync.acquire(1); |
905 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
904 |
> |
sync.acquire(); |
905 |
> |
assertHasWaitersLocked(sync, c, t1); |
906 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
907 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
908 |
> |
assertEquals(1, sync.getWaitingThreads(c).size()); |
909 |
> |
assertTrue(acquired2.releaseShared(0)); |
910 |
|
c.await(); |
911 |
< |
sync.release(1); |
911 |
> |
sync.release(); |
912 |
|
}}); |
913 |
|
|
914 |
< |
sync.acquire(1); |
915 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
916 |
< |
sync.release(1); |
917 |
< |
t1.start(); |
918 |
< |
Thread.sleep(SHORT_DELAY_MS); |
919 |
< |
t2.start(); |
920 |
< |
Thread.sleep(SHORT_DELAY_MS); |
921 |
< |
sync.acquire(1); |
922 |
< |
assertTrue(sync.hasWaiters(c)); |
923 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
924 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
925 |
< |
c.signalAll(); |
926 |
< |
sync.release(1); |
927 |
< |
Thread.sleep(SHORT_DELAY_MS); |
928 |
< |
sync.acquire(1); |
929 |
< |
assertFalse(sync.hasWaiters(c)); |
930 |
< |
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 |
< |
} |
914 |
> |
sync.acquire(); |
915 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
916 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
917 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
918 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
919 |
> |
assertEquals(0, sync.getWaitingThreads(c).size()); |
920 |
> |
sync.release(); |
921 |
> |
|
922 |
> |
t1.start(); |
923 |
> |
acquired1.acquireShared(0); |
924 |
> |
sync.acquire(); |
925 |
> |
assertHasWaitersLocked(sync, c, t1); |
926 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
927 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
928 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
929 |
> |
assertEquals(1, sync.getWaitingThreads(c).size()); |
930 |
> |
sync.release(); |
931 |
|
|
932 |
+ |
t2.start(); |
933 |
+ |
acquired2.acquireShared(0); |
934 |
+ |
sync.acquire(); |
935 |
+ |
assertHasWaitersLocked(sync, c, t1, t2); |
936 |
+ |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
937 |
+ |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
938 |
+ |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
939 |
+ |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
940 |
+ |
assertEquals(2, sync.getWaitingThreads(c).size()); |
941 |
+ |
c.signalAll(); |
942 |
+ |
assertHasWaitersLocked(sync, c, NO_THREADS); |
943 |
+ |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
944 |
+ |
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
945 |
+ |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
946 |
+ |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
947 |
+ |
assertEquals(0, sync.getWaitingThreads(c).size()); |
948 |
+ |
sync.release(); |
949 |
|
|
950 |
+ |
awaitTermination(t1); |
951 |
+ |
awaitTermination(t2); |
952 |
+ |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
953 |
+ |
} |
954 |
|
|
955 |
|
/** |
956 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
956 |
> |
* awaitUninterruptibly is uninterruptible |
957 |
|
*/ |
958 |
< |
public void testAwaitUninterruptibly() throws InterruptedException { |
958 |
> |
public void testAwaitUninterruptibly() { |
959 |
|
final Mutex sync = new Mutex(); |
960 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
961 |
< |
Thread t = new Thread(new CheckedRunnable() { |
960 |
> |
final ConditionObject c = sync.newCondition(); |
961 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
962 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
963 |
|
public void realRun() { |
964 |
< |
sync.acquire(1); |
964 |
> |
sync.acquire(); |
965 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
966 |
|
c.awaitUninterruptibly(); |
967 |
< |
sync.release(1); |
967 |
> |
assertTrue(Thread.interrupted()); |
968 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
969 |
> |
sync.release(); |
970 |
|
}}); |
971 |
|
|
972 |
< |
t.start(); |
973 |
< |
Thread.sleep(SHORT_DELAY_MS); |
972 |
> |
pleaseInterrupt.acquireShared(0); |
973 |
> |
sync.acquire(); |
974 |
> |
assertHasWaitersLocked(sync, c, t); |
975 |
> |
sync.release(); |
976 |
|
t.interrupt(); |
977 |
< |
sync.acquire(1); |
977 |
> |
assertHasWaitersUnlocked(sync, c, t); |
978 |
> |
assertThreadStaysAlive(t); |
979 |
> |
sync.acquire(); |
980 |
> |
assertHasWaitersLocked(sync, c, t); |
981 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
982 |
|
c.signal(); |
983 |
< |
sync.release(1); |
984 |
< |
t.join(SHORT_DELAY_MS); |
985 |
< |
assertFalse(t.isAlive()); |
986 |
< |
} |
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()); |
983 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
984 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
985 |
> |
sync.release(); |
986 |
> |
awaitTermination(t); |
987 |
|
} |
988 |
|
|
989 |
|
/** |
990 |
< |
* awaitNanos is interruptible |
990 |
> |
* await/awaitNanos/awaitUntil is interruptible |
991 |
|
*/ |
992 |
< |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
992 |
> |
public void testInterruptible_await() { testInterruptible(AwaitMethod.await); } |
993 |
> |
public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); } |
994 |
> |
public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); } |
995 |
> |
public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); } |
996 |
> |
public void testInterruptible(final AwaitMethod awaitMethod) { |
997 |
|
final Mutex sync = new Mutex(); |
998 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
999 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
998 |
> |
final ConditionObject c = sync.newCondition(); |
999 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
1000 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1001 |
|
public void realRun() throws InterruptedException { |
1002 |
< |
sync.acquire(1); |
1003 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1004 |
< |
sync.release(1); |
1002 |
> |
sync.acquire(); |
1003 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1004 |
> |
await(c, awaitMethod); |
1005 |
|
}}); |
1006 |
|
|
1007 |
< |
t.start(); |
814 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1007 |
> |
pleaseInterrupt.acquireShared(0); |
1008 |
|
t.interrupt(); |
1009 |
< |
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()); |
1009 |
> |
awaitTermination(t); |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1013 |
|
* signalAll wakes up all threads |
1014 |
|
*/ |
1015 |
< |
public void testSignalAll() throws InterruptedException { |
1016 |
< |
final Mutex sync = new Mutex(); |
1017 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1018 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
1015 |
> |
public void testSignalAll_await() { testSignalAll(AwaitMethod.await); } |
1016 |
> |
public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); } |
1017 |
> |
public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); } |
1018 |
> |
public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); } |
1019 |
> |
public void testSignalAll(final AwaitMethod awaitMethod) { |
1020 |
> |
final Mutex sync = new Mutex(); |
1021 |
> |
final ConditionObject c = sync.newCondition(); |
1022 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
1023 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
1024 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1025 |
|
public void realRun() throws InterruptedException { |
1026 |
< |
sync.acquire(1); |
1027 |
< |
c.await(); |
1028 |
< |
sync.release(1); |
1026 |
> |
sync.acquire(); |
1027 |
> |
acquired1.releaseShared(0); |
1028 |
> |
await(c, awaitMethod); |
1029 |
> |
sync.release(); |
1030 |
|
}}); |
1031 |
|
|
1032 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
1032 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1033 |
|
public void realRun() throws InterruptedException { |
1034 |
< |
sync.acquire(1); |
1035 |
< |
c.await(); |
1036 |
< |
sync.release(1); |
1034 |
> |
sync.acquire(); |
1035 |
> |
acquired2.releaseShared(0); |
1036 |
> |
await(c, awaitMethod); |
1037 |
> |
sync.release(); |
1038 |
|
}}); |
1039 |
|
|
1040 |
< |
t1.start(); |
1041 |
< |
t2.start(); |
1042 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1043 |
< |
sync.acquire(1); |
1040 |
> |
acquired1.acquireShared(0); |
1041 |
> |
acquired2.acquireShared(0); |
1042 |
> |
sync.acquire(); |
1043 |
> |
assertHasWaitersLocked(sync, c, t1, t2); |
1044 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1045 |
|
c.signalAll(); |
1046 |
< |
sync.release(1); |
1047 |
< |
t1.join(SHORT_DELAY_MS); |
1048 |
< |
t2.join(SHORT_DELAY_MS); |
1049 |
< |
assertFalse(t1.isAlive()); |
1050 |
< |
assertFalse(t2.isAlive()); |
1046 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
1047 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
1048 |
> |
sync.release(); |
1049 |
> |
awaitTermination(t1); |
1050 |
> |
awaitTermination(t2); |
1051 |
|
} |
1052 |
|
|
873 |
– |
|
1053 |
|
/** |
1054 |
|
* toString indicates current state |
1055 |
|
*/ |
1056 |
|
public void testToString() { |
1057 |
|
Mutex sync = new Mutex(); |
1058 |
< |
String us = sync.toString(); |
1059 |
< |
assertTrue(us.indexOf("State = 0") >= 0); |
1060 |
< |
sync.acquire(1); |
882 |
< |
String ls = sync.toString(); |
883 |
< |
assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0); |
1058 |
> |
assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED)); |
1059 |
> |
sync.acquire(); |
1060 |
> |
assertTrue(sync.toString().contains("State = " + Mutex.LOCKED)); |
1061 |
|
} |
1062 |
|
|
1063 |
|
/** |
1064 |
< |
* A serialized AQS deserializes with current state |
1065 |
< |
*/ |
1066 |
< |
public void testSerialization() throws Exception { |
1067 |
< |
Mutex l = new Mutex(); |
1068 |
< |
l.acquire(1); |
1069 |
< |
assertTrue(l.isHeldExclusively()); |
1070 |
< |
|
1071 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1072 |
< |
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 |
< |
} |
1064 |
> |
* A serialized AQS deserializes with current state, but no queued threads |
1065 |
> |
*/ |
1066 |
> |
public void testSerialization() { |
1067 |
> |
Mutex sync = new Mutex(); |
1068 |
> |
assertFalse(serialClone(sync).isHeldExclusively()); |
1069 |
> |
sync.acquire(); |
1070 |
> |
Thread t = newStartedThread(new InterruptedSyncRunnable(sync)); |
1071 |
> |
waitForQueuedThread(sync, t); |
1072 |
> |
assertTrue(sync.isHeldExclusively()); |
1073 |
|
|
1074 |
+ |
Mutex clone = serialClone(sync); |
1075 |
+ |
assertTrue(clone.isHeldExclusively()); |
1076 |
+ |
assertHasExclusiveQueuedThreads(sync, t); |
1077 |
+ |
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1078 |
+ |
t.interrupt(); |
1079 |
+ |
awaitTermination(t); |
1080 |
+ |
sync.release(); |
1081 |
+ |
assertFalse(sync.isHeldExclusively()); |
1082 |
+ |
assertTrue(clone.isHeldExclusively()); |
1083 |
+ |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1084 |
+ |
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1085 |
+ |
} |
1086 |
|
|
1087 |
|
/** |
1088 |
|
* tryReleaseShared setting state changes getState |
1090 |
|
public void testGetStateWithReleaseShared() { |
1091 |
|
final BooleanLatch l = new BooleanLatch(); |
1092 |
|
assertFalse(l.isSignalled()); |
1093 |
< |
l.releaseShared(0); |
1093 |
> |
assertTrue(l.releaseShared(0)); |
1094 |
|
assertTrue(l.isSignalled()); |
1095 |
|
} |
1096 |
|
|
1100 |
|
public void testReleaseShared() { |
1101 |
|
final BooleanLatch l = new BooleanLatch(); |
1102 |
|
assertFalse(l.isSignalled()); |
1103 |
< |
l.releaseShared(0); |
1103 |
> |
assertTrue(l.releaseShared(0)); |
1104 |
|
assertTrue(l.isSignalled()); |
1105 |
< |
l.releaseShared(0); |
1105 |
> |
assertTrue(l.releaseShared(0)); |
1106 |
|
assertTrue(l.isSignalled()); |
1107 |
|
} |
1108 |
|
|
1109 |
|
/** |
1110 |
|
* acquireSharedInterruptibly returns after release, but not before |
1111 |
|
*/ |
1112 |
< |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
1112 |
> |
public void testAcquireSharedInterruptibly() { |
1113 |
|
final BooleanLatch l = new BooleanLatch(); |
1114 |
|
|
1115 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1115 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1116 |
|
public void realRun() throws InterruptedException { |
1117 |
< |
threadAssertFalse(l.isSignalled()); |
1117 |
> |
assertFalse(l.isSignalled()); |
1118 |
|
l.acquireSharedInterruptibly(0); |
1119 |
< |
threadAssertTrue(l.isSignalled()); |
1119 |
> |
assertTrue(l.isSignalled()); |
1120 |
> |
l.acquireSharedInterruptibly(0); |
1121 |
> |
assertTrue(l.isSignalled()); |
1122 |
|
}}); |
1123 |
|
|
1124 |
< |
t.start(); |
1124 |
> |
waitForQueuedThread(l, t); |
1125 |
|
assertFalse(l.isSignalled()); |
1126 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1127 |
< |
l.releaseShared(0); |
1126 |
> |
assertThreadStaysAlive(t); |
1127 |
> |
assertHasSharedQueuedThreads(l, t); |
1128 |
> |
assertTrue(l.releaseShared(0)); |
1129 |
|
assertTrue(l.isSignalled()); |
1130 |
< |
t.join(); |
1130 |
> |
awaitTermination(t); |
1131 |
|
} |
1132 |
|
|
949 |
– |
|
1133 |
|
/** |
1134 |
< |
* acquireSharedTimed returns after release |
1134 |
> |
* tryAcquireSharedNanos returns after release, but not before |
1135 |
|
*/ |
1136 |
< |
public void testAsquireSharedTimed() throws InterruptedException { |
1136 |
> |
public void testTryAcquireSharedNanos() { |
1137 |
|
final BooleanLatch l = new BooleanLatch(); |
1138 |
|
|
1139 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1139 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1140 |
|
public void realRun() throws InterruptedException { |
1141 |
< |
threadAssertFalse(l.isSignalled()); |
1142 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1143 |
< |
threadAssertTrue(l.isSignalled()); |
1141 |
> |
assertFalse(l.isSignalled()); |
1142 |
> |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1143 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1144 |
> |
assertTrue(l.isSignalled()); |
1145 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1146 |
> |
assertTrue(l.isSignalled()); |
1147 |
|
}}); |
1148 |
|
|
1149 |
< |
t.start(); |
1149 |
> |
waitForQueuedThread(l, t); |
1150 |
|
assertFalse(l.isSignalled()); |
1151 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1152 |
< |
l.releaseShared(0); |
1151 |
> |
assertThreadStaysAlive(t); |
1152 |
> |
assertTrue(l.releaseShared(0)); |
1153 |
|
assertTrue(l.isSignalled()); |
1154 |
< |
t.join(); |
1154 |
> |
awaitTermination(t); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
< |
* acquireSharedInterruptibly throws IE if interrupted before released |
1158 |
> |
* acquireSharedInterruptibly is interruptible |
1159 |
|
*/ |
1160 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
1160 |
> |
public void testAcquireSharedInterruptibly_Interruptible() { |
1161 |
|
final BooleanLatch l = new BooleanLatch(); |
1162 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
1162 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1163 |
|
public void realRun() throws InterruptedException { |
1164 |
< |
threadAssertFalse(l.isSignalled()); |
1164 |
> |
assertFalse(l.isSignalled()); |
1165 |
|
l.acquireSharedInterruptibly(0); |
1166 |
|
}}); |
1167 |
|
|
1168 |
< |
t.start(); |
1168 |
> |
waitForQueuedThread(l, t); |
1169 |
|
assertFalse(l.isSignalled()); |
1170 |
|
t.interrupt(); |
1171 |
< |
t.join(); |
1171 |
> |
awaitTermination(t); |
1172 |
> |
assertFalse(l.isSignalled()); |
1173 |
|
} |
1174 |
|
|
1175 |
|
/** |
1176 |
< |
* acquireSharedTimed throws IE if interrupted before released |
1176 |
> |
* tryAcquireSharedNanos is interruptible |
1177 |
|
*/ |
1178 |
< |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
1178 |
> |
public void testTryAcquireSharedNanos_Interruptible() { |
1179 |
|
final BooleanLatch l = new BooleanLatch(); |
1180 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
1180 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1181 |
|
public void realRun() throws InterruptedException { |
1182 |
< |
threadAssertFalse(l.isSignalled()); |
1183 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1182 |
> |
assertFalse(l.isSignalled()); |
1183 |
> |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1184 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1185 |
|
}}); |
1186 |
|
|
1187 |
< |
t.start(); |
1000 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1187 |
> |
waitForQueuedThread(l, t); |
1188 |
|
assertFalse(l.isSignalled()); |
1189 |
|
t.interrupt(); |
1190 |
< |
t.join(); |
1190 |
> |
awaitTermination(t); |
1191 |
> |
assertFalse(l.isSignalled()); |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1195 |
< |
* acquireSharedTimed times out if not released before timeout |
1195 |
> |
* tryAcquireSharedNanos times out if not released before timeout |
1196 |
|
*/ |
1197 |
< |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1197 |
> |
public void testTryAcquireSharedNanos_Timeout() { |
1198 |
|
final BooleanLatch l = new BooleanLatch(); |
1199 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1199 |
> |
final BooleanLatch observedQueued = new BooleanLatch(); |
1200 |
> |
final long timeoutMillis = timeoutMillis(); |
1201 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1202 |
|
public void realRun() throws InterruptedException { |
1203 |
< |
threadAssertFalse(l.isSignalled()); |
1204 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1203 |
> |
assertFalse(l.isSignalled()); |
1204 |
> |
for (long millis = timeoutMillis(); |
1205 |
> |
!observedQueued.isSignalled(); |
1206 |
> |
millis *= 2) { |
1207 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1208 |
> |
long startTime = System.nanoTime(); |
1209 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1210 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1211 |
> |
} |
1212 |
> |
assertFalse(l.isSignalled()); |
1213 |
|
}}); |
1214 |
|
|
1215 |
< |
t.start(); |
1216 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1215 |
> |
waitForQueuedThread(l, t); |
1216 |
> |
observedQueued.releaseShared(0); |
1217 |
> |
assertFalse(l.isSignalled()); |
1218 |
> |
awaitTermination(t); |
1219 |
|
assertFalse(l.isSignalled()); |
1220 |
< |
t.join(); |
1220 |
> |
} |
1221 |
> |
|
1222 |
> |
/** |
1223 |
> |
* awaitNanos/timed await with 0 wait times out immediately |
1224 |
> |
*/ |
1225 |
> |
public void testAwait_Zero() throws InterruptedException { |
1226 |
> |
final Mutex sync = new Mutex(); |
1227 |
> |
final ConditionObject c = sync.newCondition(); |
1228 |
> |
sync.acquire(); |
1229 |
> |
assertTrue(c.awaitNanos(0L) <= 0); |
1230 |
> |
assertFalse(c.await(0L, NANOSECONDS)); |
1231 |
> |
sync.release(); |
1232 |
> |
} |
1233 |
> |
|
1234 |
> |
/** |
1235 |
> |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1236 |
> |
*/ |
1237 |
> |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1238 |
> |
final Mutex sync = new Mutex(); |
1239 |
> |
final ConditionObject c = sync.newCondition(); |
1240 |
> |
sync.acquire(); |
1241 |
> |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1242 |
> |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1243 |
> |
sync.release(); |
1244 |
|
} |
1245 |
|
|
1246 |
|
} |