--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2019/08/13 00:54:51 1.66 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2019/08/14 23:06:11 1.69 @@ -13,7 +13,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; @@ -1310,13 +1309,14 @@ public class AbstractQueuedSynchronizerT /** * Tests scenario for * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw + * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck */ - public void testInterruptedFailingAcquire() throws InterruptedException { + public void testInterruptedFailingAcquire() throws Throwable { final RuntimeException ex = new RuntimeException(); // A synchronizer only offering a choice of failure modes class Sync extends AbstractQueuedSynchronizer { - boolean pleaseThrow; + volatile boolean pleaseThrow; @Override protected boolean tryAcquire(int ignored) { if (pleaseThrow) throw ex; return false; @@ -1334,29 +1334,65 @@ public class AbstractQueuedSynchronizerT } final Sync s = new Sync(); - + final Action[] uninterruptibleAcquireMethods = { + () -> s.acquire(1), + () -> s.acquireShared(1), + // TODO: test interruptible acquire methods + }; + final Action[] releaseMethods = { + () -> s.release(1), + () -> s.releaseShared(1), + }; + final Action acquireMethod + = chooseRandomly(uninterruptibleAcquireMethods); + final Action releaseMethod + = chooseRandomly(releaseMethods); + + // From os_posix.cpp: + // + // NOTE that since there is no "lock" around the interrupt and + // is_interrupted operations, there is the possibility that the + // interrupted flag (in osThread) will be "false" but that the + // low-level events will be in the signaled state. This is + // intentional. The effect of this is that Object.wait() and + // LockSupport.park() will appear to have a spurious wakeup, which + // is allowed and not harmful, and the possibility is so rare that + // it is not worth the added complexity to add yet another lock. final Thread thread = newStartedThread(new CheckedRunnable() { public void realRun() { try { - if (randomBoolean()) - s.acquire(1); - else - s.acquireShared(1); + acquireMethod.run(); shouldThrow(); } catch (Throwable t) { assertSame(ex, t); - assertTrue(Thread.interrupted()); + awaitInterrupted(); } }}); - waitForThreadToEnterWaitState(thread); - assertSame(thread, s.getFirstQueuedThread()); - assertTrue(s.hasQueuedPredecessors()); - assertTrue(s.hasQueuedThreads()); - assertEquals(1, s.getQueueLength()); + for (long startTime = 0L;; ) { + waitForThreadToEnterWaitState(thread); + if (s.getFirstQueuedThread() == thread + && s.hasQueuedPredecessors() + && s.hasQueuedThreads() + && s.getQueueLength() == 1) + break; + if (startTime == 0L) + startTime = System.nanoTime(); + else if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out waiting for AQS state: " + + "thread state=" + thread.getState() + + ", queued threads=" + s.getQueuedThreads()); + Thread.yield(); + } s.pleaseThrow = true; - thread.interrupt(); - s.release(1); + // release and interrupt, in random order + if (randomBoolean()) { + thread.interrupt(); + releaseMethod.run(); + } else { + releaseMethod.run(); + thread.interrupt(); + } awaitTermination(thread); }