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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8