--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2015/02/22 04:34:44 1.49 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2017/11/27 03:25:42 1.59 @@ -9,6 +9,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -19,9 +20,10 @@ import junit.framework.AssertionFailedEr import junit.framework.Test; import junit.framework.TestSuite; +@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom public class AbstractQueuedSynchronizerTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(AbstractQueuedSynchronizerTest.class); @@ -33,6 +35,9 @@ public class AbstractQueuedSynchronizerT * methods/features of AbstractQueuedSynchronizer are tested via * other test classes, including those for ReentrantLock, * ReentrantReadWriteLock, and Semaphore. + * + * Unlike the javadoc sample, we don't track owner thread via + * AbstractOwnableSynchronizer methods. */ static class Mutex extends AbstractQueuedSynchronizer { /** An eccentric value for locked synchronizer state. */ @@ -40,18 +45,19 @@ public class AbstractQueuedSynchronizerT static final int UNLOCKED = 0; + /** Owner thread is untracked, so this is really just isLocked(). */ @Override public boolean isHeldExclusively() { int state = getState(); assertTrue(state == UNLOCKED || state == LOCKED); return state == LOCKED; } - @Override public boolean tryAcquire(int acquires) { + @Override protected boolean tryAcquire(int acquires) { assertEquals(LOCKED, acquires); return compareAndSetState(UNLOCKED, LOCKED); } - @Override public boolean tryRelease(int releases) { + @Override protected boolean tryRelease(int releases) { if (getState() != LOCKED) throw new IllegalMonitorStateException(); assertEquals(LOCKED, releases); setState(UNLOCKED); @@ -82,13 +88,14 @@ public class AbstractQueuedSynchronizerT release(LOCKED); } + /** Faux-Implements Lock.newCondition(). */ public ConditionObject newCondition() { return new ConditionObject(); } } /** - * A simple latch class, to test shared mode. + * A minimal latch class, to test shared mode. */ static class BooleanLatch extends AbstractQueuedSynchronizer { public boolean isSignalled() { return getState() != 0; } @@ -238,26 +245,34 @@ public class AbstractQueuedSynchronizerT * default timeout duration). */ void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { - long timeoutMillis = timeoutMillis(); - long startTime = System.nanoTime(); + final long timeoutMillis = timeoutMillis(); + final long startTime; try { switch (awaitMethod) { case awaitTimed: + startTime = System.nanoTime(); assertFalse(c.await(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); break; case awaitNanos: + startTime = System.nanoTime(); long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); long nanosRemaining = c.awaitNanos(nanosTimeout); assertTrue(nanosRemaining <= 0); + assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); break; case awaitUntil: + // We shouldn't assume that nanoTime and currentTimeMillis + // use the same time source, so don't use nanoTime here. + java.util.Date delayedDate = delayedDate(timeoutMillis); assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); + assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); break; default: throw new UnsupportedOperationException(); } } catch (InterruptedException ie) { threadUnexpectedException(ie); } - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); } /** @@ -960,30 +975,30 @@ public class AbstractQueuedSynchronizerT */ public void testAwaitUninterruptibly() { final Mutex sync = new Mutex(); - final ConditionObject c = sync.newCondition(); + final ConditionObject condition = sync.newCondition(); final BooleanLatch pleaseInterrupt = new BooleanLatch(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { sync.acquire(); assertTrue(pleaseInterrupt.releaseShared(0)); - c.awaitUninterruptibly(); + condition.awaitUninterruptibly(); assertTrue(Thread.interrupted()); - assertHasWaitersLocked(sync, c, NO_THREADS); + assertHasWaitersLocked(sync, condition, NO_THREADS); sync.release(); }}); pleaseInterrupt.acquireShared(0); sync.acquire(); - assertHasWaitersLocked(sync, c, t); + assertHasWaitersLocked(sync, condition, t); sync.release(); t.interrupt(); - assertHasWaitersUnlocked(sync, c, t); - assertThreadStaysAlive(t); + assertHasWaitersUnlocked(sync, condition, t); + assertThreadBlocks(t, Thread.State.WAITING); sync.acquire(); - assertHasWaitersLocked(sync, c, t); + assertHasWaitersLocked(sync, condition, t); assertHasExclusiveQueuedThreads(sync, NO_THREADS); - c.signal(); - assertHasWaitersLocked(sync, c, NO_THREADS); + condition.signal(); + assertHasWaitersLocked(sync, condition, NO_THREADS); assertHasExclusiveQueuedThreads(sync, t); sync.release(); awaitTermination(t); @@ -1126,7 +1141,7 @@ public class AbstractQueuedSynchronizerT waitForQueuedThread(l, t); assertFalse(l.isSignalled()); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); assertHasSharedQueuedThreads(l, t); assertTrue(l.releaseShared(0)); assertTrue(l.isSignalled()); @@ -1151,7 +1166,7 @@ public class AbstractQueuedSynchronizerT waitForQueuedThread(l, t); assertFalse(l.isSignalled()); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); assertTrue(l.releaseShared(0)); assertTrue(l.isSignalled()); awaitTermination(t); @@ -1245,4 +1260,55 @@ public class AbstractQueuedSynchronizerT sync.release(); } + /** + * Disabled demo test for (unfixed as of 2017-11) + * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race + * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck + */ + public void XXXXtestCancelCancelRace() throws InterruptedException { + class Sync extends AbstractQueuedSynchronizer { + private static final long serialVersionUID = 1L; + + public boolean tryAcquire(int acquires) { + return !hasQueuedPredecessors() && compareAndSetState(0, 1); + } + + protected boolean tryRelease(int releases) { + return compareAndSetState(1, 0); + } + } + + Sync s = new Sync(); + s.acquire(1); // acquire to force other threads to enqueue + + // try to trigger double cancel race with two background threads + ArrayList ts = new ArrayList<>(); + Runnable failedAcquire = () -> { + try { + s.acquireInterruptibly(1); + throw new AssertionError(); + } catch (InterruptedException expected) {} + }; + for (int i = 0; i < 2; i++) { + Thread t = new Thread(failedAcquire); + t.start(); + ts.add(t); + } + Thread.sleep(100); + for (Thread t : ts) t.interrupt(); + for (Thread t : ts) t.join(); + + s.release(1); + + // no one holds lock now, we should be able to acquire + if (!s.tryAcquire(1)) + throw new RuntimeException( + String.format( + "Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s", + s.hasQueuedPredecessors(), + s.hasQueuedThreads(), + s.getQueueLength(), + s.getFirstQueuedThread())); + } + }