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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines