--- jsr166/src/test/tck/ReentrantLockTest.java 2015/07/03 01:25:15 1.62 +++ jsr166/src/test/tck/ReentrantLockTest.java 2019/09/26 20:48:52 1.71 @@ -8,18 +8,23 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; +@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom public class ReentrantLockTest extends JSR166TestCase { public static void main(String[] args) { main(suite(), args); @@ -85,7 +90,7 @@ public class ReentrantLockTest extends J long startTime = System.nanoTime(); while (!lock.hasQueuedThread(t)) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) - throw new AssertionFailedError("timed out"); + throw new AssertionError("timed out"); Thread.yield(); } assertTrue(t.isAlive()); @@ -145,6 +150,11 @@ public class ReentrantLockTest extends J enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } + static AwaitMethod randomAwaitMethod() { + AwaitMethod[] awaitMethods = AwaitMethod.values(); + return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)]; + } + /** * Awaits condition "indefinitely" using the specified AwaitMethod. */ @@ -209,7 +219,7 @@ public class ReentrantLockTest extends J public void testUnlock_IMSE() { testUnlock_IMSE(false); } public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } public void testUnlock_IMSE(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); try { lock.unlock(); shouldThrow(); @@ -399,11 +409,11 @@ public class ReentrantLockTest extends J public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } public void testTryLock_Timeout(boolean fair) { final PublicReentrantLock lock = new PublicReentrantLock(fair); + final long timeoutMillis = timeoutMillis(); lock.lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { long startTime = System.nanoTime(); - long timeoutMillis = 10; assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); }}); @@ -418,7 +428,7 @@ public class ReentrantLockTest extends J public void testGetHoldCount() { testGetHoldCount(false); } public void testGetHoldCount_fair() { testGetHoldCount(true); } public void testGetHoldCount(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); for (int i = 1; i <= SIZE; i++) { lock.lock(); assertEquals(i, lock.getHoldCount()); @@ -435,8 +445,8 @@ public class ReentrantLockTest extends J public void testIsLocked() { testIsLocked(false); } public void testIsLocked_fair() { testIsLocked(true); } public void testIsLocked(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); try { - final ReentrantLock lock = new ReentrantLock(fair); assertFalse(lock.isLocked()); lock.lock(); assertTrue(lock.isLocked()); @@ -523,18 +533,18 @@ public class ReentrantLockTest extends J public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } public void testAwaitNanos_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + final long timeoutMillis = timeoutMillis(); + final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + lock.lock(); + final long startTime = System.nanoTime(); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; - long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); long nanosRemaining = c.awaitNanos(timeoutNanos); assertTrue(nanosRemaining <= 0); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.unlock(); } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); } /** @@ -543,16 +553,16 @@ public class ReentrantLockTest extends J public void testAwait_Timeout() { testAwait_Timeout(false); } public void testAwait_Timeout_fair() { testAwait_Timeout(true); } public void testAwait_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + final long timeoutMillis = timeoutMillis(); + lock.lock(); + final long startTime = System.nanoTime(); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - long timeoutMillis = 10; assertFalse(c.await(timeoutMillis, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); - lock.unlock(); } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + lock.unlock(); } /** @@ -561,15 +571,17 @@ public class ReentrantLockTest extends J public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } public void testAwaitUntil_Timeout(boolean fair) { + final ReentrantLock lock = new ReentrantLock(fair); + final Condition c = lock.newCondition(); + lock.lock(); + // We shouldn't assume that nanoTime and currentTimeMillis + // use the same time source, so don't use nanoTime here. + final java.util.Date delayedDate = delayedDate(timeoutMillis()); try { - final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); - lock.lock(); - long startTime = System.nanoTime(); - assertFalse(c.awaitUntil(delayedDate(timeoutMillis()))); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); - lock.unlock(); + assertFalse(c.awaitUntil(delayedDate)); } catch (InterruptedException fail) { threadUnexpectedException(fail); } + assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); + lock.unlock(); } /** @@ -883,7 +895,7 @@ public class ReentrantLockTest extends J public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } public void testAwaitUninterruptibly(boolean fair) { final ReentrantLock lock = new ReentrantLock(fair); - final Condition c = lock.newCondition(); + final Condition condition = lock.newCondition(); final CountDownLatch pleaseInterrupt = new CountDownLatch(2); Thread t1 = newStartedThread(new CheckedRunnable() { @@ -892,7 +904,7 @@ public class ReentrantLockTest extends J lock.lock(); pleaseInterrupt.countDown(); Thread.currentThread().interrupt(); - c.awaitUninterruptibly(); + condition.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); @@ -902,21 +914,20 @@ public class ReentrantLockTest extends J // Interrupt during awaitUninterruptibly lock.lock(); pleaseInterrupt.countDown(); - c.awaitUninterruptibly(); + condition.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); await(pleaseInterrupt); + t2.interrupt(); lock.lock(); lock.unlock(); - t2.interrupt(); - - assertThreadStaysAlive(t1); - assertTrue(t2.isAlive()); + assertThreadBlocks(t1, Thread.State.WAITING); + assertThreadBlocks(t2, Thread.State.WAITING); lock.lock(); - c.signalAll(); + condition.signalAll(); lock.unlock(); awaitTermination(t1); @@ -1098,7 +1109,7 @@ public class ReentrantLockTest extends J public void testSerialization() { testSerialization(false); } public void testSerialization_fair() { testSerialization(true); } public void testSerialization(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); lock.lock(); ReentrantLock clone = serialClone(lock); @@ -1124,11 +1135,128 @@ public class ReentrantLockTest extends J public void testToString() { testToString(false); } public void testToString_fair() { testToString(true); } public void testToString(boolean fair) { - ReentrantLock lock = new ReentrantLock(fair); + final ReentrantLock lock = new ReentrantLock(fair); assertTrue(lock.toString().contains("Unlocked")); lock.lock(); - assertTrue(lock.toString().contains("Locked")); + assertTrue(lock.toString().contains("Locked by")); lock.unlock(); assertTrue(lock.toString().contains("Unlocked")); } + + /** + * Tests scenario for JDK-8187408 + * AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock + */ + public void testBug8187408() throws InterruptedException { + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final AwaitMethod awaitMethod = randomAwaitMethod(); + final int nThreads = rnd.nextInt(2, 10); + final ReentrantLock lock = new ReentrantLock(); + final Condition cond = lock.newCondition(); + final CountDownLatch done = new CountDownLatch(nThreads); + final ArrayList threads = new ArrayList<>(); + + Runnable rogue = () -> { + while (done.getCount() > 0) { + try { + // call await without holding lock?! + await(cond, awaitMethod); + throw new AssertionError("should throw"); + } + catch (IllegalMonitorStateException success) {} + catch (Throwable fail) { threadUnexpectedException(fail); }}}; + Thread rogueThread = new Thread(rogue, "rogue"); + threads.add(rogueThread); + rogueThread.start(); + + Runnable waiter = () -> { + lock.lock(); + try { + done.countDown(); + cond.await(); + } catch (Throwable fail) { + threadUnexpectedException(fail); + } finally { + lock.unlock(); + }}; + for (int i = 0; i < nThreads; i++) { + Thread thread = new Thread(waiter, "waiter"); + threads.add(thread); + thread.start(); + } + + assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + lock.lock(); + try { + assertEquals(nThreads, lock.getWaitQueueLength(cond)); + } finally { + cond.signalAll(); + lock.unlock(); + } + for (Thread thread : threads) { + thread.join(LONG_DELAY_MS); + assertFalse(thread.isAlive()); + } + } + + /** + * ThreadMXBean reports the blockers that we expect. + */ + public void testBlockers() { + if (!testImplementationDetails) return; + final boolean fair = randomBoolean(); + final boolean timedAcquire = randomBoolean(); + final boolean timedAwait = randomBoolean(); + final String syncClassName = fair + ? "ReentrantLock$FairSync" + : "ReentrantLock$NonfairSync"; + final String conditionClassName + = "AbstractQueuedSynchronizer$ConditionObject"; + final Thread.State expectedAcquireState = timedAcquire + ? Thread.State.TIMED_WAITING + : Thread.State.WAITING; + final Thread.State expectedAwaitState = timedAwait + ? Thread.State.TIMED_WAITING + : Thread.State.WAITING; + final Lock lock = new ReentrantLock(fair); + final Condition condition = lock.newCondition(); + final AtomicBoolean conditionSatisfied = new AtomicBoolean(false); + lock.lock(); + final Thread thread = newStartedThread((Action) () -> { + if (timedAcquire) + lock.tryLock(LONGER_DELAY_MS, MILLISECONDS); + else + lock.lock(); + while (!conditionSatisfied.get()) + if (timedAwait) + condition.await(LONGER_DELAY_MS, MILLISECONDS); + else + condition.await(); + }); + Callable waitingForLock = () -> { + String className; + return thread.getState() == expectedAcquireState + && (className = blockerClassName(thread)) != null + && className.endsWith(syncClassName); + }; + waitForThreadToEnterWaitState(thread, waitingForLock); + + lock.unlock(); + Callable waitingForCondition = () -> { + String className; + return thread.getState() == expectedAwaitState + && (className = blockerClassName(thread)) != null + && className.endsWith(conditionClassName); + }; + waitForThreadToEnterWaitState(thread, waitingForCondition); + + // politely release the waiter + conditionSatisfied.set(true); + lock.lock(); + try { + condition.signal(); + } finally { lock.unlock(); } + + awaitTermination(thread); + } }