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.23 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.24 by jsr166, Sat May 21 06:24:33 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines