--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/10 20:37:20 1.15 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/11 16:02:33 1.16 @@ -30,26 +30,23 @@ public class AbstractQueuedSynchronizerT * ReentrantReadWriteLock, and Semaphore */ static class Mutex extends AbstractQueuedSynchronizer { - public boolean isLocked() { return getState() == 1; } + public boolean isHeldExclusively() { return getState() == 1; } - public boolean tryAcquireExclusive(int acquires) { + public boolean tryAcquire(int acquires) { assertTrue(acquires == 1); return compareAndSetState(0, 1); } - public boolean tryReleaseExclusive(int releases) { + public boolean tryRelease(int releases) { + if (getState() == 0) throw new IllegalMonitorStateException(); setState(0); return true; } - public void checkConditionAccess(Thread thread) { - if (getState() == 0) throw new IllegalMonitorStateException(); - } - public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } public void lock() { - acquireExclusiveUninterruptibly(1); + acquire(1); } } @@ -72,21 +69,21 @@ public class AbstractQueuedSynchronizerT } /** - * A runnable calling acquireExclusiveInterruptibly + * A runnable calling acquireInterruptibly */ class InterruptibleLockRunnable implements Runnable { final Mutex lock; InterruptibleLockRunnable(Mutex l) { lock = l; } public void run() { try { - lock.acquireExclusiveInterruptibly(1); + lock.acquireInterruptibly(1); } catch(InterruptedException success){} } } /** - * A runnable calling acquireExclusiveInterruptibly that expects to be + * A runnable calling acquireInterruptibly that expects to be * interrupted */ class InterruptedLockRunnable implements Runnable { @@ -94,30 +91,38 @@ public class AbstractQueuedSynchronizerT InterruptedLockRunnable(Mutex l) { lock = l; } public void run() { try { - lock.acquireExclusiveInterruptibly(1); + lock.acquireInterruptibly(1); threadShouldThrow(); } catch(InterruptedException success){} } } + + /** + * isHeldExclusively is false upon construction + */ + public void testIsHeldExclusively() { + Mutex rl = new Mutex(); + assertFalse(rl.isHeldExclusively()); + } /** - * acquireExclusiveUninterruptiblying an releaseExclusiveed lock succeeds + * acquiring released lock succeeds */ - public void testAcquireExclusiveUninterruptibly() { + public void testAcquire() { Mutex rl = new Mutex(); - rl.acquireExclusiveUninterruptibly(1); - assertTrue(rl.isLocked()); - rl.releaseExclusive(1); + rl.acquire(1); + assertTrue(rl.isHeldExclusively()); + rl.release(1); } /** - * tryAcquireExclusive on an releaseExclusiveed lock succeeds + * tryAcquire on an released lock succeeds */ - public void testTryAcquireExclusive() { + public void testTryAcquire() { Mutex rl = new Mutex(); - assertTrue(rl.tryAcquireExclusive(1)); - assertTrue(rl.isLocked()); - rl.releaseExclusive(1); + assertTrue(rl.tryAcquire(1)); + assertTrue(rl.isHeldExclusively()); + rl.release(1); } /** @@ -129,7 +134,7 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); try { assertFalse(lock.hasQueuedThreads()); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.hasQueuedThreads()); @@ -139,7 +144,7 @@ public class AbstractQueuedSynchronizerT t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.hasQueuedThreads()); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); assertFalse(lock.hasQueuedThreads()); t1.join(); @@ -171,7 +176,7 @@ public class AbstractQueuedSynchronizerT try { assertFalse(lock.isQueued(t1)); assertFalse(lock.isQueued(t2)); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.isQueued(t1)); @@ -183,7 +188,7 @@ public class AbstractQueuedSynchronizerT Thread.sleep(SHORT_DELAY_MS); assertFalse(lock.isQueued(t1)); assertTrue(lock.isQueued(t2)); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); assertFalse(lock.isQueued(t1)); assertFalse(lock.isQueued(t2)); @@ -203,7 +208,7 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); try { assertNull(lock.getFirstQueuedThread()); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); assertEquals(t1, lock.getFirstQueuedThread()); @@ -213,7 +218,7 @@ public class AbstractQueuedSynchronizerT t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); assertEquals(t2, lock.getFirstQueuedThread()); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); assertNull(lock.getFirstQueuedThread()); t1.join(); @@ -233,7 +238,7 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); try { assertFalse(lock.hasContended()); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.hasContended()); @@ -243,7 +248,7 @@ public class AbstractQueuedSynchronizerT t1.interrupt(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.hasContended()); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.hasContended()); t1.join(); @@ -254,15 +259,15 @@ public class AbstractQueuedSynchronizerT } /** - * acquireExclusiveNanos is interruptible. + * tryAcquireNanos is interruptible. */ public void testInterruptedException2() { final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); + lock.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); threadShouldThrow(); } catch(InterruptedException success){} } @@ -277,35 +282,35 @@ public class AbstractQueuedSynchronizerT /** - * TryAcquireExclusive on a locked lock fails + * TryAcquire on a locked lock fails */ - public void testTryAcquireExclusiveWhenLocked() { + public void testTryAcquireWhenLocked() { final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); Thread t = new Thread(new Runnable() { public void run() { - threadAssertFalse(lock.tryAcquireExclusive(1)); + threadAssertFalse(lock.tryAcquire(1)); } }); try { t.start(); t.join(); - lock.releaseExclusive(1); + lock.release(1); } catch(Exception e){ unexpectedException(); } } /** - * acquireExclusiveNanos on a locked lock times out + * tryAcquireNanos on a locked lock times out */ - public void testAcquireExclusiveNanos_Timeout() { + public void testAcquireNanos_Timeout() { final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); Thread t = new Thread(new Runnable() { public void run() { try { - threadAssertFalse(lock.acquireExclusiveNanos(1, 1000 * 1000)); + threadAssertFalse(lock.tryAcquireNanos(1, 1000 * 1000)); } catch (Exception ex) { threadUnexpectedException(); } @@ -314,7 +319,7 @@ public class AbstractQueuedSynchronizerT try { t.start(); t.join(); - lock.releaseExclusive(1); + lock.release(1); } catch(Exception e){ unexpectedException(); } @@ -326,28 +331,28 @@ public class AbstractQueuedSynchronizerT */ public void testGetState() { final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); - assertTrue(lock.isLocked()); - lock.releaseExclusive(1); - assertFalse(lock.isLocked()); + lock.acquire(1); + assertTrue(lock.isHeldExclusively()); + lock.release(1); + assertFalse(lock.isHeldExclusively()); Thread t = new Thread(new Runnable() { public void run() { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); try { Thread.sleep(SMALL_DELAY_MS); } catch(Exception e) { threadUnexpectedException(); } - lock.releaseExclusive(1); + lock.release(1); } }); try { t.start(); Thread.sleep(SHORT_DELAY_MS); - assertTrue(lock.isLocked()); + assertTrue(lock.isHeldExclusively()); t.join(); - assertFalse(lock.isLocked()); + assertFalse(lock.isHeldExclusively()); } catch(Exception e){ unexpectedException(); } @@ -355,16 +360,16 @@ public class AbstractQueuedSynchronizerT /** - * acquireExclusiveInterruptibly is interruptible. + * acquireInterruptibly is interruptible. */ public void testAcquireInterruptibly1() { final Mutex lock = new Mutex(); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); Thread t = new Thread(new InterruptedLockRunnable(lock)); try { t.start(); t.interrupt(); - lock.releaseExclusive(1); + lock.release(1); t.join(); } catch(Exception e){ unexpectedException(); @@ -372,12 +377,12 @@ public class AbstractQueuedSynchronizerT } /** - * acquireExclusiveInterruptibly succeeds when released, else is interruptible + * acquireInterruptibly succeeds when released, else is interruptible */ public void testAcquireInterruptibly2() { final Mutex lock = new Mutex(); try { - lock.acquireExclusiveInterruptibly(1); + lock.acquireInterruptibly(1); } catch(Exception e) { unexpectedException(); } @@ -385,7 +390,7 @@ public class AbstractQueuedSynchronizerT try { t.start(); t.interrupt(); - assertTrue(lock.isLocked()); + assertTrue(lock.isHeldExclusively()); t.join(); } catch(Exception e){ unexpectedException(); @@ -444,10 +449,10 @@ public class AbstractQueuedSynchronizerT final Mutex lock = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); long t = c.awaitNanos(100); assertTrue(t <= 0); - lock.releaseExclusive(1); + lock.release(1); } catch (Exception ex) { unexpectedException(); @@ -461,9 +466,9 @@ public class AbstractQueuedSynchronizerT final Mutex lock = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - lock.releaseExclusive(1); + lock.release(1); } catch (Exception ex) { unexpectedException(); @@ -477,10 +482,10 @@ public class AbstractQueuedSynchronizerT final Mutex lock = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); java.util.Date d = new java.util.Date(); assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); - lock.releaseExclusive(1); + lock.release(1); } catch (Exception ex) { unexpectedException(); @@ -496,9 +501,9 @@ public class AbstractQueuedSynchronizerT Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -509,9 +514,9 @@ public class AbstractQueuedSynchronizerT try { t.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.signal(); - lock.releaseExclusive(1); + lock.release(1); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } @@ -672,11 +677,11 @@ public class AbstractQueuedSynchronizerT Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); threadAssertFalse(lock.hasWaiters(c)); threadAssertEquals(0, lock.getWaitQueueLength(c)); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -687,16 +692,16 @@ public class AbstractQueuedSynchronizerT try { t.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertTrue(lock.hasWaiters(c)); assertEquals(1, lock.getWaitQueueLength(c)); c.signal(); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); - lock.releaseExclusive(1); + lock.release(1); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } @@ -714,11 +719,11 @@ public class AbstractQueuedSynchronizerT Thread t1 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); threadAssertFalse(lock.hasWaiters(c)); threadAssertEquals(0, lock.getWaitQueueLength(c)); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -729,11 +734,11 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); threadAssertTrue(lock.hasWaiters(c)); threadAssertEquals(1, lock.getWaitQueueLength(c)); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -746,16 +751,16 @@ public class AbstractQueuedSynchronizerT Thread.sleep(SHORT_DELAY_MS); t2.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertTrue(lock.hasWaiters(c)); assertEquals(2, lock.getWaitQueueLength(c)); c.signalAll(); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); - lock.releaseExclusive(1); + lock.release(1); t1.join(SHORT_DELAY_MS); t2.join(SHORT_DELAY_MS); assertFalse(t1.isAlive()); @@ -775,10 +780,10 @@ public class AbstractQueuedSynchronizerT Thread t1 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -789,10 +794,10 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -801,24 +806,24 @@ public class AbstractQueuedSynchronizerT }); try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertTrue(lock.getWaitingThreads(c).isEmpty()); - lock.releaseExclusive(1); + lock.release(1); t1.start(); Thread.sleep(SHORT_DELAY_MS); t2.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertTrue(lock.hasWaiters(c)); assertTrue(lock.getWaitingThreads(c).contains(t1)); assertTrue(lock.getWaitingThreads(c).contains(t2)); c.signalAll(); - lock.releaseExclusive(1); + lock.release(1); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); assertFalse(lock.hasWaiters(c)); assertTrue(lock.getWaitingThreads(c).isEmpty()); - lock.releaseExclusive(1); + lock.release(1); t1.join(SHORT_DELAY_MS); t2.join(SHORT_DELAY_MS); assertFalse(t1.isAlive()); @@ -839,9 +844,9 @@ public class AbstractQueuedSynchronizerT final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); Thread t = new Thread(new Runnable() { public void run() { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.awaitUninterruptibly(); - lock.releaseExclusive(1); + lock.release(1); } }); @@ -849,9 +854,9 @@ public class AbstractQueuedSynchronizerT t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.signal(); - lock.releaseExclusive(1); + lock.release(1); assert(t.isInterrupted()); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); @@ -870,9 +875,9 @@ public class AbstractQueuedSynchronizerT Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.await(); - lock.releaseExclusive(1); + lock.release(1); threadShouldThrow(); } catch(InterruptedException success) { @@ -901,9 +906,9 @@ public class AbstractQueuedSynchronizerT Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.awaitNanos(1000 * 1000 * 1000); // 1 sec - lock.releaseExclusive(1); + lock.release(1); threadShouldThrow(); } catch(InterruptedException success) { @@ -932,10 +937,10 @@ public class AbstractQueuedSynchronizerT Thread t = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); java.util.Date d = new java.util.Date(); c.awaitUntil(new java.util.Date(d.getTime() + 10000)); - lock.releaseExclusive(1); + lock.release(1); threadShouldThrow(); } catch(InterruptedException success) { @@ -964,9 +969,9 @@ public class AbstractQueuedSynchronizerT Thread t1 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -977,9 +982,9 @@ public class AbstractQueuedSynchronizerT Thread t2 = new Thread(new Runnable() { public void run() { try { - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.await(); - lock.releaseExclusive(1); + lock.release(1); } catch(InterruptedException e) { threadUnexpectedException(); @@ -991,9 +996,9 @@ public class AbstractQueuedSynchronizerT t1.start(); t2.start(); Thread.sleep(SHORT_DELAY_MS); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); c.signalAll(); - lock.releaseExclusive(1); + lock.release(1); t1.join(SHORT_DELAY_MS); t2.join(SHORT_DELAY_MS); assertFalse(t1.isAlive()); @@ -1012,7 +1017,7 @@ public class AbstractQueuedSynchronizerT Mutex lock = new Mutex(); String us = lock.toString(); assertTrue(us.indexOf("State = 0") >= 0); - lock.acquireExclusiveUninterruptibly(1); + lock.acquire(1); String ls = lock.toString(); assertTrue(ls.indexOf("State = 1") >= 0); } @@ -1022,8 +1027,8 @@ public class AbstractQueuedSynchronizerT */ public void testSerialization() { Mutex l = new Mutex(); - l.acquireExclusiveUninterruptibly(1); - assertTrue(l.isLocked()); + l.acquire(1); + assertTrue(l.isHeldExclusively()); try { ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); @@ -1034,7 +1039,7 @@ public class AbstractQueuedSynchronizerT ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); Mutex r = (Mutex) in.readObject(); - assertTrue(r.isLocked()); + assertTrue(r.isHeldExclusively()); } catch(Exception e){ e.printStackTrace(); unexpectedException(); @@ -1104,7 +1109,7 @@ public class AbstractQueuedSynchronizerT public void run() { try { threadAssertFalse(l.isSignalled()); - threadAssertTrue(l.acquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); + threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); threadAssertTrue(l.isSignalled()); } catch(InterruptedException e){ @@ -1157,7 +1162,7 @@ public class AbstractQueuedSynchronizerT public void run() { try { threadAssertFalse(l.isSignalled()); - l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); + l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); threadShouldThrow(); } catch(InterruptedException success){} } @@ -1182,7 +1187,7 @@ public class AbstractQueuedSynchronizerT public void run() { try { threadAssertFalse(l.isSignalled()); - threadAssertFalse(l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); + threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); } catch(InterruptedException ie){ threadUnexpectedException(); }