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