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.3 by dl, Wed Apr 5 11:24:55 2006 UTC vs.
Revision 1.30 by jsr166, Wed Dec 31 19:23:56 2014 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 >        junit.textui.TestRunner.run(suite());
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);
98              return true;
# Line 68 | Line 100 | public class AbstractQueuedLongSynchroni
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          }
229      }
230  
231      /**
232 +     * Checks that awaiting the given condition times out (using the
233 +     * default timeout duration).
234 +     */
235 +    void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
236 +        long timeoutMillis = timeoutMillis();
237 +        long startTime = System.nanoTime();
238 +        try {
239 +            switch (awaitMethod) {
240 +            case awaitTimed:
241 +                assertFalse(c.await(timeoutMillis, MILLISECONDS));
242 +                break;
243 +            case awaitNanos:
244 +                long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
245 +                long nanosRemaining = c.awaitNanos(nanosTimeout);
246 +                assertTrue(nanosRemaining <= 0);
247 +                break;
248 +            case awaitUntil:
249 +                assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
250 +                break;
251 +            default:
252 +                throw new UnsupportedOperationException();
253 +            }
254 +        } catch (InterruptedException ie) { threadUnexpectedException(ie); }
255 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
256 +    }
257 +
258 +    /**
259       * isHeldExclusively is false upon construction
260       */
261 <    public void testIsHeldExclusively() {
262 <        Mutex rl = new Mutex();
263 <        assertFalse(rl.isHeldExclusively());
261 >    public void testIsHeldExclusively() {
262 >        Mutex sync = new Mutex();
263 >        assertFalse(sync.isHeldExclusively());
264      }
265 <    
265 >
266      /**
267       * acquiring released sync succeeds
268       */
269 <    public void testAcquire() {
270 <        Mutex rl = new Mutex();
271 <        rl.acquire(1);
272 <        assertTrue(rl.isHeldExclusively());
273 <        rl.release(1);
274 <        assertFalse(rl.isHeldExclusively());
269 >    public void testAcquire() {
270 >        Mutex sync = new Mutex();
271 >        sync.acquire();
272 >        assertTrue(sync.isHeldExclusively());
273 >        sync.release();
274 >        assertFalse(sync.isHeldExclusively());
275      }
276  
277      /**
278 <     * tryAcquire on an released sync succeeds
279 <     */
280 <    public void testTryAcquire() {
281 <        Mutex rl = new Mutex();
282 <        assertTrue(rl.tryAcquire(1));
283 <        assertTrue(rl.isHeldExclusively());
284 <        rl.release(1);
278 >     * tryAcquire on a released sync succeeds
279 >     */
280 >    public void testTryAcquire() {
281 >        Mutex sync = new Mutex();
282 >        assertTrue(sync.tryAcquire());
283 >        assertTrue(sync.isHeldExclusively());
284 >        sync.release();
285 >        assertFalse(sync.isHeldExclusively());
286      }
287  
288      /**
289       * hasQueuedThreads reports whether there are waiting threads
290       */
291 <    public void testhasQueuedThreads() {
292 <        final Mutex sync = new Mutex();
293 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
294 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
295 <        try {
296 <            assertFalse(sync.hasQueuedThreads());
297 <            sync.acquire(1);
298 <            t1.start();
299 <            Thread.sleep(SHORT_DELAY_MS);
300 <            assertTrue(sync.hasQueuedThreads());
301 <            t2.start();
302 <            Thread.sleep(SHORT_DELAY_MS);
303 <            assertTrue(sync.hasQueuedThreads());
304 <            t1.interrupt();
305 <            Thread.sleep(SHORT_DELAY_MS);
306 <            assertTrue(sync.hasQueuedThreads());
307 <            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 <    }
291 >    public void testHasQueuedThreads() {
292 >        final Mutex sync = new Mutex();
293 >        assertFalse(sync.hasQueuedThreads());
294 >        sync.acquire();
295 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
296 >        waitForQueuedThread(sync, t1);
297 >        assertTrue(sync.hasQueuedThreads());
298 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
299 >        waitForQueuedThread(sync, t2);
300 >        assertTrue(sync.hasQueuedThreads());
301 >        t1.interrupt();
302 >        awaitTermination(t1);
303 >        assertTrue(sync.hasQueuedThreads());
304 >        sync.release();
305 >        awaitTermination(t2);
306 >        assertFalse(sync.hasQueuedThreads());
307 >    }
308  
309      /**
310 <     * isQueued(null) throws NPE
310 >     * isQueued(null) throws NullPointerException
311       */
312 <    public void testIsQueuedNPE() {
313 <        final Mutex sync = new Mutex();
312 >    public void testIsQueuedNPE() {
313 >        final Mutex sync = new Mutex();
314          try {
315              sync.isQueued(null);
316              shouldThrow();
317 <        } catch (NullPointerException success) {
166 <        }
317 >        } catch (NullPointerException success) {}
318      }
319  
320      /**
321 <     * isQueued reports whether a thread is queued.
321 >     * isQueued reports whether a thread is queued
322       */
323 <    public void testIsQueued() {
324 <        final Mutex sync = new Mutex();
323 >    public void testIsQueued() {
324 >        final Mutex sync = new Mutex();
325          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
326          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
327 <        try {
328 <            assertFalse(sync.isQueued(t1));
329 <            assertFalse(sync.isQueued(t2));
330 <            sync.acquire(1);
331 <            t1.start();
332 <            Thread.sleep(SHORT_DELAY_MS);
333 <            assertTrue(sync.isQueued(t1));
334 <            t2.start();
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <            assertTrue(sync.isQueued(t1));
337 <            assertTrue(sync.isQueued(t2));
338 <            t1.interrupt();
339 <            Thread.sleep(SHORT_DELAY_MS);
340 <            assertFalse(sync.isQueued(t1));
341 <            assertTrue(sync.isQueued(t2));
342 <            sync.release(1);
343 <            Thread.sleep(SHORT_DELAY_MS);
344 <            assertFalse(sync.isQueued(t1));
345 <            Thread.sleep(SHORT_DELAY_MS);
346 <            assertFalse(sync.isQueued(t2));
196 <            t1.join();
197 <            t2.join();
198 <        } catch(Exception e){
199 <            unexpectedException();
200 <        }
201 <    }
327 >        assertFalse(sync.isQueued(t1));
328 >        assertFalse(sync.isQueued(t2));
329 >        sync.acquire();
330 >        t1.start();
331 >        waitForQueuedThread(sync, t1);
332 >        assertTrue(sync.isQueued(t1));
333 >        assertFalse(sync.isQueued(t2));
334 >        t2.start();
335 >        waitForQueuedThread(sync, t2);
336 >        assertTrue(sync.isQueued(t1));
337 >        assertTrue(sync.isQueued(t2));
338 >        t1.interrupt();
339 >        awaitTermination(t1);
340 >        assertFalse(sync.isQueued(t1));
341 >        assertTrue(sync.isQueued(t2));
342 >        sync.release();
343 >        awaitTermination(t2);
344 >        assertFalse(sync.isQueued(t1));
345 >        assertFalse(sync.isQueued(t2));
346 >    }
347  
348      /**
349       * getFirstQueuedThread returns first waiting thread or null if none
350       */
351 <    public void testGetFirstQueuedThread() {
352 <        final Mutex sync = new Mutex();
353 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
354 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
355 <        try {
356 <            assertNull(sync.getFirstQueuedThread());
357 <            sync.acquire(1);
358 <            t1.start();
359 <            Thread.sleep(SHORT_DELAY_MS);
360 <            assertEquals(t1, sync.getFirstQueuedThread());
361 <            t2.start();
362 <            Thread.sleep(SHORT_DELAY_MS);
363 <            assertEquals(t1, sync.getFirstQueuedThread());
364 <            t1.interrupt();
365 <            Thread.sleep(SHORT_DELAY_MS);
366 <            Thread.sleep(SHORT_DELAY_MS);
367 <            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 <
351 >    public void testGetFirstQueuedThread() {
352 >        final Mutex sync = new Mutex();
353 >        assertNull(sync.getFirstQueuedThread());
354 >        sync.acquire();
355 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
356 >        waitForQueuedThread(sync, t1);
357 >        assertEquals(t1, sync.getFirstQueuedThread());
358 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
359 >        waitForQueuedThread(sync, t2);
360 >        assertEquals(t1, sync.getFirstQueuedThread());
361 >        t1.interrupt();
362 >        awaitTermination(t1);
363 >        assertEquals(t2, sync.getFirstQueuedThread());
364 >        sync.release();
365 >        awaitTermination(t2);
366 >        assertNull(sync.getFirstQueuedThread());
367 >    }
368  
369      /**
370       * hasContended reports false if no thread has ever blocked, else true
371       */
372 <    public void testHasContended() {
373 <        final Mutex sync = new Mutex();
374 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
375 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
376 <        try {
377 <            assertFalse(sync.hasContended());
378 <            sync.acquire(1);
379 <            t1.start();
380 <            Thread.sleep(SHORT_DELAY_MS);
381 <            assertTrue(sync.hasContended());
382 <            t2.start();
383 <            Thread.sleep(SHORT_DELAY_MS);
384 <            assertTrue(sync.hasContended());
385 <            t1.interrupt();
386 <            Thread.sleep(SHORT_DELAY_MS);
387 <            assertTrue(sync.hasContended());
388 <            sync.release(1);
389 <            Thread.sleep(SHORT_DELAY_MS);
255 <            assertTrue(sync.hasContended());
256 <            t1.join();
257 <            t2.join();
258 <        } catch(Exception e){
259 <            unexpectedException();
260 <        }
261 <    }
372 >    public void testHasContended() {
373 >        final Mutex sync = new Mutex();
374 >        assertFalse(sync.hasContended());
375 >        sync.acquire();
376 >        assertFalse(sync.hasContended());
377 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
378 >        waitForQueuedThread(sync, t1);
379 >        assertTrue(sync.hasContended());
380 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
381 >        waitForQueuedThread(sync, t2);
382 >        assertTrue(sync.hasContended());
383 >        t1.interrupt();
384 >        awaitTermination(t1);
385 >        assertTrue(sync.hasContended());
386 >        sync.release();
387 >        awaitTermination(t2);
388 >        assertTrue(sync.hasContended());
389 >    }
390  
391      /**
392 <     * getQueuedThreads includes waiting threads
392 >     * getQueuedThreads returns all waiting threads
393       */
394 <    public void testGetQueuedThreads() {
395 <        final Mutex sync = new Mutex();
394 >    public void testGetQueuedThreads() {
395 >        final Mutex sync = new Mutex();
396          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
397          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
398 <        try {
399 <            assertTrue(sync.getQueuedThreads().isEmpty());
400 <            sync.acquire(1);
401 <            assertTrue(sync.getQueuedThreads().isEmpty());
402 <            t1.start();
403 <            Thread.sleep(SHORT_DELAY_MS);
404 <            assertTrue(sync.getQueuedThreads().contains(t1));
405 <            t2.start();
406 <            Thread.sleep(SHORT_DELAY_MS);
407 <            assertTrue(sync.getQueuedThreads().contains(t1));
408 <            assertTrue(sync.getQueuedThreads().contains(t2));
409 <            t1.interrupt();
410 <            Thread.sleep(SHORT_DELAY_MS);
411 <            assertFalse(sync.getQueuedThreads().contains(t1));
412 <            assertTrue(sync.getQueuedThreads().contains(t2));
413 <            sync.release(1);
414 <            Thread.sleep(SHORT_DELAY_MS);
415 <            assertTrue(sync.getQueuedThreads().isEmpty());
416 <            t1.join();
417 <            t2.join();
290 <        } catch(Exception e){
291 <            unexpectedException();
292 <        }
293 <    }
398 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
399 >        sync.acquire();
400 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
401 >        t1.start();
402 >        waitForQueuedThread(sync, t1);
403 >        assertHasExclusiveQueuedThreads(sync, t1);
404 >        assertTrue(sync.getQueuedThreads().contains(t1));
405 >        assertFalse(sync.getQueuedThreads().contains(t2));
406 >        t2.start();
407 >        waitForQueuedThread(sync, t2);
408 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
409 >        assertTrue(sync.getQueuedThreads().contains(t1));
410 >        assertTrue(sync.getQueuedThreads().contains(t2));
411 >        t1.interrupt();
412 >        awaitTermination(t1);
413 >        assertHasExclusiveQueuedThreads(sync, t2);
414 >        sync.release();
415 >        awaitTermination(t2);
416 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
417 >    }
418  
419      /**
420 <     * getExclusiveQueuedThreads includes waiting threads
420 >     * getExclusiveQueuedThreads returns all exclusive waiting threads
421       */
422 <    public void testGetExclusiveQueuedThreads() {
423 <        final Mutex sync = new Mutex();
422 >    public void testGetExclusiveQueuedThreads() {
423 >        final Mutex sync = new Mutex();
424          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
425          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
426 <        try {
427 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
428 <            sync.acquire(1);
429 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
430 <            t1.start();
431 <            Thread.sleep(SHORT_DELAY_MS);
432 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
433 <            t2.start();
434 <            Thread.sleep(SHORT_DELAY_MS);
435 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
436 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
437 <            t1.interrupt();
438 <            Thread.sleep(SHORT_DELAY_MS);
439 <            assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
440 <            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
441 <            sync.release(1);
442 <            Thread.sleep(SHORT_DELAY_MS);
443 <            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
444 <            t1.join();
445 <            t2.join();
322 <        } catch(Exception e){
323 <            unexpectedException();
324 <        }
325 <    }
426 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
427 >        sync.acquire();
428 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
429 >        t1.start();
430 >        waitForQueuedThread(sync, t1);
431 >        assertHasExclusiveQueuedThreads(sync, t1);
432 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
433 >        assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
434 >        t2.start();
435 >        waitForQueuedThread(sync, t2);
436 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
437 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
438 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
439 >        t1.interrupt();
440 >        awaitTermination(t1);
441 >        assertHasExclusiveQueuedThreads(sync, t2);
442 >        sync.release();
443 >        awaitTermination(t2);
444 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
445 >    }
446  
447      /**
448       * getSharedQueuedThreads does not include exclusively waiting threads
449       */
450 <    public void testGetSharedQueuedThreads() {
451 <        final Mutex sync = new Mutex();
452 <        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
453 <        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
454 <        try {
455 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
456 <            sync.acquire(1);
457 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
458 <            t1.start();
459 <            Thread.sleep(SHORT_DELAY_MS);
460 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
461 <            t2.start();
462 <            Thread.sleep(SHORT_DELAY_MS);
463 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
464 <            t1.interrupt();
465 <            Thread.sleep(SHORT_DELAY_MS);
466 <            assertTrue(sync.getSharedQueuedThreads().isEmpty());
467 <            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 <    }
450 >    public void testGetSharedQueuedThreads_Exclusive() {
451 >        final Mutex sync = new Mutex();
452 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
453 >        sync.acquire();
454 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
455 >        Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
456 >        waitForQueuedThread(sync, t1);
457 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
458 >        Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
459 >        waitForQueuedThread(sync, t2);
460 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
461 >        t1.interrupt();
462 >        awaitTermination(t1);
463 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
464 >        sync.release();
465 >        awaitTermination(t2);
466 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
467 >    }
468  
469      /**
470 <     * tryAcquireNanos is interruptible.
470 >     * getSharedQueuedThreads returns all shared waiting threads
471       */
472 <    public void testInterruptedException2() {
473 <        final Mutex sync = new Mutex();
474 <        sync.acquire(1);
475 <        Thread t = new Thread(new Runnable() {
476 <                public void run() {
477 <                    try {
478 <                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
479 <                        threadShouldThrow();
480 <                    } catch(InterruptedException success){}
481 <                }
482 <            });
483 <        try {
484 <            t.start();
485 <            t.interrupt();
486 <        } catch(Exception e){
487 <            unexpectedException();
488 <        }
472 >    public void testGetSharedQueuedThreads_Shared() {
473 >        final BooleanLatch l = new BooleanLatch();
474 >        assertHasSharedQueuedThreads(l, NO_THREADS);
475 >        Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
476 >            public void realRun() throws InterruptedException {
477 >                l.acquireSharedInterruptibly(0);
478 >            }});
479 >        waitForQueuedThread(l, t1);
480 >        assertHasSharedQueuedThreads(l, t1);
481 >        Thread t2 = newStartedThread(new CheckedRunnable() {
482 >            public void realRun() throws InterruptedException {
483 >                l.acquireSharedInterruptibly(0);
484 >            }});
485 >        waitForQueuedThread(l, t2);
486 >        assertHasSharedQueuedThreads(l, t1, t2);
487 >        t1.interrupt();
488 >        awaitTermination(t1);
489 >        assertHasSharedQueuedThreads(l, t2);
490 >        assertTrue(l.releaseShared(0));
491 >        awaitTermination(t2);
492 >        assertHasSharedQueuedThreads(l, NO_THREADS);
493      }
494  
495 +    /**
496 +     * tryAcquireNanos is interruptible
497 +     */
498 +    public void testTryAcquireNanos_Interruptible() {
499 +        final Mutex sync = new Mutex();
500 +        sync.acquire();
501 +        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
502 +            public void realRun() throws InterruptedException {
503 +                sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
504 +            }});
505 +
506 +        waitForQueuedThread(sync, t);
507 +        t.interrupt();
508 +        awaitTermination(t);
509 +    }
510  
511      /**
512 <     * TryAcquire on exclusively held sync fails
513 <     */
514 <    public void testTryAcquireWhenSynced() {
515 <        final Mutex sync = new Mutex();
516 <        sync.acquire(1);
517 <        Thread t = new Thread(new Runnable() {
518 <                public void run() {
519 <                    threadAssertFalse(sync.tryAcquire(1));
520 <                }
521 <            });
522 <        try {
523 <            t.start();
524 <            t.join();
394 <            sync.release(1);
395 <        } catch(Exception e){
396 <            unexpectedException();
397 <        }
398 <    }
512 >     * tryAcquire on exclusively held sync fails
513 >     */
514 >    public void testTryAcquireWhenSynced() {
515 >        final Mutex sync = new Mutex();
516 >        sync.acquire();
517 >        Thread t = newStartedThread(new CheckedRunnable() {
518 >            public void realRun() {
519 >                assertFalse(sync.tryAcquire());
520 >            }});
521 >
522 >        awaitTermination(t);
523 >        sync.release();
524 >    }
525  
526      /**
527       * tryAcquireNanos on an exclusively held sync times out
528       */
529 <    public void testAcquireNanos_Timeout() {
530 <        final Mutex sync = new Mutex();
531 <        sync.acquire(1);
532 <        Thread t = new Thread(new Runnable() {
533 <                public void run() {
534 <                    try {
535 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
536 <                    } catch (Exception ex) {
537 <                        threadUnexpectedException();
538 <                    }
539 <                }
540 <            });
541 <        try {
542 <            t.start();
543 <            t.join();
418 <            sync.release(1);
419 <        } catch(Exception e){
420 <            unexpectedException();
421 <        }
422 <    }
423 <    
424 <  
529 >    public void testAcquireNanos_Timeout() {
530 >        final Mutex sync = new Mutex();
531 >        sync.acquire();
532 >        Thread t = newStartedThread(new CheckedRunnable() {
533 >            public void realRun() throws InterruptedException {
534 >                long startTime = System.nanoTime();
535 >                long nanos = MILLISECONDS.toNanos(timeoutMillis());
536 >                assertFalse(sync.tryAcquireNanos(nanos));
537 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
538 >            }});
539 >
540 >        awaitTermination(t);
541 >        sync.release();
542 >    }
543 >
544      /**
545       * getState is true when acquired and false when not
546       */
547      public void testGetState() {
548 <        final Mutex sync = new Mutex();
549 <        sync.acquire(1);
550 <        assertTrue(sync.isHeldExclusively());
551 <        sync.release(1);
552 <        assertFalse(sync.isHeldExclusively());
553 <        Thread t = new Thread(new Runnable() {
554 <                public void run() {
555 <                    sync.acquire(1);
556 <                    try {
557 <                        Thread.sleep(SMALL_DELAY_MS);
558 <                    }
559 <                    catch(Exception e) {
560 <                        threadUnexpectedException();
561 <                    }
562 <                    sync.release(1);
563 <                }
564 <            });
565 <        try {
566 <            t.start();
567 <            Thread.sleep(SHORT_DELAY_MS);
568 <            assertTrue(sync.isHeldExclusively());
450 <            t.join();
451 <            assertFalse(sync.isHeldExclusively());
452 <        } catch(Exception e){
453 <            unexpectedException();
454 <        }
548 >        final Mutex sync = new Mutex();
549 >        sync.acquire();
550 >        assertTrue(sync.isHeldExclusively());
551 >        sync.release();
552 >        assertFalse(sync.isHeldExclusively());
553 >
554 >        final BooleanLatch acquired = new BooleanLatch();
555 >        final BooleanLatch done = new BooleanLatch();
556 >        Thread t = newStartedThread(new CheckedRunnable() {
557 >            public void realRun() throws InterruptedException {
558 >                sync.acquire();
559 >                assertTrue(acquired.releaseShared(0));
560 >                done.acquireShared(0);
561 >                sync.release();
562 >            }});
563 >
564 >        acquired.acquireShared(0);
565 >        assertTrue(sync.isHeldExclusively());
566 >        assertTrue(done.releaseShared(0));
567 >        awaitTermination(t);
568 >        assertFalse(sync.isHeldExclusively());
569      }
570  
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            sync.release(1);
470            t.join();
471        } catch(Exception e){
472            unexpectedException();
473        }
474    }
475
571      /**
572       * acquireInterruptibly succeeds when released, else is interruptible
573       */
574 <    public void testAcquireInterruptibly2() {
575 <        final Mutex sync = new Mutex();
576 <        try {
577 <            sync.acquireInterruptibly(1);
578 <        } catch(Exception e) {
579 <            unexpectedException();
580 <        }
581 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
582 <        try {
583 <            t.start();
584 <            t.interrupt();
585 <            assertTrue(sync.isHeldExclusively());
586 <            t.join();
587 <        } catch(Exception e){
588 <            unexpectedException();
494 <        }
574 >    public void testAcquireInterruptibly() throws InterruptedException {
575 >        final Mutex sync = new Mutex();
576 >        final BooleanLatch threadStarted = new BooleanLatch();
577 >        sync.acquireInterruptibly();
578 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
579 >            public void realRun() throws InterruptedException {
580 >                assertTrue(threadStarted.releaseShared(0));
581 >                sync.acquireInterruptibly();
582 >            }});
583 >
584 >        threadStarted.acquireShared(0);
585 >        waitForQueuedThread(sync, t);
586 >        t.interrupt();
587 >        awaitTermination(t);
588 >        assertTrue(sync.isHeldExclusively());
589      }
590  
591      /**
592       * owns is true for a condition created by sync else false
593       */
594      public void testOwns() {
595 <        final Mutex sync = new Mutex();
596 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
595 >        final Mutex sync = new Mutex();
596 >        final ConditionObject c = sync.newCondition();
597          final Mutex sync2 = new Mutex();
598          assertTrue(sync.owns(c));
599          assertFalse(sync2.owns(c));
# Line 508 | Line 602 | public class AbstractQueuedLongSynchroni
602      /**
603       * Calling await without holding sync throws IllegalMonitorStateException
604       */
605 <    public void testAwait_IllegalMonitor() {
606 <        final Mutex sync = new Mutex();
607 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
608 <        try {
609 <            c.await();
610 <            shouldThrow();
611 <        }
612 <        catch (IllegalMonitorStateException success) {
613 <        }
614 <        catch (Exception ex) {
615 <            unexpectedException();
605 >    public void testAwait_IMSE() {
606 >        final Mutex sync = new Mutex();
607 >        final ConditionObject c = sync.newCondition();
608 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
609 >            long startTime = System.nanoTime();
610 >            try {
611 >                await(c, awaitMethod);
612 >                shouldThrow();
613 >            } catch (IllegalMonitorStateException success) {
614 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
615 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
616          }
617      }
618  
619      /**
620       * Calling signal without holding sync throws IllegalMonitorStateException
621       */
622 <    public void testSignal_IllegalMonitor() {
623 <        final Mutex sync = new Mutex();
624 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
622 >    public void testSignal_IMSE() {
623 >        final Mutex sync = new Mutex();
624 >        final ConditionObject c = sync.newCondition();
625          try {
626              c.signal();
627              shouldThrow();
628 <        }
629 <        catch (IllegalMonitorStateException success) {
536 <        }
537 <        catch (Exception ex) {
538 <            unexpectedException();
539 <        }
628 >        } catch (IllegalMonitorStateException success) {}
629 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
630      }
631  
632      /**
633 <     * awaitNanos without a signal times out
633 >     * Calling signalAll without holding sync throws IllegalMonitorStateException
634       */
635 <    public void testAwaitNanos_Timeout() {
636 <        final Mutex sync = new Mutex();
637 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
635 >    public void testSignalAll_IMSE() {
636 >        final Mutex sync = new Mutex();
637 >        final ConditionObject c = sync.newCondition();
638          try {
639 <            sync.acquire(1);
640 <            long t = c.awaitNanos(100);
641 <            assertTrue(t <= 0);
552 <            sync.release(1);
553 <        }
554 <        catch (Exception ex) {
555 <            unexpectedException();
556 <        }
639 >            c.signalAll();
640 >            shouldThrow();
641 >        } catch (IllegalMonitorStateException success) {}
642      }
643  
644      /**
645 <     *  Timed await without a signal times out
645 >     * await/awaitNanos/awaitUntil without a signal times out
646       */
647 <    public void testAwait_Timeout() {
648 <        final Mutex sync = new Mutex();
649 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
650 <        try {
651 <            sync.acquire(1);
652 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
653 <            sync.release(1);
654 <        }
655 <        catch (Exception ex) {
571 <            unexpectedException();
572 <        }
647 >    public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); }
648 >    public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); }
649 >    public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); }
650 >    public void testAwait_Timeout(AwaitMethod awaitMethod) {
651 >        final Mutex sync = new Mutex();
652 >        final ConditionObject c = sync.newCondition();
653 >        sync.acquire();
654 >        assertAwaitTimesOut(c, awaitMethod);
655 >        sync.release();
656      }
657  
658      /**
659 <     * awaitUntil without a signal times out
660 <     */
661 <    public void testAwaitUntil_Timeout() {
662 <        final Mutex sync = new Mutex();
663 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
664 <        try {
665 <            sync.acquire(1);
666 <            java.util.Date d = new java.util.Date();
667 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
668 <            sync.release(1);
669 <        }
670 <        catch (Exception ex) {
671 <            unexpectedException();
672 <        }
659 >     * await/awaitNanos/awaitUntil returns when signalled
660 >     */
661 >    public void testSignal_await()      { testSignal(AwaitMethod.await); }
662 >    public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); }
663 >    public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); }
664 >    public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); }
665 >    public void testSignal(final AwaitMethod awaitMethod) {
666 >        final Mutex sync = new Mutex();
667 >        final ConditionObject c = sync.newCondition();
668 >        final BooleanLatch acquired = new BooleanLatch();
669 >        Thread t = newStartedThread(new CheckedRunnable() {
670 >            public void realRun() throws InterruptedException {
671 >                sync.acquire();
672 >                assertTrue(acquired.releaseShared(0));
673 >                await(c, awaitMethod);
674 >                sync.release();
675 >            }});
676 >
677 >        acquired.acquireShared(0);
678 >        sync.acquire();
679 >        assertHasWaitersLocked(sync, c, t);
680 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
681 >        c.signal();
682 >        assertHasWaitersLocked(sync, c, NO_THREADS);
683 >        assertHasExclusiveQueuedThreads(sync, t);
684 >        sync.release();
685 >        awaitTermination(t);
686      }
687  
688      /**
689 <     * await returns when signalled
594 <     */
595 <    public void testAwait() {
596 <        final Mutex sync = new Mutex();
597 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
598 <        Thread t = new Thread(new Runnable() {
599 <                public void run() {
600 <                    try {
601 <                        sync.acquire(1);
602 <                        c.await();
603 <                        sync.release(1);
604 <                    }
605 <                    catch(InterruptedException e) {
606 <                        threadUnexpectedException();
607 <                    }
608 <                }
609 <            });
610 <
611 <        try {
612 <            t.start();
613 <            Thread.sleep(SHORT_DELAY_MS);
614 <            sync.acquire(1);
615 <            c.signal();
616 <            sync.release(1);
617 <            t.join(SHORT_DELAY_MS);
618 <            assertFalse(t.isAlive());
619 <        }
620 <        catch (Exception ex) {
621 <            unexpectedException();
622 <        }
623 <    }
624 <
625 <
626 <
627 <    /**
628 <     * hasWaiters throws NPE if null
689 >     * hasWaiters(null) throws NullPointerException
690       */
691      public void testHasWaitersNPE() {
692 <        final Mutex sync = new Mutex();
692 >        final Mutex sync = new Mutex();
693          try {
694              sync.hasWaiters(null);
695              shouldThrow();
696 <        } catch (NullPointerException success) {
636 <        } catch (Exception ex) {
637 <            unexpectedException();
638 <        }
696 >        } catch (NullPointerException success) {}
697      }
698  
699      /**
700 <     * getWaitQueueLength throws NPE if null
700 >     * getWaitQueueLength(null) throws NullPointerException
701       */
702      public void testGetWaitQueueLengthNPE() {
703 <        final Mutex sync = new Mutex();
703 >        final Mutex sync = new Mutex();
704          try {
705              sync.getWaitQueueLength(null);
706              shouldThrow();
707 <        } catch (NullPointerException success) {
650 <        } catch (Exception ex) {
651 <            unexpectedException();
652 <        }
707 >        } catch (NullPointerException success) {}
708      }
709  
655
710      /**
711       * getWaitingThreads throws NPE if null
712       */
713      public void testGetWaitingThreadsNPE() {
714 <        final Mutex sync = new Mutex();
714 >        final Mutex sync = new Mutex();
715          try {
716              sync.getWaitingThreads(null);
717              shouldThrow();
718 <        } catch (NullPointerException success) {
665 <        } catch (Exception ex) {
666 <            unexpectedException();
667 <        }
718 >        } catch (NullPointerException success) {}
719      }
720  
670
721      /**
722 <     * hasWaiters throws IAE if not owned
722 >     * hasWaiters throws IllegalArgumentException if not owned
723       */
724      public void testHasWaitersIAE() {
725 <        final Mutex sync = new Mutex();
726 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
727 <        final Mutex sync2 = new Mutex();
725 >        final Mutex sync = new Mutex();
726 >        final ConditionObject c = sync.newCondition();
727 >        final Mutex sync2 = new Mutex();
728          try {
729              sync2.hasWaiters(c);
730              shouldThrow();
731 <        } catch (IllegalArgumentException success) {
732 <        } catch (Exception ex) {
683 <            unexpectedException();
684 <        }
731 >        } catch (IllegalArgumentException success) {}
732 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
733      }
734  
735      /**
736 <     * hasWaiters throws IMSE if not synced
736 >     * hasWaiters throws IllegalMonitorStateException if not synced
737       */
738      public void testHasWaitersIMSE() {
739 <        final Mutex sync = new Mutex();
740 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
739 >        final Mutex sync = new Mutex();
740 >        final ConditionObject c = sync.newCondition();
741          try {
742              sync.hasWaiters(c);
743              shouldThrow();
744 <        } catch (IllegalMonitorStateException success) {
745 <        } catch (Exception ex) {
698 <            unexpectedException();
699 <        }
744 >        } catch (IllegalMonitorStateException success) {}
745 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
746      }
747  
702
748      /**
749 <     * getWaitQueueLength throws IAE if not owned
749 >     * getWaitQueueLength throws IllegalArgumentException if not owned
750       */
751      public void testGetWaitQueueLengthIAE() {
752 <        final Mutex sync = new Mutex();
753 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
754 <        final Mutex sync2 = new Mutex();
752 >        final Mutex sync = new Mutex();
753 >        final ConditionObject c = sync.newCondition();
754 >        final Mutex sync2 = new Mutex();
755          try {
756              sync2.getWaitQueueLength(c);
757              shouldThrow();
758 <        } catch (IllegalArgumentException success) {
759 <        } catch (Exception ex) {
715 <            unexpectedException();
716 <        }
758 >        } catch (IllegalArgumentException success) {}
759 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
760      }
761  
762      /**
763 <     * getWaitQueueLength throws IMSE if not synced
763 >     * getWaitQueueLength throws IllegalMonitorStateException if not synced
764       */
765      public void testGetWaitQueueLengthIMSE() {
766 <        final Mutex sync = new Mutex();
767 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
766 >        final Mutex sync = new Mutex();
767 >        final ConditionObject c = sync.newCondition();
768          try {
769              sync.getWaitQueueLength(c);
770              shouldThrow();
771 <        } catch (IllegalMonitorStateException success) {
772 <        } catch (Exception ex) {
730 <            unexpectedException();
731 <        }
771 >        } catch (IllegalMonitorStateException success) {}
772 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
773      }
774  
734
775      /**
776 <     * getWaitingThreads throws IAE if not owned
776 >     * getWaitingThreads throws IllegalArgumentException if not owned
777       */
778      public void testGetWaitingThreadsIAE() {
779 <        final Mutex sync = new Mutex();
780 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
781 <        final Mutex sync2 = new Mutex();        
779 >        final Mutex sync = new Mutex();
780 >        final ConditionObject c = sync.newCondition();
781 >        final Mutex sync2 = new Mutex();
782          try {
783              sync2.getWaitingThreads(c);
784              shouldThrow();
785 <        } catch (IllegalArgumentException success) {
786 <        } catch (Exception ex) {
747 <            unexpectedException();
748 <        }
785 >        } catch (IllegalArgumentException success) {}
786 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
787      }
788  
789      /**
790 <     * getWaitingThreads throws IMSE if not synced
790 >     * getWaitingThreads throws IllegalMonitorStateException if not synced
791       */
792      public void testGetWaitingThreadsIMSE() {
793 <        final Mutex sync = new Mutex();
794 <        final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition());
793 >        final Mutex sync = new Mutex();
794 >        final ConditionObject c = sync.newCondition();
795          try {
796              sync.getWaitingThreads(c);
797              shouldThrow();
798 <        } catch (IllegalMonitorStateException success) {
799 <        } catch (Exception ex) {
762 <            unexpectedException();
763 <        }
798 >        } catch (IllegalMonitorStateException success) {}
799 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
800      }
801  
766
767
802      /**
803       * hasWaiters returns true when a thread is waiting, else false
804       */
805      public void testHasWaiters() {
806 <        final Mutex sync = new Mutex();
807 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
808 <        Thread t = new Thread(new Runnable() {
809 <                public void run() {
810 <                    try {
811 <                        sync.acquire(1);
812 <                        threadAssertFalse(sync.hasWaiters(c));
813 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
814 <                        c.await();
815 <                        sync.release(1);
816 <                    }
817 <                    catch(InterruptedException e) {
818 <                        threadUnexpectedException();
819 <                    }
820 <                }
821 <            });
806 >        final Mutex sync = new Mutex();
807 >        final ConditionObject c = sync.newCondition();
808 >        final BooleanLatch acquired = new BooleanLatch();
809 >        Thread t = newStartedThread(new CheckedRunnable() {
810 >            public void realRun() throws InterruptedException {
811 >                sync.acquire();
812 >                assertHasWaitersLocked(sync, c, NO_THREADS);
813 >                assertFalse(sync.hasWaiters(c));
814 >                assertTrue(acquired.releaseShared(0));
815 >                c.await();
816 >                sync.release();
817 >            }});
818 >
819 >        acquired.acquireShared(0);
820 >        sync.acquire();
821 >        assertHasWaitersLocked(sync, c, t);
822 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
823 >        assertTrue(sync.hasWaiters(c));
824 >        c.signal();
825 >        assertHasWaitersLocked(sync, c, NO_THREADS);
826 >        assertHasExclusiveQueuedThreads(sync, t);
827 >        assertFalse(sync.hasWaiters(c));
828 >        sync.release();
829  
830 <        try {
831 <            t.start();
791 <            Thread.sleep(SHORT_DELAY_MS);
792 <            sync.acquire(1);
793 <            assertTrue(sync.hasWaiters(c));
794 <            assertEquals(1, sync.getWaitQueueLength(c));
795 <            c.signal();
796 <            sync.release(1);
797 <            Thread.sleep(SHORT_DELAY_MS);
798 <            sync.acquire(1);
799 <            assertFalse(sync.hasWaiters(c));
800 <            assertEquals(0, sync.getWaitQueueLength(c));
801 <            sync.release(1);
802 <            t.join(SHORT_DELAY_MS);
803 <            assertFalse(t.isAlive());
804 <        }
805 <        catch (Exception ex) {
806 <            unexpectedException();
807 <        }
830 >        awaitTermination(t);
831 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
832      }
833  
834      /**
835       * getWaitQueueLength returns number of waiting threads
836       */
837      public void testGetWaitQueueLength() {
838 <        final Mutex sync = new Mutex();
839 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
840 <        Thread t1 = new Thread(new Runnable() {
841 <                public void run() {
842 <                    try {
843 <                        sync.acquire(1);
844 <                        threadAssertFalse(sync.hasWaiters(c));
845 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
846 <                        c.await();
847 <                        sync.release(1);
848 <                    }
849 <                    catch(InterruptedException e) {
850 <                        threadUnexpectedException();
851 <                    }
852 <                }
853 <            });
854 <
855 <        Thread t2 = new Thread(new Runnable() {
856 <                public void run() {
857 <                    try {
858 <                        sync.acquire(1);
859 <                        threadAssertTrue(sync.hasWaiters(c));
860 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
861 <                        c.await();
862 <                        sync.release(1);
863 <                    }
864 <                    catch(InterruptedException e) {
865 <                        threadUnexpectedException();
866 <                    }
867 <                }
868 <            });
869 <
870 <        try {
871 <            t1.start();
872 <            Thread.sleep(SHORT_DELAY_MS);
873 <            t2.start();
874 <            Thread.sleep(SHORT_DELAY_MS);
875 <            sync.acquire(1);
876 <            assertTrue(sync.hasWaiters(c));
877 <            assertEquals(2, sync.getWaitQueueLength(c));
878 <            c.signalAll();
879 <            sync.release(1);
856 <            Thread.sleep(SHORT_DELAY_MS);
857 <            sync.acquire(1);
858 <            assertFalse(sync.hasWaiters(c));
859 <            assertEquals(0, sync.getWaitQueueLength(c));
860 <            sync.release(1);
861 <            t1.join(SHORT_DELAY_MS);
862 <            t2.join(SHORT_DELAY_MS);
863 <            assertFalse(t1.isAlive());
864 <            assertFalse(t2.isAlive());
865 <        }
866 <        catch (Exception ex) {
867 <            unexpectedException();
868 <        }
838 >        final Mutex sync = new Mutex();
839 >        final ConditionObject c = sync.newCondition();
840 >        final BooleanLatch acquired1 = new BooleanLatch();
841 >        final BooleanLatch acquired2 = new BooleanLatch();
842 >        final Thread t1 = newStartedThread(new CheckedRunnable() {
843 >            public void realRun() throws InterruptedException {
844 >                sync.acquire();
845 >                assertHasWaitersLocked(sync, c, NO_THREADS);
846 >                assertEquals(0, sync.getWaitQueueLength(c));
847 >                assertTrue(acquired1.releaseShared(0));
848 >                c.await();
849 >                sync.release();
850 >            }});
851 >        acquired1.acquireShared(0);
852 >        sync.acquire();
853 >        assertHasWaitersLocked(sync, c, t1);
854 >        assertEquals(1, sync.getWaitQueueLength(c));
855 >        sync.release();
856 >
857 >        final Thread t2 = newStartedThread(new CheckedRunnable() {
858 >            public void realRun() throws InterruptedException {
859 >                sync.acquire();
860 >                assertHasWaitersLocked(sync, c, t1);
861 >                assertEquals(1, sync.getWaitQueueLength(c));
862 >                assertTrue(acquired2.releaseShared(0));
863 >                c.await();
864 >                sync.release();
865 >            }});
866 >        acquired2.acquireShared(0);
867 >        sync.acquire();
868 >        assertHasWaitersLocked(sync, c, t1, t2);
869 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
870 >        assertEquals(2, sync.getWaitQueueLength(c));
871 >        c.signalAll();
872 >        assertHasWaitersLocked(sync, c, NO_THREADS);
873 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
874 >        assertEquals(0, sync.getWaitQueueLength(c));
875 >        sync.release();
876 >
877 >        awaitTermination(t1);
878 >        awaitTermination(t2);
879 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
880      }
881  
882      /**
883       * getWaitingThreads returns only and all waiting threads
884       */
885      public void testGetWaitingThreads() {
886 <        final Mutex sync = new Mutex();
887 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
888 <        Thread t1 = new Thread(new Runnable() {
889 <                public void run() {
890 <                    try {
891 <                        sync.acquire(1);
892 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
893 <                        c.await();
894 <                        sync.release(1);
895 <                    }
896 <                    catch(InterruptedException e) {
897 <                        threadUnexpectedException();
898 <                    }
899 <                }
900 <            });
901 <
902 <        Thread t2 = new Thread(new Runnable() {
903 <                public void run() {
904 <                    try {
905 <                        sync.acquire(1);
906 <                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
907 <                        c.await();
908 <                        sync.release(1);
909 <                    }
910 <                    catch(InterruptedException e) {
911 <                        threadUnexpectedException();
912 <                    }
913 <                }
914 <            });
915 <
916 <        try {
917 <            sync.acquire(1);
918 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
919 <            sync.release(1);
920 <            t1.start();
921 <            Thread.sleep(SHORT_DELAY_MS);
922 <            t2.start();
923 <            Thread.sleep(SHORT_DELAY_MS);
924 <            sync.acquire(1);
925 <            assertTrue(sync.hasWaiters(c));
926 <            assertTrue(sync.getWaitingThreads(c).contains(t1));
927 <            assertTrue(sync.getWaitingThreads(c).contains(t2));
928 <            c.signalAll();
929 <            sync.release(1);
930 <            Thread.sleep(SHORT_DELAY_MS);
931 <            sync.acquire(1);
932 <            assertFalse(sync.hasWaiters(c));
933 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
934 <            sync.release(1);
935 <            t1.join(SHORT_DELAY_MS);
936 <            t2.join(SHORT_DELAY_MS);
937 <            assertFalse(t1.isAlive());
938 <            assertFalse(t2.isAlive());
939 <        }
940 <        catch (Exception ex) {
941 <            unexpectedException();
942 <        }
886 >        final Mutex sync = new Mutex();
887 >        final ConditionObject c = sync.newCondition();
888 >        final BooleanLatch acquired1 = new BooleanLatch();
889 >        final BooleanLatch acquired2 = new BooleanLatch();
890 >        final Thread t1 = new Thread(new CheckedRunnable() {
891 >            public void realRun() throws InterruptedException {
892 >                sync.acquire();
893 >                assertHasWaitersLocked(sync, c, NO_THREADS);
894 >                assertTrue(sync.getWaitingThreads(c).isEmpty());
895 >                assertTrue(acquired1.releaseShared(0));
896 >                c.await();
897 >                sync.release();
898 >            }});
899 >
900 >        final Thread t2 = new Thread(new CheckedRunnable() {
901 >            public void realRun() throws InterruptedException {
902 >                sync.acquire();
903 >                assertHasWaitersLocked(sync, c, t1);
904 >                assertTrue(sync.getWaitingThreads(c).contains(t1));
905 >                assertFalse(sync.getWaitingThreads(c).isEmpty());
906 >                assertEquals(1, sync.getWaitingThreads(c).size());
907 >                assertTrue(acquired2.releaseShared(0));
908 >                c.await();
909 >                sync.release();
910 >            }});
911 >
912 >        sync.acquire();
913 >        assertHasWaitersLocked(sync, c, NO_THREADS);
914 >        assertFalse(sync.getWaitingThreads(c).contains(t1));
915 >        assertFalse(sync.getWaitingThreads(c).contains(t2));
916 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
917 >        assertEquals(0, sync.getWaitingThreads(c).size());
918 >        sync.release();
919 >
920 >        t1.start();
921 >        acquired1.acquireShared(0);
922 >        sync.acquire();
923 >        assertHasWaitersLocked(sync, c, t1);
924 >        assertTrue(sync.getWaitingThreads(c).contains(t1));
925 >        assertFalse(sync.getWaitingThreads(c).contains(t2));
926 >        assertFalse(sync.getWaitingThreads(c).isEmpty());
927 >        assertEquals(1, sync.getWaitingThreads(c).size());
928 >        sync.release();
929 >
930 >        t2.start();
931 >        acquired2.acquireShared(0);
932 >        sync.acquire();
933 >        assertHasWaitersLocked(sync, c, t1, t2);
934 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
935 >        assertTrue(sync.getWaitingThreads(c).contains(t1));
936 >        assertTrue(sync.getWaitingThreads(c).contains(t2));
937 >        assertFalse(sync.getWaitingThreads(c).isEmpty());
938 >        assertEquals(2, sync.getWaitingThreads(c).size());
939 >        c.signalAll();
940 >        assertHasWaitersLocked(sync, c, NO_THREADS);
941 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
942 >        assertFalse(sync.getWaitingThreads(c).contains(t1));
943 >        assertFalse(sync.getWaitingThreads(c).contains(t2));
944 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
945 >        assertEquals(0, sync.getWaitingThreads(c).size());
946 >        sync.release();
947 >
948 >        awaitTermination(t1);
949 >        awaitTermination(t2);
950 >        assertHasWaitersUnlocked(sync, c, NO_THREADS);
951      }
952  
934
935
953      /**
954 <     * awaitUninterruptibly doesn't abort on interrupt
954 >     * awaitUninterruptibly is uninterruptible
955       */
956      public void testAwaitUninterruptibly() {
957 <        final Mutex sync = new Mutex();
958 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
959 <        Thread t = new Thread(new Runnable() {
960 <                public void run() {
961 <                    sync.acquire(1);
962 <                    c.awaitUninterruptibly();
963 <                    sync.release(1);
964 <                }
965 <            });
966 <
967 <        try {
968 <            t.start();
969 <            Thread.sleep(SHORT_DELAY_MS);
970 <            t.interrupt();
971 <            sync.acquire(1);
972 <            c.signal();
973 <            sync.release(1);
974 <            t.join(SHORT_DELAY_MS);
975 <            assertFalse(t.isAlive());
976 <        }
977 <        catch (Exception ex) {
978 <            unexpectedException();
979 <        }
957 >        final Mutex sync = new Mutex();
958 >        final ConditionObject c = sync.newCondition();
959 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
960 >        Thread t = newStartedThread(new CheckedRunnable() {
961 >            public void realRun() {
962 >                sync.acquire();
963 >                assertTrue(pleaseInterrupt.releaseShared(0));
964 >                c.awaitUninterruptibly();
965 >                assertTrue(Thread.interrupted());
966 >                assertHasWaitersLocked(sync, c, NO_THREADS);
967 >                sync.release();
968 >            }});
969 >
970 >        pleaseInterrupt.acquireShared(0);
971 >        sync.acquire();
972 >        assertHasWaitersLocked(sync, c, t);
973 >        sync.release();
974 >        t.interrupt();
975 >        assertHasWaitersUnlocked(sync, c, t);
976 >        assertThreadStaysAlive(t);
977 >        sync.acquire();
978 >        assertHasWaitersLocked(sync, c, t);
979 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
980 >        c.signal();
981 >        assertHasWaitersLocked(sync, c, NO_THREADS);
982 >        assertHasExclusiveQueuedThreads(sync, t);
983 >        sync.release();
984 >        awaitTermination(t);
985      }
986  
987      /**
988 <     * await is interruptible
989 <     */
990 <    public void testAwait_Interrupt() {
991 <        final Mutex sync = new Mutex();
992 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
993 <        Thread t = new Thread(new Runnable() {
994 <                public void run() {
995 <                    try {
996 <                        sync.acquire(1);
997 <                        c.await();
998 <                        sync.release(1);
999 <                        threadShouldThrow();
1000 <                    }
1001 <                    catch(InterruptedException success) {
1002 <                    }
1003 <                }
1004 <            });
1005 <
1006 <        try {
1007 <            t.start();
986 <            Thread.sleep(SHORT_DELAY_MS);
987 <            t.interrupt();
988 <            t.join(SHORT_DELAY_MS);
989 <            assertFalse(t.isAlive());
990 <        }
991 <        catch (Exception ex) {
992 <            unexpectedException();
993 <        }
994 <    }
995 <
996 <    /**
997 <     * awaitNanos is interruptible
998 <     */
999 <    public void testAwaitNanos_Interrupt() {
1000 <        final Mutex sync = new Mutex();
1001 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1002 <        Thread t = new Thread(new Runnable() {
1003 <                public void run() {
1004 <                    try {
1005 <                        sync.acquire(1);
1006 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1007 <                        sync.release(1);
1008 <                        threadShouldThrow();
1009 <                    }
1010 <                    catch(InterruptedException success) {
1011 <                    }
1012 <                }
1013 <            });
1014 <
1015 <        try {
1016 <            t.start();
1017 <            Thread.sleep(SHORT_DELAY_MS);
1018 <            t.interrupt();
1019 <            t.join(SHORT_DELAY_MS);
1020 <            assertFalse(t.isAlive());
1021 <        }
1022 <        catch (Exception ex) {
1023 <            unexpectedException();
1024 <        }
1025 <    }
1026 <
1027 <    /**
1028 <     * awaitUntil is interruptible
1029 <     */
1030 <    public void testAwaitUntil_Interrupt() {
1031 <        final Mutex sync = new Mutex();
1032 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1033 <        Thread t = new Thread(new Runnable() {
1034 <                public void run() {
1035 <                    try {
1036 <                        sync.acquire(1);
1037 <                        java.util.Date d = new java.util.Date();
1038 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1039 <                        sync.release(1);
1040 <                        threadShouldThrow();
1041 <                    }
1042 <                    catch(InterruptedException success) {
1043 <                    }
1044 <                }
1045 <            });
1046 <
1047 <        try {
1048 <            t.start();
1049 <            Thread.sleep(SHORT_DELAY_MS);
1050 <            t.interrupt();
1051 <            t.join(SHORT_DELAY_MS);
1052 <            assertFalse(t.isAlive());
1053 <        }
1054 <        catch (Exception ex) {
1055 <            unexpectedException();
1056 <        }
988 >     * await/awaitNanos/awaitUntil is interruptible
989 >     */
990 >    public void testInterruptible_await()      { testInterruptible(AwaitMethod.await); }
991 >    public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); }
992 >    public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); }
993 >    public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); }
994 >    public void testInterruptible(final AwaitMethod awaitMethod) {
995 >        final Mutex sync = new Mutex();
996 >        final ConditionObject c = sync.newCondition();
997 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
998 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
999 >            public void realRun() throws InterruptedException {
1000 >                sync.acquire();
1001 >                assertTrue(pleaseInterrupt.releaseShared(0));
1002 >                await(c, awaitMethod);
1003 >            }});
1004 >
1005 >        pleaseInterrupt.acquireShared(0);
1006 >        t.interrupt();
1007 >        awaitTermination(t);
1008      }
1009  
1010      /**
1011       * signalAll wakes up all threads
1012       */
1013 <    public void testSignalAll() {
1014 <        final Mutex sync = new Mutex();
1015 <        final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
1016 <        Thread t1 = new Thread(new Runnable() {
1017 <                public void run() {
1018 <                    try {
1019 <                        sync.acquire(1);
1020 <                        c.await();
1021 <                        sync.release(1);
1022 <                    }
1023 <                    catch(InterruptedException e) {
1024 <                        threadUnexpectedException();
1025 <                    }
1026 <                }
1027 <            });
1028 <
1029 <        Thread t2 = new Thread(new Runnable() {
1030 <                public void run() {
1031 <                    try {
1032 <                        sync.acquire(1);
1033 <                        c.await();
1034 <                        sync.release(1);
1035 <                    }
1036 <                    catch(InterruptedException e) {
1037 <                        threadUnexpectedException();
1038 <                    }
1039 <                }
1040 <            });
1041 <
1042 <        try {
1043 <            t1.start();
1044 <            t2.start();
1045 <            Thread.sleep(SHORT_DELAY_MS);
1046 <            sync.acquire(1);
1047 <            c.signalAll();
1048 <            sync.release(1);
1098 <            t1.join(SHORT_DELAY_MS);
1099 <            t2.join(SHORT_DELAY_MS);
1100 <            assertFalse(t1.isAlive());
1101 <            assertFalse(t2.isAlive());
1102 <        }
1103 <        catch (Exception ex) {
1104 <            unexpectedException();
1105 <        }
1013 >    public void testSignalAll_await()      { testSignalAll(AwaitMethod.await); }
1014 >    public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); }
1015 >    public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); }
1016 >    public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); }
1017 >    public void testSignalAll(final AwaitMethod awaitMethod) {
1018 >        final Mutex sync = new Mutex();
1019 >        final ConditionObject c = sync.newCondition();
1020 >        final BooleanLatch acquired1 = new BooleanLatch();
1021 >        final BooleanLatch acquired2 = new BooleanLatch();
1022 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1023 >            public void realRun() throws InterruptedException {
1024 >                sync.acquire();
1025 >                acquired1.releaseShared(0);
1026 >                await(c, awaitMethod);
1027 >                sync.release();
1028 >            }});
1029 >
1030 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1031 >            public void realRun() throws InterruptedException {
1032 >                sync.acquire();
1033 >                acquired2.releaseShared(0);
1034 >                await(c, awaitMethod);
1035 >                sync.release();
1036 >            }});
1037 >
1038 >        acquired1.acquireShared(0);
1039 >        acquired2.acquireShared(0);
1040 >        sync.acquire();
1041 >        assertHasWaitersLocked(sync, c, t1, t2);
1042 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1043 >        c.signalAll();
1044 >        assertHasWaitersLocked(sync, c, NO_THREADS);
1045 >        assertHasExclusiveQueuedThreads(sync, t1, t2);
1046 >        sync.release();
1047 >        awaitTermination(t1);
1048 >        awaitTermination(t2);
1049      }
1050  
1108
1051      /**
1052       * toString indicates current state
1053       */
1054      public void testToString() {
1055          Mutex sync = new Mutex();
1056 <        String us = sync.toString();
1057 <        assertTrue(us.indexOf("State = 0") >= 0);
1058 <        sync.acquire(1);
1117 <        String ls = sync.toString();
1118 <        assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0);
1056 >        assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1057 >        sync.acquire();
1058 >        assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
1059      }
1060  
1061      /**
1062 <     * A serialized AQS deserializes with current state
1062 >     * A serialized AQS deserializes with current state, but no queued threads
1063       */
1064      public void testSerialization() {
1065 <        Mutex l = new Mutex();
1066 <        l.acquire(1);
1067 <        assertTrue(l.isHeldExclusively());
1068 <
1069 <        try {
1070 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1071 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1072 <            out.writeObject(l);
1073 <            out.close();
1074 <
1075 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1076 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1077 <            Mutex r = (Mutex) in.readObject();
1078 <            assertTrue(r.isHeldExclusively());
1079 <        } catch(Exception e){
1080 <            e.printStackTrace();
1081 <            unexpectedException();
1082 <        }
1065 >        Mutex sync = new Mutex();
1066 >        assertFalse(serialClone(sync).isHeldExclusively());
1067 >        sync.acquire();
1068 >        Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1069 >        waitForQueuedThread(sync, t);
1070 >        assertTrue(sync.isHeldExclusively());
1071 >
1072 >        Mutex clone = serialClone(sync);
1073 >        assertTrue(clone.isHeldExclusively());
1074 >        assertHasExclusiveQueuedThreads(sync, t);
1075 >        assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1076 >        t.interrupt();
1077 >        awaitTermination(t);
1078 >        sync.release();
1079 >        assertFalse(sync.isHeldExclusively());
1080 >        assertTrue(clone.isHeldExclusively());
1081 >        assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1082 >        assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1083      }
1084  
1145
1085      /**
1086       * tryReleaseShared setting state changes getState
1087       */
1088      public void testGetStateWithReleaseShared() {
1089 <        final BooleanLatch l = new BooleanLatch();
1090 <        assertFalse(l.isSignalled());
1091 <        l.releaseShared(0);
1092 <        assertTrue(l.isSignalled());
1089 >        final BooleanLatch l = new BooleanLatch();
1090 >        assertFalse(l.isSignalled());
1091 >        assertTrue(l.releaseShared(0));
1092 >        assertTrue(l.isSignalled());
1093      }
1094  
1095      /**
1096       * releaseShared has no effect when already signalled
1097       */
1098      public void testReleaseShared() {
1099 <        final BooleanLatch l = new BooleanLatch();
1100 <        assertFalse(l.isSignalled());
1101 <        l.releaseShared(0);
1102 <        assertTrue(l.isSignalled());
1103 <        l.releaseShared(0);
1104 <        assertTrue(l.isSignalled());
1099 >        final BooleanLatch l = new BooleanLatch();
1100 >        assertFalse(l.isSignalled());
1101 >        assertTrue(l.releaseShared(0));
1102 >        assertTrue(l.isSignalled());
1103 >        assertTrue(l.releaseShared(0));
1104 >        assertTrue(l.isSignalled());
1105      }
1106  
1107      /**
1108       * acquireSharedInterruptibly returns after release, but not before
1109       */
1110      public void testAcquireSharedInterruptibly() {
1111 <        final BooleanLatch l = new BooleanLatch();
1111 >        final BooleanLatch l = new BooleanLatch();
1112  
1113 <        Thread t = new Thread(new Runnable() {
1114 <                public void run() {
1115 <                    try {
1116 <                        threadAssertFalse(l.isSignalled());
1117 <                        l.acquireSharedInterruptibly(0);
1118 <                        threadAssertTrue(l.isSignalled());
1119 <                    } catch(InterruptedException e){
1120 <                        threadUnexpectedException();
1121 <                    }
1122 <                }
1123 <            });
1124 <        try {
1125 <            t.start();
1126 <            assertFalse(l.isSignalled());
1127 <            Thread.sleep(SHORT_DELAY_MS);
1128 <            l.releaseShared(0);
1190 <            assertTrue(l.isSignalled());
1191 <            t.join();
1192 <        } catch (InterruptedException e){
1193 <            unexpectedException();
1194 <        }
1113 >        Thread t = newStartedThread(new CheckedRunnable() {
1114 >            public void realRun() throws InterruptedException {
1115 >                assertFalse(l.isSignalled());
1116 >                l.acquireSharedInterruptibly(0);
1117 >                assertTrue(l.isSignalled());
1118 >                l.acquireSharedInterruptibly(0);
1119 >                assertTrue(l.isSignalled());
1120 >            }});
1121 >
1122 >        waitForQueuedThread(l, t);
1123 >        assertFalse(l.isSignalled());
1124 >        assertThreadStaysAlive(t);
1125 >        assertHasSharedQueuedThreads(l, t);
1126 >        assertTrue(l.releaseShared(0));
1127 >        assertTrue(l.isSignalled());
1128 >        awaitTermination(t);
1129      }
1196    
1130  
1131      /**
1132 <     * acquireSharedTimed returns after release
1132 >     * tryAcquireSharedNanos returns after release, but not before
1133       */
1134 <    public void testAsquireSharedTimed() {
1135 <        final BooleanLatch l = new BooleanLatch();
1134 >    public void testTryAcquireSharedNanos() {
1135 >        final BooleanLatch l = new BooleanLatch();
1136  
1137 <        Thread t = new Thread(new Runnable() {
1138 <                public void run() {
1139 <                    try {
1140 <                        threadAssertFalse(l.isSignalled());
1141 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
1142 <                        threadAssertTrue(l.isSignalled());
1143 <
1144 <                    } catch(InterruptedException e){
1145 <                        threadUnexpectedException();
1146 <                    }
1147 <                }
1148 <            });
1149 <        try {
1150 <            t.start();
1151 <            assertFalse(l.isSignalled());
1152 <            Thread.sleep(SHORT_DELAY_MS);
1220 <            l.releaseShared(0);
1221 <            assertTrue(l.isSignalled());
1222 <            t.join();
1223 <        } catch (InterruptedException e){
1224 <            unexpectedException();
1225 <        }
1137 >        Thread t = newStartedThread(new CheckedRunnable() {
1138 >            public void realRun() throws InterruptedException {
1139 >                assertFalse(l.isSignalled());
1140 >                long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1141 >                assertTrue(l.tryAcquireSharedNanos(0, nanos));
1142 >                assertTrue(l.isSignalled());
1143 >                assertTrue(l.tryAcquireSharedNanos(0, nanos));
1144 >                assertTrue(l.isSignalled());
1145 >            }});
1146 >
1147 >        waitForQueuedThread(l, t);
1148 >        assertFalse(l.isSignalled());
1149 >        assertThreadStaysAlive(t);
1150 >        assertTrue(l.releaseShared(0));
1151 >        assertTrue(l.isSignalled());
1152 >        awaitTermination(t);
1153      }
1154 <    
1154 >
1155      /**
1156 <     * acquireSharedInterruptibly throws IE if interrupted before released
1156 >     * acquireSharedInterruptibly is interruptible
1157       */
1158 <    public void testAcquireSharedInterruptibly_InterruptedException() {
1158 >    public void testAcquireSharedInterruptibly_Interruptible() {
1159          final BooleanLatch l = new BooleanLatch();
1160 <        Thread t = new Thread(new Runnable() {
1161 <                public void run() {
1162 <                    try {
1163 <                        threadAssertFalse(l.isSignalled());
1164 <                        l.acquireSharedInterruptibly(0);
1165 <                        threadShouldThrow();
1166 <                    } catch(InterruptedException success){}
1167 <                }
1168 <            });
1169 <        t.start();
1170 <        try {
1244 <            assertFalse(l.isSignalled());
1245 <            t.interrupt();
1246 <            t.join();
1247 <        } catch (InterruptedException e){
1248 <            unexpectedException();
1249 <        }
1160 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1161 >            public void realRun() throws InterruptedException {
1162 >                assertFalse(l.isSignalled());
1163 >                l.acquireSharedInterruptibly(0);
1164 >            }});
1165 >
1166 >        waitForQueuedThread(l, t);
1167 >        assertFalse(l.isSignalled());
1168 >        t.interrupt();
1169 >        awaitTermination(t);
1170 >        assertFalse(l.isSignalled());
1171      }
1172  
1173      /**
1174 <     * acquireSharedTimed throws IE if interrupted before released
1174 >     * tryAcquireSharedNanos is interruptible
1175       */
1176 <    public void testAcquireSharedNanos_InterruptedException() {
1176 >    public void testTryAcquireSharedNanos_Interruptible() {
1177          final BooleanLatch l = new BooleanLatch();
1178 <        Thread t = new Thread(new Runnable() {
1179 <                public void run() {
1180 <                    try {
1181 <                        threadAssertFalse(l.isSignalled());
1182 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
1183 <                        threadShouldThrow();                        
1184 <                    } catch(InterruptedException success){}
1185 <                }
1186 <            });
1187 <        t.start();
1188 <        try {
1189 <            Thread.sleep(SHORT_DELAY_MS);
1269 <            assertFalse(l.isSignalled());
1270 <            t.interrupt();
1271 <            t.join();
1272 <        } catch (InterruptedException e){
1273 <            unexpectedException();
1274 <        }
1178 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1179 >            public void realRun() throws InterruptedException {
1180 >                assertFalse(l.isSignalled());
1181 >                long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1182 >                l.tryAcquireSharedNanos(0, nanos);
1183 >            }});
1184 >
1185 >        waitForQueuedThread(l, t);
1186 >        assertFalse(l.isSignalled());
1187 >        t.interrupt();
1188 >        awaitTermination(t);
1189 >        assertFalse(l.isSignalled());
1190      }
1191  
1192      /**
1193 <     * acquireSharedTimed times out if not released before timeout
1193 >     * tryAcquireSharedNanos times out if not released before timeout
1194       */
1195 <    public void testAcquireSharedNanos_Timeout() {
1195 >    public void testTryAcquireSharedNanos_Timeout() {
1196          final BooleanLatch l = new BooleanLatch();
1197 <        Thread t = new Thread(new Runnable() {
1198 <                public void run() {
1199 <                    try {
1200 <                        threadAssertFalse(l.isSignalled());
1201 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1202 <                    } catch(InterruptedException ie){
1203 <                        threadUnexpectedException();
1204 <                    }
1197 >        final BooleanLatch observedQueued = new BooleanLatch();
1198 >        final long timeoutMillis = timeoutMillis();
1199 >        Thread t = newStartedThread(new CheckedRunnable() {
1200 >            public void realRun() throws InterruptedException {
1201 >                assertFalse(l.isSignalled());
1202 >                for (long millis = timeoutMillis();
1203 >                     !observedQueued.isSignalled();
1204 >                     millis *= 2) {
1205 >                    long nanos = MILLISECONDS.toNanos(millis);
1206 >                    long startTime = System.nanoTime();
1207 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1208 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1209                  }
1210 <            });
1211 <        t.start();
1212 <        try {
1213 <            Thread.sleep(SHORT_DELAY_MS);
1214 <            assertFalse(l.isSignalled());
1215 <            t.join();
1216 <        } catch (InterruptedException e){
1217 <            unexpectedException();
1218 <        }
1210 >                assertFalse(l.isSignalled());
1211 >            }});
1212 >
1213 >        waitForQueuedThread(l, t);
1214 >        observedQueued.releaseShared(0);
1215 >        assertFalse(l.isSignalled());
1216 >        awaitTermination(t);
1217 >        assertFalse(l.isSignalled());
1218 >    }
1219 >
1220 >    /**
1221 >     * awaitNanos/timed await with 0 wait times out immediately
1222 >     */
1223 >    public void testAwait_Zero() throws InterruptedException {
1224 >        final Mutex sync = new Mutex();
1225 >        final ConditionObject c = sync.newCondition();
1226 >        sync.acquire();
1227 >        assertTrue(c.awaitNanos(0L) <= 0);
1228 >        assertFalse(c.await(0L, NANOSECONDS));
1229 >        sync.release();
1230 >    }
1231 >
1232 >    /**
1233 >     * awaitNanos/timed await with maximum negative wait times does not underflow
1234 >     */
1235 >    public void testAwait_NegativeInfinity() throws InterruptedException {
1236 >        final Mutex sync = new Mutex();
1237 >        final ConditionObject c = sync.newCondition();
1238 >        sync.acquire();
1239 >        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1240 >        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1241 >        sync.release();
1242      }
1243  
1302    
1244   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines