ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java (file contents):
Revision 1.9 by jsr166, Wed Nov 18 08:22:57 2009 UTC vs.
Revision 1.39 by jsr166, Sun May 14 02:20:48 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines