ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.65
Committed: Sun Jul 22 22:13:55 2018 UTC (5 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.64: +1 -1 lines
Log Message:
use standard variable name "success" in catch clauses

File Contents

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