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.4 by dl, Thu May 18 10:29:23 2006 UTC vs.
Revision 1.40 by jsr166, Sun May 14 02:24:10 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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;
37 <        public boolean isHeldExclusively() {
38 <            return getState() == LOCKED;
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 >            long state = getState();
42 >            assertTrue(state == UNLOCKED || state == LOCKED);
43 >            return state == LOCKED;
44          }
45 <        
45 >
46          public boolean tryAcquire(long acquires) {
47 <            return compareAndSetState(0, LOCKED);
47 >            assertEquals(LOCKED, acquires);
48 >            return compareAndSetState(UNLOCKED, LOCKED);
49          }
50 <        
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          }
48        
49        public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); }
56  
57 +        public boolean tryAcquireNanos(long nanos) throws InterruptedException {
58 +            return tryAcquireNanos(LOCKED, nanos);
59 +        }
60 +
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  
53    
86      /**
87       * A simple latch class, to test shared mode.
88       */
89 <    static class BooleanLatch extends AbstractQueuedLongSynchronizer {
89 >    static class BooleanLatch extends AbstractQueuedLongSynchronizer {
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 <        
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());
271 >    public void testIsHeldExclusively() {
272 >        Mutex sync = new Mutex();
273 >        assertFalse(sync.isHeldExclusively());
274      }
275 <    
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());
279 >    public void testAcquire() {
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
289 <     */
290 <    public void testTryAcquire() {
291 <        Mutex rl = new Mutex();
292 <        assertTrue(rl.tryAcquire(1));
293 <        assertTrue(rl.isHeldExclusively());
294 <        rl.release(1);
288 >     * tryAcquire on a released sync succeeds
289 >     */
290 >    public void testTryAcquire() {
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() {
302 <        final Mutex sync = new Mutex();
303 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
304 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
305 <        try {
306 <            assertFalse(sync.hasQueuedThreads());
307 <            sync.acquire(1);
308 <            t1.start();
309 <            Thread.sleep(SHORT_DELAY_MS);
310 <            assertTrue(sync.hasQueuedThreads());
311 <            t2.start();
312 <            Thread.sleep(SHORT_DELAY_MS);
313 <            assertTrue(sync.hasQueuedThreads());
314 <            t1.interrupt();
315 <            Thread.sleep(SHORT_DELAY_MS);
316 <            assertTrue(sync.hasQueuedThreads());
317 <            sync.release(1);
148 <            Thread.sleep(SHORT_DELAY_MS);
149 <            assertFalse(sync.hasQueuedThreads());
150 <            t1.join();
151 <            t2.join();
152 <        } catch(Exception e){
153 <            unexpectedException();
154 <        }
155 <    }
301 >    public void testHasQueuedThreads() {
302 >        final Mutex sync = new Mutex();
303 >        assertFalse(sync.hasQueuedThreads());
304 >        sync.acquire();
305 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
306 >        waitForQueuedThread(sync, t1);
307 >        assertTrue(sync.hasQueuedThreads());
308 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
309 >        waitForQueuedThread(sync, t2);
310 >        assertTrue(sync.hasQueuedThreads());
311 >        t1.interrupt();
312 >        awaitTermination(t1);
313 >        assertTrue(sync.hasQueuedThreads());
314 >        sync.release();
315 >        awaitTermination(t2);
316 >        assertFalse(sync.hasQueuedThreads());
317 >    }
318  
319      /**
320 <     * isQueued(null) throws NPE
320 >     * isQueued(null) throws NullPointerException
321       */
322 <    public void testIsQueuedNPE() {
323 <        final Mutex sync = new Mutex();
322 >    public void testIsQueuedNPE() {
323 >        final Mutex sync = new Mutex();
324          try {
325              sync.isQueued(null);
326              shouldThrow();
327 <        } catch (NullPointerException success) {
166 <        }
327 >        } catch (NullPointerException success) {}
328      }
329  
330      /**
331 <     * isQueued reports whether a thread is queued.
331 >     * isQueued reports whether a thread is queued
332       */
333 <    public void testIsQueued() {
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 <        try {
338 <            assertFalse(sync.isQueued(t1));
339 <            assertFalse(sync.isQueued(t2));
340 <            sync.acquire(1);
341 <            t1.start();
342 <            Thread.sleep(SHORT_DELAY_MS);
343 <            assertTrue(sync.isQueued(t1));
344 <            t2.start();
345 <            Thread.sleep(SHORT_DELAY_MS);
346 <            assertTrue(sync.isQueued(t1));
347 <            assertTrue(sync.isQueued(t2));
348 <            t1.interrupt();
349 <            Thread.sleep(SHORT_DELAY_MS);
350 <            assertFalse(sync.isQueued(t1));
351 <            assertTrue(sync.isQueued(t2));
352 <            sync.release(1);
353 <            Thread.sleep(SHORT_DELAY_MS);
354 <            assertFalse(sync.isQueued(t1));
355 <            Thread.sleep(SHORT_DELAY_MS);
356 <            assertFalse(sync.isQueued(t2));
196 <            t1.join();
197 <            t2.join();
198 <        } catch(Exception e){
199 <            unexpectedException();
200 <        }
201 <    }
337 >        assertFalse(sync.isQueued(t1));
338 >        assertFalse(sync.isQueued(t2));
339 >        sync.acquire();
340 >        t1.start();
341 >        waitForQueuedThread(sync, t1);
342 >        assertTrue(sync.isQueued(t1));
343 >        assertFalse(sync.isQueued(t2));
344 >        t2.start();
345 >        waitForQueuedThread(sync, t2);
346 >        assertTrue(sync.isQueued(t1));
347 >        assertTrue(sync.isQueued(t2));
348 >        t1.interrupt();
349 >        awaitTermination(t1);
350 >        assertFalse(sync.isQueued(t1));
351 >        assertTrue(sync.isQueued(t2));
352 >        sync.release();
353 >        awaitTermination(t2);
354 >        assertFalse(sync.isQueued(t1));
355 >        assertFalse(sync.isQueued(t2));
356 >    }
357  
358      /**
359       * getFirstQueuedThread returns first waiting thread or null if none
360       */
361 <    public void testGetFirstQueuedThread() {
362 <        final Mutex sync = new Mutex();
363 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
364 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
365 <        try {
366 <            assertNull(sync.getFirstQueuedThread());
367 <            sync.acquire(1);
368 <            t1.start();
369 <            Thread.sleep(SHORT_DELAY_MS);
370 <            assertEquals(t1, sync.getFirstQueuedThread());
371 <            t2.start();
372 <            Thread.sleep(SHORT_DELAY_MS);
373 <            assertEquals(t1, sync.getFirstQueuedThread());
374 <            t1.interrupt();
375 <            Thread.sleep(SHORT_DELAY_MS);
376 <            Thread.sleep(SHORT_DELAY_MS);
377 <            assertEquals(t2, sync.getFirstQueuedThread());
223 <            sync.release(1);
224 <            Thread.sleep(SHORT_DELAY_MS);
225 <            assertNull(sync.getFirstQueuedThread());
226 <            t1.join();
227 <            t2.join();
228 <        } catch(Exception e){
229 <            unexpectedException();
230 <        }
231 <    }
232 <
361 >    public void testGetFirstQueuedThread() {
362 >        final Mutex sync = new Mutex();
363 >        assertNull(sync.getFirstQueuedThread());
364 >        sync.acquire();
365 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
366 >        waitForQueuedThread(sync, t1);
367 >        assertEquals(t1, sync.getFirstQueuedThread());
368 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
369 >        waitForQueuedThread(sync, t2);
370 >        assertEquals(t1, sync.getFirstQueuedThread());
371 >        t1.interrupt();
372 >        awaitTermination(t1);
373 >        assertEquals(t2, sync.getFirstQueuedThread());
374 >        sync.release();
375 >        awaitTermination(t2);
376 >        assertNull(sync.getFirstQueuedThread());
377 >    }
378  
379      /**
380       * hasContended reports false if no thread has ever blocked, else true
381       */
382 <    public void testHasContended() {
383 <        final Mutex sync = new Mutex();
384 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
385 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
386 <        try {
387 <            assertFalse(sync.hasContended());
388 <            sync.acquire(1);
389 <            t1.start();
390 <            Thread.sleep(SHORT_DELAY_MS);
391 <            assertTrue(sync.hasContended());
392 <            t2.start();
393 <            Thread.sleep(SHORT_DELAY_MS);
394 <            assertTrue(sync.hasContended());
395 <            t1.interrupt();
396 <            Thread.sleep(SHORT_DELAY_MS);
397 <            assertTrue(sync.hasContended());
398 <            sync.release(1);
399 <            Thread.sleep(SHORT_DELAY_MS);
255 <            assertTrue(sync.hasContended());
256 <            t1.join();
257 <            t2.join();
258 <        } catch(Exception e){
259 <            unexpectedException();
260 <        }
261 <    }
382 >    public void testHasContended() {
383 >        final Mutex sync = new Mutex();
384 >        assertFalse(sync.hasContended());
385 >        sync.acquire();
386 >        assertFalse(sync.hasContended());
387 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
388 >        waitForQueuedThread(sync, t1);
389 >        assertTrue(sync.hasContended());
390 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
391 >        waitForQueuedThread(sync, t2);
392 >        assertTrue(sync.hasContended());
393 >        t1.interrupt();
394 >        awaitTermination(t1);
395 >        assertTrue(sync.hasContended());
396 >        sync.release();
397 >        awaitTermination(t2);
398 >        assertTrue(sync.hasContended());
399 >    }
400  
401      /**
402 <     * getQueuedThreads includes waiting threads
402 >     * getQueuedThreads returns all waiting threads
403       */
404 <    public void testGetQueuedThreads() {
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 <        try {
409 <            assertTrue(sync.getQueuedThreads().isEmpty());
410 <            sync.acquire(1);
411 <            assertTrue(sync.getQueuedThreads().isEmpty());
412 <            t1.start();
413 <            Thread.sleep(SHORT_DELAY_MS);
414 <            assertTrue(sync.getQueuedThreads().contains(t1));
415 <            t2.start();
416 <            Thread.sleep(SHORT_DELAY_MS);
417 <            assertTrue(sync.getQueuedThreads().contains(t1));
418 <            assertTrue(sync.getQueuedThreads().contains(t2));
419 <            t1.interrupt();
420 <            Thread.sleep(SHORT_DELAY_MS);
421 <            assertFalse(sync.getQueuedThreads().contains(t1));
422 <            assertTrue(sync.getQueuedThreads().contains(t2));
423 <            sync.release(1);
424 <            Thread.sleep(SHORT_DELAY_MS);
425 <            assertTrue(sync.getQueuedThreads().isEmpty());
426 <            t1.join();
427 <            t2.join();
290 <        } catch(Exception e){
291 <            unexpectedException();
292 <        }
293 <    }
408 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
409 >        sync.acquire();
410 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
411 >        t1.start();
412 >        waitForQueuedThread(sync, t1);
413 >        assertHasExclusiveQueuedThreads(sync, t1);
414 >        assertTrue(sync.getQueuedThreads().contains(t1));
415 >        assertFalse(sync.getQueuedThreads().contains(t2));
416 >        t2.start();
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 >        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() {
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 <        try {
437 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
438 <            sync.acquire(1);
439 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
440 <            t1.start();
441 <            Thread.sleep(SHORT_DELAY_MS);
442 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
443 <            t2.start();
444 <            Thread.sleep(SHORT_DELAY_MS);
445 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
446 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
447 <            t1.interrupt();
448 <            Thread.sleep(SHORT_DELAY_MS);
449 <            assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
450 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
451 <            sync.release(1);
452 <            Thread.sleep(SHORT_DELAY_MS);
453 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
454 <            t1.join();
455 <            t2.join();
322 <        } catch(Exception e){
323 <            unexpectedException();
324 <        }
325 <    }
436 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
437 >        sync.acquire();
438 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
439 >        t1.start();
440 >        waitForQueuedThread(sync, t1);
441 >        assertHasExclusiveQueuedThreads(sync, t1);
442 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
443 >        assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
444 >        t2.start();
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 >        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() {
461 <        final Mutex sync = new Mutex();
462 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
463 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
464 <        try {
465 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
466 <            sync.acquire(1);
467 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
468 <            t1.start();
469 <            Thread.sleep(SHORT_DELAY_MS);
470 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
471 <            t2.start();
472 <            Thread.sleep(SHORT_DELAY_MS);
473 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
474 <            t1.interrupt();
475 <            Thread.sleep(SHORT_DELAY_MS);
476 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
477 <            sync.release(1);
348 <            Thread.sleep(SHORT_DELAY_MS);
349 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
350 <            t1.join();
351 <            t2.join();
352 <        } catch(Exception e){
353 <            unexpectedException();
354 <        }
355 <    }
460 >    public void testGetSharedQueuedThreads_Exclusive() {
461 >        final Mutex sync = new Mutex();
462 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
463 >        sync.acquire();
464 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
465 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
466 >        waitForQueuedThread(sync, t1);
467 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
468 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
469 >        waitForQueuedThread(sync, t2);
470 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
471 >        t1.interrupt();
472 >        awaitTermination(t1);
473 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
474 >        sync.release();
475 >        awaitTermination(t2);
476 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
477 >    }
478  
479      /**
480 <     * tryAcquireNanos is interruptible.
480 >     * getSharedQueuedThreads returns all shared waiting threads
481       */
482 <    public void testInterruptedException2() {
483 <        final Mutex sync = new Mutex();
484 <        sync.acquire(1);
485 <        Thread t = new Thread(new Runnable() {
486 <                public void run() {
487 <                    try {
488 <                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
489 <                        threadShouldThrow();
490 <                    } catch(InterruptedException success){}
491 <                }
492 <            });
493 <        try {
494 <            t.start();
495 <            t.interrupt();
496 <        } catch(Exception e){
497 <            unexpectedException();
498 <        }
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 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 +        waitForQueuedThread(sync, t);
517 +        t.interrupt();
518 +        awaitTermination(t);
519 +    }
520  
521      /**
522 <     * TryAcquire on exclusively held sync fails
523 <     */
524 <    public void testTryAcquireWhenSynced() {
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 <                }
531 <            });
532 <        try {
533 <            t.start();
534 <            t.join();
394 <            sync.release(1);
395 <        } catch(Exception e){
396 <            unexpectedException();
397 <        }
398 <    }
522 >     * tryAcquire on exclusively held sync fails
523 >     */
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 >        awaitTermination(t);
533 >        sync.release();
534 >    }
535  
536      /**
537       * tryAcquireNanos on an exclusively held sync times out
538       */
539 <    public void testAcquireNanos_Timeout() {
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();
548 <                    }
549 <                }
550 <            });
551 <        try {
552 <            t.start();
553 <            t.join();
418 <            sync.release(1);
419 <        } catch(Exception e){
420 <            unexpectedException();
421 <        }
422 <    }
423 <    
424 <  
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() {
558 <        final Mutex sync = new Mutex();
559 <        sync.acquire(1);
560 <        assertTrue(sync.isHeldExclusively());
561 <        sync.release(1);
562 <        assertFalse(sync.isHeldExclusively());
563 <        Thread t = new Thread(new Runnable() {
564 <                public void run() {
565 <                    sync.acquire(1);
566 <                    try {
567 <                        Thread.sleep(SMALL_DELAY_MS);
568 <                    }
569 <                    catch(Exception e) {
570 <                        threadUnexpectedException();
571 <                    }
572 <                    sync.release(1);
573 <                }
574 <            });
575 <        try {
576 <            t.start();
577 <            Thread.sleep(SHORT_DELAY_MS);
578 <            assertTrue(sync.isHeldExclusively());
450 <            t.join();
451 <            assertFalse(sync.isHeldExclusively());
452 <        } catch(Exception e){
453 <            unexpectedException();
454 <        }
558 >        final Mutex sync = new Mutex();
559 >        sync.acquire();
560 >        assertTrue(sync.isHeldExclusively());
561 >        sync.release();
562 >        assertFalse(sync.isHeldExclusively());
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 >        acquired.acquireShared(0);
575 >        assertTrue(sync.isHeldExclusively());
576 >        assertTrue(done.releaseShared(0));
577 >        awaitTermination(t);
578 >        assertFalse(sync.isHeldExclusively());
579      }
580  
457
458    /**
459     * acquireInterruptibly is interruptible.
460     */
461    public void testAcquireInterruptibly1() {
462        final Mutex sync = new Mutex();
463        sync.acquire(1);
464        Thread t = new Thread(new InterruptedSyncRunnable(sync));
465        try {
466            t.start();
467            Thread.sleep(SHORT_DELAY_MS);
468            t.interrupt();
469            Thread.sleep(SHORT_DELAY_MS);
470            sync.release(1);
471            t.join();
472        } catch(Exception e){
473            unexpectedException();
474        }
475    }
476
581      /**
582       * acquireInterruptibly succeeds when released, else is interruptible
583       */
584 <    public void testAcquireInterruptibly2() {
585 <        final Mutex sync = new Mutex();
586 <        try {
587 <            sync.acquireInterruptibly(1);
588 <        } catch(Exception e) {
589 <            unexpectedException();
590 <        }
591 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
592 <        try {
593 <            t.start();
594 <            t.interrupt();
595 <            assertTrue(sync.isHeldExclusively());
596 <            t.join();
597 <        } catch(Exception e){
598 <            unexpectedException();
495 <        }
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());
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 509 | Line 612 | public class AbstractQueuedLongSynchroni
612      /**
613       * Calling await without holding sync throws IllegalMonitorStateException
614       */
615 <    public void testAwait_IllegalMonitor() {
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) {
623 <        }
624 <        catch (Exception ex) {
625 <            unexpectedException();
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() {
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) {
537 <        }
538 <        catch (Exception ex) {
539 <            unexpectedException();
540 <        }
638 >        } catch (IllegalMonitorStateException success) {}
639 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
640      }
641  
642      /**
643 <     * awaitNanos without a signal times out
643 >     * Calling signalAll without holding sync throws IllegalMonitorStateException
644       */
645 <    public void testAwaitNanos_Timeout() {
646 <        final Mutex sync = new Mutex();
647 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
645 >    public void testSignalAll_IMSE() {
646 >        final Mutex sync = new Mutex();
647 >        final ConditionObject c = sync.newCondition();
648          try {
649 <            sync.acquire(1);
650 <            long t = c.awaitNanos(100);
651 <            assertTrue(t <= 0);
553 <            sync.release(1);
554 <        }
555 <        catch (Exception ex) {
556 <            unexpectedException();
557 <        }
649 >            c.signalAll();
650 >            shouldThrow();
651 >        } catch (IllegalMonitorStateException success) {}
652      }
653  
654      /**
655 <     *  Timed await without a signal times out
655 >     * await/awaitNanos/awaitUntil without a signal times out
656       */
657 <    public void testAwait_Timeout() {
658 <        final Mutex sync = new Mutex();
659 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
660 <        try {
661 <            sync.acquire(1);
662 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
663 <            sync.release(1);
664 <        }
665 <        catch (Exception ex) {
572 <            unexpectedException();
573 <        }
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 <     * awaitUntil without a signal times out
670 <     */
671 <    public void testAwaitUntil_Timeout() {
672 <        final Mutex sync = new Mutex();
673 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
674 <        try {
675 <            sync.acquire(1);
676 <            java.util.Date d = new java.util.Date();
677 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
678 <            sync.release(1);
679 <        }
680 <        catch (Exception ex) {
681 <            unexpectedException();
682 <        }
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 >        assertHasWaitersLocked(sync, c, NO_THREADS);
693 >        assertHasExclusiveQueuedThreads(sync, t);
694 >        sync.release();
695 >        awaitTermination(t);
696      }
697  
698      /**
699 <     * await returns when signalled
595 <     */
596 <    public void testAwait() {
597 <        final Mutex sync = new Mutex();
598 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
599 <        Thread t = new Thread(new Runnable() {
600 <                public void run() {
601 <                    try {
602 <                        sync.acquire(1);
603 <                        c.await();
604 <                        sync.release(1);
605 <                    }
606 <                    catch(InterruptedException e) {
607 <                        threadUnexpectedException();
608 <                    }
609 <                }
610 <            });
611 <
612 <        try {
613 <            t.start();
614 <            Thread.sleep(SHORT_DELAY_MS);
615 <            sync.acquire(1);
616 <            c.signal();
617 <            sync.release(1);
618 <            t.join(SHORT_DELAY_MS);
619 <            assertFalse(t.isAlive());
620 <        }
621 <        catch (Exception ex) {
622 <            unexpectedException();
623 <        }
624 <    }
625 <
626 <
627 <
628 <    /**
629 <     * 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();
706 <        } catch (NullPointerException success) {
637 <        } catch (Exception ex) {
638 <            unexpectedException();
639 <        }
706 >        } catch (NullPointerException success) {}
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) {
651 <        } catch (Exception ex) {
652 <            unexpectedException();
653 <        }
717 >        } catch (NullPointerException success) {}
718      }
719  
656
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) {
666 <        } catch (Exception ex) {
667 <            unexpectedException();
668 <        }
728 >        } catch (NullPointerException success) {}
729      }
730  
671
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 <        } catch (Exception ex) {
684 <            unexpectedException();
685 <        }
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 <        } catch (Exception ex) {
699 <            unexpectedException();
700 <        }
754 >        } catch (IllegalMonitorStateException success) {}
755 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
756      }
757  
703
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 <        } catch (Exception ex) {
716 <            unexpectedException();
717 <        }
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 <        } catch (Exception ex) {
731 <            unexpectedException();
732 <        }
781 >        } catch (IllegalMonitorStateException success) {}
782 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
783      }
784  
735
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 <        } catch (Exception ex) {
748 <            unexpectedException();
749 <        }
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 <        } catch (Exception ex) {
763 <            unexpectedException();
764 <        }
808 >        } catch (IllegalMonitorStateException success) {}
809 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
810      }
811  
767
768
812      /**
813       * hasWaiters returns true when a thread is waiting, else false
814       */
815      public void testHasWaiters() {
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();
829 <                    }
830 <                }
831 <            });
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));
834 >        c.signal();
835 >        assertHasWaitersLocked(sync, c, NO_THREADS);
836 >        assertHasExclusiveQueuedThreads(sync, t);
837 >        assertFalse(sync.hasWaiters(c));
838 >        sync.release();
839  
840 <        try {
841 <            t.start();
792 <            Thread.sleep(SHORT_DELAY_MS);
793 <            sync.acquire(1);
794 <            assertTrue(sync.hasWaiters(c));
795 <            assertEquals(1, sync.getWaitQueueLength(c));
796 <            c.signal();
797 <            sync.release(1);
798 <            Thread.sleep(SHORT_DELAY_MS);
799 <            sync.acquire(1);
800 <            assertFalse(sync.hasWaiters(c));
801 <            assertEquals(0, sync.getWaitQueueLength(c));
802 <            sync.release(1);
803 <            t.join(SHORT_DELAY_MS);
804 <            assertFalse(t.isAlive());
805 <        }
806 <        catch (Exception ex) {
807 <            unexpectedException();
808 <        }
840 >        awaitTermination(t);
841 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
842      }
843  
844      /**
845       * getWaitQueueLength returns number of waiting threads
846       */
847      public void testGetWaitQueueLength() {
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();
861 <                    }
862 <                }
863 <            });
864 <
865 <        Thread t2 = new Thread(new Runnable() {
866 <                public void run() {
867 <                    try {
868 <                        sync.acquire(1);
869 <                        threadAssertTrue(sync.hasWaiters(c));
870 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
871 <                        c.await();
872 <                        sync.release(1);
873 <                    }
874 <                    catch(InterruptedException e) {
875 <                        threadUnexpectedException();
876 <                    }
877 <                }
878 <            });
879 <
880 <        try {
881 <            t1.start();
882 <            Thread.sleep(SHORT_DELAY_MS);
883 <            t2.start();
884 <            Thread.sleep(SHORT_DELAY_MS);
885 <            sync.acquire(1);
886 <            assertTrue(sync.hasWaiters(c));
887 <            assertEquals(2, sync.getWaitQueueLength(c));
888 <            c.signalAll();
889 <            sync.release(1);
857 <            Thread.sleep(SHORT_DELAY_MS);
858 <            sync.acquire(1);
859 <            assertFalse(sync.hasWaiters(c));
860 <            assertEquals(0, sync.getWaitQueueLength(c));
861 <            sync.release(1);
862 <            t1.join(SHORT_DELAY_MS);
863 <            t2.join(SHORT_DELAY_MS);
864 <            assertFalse(t1.isAlive());
865 <            assertFalse(t2.isAlive());
866 <        }
867 <        catch (Exception ex) {
868 <            unexpectedException();
869 <        }
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 >        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 >        assertHasWaitersLocked(sync, c, NO_THREADS);
883 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
884 >        assertEquals(0, sync.getWaitQueueLength(c));
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() {
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();
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();
922 <                    }
923 <                }
924 <            });
925 <
926 <        try {
927 <            sync.acquire(1);
928 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
929 <            sync.release(1);
930 <            t1.start();
931 <            Thread.sleep(SHORT_DELAY_MS);
932 <            t2.start();
933 <            Thread.sleep(SHORT_DELAY_MS);
934 <            sync.acquire(1);
935 <            assertTrue(sync.hasWaiters(c));
936 <            assertTrue(sync.getWaitingThreads(c).contains(t1));
937 <            assertTrue(sync.getWaitingThreads(c).contains(t2));
938 <            c.signalAll();
939 <            sync.release(1);
940 <            Thread.sleep(SHORT_DELAY_MS);
941 <            sync.acquire(1);
942 <            assertFalse(sync.hasWaiters(c));
943 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
944 <            sync.release(1);
945 <            t1.join(SHORT_DELAY_MS);
946 <            t2.join(SHORT_DELAY_MS);
947 <            assertFalse(t1.isAlive());
948 <            assertFalse(t2.isAlive());
949 <        }
950 <        catch (Exception ex) {
951 <            unexpectedException();
952 <        }
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 >        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  
935
936
963      /**
964 <     * awaitUninterruptibly doesn't abort on interrupt
964 >     * awaitUninterruptibly is uninterruptible
965       */
966      public void testAwaitUninterruptibly() {
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 <                    sync.acquire(1);
972 <                    c.awaitUninterruptibly();
973 <                    sync.release(1);
974 <                }
975 <            });
976 <
977 <        try {
978 <            t.start();
979 <            Thread.sleep(SHORT_DELAY_MS);
980 <            t.interrupt();
981 <            sync.acquire(1);
982 <            c.signal();
983 <            sync.release(1);
984 <            t.join(SHORT_DELAY_MS);
985 <            assertFalse(t.isAlive());
986 <        }
987 <        catch (Exception ex) {
988 <            unexpectedException();
989 <        }
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 >        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 <     * await is interruptible
999 <     */
1000 <    public void testAwait_Interrupt() {
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 <                        c.await();
1008 <                        sync.release(1);
1009 <                        threadShouldThrow();
1010 <                    }
1011 <                    catch(InterruptedException success) {
1012 <                    }
1013 <                }
1014 <            });
1015 <
1016 <        try {
1017 <            t.start();
987 <            Thread.sleep(SHORT_DELAY_MS);
988 <            t.interrupt();
989 <            t.join(SHORT_DELAY_MS);
990 <            assertFalse(t.isAlive());
991 <        }
992 <        catch (Exception ex) {
993 <            unexpectedException();
994 <        }
995 <    }
996 <
997 <    /**
998 <     * awaitNanos is interruptible
999 <     */
1000 <    public void testAwaitNanos_Interrupt() {
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 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1008 <                        sync.release(1);
1009 <                        threadShouldThrow();
1010 <                    }
1011 <                    catch(InterruptedException success) {
1012 <                    }
1013 <                }
1014 <            });
1015 <
1016 <        try {
1017 <            t.start();
1018 <            Thread.sleep(SHORT_DELAY_MS);
1019 <            t.interrupt();
1020 <            t.join(SHORT_DELAY_MS);
1021 <            assertFalse(t.isAlive());
1022 <        }
1023 <        catch (Exception ex) {
1024 <            unexpectedException();
1025 <        }
1026 <    }
1027 <
1028 <    /**
1029 <     * awaitUntil is interruptible
1030 <     */
1031 <    public void testAwaitUntil_Interrupt() {
1032 <        final Mutex sync = new Mutex();
1033 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1034 <        Thread t = new Thread(new Runnable() {
1035 <                public void run() {
1036 <                    try {
1037 <                        sync.acquire(1);
1038 <                        java.util.Date d = new java.util.Date();
1039 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1040 <                        sync.release(1);
1041 <                        threadShouldThrow();
1042 <                    }
1043 <                    catch(InterruptedException success) {
1044 <                    }
1045 <                }
1046 <            });
1047 <
1048 <        try {
1049 <            t.start();
1050 <            Thread.sleep(SHORT_DELAY_MS);
1051 <            t.interrupt();
1052 <            t.join(SHORT_DELAY_MS);
1053 <            assertFalse(t.isAlive());
1054 <        }
1055 <        catch (Exception ex) {
1056 <            unexpectedException();
1057 <        }
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 >        pleaseInterrupt.acquireShared(0);
1016 >        t.interrupt();
1017 >        awaitTermination(t);
1018      }
1019  
1020      /**
1021       * signalAll wakes up all threads
1022       */
1023 <    public void testSignalAll() {
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 <        try {
1053 <            t1.start();
1054 <            t2.start();
1055 <            Thread.sleep(SHORT_DELAY_MS);
1056 <            sync.acquire(1);
1057 <            c.signalAll();
1058 <            sync.release(1);
1099 <            t1.join(SHORT_DELAY_MS);
1100 <            t2.join(SHORT_DELAY_MS);
1101 <            assertFalse(t1.isAlive());
1102 <            assertFalse(t2.isAlive());
1103 <        }
1104 <        catch (Exception ex) {
1105 <            unexpectedException();
1106 <        }
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 >        assertHasWaitersLocked(sync, c, NO_THREADS);
1055 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
1056 >        sync.release();
1057 >        awaitTermination(t1);
1058 >        awaitTermination(t2);
1059      }
1060  
1109
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);
1118 <        String ls = sync.toString();
1119 <        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
1072 >     * A serialized AQS deserializes with current state, but no queued threads
1073       */
1074      public void testSerialization() {
1075 <        Mutex l = new Mutex();
1076 <        l.acquire(1);
1077 <        assertTrue(l.isHeldExclusively());
1078 <
1079 <        try {
1080 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1081 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1082 <            out.writeObject(l);
1083 <            out.close();
1084 <
1085 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1086 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1087 <            Mutex r = (Mutex) in.readObject();
1088 <            assertTrue(r.isHeldExclusively());
1089 <        } catch(Exception e){
1090 <            e.printStackTrace();
1091 <            unexpectedException();
1092 <        }
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  
1146
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() {
1121 <        final BooleanLatch l = new BooleanLatch();
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();
1131 <                    }
1132 <                }
1133 <            });
1134 <        try {
1135 <            t.start();
1136 <            assertFalse(l.isSignalled());
1137 <            Thread.sleep(SHORT_DELAY_MS);
1138 <            l.releaseShared(0);
1191 <            assertTrue(l.isSignalled());
1192 <            t.join();
1193 <        } catch (InterruptedException e){
1194 <            unexpectedException();
1195 <        }
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 >        waitForQueuedThread(l, t);
1133 >        assertFalse(l.isSignalled());
1134 >        assertThreadBlocks(t, Thread.State.WAITING);
1135 >        assertHasSharedQueuedThreads(l, t);
1136 >        assertTrue(l.releaseShared(0));
1137 >        assertTrue(l.isSignalled());
1138 >        awaitTermination(t);
1139      }
1197    
1140  
1141      /**
1142 <     * acquireSharedTimed returns after release
1142 >     * tryAcquireSharedNanos returns after release, but not before
1143       */
1144 <    public void testAsquireSharedTimed() {
1145 <        final BooleanLatch l = new BooleanLatch();
1144 >    public void testTryAcquireSharedNanos() {
1145 >        final BooleanLatch l = new BooleanLatch();
1146  
1147 <        Thread t = new Thread(new Runnable() {
1148 <                public void run() {
1149 <                    try {
1150 <                        threadAssertFalse(l.isSignalled());
1151 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
1152 <                        threadAssertTrue(l.isSignalled());
1153 <
1154 <                    } catch(InterruptedException e){
1155 <                        threadUnexpectedException();
1156 <                    }
1157 <                }
1158 <            });
1159 <        try {
1160 <            t.start();
1161 <            assertFalse(l.isSignalled());
1162 <            Thread.sleep(SHORT_DELAY_MS);
1221 <            l.releaseShared(0);
1222 <            assertTrue(l.isSignalled());
1223 <            t.join();
1224 <        } catch (InterruptedException e){
1225 <            unexpectedException();
1226 <        }
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 >        waitForQueuedThread(l, t);
1158 >        assertFalse(l.isSignalled());
1159 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1160 >        assertTrue(l.releaseShared(0));
1161 >        assertTrue(l.isSignalled());
1162 >        awaitTermination(t);
1163      }
1164 <    
1164 >
1165      /**
1166 <     * acquireSharedInterruptibly throws IE if interrupted before released
1166 >     * acquireSharedInterruptibly is interruptible
1167       */
1168 <    public void testAcquireSharedInterruptibly_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);
1175 <                        threadShouldThrow();
1176 <                    } catch(InterruptedException success){}
1177 <                }
1178 <            });
1179 <        t.start();
1180 <        try {
1245 <            assertFalse(l.isSignalled());
1246 <            t.interrupt();
1247 <            t.join();
1248 <        } catch (InterruptedException e){
1249 <            unexpectedException();
1250 <        }
1170 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1171 >            public void realRun() throws InterruptedException {
1172 >                assertFalse(l.isSignalled());
1173 >                l.acquireSharedInterruptibly(0);
1174 >            }});
1175 >
1176 >        waitForQueuedThread(l, t);
1177 >        assertFalse(l.isSignalled());
1178 >        t.interrupt();
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() {
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();                        
1194 <                    } catch(InterruptedException success){}
1195 <                }
1196 <            });
1197 <        t.start();
1198 <        try {
1199 <            Thread.sleep(SHORT_DELAY_MS);
1270 <            assertFalse(l.isSignalled());
1271 <            t.interrupt();
1272 <            t.join();
1273 <        } catch (InterruptedException e){
1274 <            unexpectedException();
1275 <        }
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 >        waitForQueuedThread(l, t);
1196 >        assertFalse(l.isSignalled());
1197 >        t.interrupt();
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() {
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 <            });
1220 <        t.start();
1221 <        try {
1222 <            Thread.sleep(SHORT_DELAY_MS);
1223 <            assertFalse(l.isSignalled());
1224 <            t.join();
1225 <        } catch (InterruptedException e){
1226 <            unexpectedException();
1227 <        }
1219 >                assertFalse(l.isSignalled());
1220 >            }});
1221 >
1222 >        waitForQueuedThread(l, t);
1223 >        observedQueued.releaseShared(0);
1224 >        assertFalse(l.isSignalled());
1225 >        awaitTermination(t);
1226 >        assertFalse(l.isSignalled());
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  
1303    
1253   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines