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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8