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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8