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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.56 by jsr166, Sun May 14 02:24:10 2017 UTC vs.
Revision 1.62 by dl, Sun Jan 7 23:05:44 2018 UTC

# Line 9 | Line 9
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10   import static java.util.concurrent.TimeUnit.NANOSECONDS;
11  
12 + import java.util.ArrayList;
13   import java.util.Arrays;
14   import java.util.Collection;
15   import java.util.HashSet;
16 + import java.util.concurrent.ThreadLocalRandom;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
18   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
19  
# Line 19 | Line 21 | import junit.framework.AssertionFailedEr
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
23  
24 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
25   public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
26      public static void main(String[] args) {
27          main(suite(), args);
# Line 33 | Line 36 | public class AbstractQueuedSynchronizerT
36       * methods/features of AbstractQueuedSynchronizer are tested via
37       * other test classes, including those for ReentrantLock,
38       * ReentrantReadWriteLock, and Semaphore.
39 +     *
40 +     * Unlike the javadoc sample, we don't track owner thread via
41 +     * AbstractOwnableSynchronizer methods.
42       */
43      static class Mutex extends AbstractQueuedSynchronizer {
44          /** An eccentric value for locked synchronizer state. */
# Line 40 | Line 46 | public class AbstractQueuedSynchronizerT
46  
47          static final int UNLOCKED = 0;
48  
49 +        /** Owner thread is untracked, so this is really just isLocked(). */
50          @Override public boolean isHeldExclusively() {
51              int state = getState();
52              assertTrue(state == UNLOCKED || state == LOCKED);
53              return state == LOCKED;
54          }
55  
56 <        @Override public boolean tryAcquire(int acquires) {
56 >        @Override protected boolean tryAcquire(int acquires) {
57              assertEquals(LOCKED, acquires);
58              return compareAndSetState(UNLOCKED, LOCKED);
59          }
60  
61 <        @Override public boolean tryRelease(int releases) {
61 >        @Override protected boolean tryRelease(int releases) {
62              if (getState() != LOCKED) throw new IllegalMonitorStateException();
63              assertEquals(LOCKED, releases);
64              setState(UNLOCKED);
# Line 82 | Line 89 | public class AbstractQueuedSynchronizerT
89              release(LOCKED);
90          }
91  
92 +        /** Faux-Implements Lock.newCondition(). */
93          public ConditionObject newCondition() {
94              return new ConditionObject();
95          }
96      }
97  
98      /**
99 <     * A simple latch class, to test shared mode.
99 >     * A minimal latch class, to test shared mode.
100       */
101      static class BooleanLatch extends AbstractQueuedSynchronizer {
102          public boolean isSignalled() { return getState() != 0; }
# Line 1253 | Line 1261 | public class AbstractQueuedSynchronizerT
1261          sync.release();
1262      }
1263  
1264 +    /**
1265 +     * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race
1266 +     * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck
1267 +     */
1268 +    public void testCancelCancelRace() throws InterruptedException {
1269 +        class Sync extends AbstractQueuedSynchronizer {
1270 +            protected boolean tryAcquire(int acquires) {
1271 +                return !hasQueuedPredecessors() && compareAndSetState(0, 1);
1272 +            }
1273 +            protected boolean tryRelease(int releases) {
1274 +                return compareAndSetState(1, 0);
1275 +            }
1276 +        }
1277 +
1278 +        Sync s = new Sync();
1279 +        s.acquire(1);           // acquire to force other threads to enqueue
1280 +
1281 +        // try to trigger double cancel race with two background threads
1282 +        ArrayList<Thread> threads = new ArrayList<>();
1283 +        Runnable failedAcquire = () -> {
1284 +            try {
1285 +                s.acquireInterruptibly(1);
1286 +                shouldThrow();
1287 +            } catch (InterruptedException expected) {}
1288 +        };
1289 +        for (int i = 0; i < 2; i++) {
1290 +            Thread thread = new Thread(failedAcquire);
1291 +            thread.start();
1292 +            threads.add(thread);
1293 +        }
1294 +        Thread.sleep(100);
1295 +        for (Thread thread : threads) thread.interrupt();
1296 +        for (Thread thread : threads) awaitTermination(thread);
1297 +
1298 +        s.release(1);
1299 +
1300 +        // no one holds lock now, we should be able to acquire
1301 +        if (!s.tryAcquire(1))
1302 +            throw new RuntimeException(
1303 +                String.format(
1304 +                    "Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s",
1305 +                    s.hasQueuedPredecessors(),
1306 +                    s.hasQueuedThreads(),
1307 +                    s.getQueueLength(),
1308 +                    s.getFirstQueuedThread()));
1309 +    }
1310 +
1311 +    /**
1312 +     * Tests scenario for
1313 +     * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1314 +     */
1315 +    public void testInterruptedFailingAcquire() throws InterruptedException {
1316 +        final RuntimeException ex = new RuntimeException();
1317 +
1318 +        // A synchronizer only offering a choice of failure modes
1319 +        class Sync extends AbstractQueuedSynchronizer {
1320 +            boolean pleaseThrow;
1321 +            @Override protected boolean tryAcquire(int ignored) {
1322 +                if (pleaseThrow) throw ex;
1323 +                return false;
1324 +            }
1325 +            @Override protected int tryAcquireShared(int ignored) {
1326 +                if (pleaseThrow) throw ex;
1327 +                return -1;
1328 +            }
1329 +            @Override protected boolean tryRelease(int ignored) {
1330 +                return true;
1331 +            }
1332 +            @Override protected boolean tryReleaseShared(int ignored) {
1333 +                return true;
1334 +            }
1335 +        }
1336 +
1337 +        final Sync s = new Sync();
1338 +
1339 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1340 +            public void realRun() {
1341 +                try {
1342 +                    if (ThreadLocalRandom.current().nextBoolean())
1343 +                        s.acquire(1);
1344 +                    else
1345 +                        s.acquireShared(1);
1346 +                    shouldThrow();
1347 +                } catch (Throwable t) {
1348 +                    assertSame(ex, t);
1349 +                    assertTrue(Thread.interrupted());
1350 +                }
1351 +            }});
1352 +        waitForThreadToEnterWaitState(thread);
1353 +        assertSame(thread, s.getFirstQueuedThread());
1354 +        assertTrue(s.hasQueuedPredecessors());
1355 +        assertTrue(s.hasQueuedThreads());
1356 +        assertEquals(1, s.getQueueLength());
1357 +
1358 +        s.pleaseThrow = true;
1359 +        thread.interrupt();
1360 +        s.release(1);
1361 +        awaitTermination(thread);
1362 +    }
1363 +
1364   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines