--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2003/12/29 14:19:27 1.3 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/08 01:29:46 1.11 @@ -30,39 +30,34 @@ public class AbstractQueuedSynchronizerT */ static class Mutex implements Lock, java.io.Serializable { private static class Sync extends AbstractQueuedSynchronizer { - public int acquireExclusiveState(boolean isQueued, int acquires) { + boolean isLocked() { return getState() == 1; } + + public boolean tryAcquireExclusive(int acquires) { assert acquires == 1; // Does not use multiple acquires - return state().compareAndSet(0, 1)? 0 : -1; + return compareAndSetState(0, 1); } - public boolean releaseExclusiveState(int releases) { - state().set(0); + public boolean tryReleaseExclusive(int releases) { + setState(0); return true; } - public int acquireSharedState(boolean isQueued, int acquires) { - throw new UnsupportedOperationException(); - } - - public boolean releaseSharedState(int releases) { - throw new UnsupportedOperationException(); - } - - public void checkConditionAccess(Thread thread, boolean waiting) { - if (state().get() == 0) throw new IllegalMonitorStateException(); + public void checkConditionAccess(Thread thread) { + if (getState() == 0) throw new IllegalMonitorStateException(); } Condition newCondition() { return new ConditionObject(); } private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); - state().set(0); // reset to unlocked state + setState(0); // reset to unlocked state } + } - + private final Sync sync = new Sync(); public boolean tryLock() { - return sync.acquireExclusiveState(false, 1) >= 0; + return sync.tryAcquireExclusive(1); } public void lock() { sync.acquireExclusiveUninterruptibly(1); @@ -75,8 +70,10 @@ public class AbstractQueuedSynchronizerT } public void unlock() { sync.releaseExclusive(1); } public Condition newCondition() { return sync.newCondition(); } - public boolean isLocked() { return sync.state().get() != 0; } + public boolean isLocked() { return sync.isLocked(); } public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } + public boolean hasContended() { return sync.hasContended(); } + public boolean isQueued(Thread t) { return sync.isQueued(t); } } /** @@ -119,7 +116,7 @@ public class AbstractQueuedSynchronizerT } /** - * trylock on an unlocked lock succeeds + * tryLock on an unlocked lock succeeds */ public void testTryLock() { Mutex rl = new Mutex(); @@ -158,7 +155,82 @@ public class AbstractQueuedSynchronizerT } /** - * timed trylock is interruptible. + * isQueued(null) throws NPE + */ + public void testIsQueuedNPE() { + final Mutex lock = new Mutex(); + try { + lock.isQueued(null); + shouldThrow(); + } catch (NullPointerException success) { + } + } + + /** + * isQueued reports whether a thread is queued. + */ + public void testIsQueued() { + final Mutex lock = new Mutex(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + assertFalse(lock.isQueued(t1)); + assertFalse(lock.isQueued(t2)); + lock.lock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.isQueued(t1)); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.isQueued(t1)); + assertTrue(lock.isQueued(t2)); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(lock.isQueued(t1)); + assertTrue(lock.isQueued(t2)); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(lock.isQueued(t1)); + assertFalse(lock.isQueued(t2)); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * hasContended reports whether there has been contention + */ + public void testhasContended() { + final Mutex lock = new Mutex(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + assertFalse(lock.hasContended()); + lock.lock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t1.join(); + t2.join(); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * timed tryLock is interruptible. */ public void testInterruptedException2() { final Mutex lock = new Mutex(); @@ -181,7 +253,7 @@ public class AbstractQueuedSynchronizerT /** - * Trylock on a locked lock fails + * TryLock on a locked lock fails */ public void testTryLockWhenLocked() { final Mutex lock = new Mutex(); @@ -201,7 +273,7 @@ public class AbstractQueuedSynchronizerT } /** - * Timed trylock on a locked lock times out + * Timed tryLock on a locked lock times out */ public void testTryLock_Timeout() { final Mutex lock = new Mutex();