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.11 by dl, Thu Jan 8 01:29:46 2004 UTC vs.
Revision 1.17 by dl, Mon Jan 12 16:37:40 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines