[cvs] / jsr166 / src / test / tck / AbstractQueuedLongSynchronizerTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (view) (download)

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8