ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
Revision: 1.71
Committed: Thu Aug 15 16:01:30 2019 UTC (4 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.70: +25 -14 lines
Log Message:
testInterruptedFailingAcquire: test interruptible acquire methods

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