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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines