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

Comparing jsr166/src/test/tck/StampedLockTest.java (file contents):
Revision 1.27 by jsr166, Wed Aug 10 17:05:13 2016 UTC vs.
Revision 1.43 by jsr166, Fri Feb 22 19:27:47 2019 UTC

# Line 7 | Line 7
7  
8   import static java.util.concurrent.TimeUnit.DAYS;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import static java.util.concurrent.TimeUnit.SECONDS;
10 >
11 > import static java.util.concurrent.locks.StampedLock.isLockStamp;
12 > import static java.util.concurrent.locks.StampedLock.isOptimisticReadStamp;
13 > import static java.util.concurrent.locks.StampedLock.isReadLockStamp;
14 > import static java.util.concurrent.locks.StampedLock.isWriteLockStamp;
15  
16   import java.util.ArrayList;
17   import java.util.List;
# Line 17 | Line 21 | import java.util.concurrent.TimeUnit;
21   import java.util.concurrent.locks.Lock;
22   import java.util.concurrent.locks.StampedLock;
23   import java.util.function.BiConsumer;
20 import java.util.function.Consumer;
24   import java.util.function.Function;
25  
26   import junit.framework.Test;
# Line 84 | Line 87 | public class StampedLockTest extends JSR
87  
88      List<Function<StampedLock, Long>> readLockers() {
89          List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
90 <        readLockers.add((sl) -> sl.readLock());
91 <        readLockers.add((sl) -> sl.tryReadLock());
92 <        readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl));
93 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
94 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0L, DAYS));
95 <        readLockers.add((sl) -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
90 >        readLockers.add(sl -> sl.readLock());
91 >        readLockers.add(sl -> sl.tryReadLock());
92 >        readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl));
93 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
94 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS));
95 >        readLockers.add(sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
96          return readLockers;
97      }
98  
# Line 105 | Line 108 | public class StampedLockTest extends JSR
108  
109      List<Function<StampedLock, Long>> writeLockers() {
110          List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
111 <        writeLockers.add((sl) -> sl.writeLock());
112 <        writeLockers.add((sl) -> sl.tryWriteLock());
113 <        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
114 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
115 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0L, DAYS));
116 <        writeLockers.add((sl) -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
111 >        writeLockers.add(sl -> sl.writeLock());
112 >        writeLockers.add(sl -> sl.tryWriteLock());
113 >        writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl));
114 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
115 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS));
116 >        writeLockers.add(sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
117          return writeLockers;
118      }
119  
# Line 249 | Line 252 | public class StampedLockTest extends JSR
252          long s = assertNonZero(lock.writeLock());
253          assertTrue(lock.validate(s));
254          assertFalse(lock.validate(lock.tryWriteLock()));
255 <        assertFalse(lock.validate(lock.tryWriteLock(0L, SECONDS)));
255 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
256 >                                                    randomTimeUnit())));
257          assertFalse(lock.validate(lock.tryReadLock()));
258 <        assertFalse(lock.validate(lock.tryReadLock(0L, SECONDS)));
258 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
259 >                                                    randomTimeUnit())));
260          assertFalse(lock.validate(lock.tryOptimisticRead()));
261          lock.unlockWrite(s);
262      }
# Line 273 | Line 278 | public class StampedLockTest extends JSR
278       * interruptible operations throw InterruptedException when pre-interrupted
279       */
280      public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
276        final CountDownLatch running = new CountDownLatch(1);
281          final StampedLock lock = new StampedLock();
282  
283          Action[] interruptibleLockActions = {
# Line 338 | Line 342 | public class StampedLockTest extends JSR
342       * interruptible operations throw InterruptedException when write locked and interrupted
343       */
344      public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
341        final CountDownLatch running = new CountDownLatch(1);
345          final StampedLock lock = new StampedLock();
346          long s = lock.writeLock();
347  
# Line 361 | Line 364 | public class StampedLockTest extends JSR
364       * interruptible operations throw InterruptedException when read locked and interrupted
365       */
366      public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
364        final CountDownLatch running = new CountDownLatch(1);
367          final StampedLock lock = new StampedLock();
368          long s = lock.readLock();
369  
# Line 480 | Line 482 | public class StampedLockTest extends JSR
482      }
483  
484      /**
485 <     * A writelock succeeds only after a reading thread unlocks
485 >     * writeLock() succeeds only after a reading thread unlocks
486       */
487      public void testWriteAfterReadLock() throws InterruptedException {
488 <        final CountDownLatch running = new CountDownLatch(1);
488 >        final CountDownLatch aboutToLock = new CountDownLatch(1);
489          final StampedLock lock = new StampedLock();
490          long rs = lock.readLock();
491          Thread t = newStartedThread(new CheckedRunnable() {
492              public void realRun() {
493 <                running.countDown();
493 >                aboutToLock.countDown();
494                  long s = lock.writeLock();
495 +                assertTrue(lock.isWriteLocked());
496 +                assertFalse(lock.isReadLocked());
497                  lock.unlockWrite(s);
498              }});
499  
500 <        running.await();
501 <        waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS);
500 >        await(aboutToLock);
501 >        assertThreadBlocks(t, Thread.State.WAITING);
502          assertFalse(lock.isWriteLocked());
503 +        assertTrue(lock.isReadLocked());
504          lock.unlockRead(rs);
505          awaitTermination(t);
506 <        assertFalse(lock.isWriteLocked());
506 >        assertUnlocked(lock);
507      }
508  
509      /**
510 <     * A writelock succeeds only after reading threads unlock
510 >     * writeLock() succeeds only after reading threads unlock
511       */
512      public void testWriteAfterMultipleReadLocks() {
513          final StampedLock lock = new StampedLock();
# Line 525 | Line 530 | public class StampedLockTest extends JSR
530          assertFalse(lock.isWriteLocked());
531          lock.unlockRead(s);
532          awaitTermination(t2);
533 <        assertFalse(lock.isWriteLocked());
533 >        assertUnlocked(lock);
534      }
535  
536      /**
537 <     * Readlocks succeed only after a writing thread unlocks
537 >     * readLock() succeed only after a writing thread unlocks
538       */
539      public void testReadAfterWriteLock() {
540          final StampedLock lock = new StampedLock();
541          final CountDownLatch threadsStarted = new CountDownLatch(2);
542          final long s = lock.writeLock();
543 <        Thread t1 = newStartedThread(new CheckedRunnable() {
539 <            public void realRun() {
540 <                threadsStarted.countDown();
541 <                long rs = lock.readLock();
542 <                lock.unlockRead(rs);
543 <            }});
544 <        Thread t2 = newStartedThread(new CheckedRunnable() {
543 >        final Runnable acquireReleaseReadLock = new CheckedRunnable() {
544              public void realRun() {
545                  threadsStarted.countDown();
546                  long rs = lock.readLock();
547 +                assertTrue(lock.isReadLocked());
548 +                assertFalse(lock.isWriteLocked());
549                  lock.unlockRead(rs);
550 <            }});
550 >            }};
551 >        Thread t1 = newStartedThread(acquireReleaseReadLock);
552 >        Thread t2 = newStartedThread(acquireReleaseReadLock);
553  
554          await(threadsStarted);
555 <        waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS);
556 <        waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS);
555 >        assertThreadBlocks(t1, Thread.State.WAITING);
556 >        assertThreadBlocks(t2, Thread.State.WAITING);
557 >        assertTrue(lock.isWriteLocked());
558 >        assertFalse(lock.isReadLocked());
559          releaseWriteLock(lock, s);
560          awaitTermination(t1);
561          awaitTermination(t2);
562 +        assertUnlocked(lock);
563      }
564  
565      /**
# Line 581 | Line 587 | public class StampedLockTest extends JSR
587          long s = lock.readLock();
588          Thread t = newStartedThread(new CheckedRunnable() {
589              public void realRun() {
590 <                threadAssertEquals(0L, lock.tryWriteLock());
590 >                assertEquals(0L, lock.tryWriteLock());
591              }});
592  
593          awaitTermination(t);
# Line 739 | Line 745 | public class StampedLockTest extends JSR
745       */
746      public void testValidateOptimisticWriteLocked2()
747              throws InterruptedException {
748 <        final CountDownLatch running = new CountDownLatch(1);
748 >        final CountDownLatch locked = new CountDownLatch(1);
749          final StampedLock lock = new StampedLock();
750          final long p = assertValid(lock, lock.tryOptimisticRead());
751  
752          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
753              public void realRun() throws InterruptedException {
754                  lock.writeLockInterruptibly();
755 <                running.countDown();
755 >                locked.countDown();
756                  lock.writeLockInterruptibly();
757              }});
758  
759 <        running.await();
759 >        await(locked);
760          assertFalse(lock.validate(p));
761          assertEquals(0L, lock.tryOptimisticRead());
762 +        assertThreadBlocks(t, Thread.State.WAITING);
763          t.interrupt();
764          awaitTermination(t);
765 +        assertTrue(lock.isWriteLocked());
766      }
767  
768      /**
# Line 974 | Line 982 | public class StampedLockTest extends JSR
982       * IllegalMonitorStateException
983       */
984      public void testCannotUnlockOptimisticReadStamps() {
985 <        Runnable[] actions = {
986 <            () -> {
987 <                StampedLock sl = new StampedLock();
988 <                long stamp = assertValid(sl, sl.tryOptimisticRead());
989 <                sl.unlockRead(stamp);
990 <            },
991 <            () -> {
992 <                StampedLock sl = new StampedLock();
993 <                long stamp = sl.tryOptimisticRead();
994 <                sl.unlock(stamp);
995 <            },
985 >        {
986 >            StampedLock sl = new StampedLock();
987 >            long stamp = assertValid(sl, sl.tryOptimisticRead());
988 >            assertThrows(IllegalMonitorStateException.class,
989 >                () -> sl.unlockRead(stamp));
990 >        }
991 >        {
992 >            StampedLock sl = new StampedLock();
993 >            long stamp = sl.tryOptimisticRead();
994 >            assertThrows(IllegalMonitorStateException.class,
995 >                () -> sl.unlock(stamp));
996 >        }
997  
998 <            () -> {
999 <                StampedLock sl = new StampedLock();
1000 <                long stamp = sl.tryOptimisticRead();
1001 <                sl.writeLock();
1002 <                sl.unlock(stamp);
1003 <            },
1004 <            () -> {
1005 <                StampedLock sl = new StampedLock();
1006 <                sl.readLock();
1007 <                long stamp = assertValid(sl, sl.tryOptimisticRead());
1008 <                sl.unlockRead(stamp);
1009 <            },
1010 <            () -> {
1011 <                StampedLock sl = new StampedLock();
1012 <                sl.readLock();
1013 <                long stamp = assertValid(sl, sl.tryOptimisticRead());
1014 <                sl.unlock(stamp);
1015 <            },
1016 <
1017 <            () -> {
1018 <                StampedLock sl = new StampedLock();
1019 <                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1020 <                assertValid(sl, stamp);
1021 <                sl.writeLock();
1022 <                sl.unlockWrite(stamp);
1023 <            },
1024 <            () -> {
1025 <                StampedLock sl = new StampedLock();
1026 <                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1027 <                sl.writeLock();
1028 <                sl.unlock(stamp);
1029 <            },
1030 <            () -> {
1031 <                StampedLock sl = new StampedLock();
1032 <                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1033 <                sl.readLock();
1034 <                sl.unlockRead(stamp);
1035 <            },
1036 <            () -> {
1037 <                StampedLock sl = new StampedLock();
1038 <                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1039 <                sl.readLock();
1040 <                sl.unlock(stamp);
1041 <            },
1042 <
1043 <            () -> {
1044 <                StampedLock sl = new StampedLock();
1045 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1046 <                assertValid(sl, stamp);
1047 <                sl.writeLock();
1048 <                sl.unlockWrite(stamp);
1040 <            },
1041 <            () -> {
1042 <                StampedLock sl = new StampedLock();
1043 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1044 <                sl.writeLock();
1045 <                sl.unlock(stamp);
1046 <            },
1047 <            () -> {
1048 <                StampedLock sl = new StampedLock();
1049 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1050 <                sl.readLock();
1051 <                sl.unlockRead(stamp);
1052 <            },
1053 <            () -> {
1054 <                StampedLock sl = new StampedLock();
1055 <                sl.readLock();
1056 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1057 <                assertValid(sl, stamp);
1058 <                sl.readLock();
1059 <                sl.unlockRead(stamp);
1060 <            },
1061 <            () -> {
1062 <                StampedLock sl = new StampedLock();
1063 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1064 <                sl.readLock();
1065 <                sl.unlock(stamp);
1066 <            },
1067 <            () -> {
1068 <                StampedLock sl = new StampedLock();
1069 <                sl.readLock();
1070 <                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1071 <                sl.readLock();
1072 <                sl.unlock(stamp);
1073 <            },
1074 <        };
998 >        {
999 >            StampedLock sl = new StampedLock();
1000 >            long stamp = sl.tryOptimisticRead();
1001 >            sl.writeLock();
1002 >            assertThrows(IllegalMonitorStateException.class,
1003 >                () -> sl.unlock(stamp));
1004 >        }
1005 >        {
1006 >            StampedLock sl = new StampedLock();
1007 >            sl.readLock();
1008 >            long stamp = assertValid(sl, sl.tryOptimisticRead());
1009 >            assertThrows(IllegalMonitorStateException.class,
1010 >                () -> sl.unlockRead(stamp));
1011 >        }
1012 >        {
1013 >            StampedLock sl = new StampedLock();
1014 >            sl.readLock();
1015 >            long stamp = assertValid(sl, sl.tryOptimisticRead());
1016 >            assertThrows(IllegalMonitorStateException.class,
1017 >                () -> sl.unlock(stamp));
1018 >        }
1019 >
1020 >        {
1021 >            StampedLock sl = new StampedLock();
1022 >            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1023 >            assertValid(sl, stamp);
1024 >            sl.writeLock();
1025 >            assertThrows(IllegalMonitorStateException.class,
1026 >                () -> sl.unlockWrite(stamp));
1027 >        }
1028 >        {
1029 >            StampedLock sl = new StampedLock();
1030 >            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1031 >            sl.writeLock();
1032 >            assertThrows(IllegalMonitorStateException.class,
1033 >                () -> sl.unlock(stamp));
1034 >        }
1035 >        {
1036 >            StampedLock sl = new StampedLock();
1037 >            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1038 >            sl.readLock();
1039 >            assertThrows(IllegalMonitorStateException.class,
1040 >                () -> sl.unlockRead(stamp));
1041 >        }
1042 >        {
1043 >            StampedLock sl = new StampedLock();
1044 >            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1045 >            sl.readLock();
1046 >            assertThrows(IllegalMonitorStateException.class,
1047 >                () -> sl.unlock(stamp));
1048 >        }
1049  
1050 <        assertThrows(IllegalMonitorStateException.class, actions);
1050 >        {
1051 >            StampedLock sl = new StampedLock();
1052 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1053 >            assertValid(sl, stamp);
1054 >            sl.writeLock();
1055 >            assertThrows(IllegalMonitorStateException.class,
1056 >                () -> sl.unlockWrite(stamp));
1057 >            }
1058 >        {
1059 >            StampedLock sl = new StampedLock();
1060 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1061 >            sl.writeLock();
1062 >            assertThrows(IllegalMonitorStateException.class,
1063 >                () -> sl.unlock(stamp));
1064 >        }
1065 >        {
1066 >            StampedLock sl = new StampedLock();
1067 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1068 >            sl.readLock();
1069 >            assertThrows(IllegalMonitorStateException.class,
1070 >                () -> sl.unlockRead(stamp));
1071 >        }
1072 >        {
1073 >            StampedLock sl = new StampedLock();
1074 >            sl.readLock();
1075 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1076 >            assertValid(sl, stamp);
1077 >            sl.readLock();
1078 >            assertThrows(IllegalMonitorStateException.class,
1079 >                () -> sl.unlockRead(stamp));
1080 >        }
1081 >        {
1082 >            StampedLock sl = new StampedLock();
1083 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1084 >            sl.readLock();
1085 >            assertThrows(IllegalMonitorStateException.class,
1086 >                () -> sl.unlock(stamp));
1087 >        }
1088 >        {
1089 >            StampedLock sl = new StampedLock();
1090 >            sl.readLock();
1091 >            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1092 >            sl.readLock();
1093 >            assertThrows(IllegalMonitorStateException.class,
1094 >                () -> sl.unlock(stamp));
1095 >        }
1096      }
1097  
1098      static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
# Line 1177 | Line 1196 | public class StampedLockTest extends JSR
1196          }
1197          assertUnlocked(lock);
1198      }
1199 +
1200 +    /**
1201 +     * Stamped locks are not reentrant.
1202 +     */
1203 +    public void testNonReentrant() throws InterruptedException {
1204 +        final StampedLock lock = new StampedLock();
1205 +        long stamp;
1206 +
1207 +        stamp = lock.writeLock();
1208 +        assertValid(lock, stamp);
1209 +        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
1210 +        assertEquals(0L, lock.tryReadLock(0L, DAYS));
1211 +        assertValid(lock, stamp);
1212 +        lock.unlockWrite(stamp);
1213 +
1214 +        stamp = lock.tryWriteLock(1L, DAYS);
1215 +        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
1216 +        assertValid(lock, stamp);
1217 +        lock.unlockWrite(stamp);
1218 +
1219 +        stamp = lock.readLock();
1220 +        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
1221 +        assertValid(lock, stamp);
1222 +        lock.unlockRead(stamp);
1223 +    }
1224 +
1225 +    /**
1226 +     * """StampedLocks have no notion of ownership. Locks acquired in
1227 +     * one thread can be released or converted in another."""
1228 +     */
1229 +    public void testNoOwnership() throws Throwable {
1230 +        ArrayList<Future<?>> futures = new ArrayList<>();
1231 +        for (Function<StampedLock, Long> writeLocker : writeLockers())
1232 +        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
1233 +            StampedLock lock = new StampedLock();
1234 +            long stamp = writeLocker.apply(lock);
1235 +            futures.add(cachedThreadPool.submit(new CheckedRunnable() {
1236 +                public void realRun() {
1237 +                    writeUnlocker.accept(lock, stamp);
1238 +                    assertUnlocked(lock);
1239 +                    assertFalse(lock.validate(stamp));
1240 +                }}));
1241 +        }
1242 +        for (Future<?> future : futures)
1243 +            assertNull(future.get());
1244 +    }
1245 +
1246 +    /** Tries out sample usage code from StampedLock javadoc. */
1247 +    public void testSampleUsage() throws Throwable {
1248 +        class Point {
1249 +            private double x, y;
1250 +            private final StampedLock sl = new StampedLock();
1251 +
1252 +            void move(double deltaX, double deltaY) { // an exclusively locked method
1253 +                long stamp = sl.writeLock();
1254 +                try {
1255 +                    x += deltaX;
1256 +                    y += deltaY;
1257 +                } finally {
1258 +                    sl.unlockWrite(stamp);
1259 +                }
1260 +            }
1261 +
1262 +            double distanceFromOrigin() { // A read-only method
1263 +                double currentX, currentY;
1264 +                long stamp = sl.tryOptimisticRead();
1265 +                do {
1266 +                    if (stamp == 0L)
1267 +                        stamp = sl.readLock();
1268 +                    try {
1269 +                        // possibly racy reads
1270 +                        currentX = x;
1271 +                        currentY = y;
1272 +                    } finally {
1273 +                        stamp = sl.tryConvertToOptimisticRead(stamp);
1274 +                    }
1275 +                } while (stamp == 0);
1276 +                return Math.hypot(currentX, currentY);
1277 +            }
1278 +
1279 +            double distanceFromOrigin2() {
1280 +                long stamp = sl.tryOptimisticRead();
1281 +                try {
1282 +                    retryHoldingLock:
1283 +                    for (;; stamp = sl.readLock()) {
1284 +                        if (stamp == 0L)
1285 +                            continue retryHoldingLock;
1286 +                        // possibly racy reads
1287 +                        double currentX = x;
1288 +                        double currentY = y;
1289 +                        if (!sl.validate(stamp))
1290 +                            continue retryHoldingLock;
1291 +                        return Math.hypot(currentX, currentY);
1292 +                    }
1293 +                } finally {
1294 +                    if (StampedLock.isReadLockStamp(stamp))
1295 +                        sl.unlockRead(stamp);
1296 +                }
1297 +            }
1298 +
1299 +            void moveIfAtOrigin(double newX, double newY) {
1300 +                long stamp = sl.readLock();
1301 +                try {
1302 +                    while (x == 0.0 && y == 0.0) {
1303 +                        long ws = sl.tryConvertToWriteLock(stamp);
1304 +                        if (ws != 0L) {
1305 +                            stamp = ws;
1306 +                            x = newX;
1307 +                            y = newY;
1308 +                            return;
1309 +                        }
1310 +                        else {
1311 +                            sl.unlockRead(stamp);
1312 +                            stamp = sl.writeLock();
1313 +                        }
1314 +                    }
1315 +                } finally {
1316 +                    sl.unlock(stamp);
1317 +                }
1318 +            }
1319 +        }
1320 +
1321 +        Point p = new Point();
1322 +        p.move(3.0, 4.0);
1323 +        assertEquals(5.0, p.distanceFromOrigin());
1324 +        p.moveIfAtOrigin(5.0, 12.0);
1325 +        assertEquals(5.0, p.distanceFromOrigin2());
1326 +    }
1327 +
1328 +    /**
1329 +     * Stamp inspection methods work as expected, and do not inspect
1330 +     * the state of the lock itself.
1331 +     */
1332 +    public void testStampStateInspectionMethods() {
1333 +        StampedLock lock = new StampedLock();
1334 +
1335 +        assertFalse(isWriteLockStamp(0L));
1336 +        assertFalse(isReadLockStamp(0L));
1337 +        assertFalse(isLockStamp(0L));
1338 +        assertFalse(isOptimisticReadStamp(0L));
1339 +
1340 +        {
1341 +            long stamp = lock.writeLock();
1342 +            for (int i = 0; i < 2; i++) {
1343 +                assertTrue(isWriteLockStamp(stamp));
1344 +                assertFalse(isReadLockStamp(stamp));
1345 +                assertTrue(isLockStamp(stamp));
1346 +                assertFalse(isOptimisticReadStamp(stamp));
1347 +                if (i == 0)
1348 +                    lock.unlockWrite(stamp);
1349 +            }
1350 +        }
1351 +
1352 +        {
1353 +            long stamp = lock.readLock();
1354 +            for (int i = 0; i < 2; i++) {
1355 +                assertFalse(isWriteLockStamp(stamp));
1356 +                assertTrue(isReadLockStamp(stamp));
1357 +                assertTrue(isLockStamp(stamp));
1358 +                assertFalse(isOptimisticReadStamp(stamp));
1359 +                if (i == 0)
1360 +                    lock.unlockRead(stamp);
1361 +            }
1362 +        }
1363 +
1364 +        {
1365 +            long optimisticStamp = lock.tryOptimisticRead();
1366 +            long readStamp = lock.tryConvertToReadLock(optimisticStamp);
1367 +            long writeStamp = lock.tryConvertToWriteLock(readStamp);
1368 +            for (int i = 0; i < 2; i++) {
1369 +                assertFalse(isWriteLockStamp(optimisticStamp));
1370 +                assertFalse(isReadLockStamp(optimisticStamp));
1371 +                assertFalse(isLockStamp(optimisticStamp));
1372 +                assertTrue(isOptimisticReadStamp(optimisticStamp));
1373 +
1374 +                assertFalse(isWriteLockStamp(readStamp));
1375 +                assertTrue(isReadLockStamp(readStamp));
1376 +                assertTrue(isLockStamp(readStamp));
1377 +                assertFalse(isOptimisticReadStamp(readStamp));
1378 +
1379 +                assertTrue(isWriteLockStamp(writeStamp));
1380 +                assertFalse(isReadLockStamp(writeStamp));
1381 +                assertTrue(isLockStamp(writeStamp));
1382 +                assertFalse(isOptimisticReadStamp(writeStamp));
1383 +                if (i == 0)
1384 +                    lock.unlockWrite(writeStamp);
1385 +            }
1386 +        }
1387 +    }
1388 +
1389   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines