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