ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.65 by jsr166, Sun May 14 02:03:15 2017 UTC vs.
Revision 1.68 by jsr166, Tue Jan 23 20:44:11 2018 UTC

# Line 8 | Line 8
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.ArrayList;
12   import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.HashSet;
15   import java.util.concurrent.CountDownLatch;
16   import java.util.concurrent.CyclicBarrier;
17 + import java.util.concurrent.ThreadLocalRandom;
18   import java.util.concurrent.locks.Condition;
19   import java.util.concurrent.locks.ReentrantLock;
20  
19 import junit.framework.AssertionFailedError;
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
23  
24 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
25   public class ReentrantLockTest extends JSR166TestCase {
26      public static void main(String[] args) {
27          main(suite(), args);
# Line 85 | Line 87 | public class ReentrantLockTest extends J
87          long startTime = System.nanoTime();
88          while (!lock.hasQueuedThread(t)) {
89              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
90 <                throw new AssertionFailedError("timed out");
90 >                throw new AssertionError("timed out");
91              Thread.yield();
92          }
93          assertTrue(t.isAlive());
# Line 145 | Line 147 | public class ReentrantLockTest extends J
147  
148      enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
149  
150 +    static AwaitMethod randomAwaitMethod() {
151 +        AwaitMethod[] awaitMethods = AwaitMethod.values();
152 +        return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)];
153 +    }
154 +
155      /**
156       * Awaits condition "indefinitely" using the specified AwaitMethod.
157       */
# Line 1132 | Line 1139 | public class ReentrantLockTest extends J
1139          lock.unlock();
1140          assertTrue(lock.toString().contains("Unlocked"));
1141      }
1142 +
1143 +    /**
1144 +     * Tests scenario for JDK-8187408
1145 +     * AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
1146 +     */
1147 +    public void testBug8187408() throws InterruptedException {
1148 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1149 +        final AwaitMethod awaitMethod = randomAwaitMethod();
1150 +        final int nThreads = rnd.nextInt(2, 10);
1151 +        final ReentrantLock lock = new ReentrantLock();
1152 +        final Condition cond = lock.newCondition();
1153 +        final CountDownLatch done = new CountDownLatch(nThreads);
1154 +        final ArrayList<Thread> threads = new ArrayList<>();
1155 +
1156 +        Runnable rogue = () -> {
1157 +            while (done.getCount() > 0) {
1158 +                try {
1159 +                    // call await without holding lock?!
1160 +                    await(cond, awaitMethod);
1161 +                    throw new AssertionError("should throw");
1162 +                }
1163 +                catch (IllegalMonitorStateException expected) {}
1164 +                catch (Throwable fail) { threadUnexpectedException(fail); }}};
1165 +        Thread rogueThread = new Thread(rogue, "rogue");
1166 +        threads.add(rogueThread);
1167 +        rogueThread.start();
1168 +
1169 +        Runnable waiter = () -> {
1170 +            lock.lock();
1171 +            try {
1172 +                done.countDown();
1173 +                cond.await();
1174 +            } catch (Throwable fail) {
1175 +                threadUnexpectedException(fail);
1176 +            } finally {
1177 +                lock.unlock();
1178 +            }};
1179 +        for (int i = 0; i < nThreads; i++) {
1180 +            Thread thread = new Thread(waiter, "waiter");
1181 +            threads.add(thread);
1182 +            thread.start();
1183 +        }
1184 +
1185 +        assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1186 +        lock.lock();
1187 +        try {
1188 +            assertEquals(nThreads, lock.getWaitQueueLength(cond));
1189 +        } finally {
1190 +            cond.signalAll();
1191 +            lock.unlock();
1192 +        }
1193 +        for (Thread thread : threads) {
1194 +            thread.join(LONG_DELAY_MS);
1195 +            assertFalse(thread.isAlive());
1196 +        }
1197 +    }
1198   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines