ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.52
Committed: Thu Aug 15 16:06:13 2019 UTC (4 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.51: +3 -1 lines
Log Message:
testInterruptedFailingAcquire: add hasContended assertions

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