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.26 by jsr166, Sun Jul 17 03:06:50 2016 UTC vs.
Revision 1.36 by jsr166, Mon May 29 19:15:03 2017 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;
11  
12   import java.util.ArrayList;
13   import java.util.List;
14   import java.util.concurrent.CountDownLatch;
15 + import java.util.concurrent.Future;
16   import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.locks.Lock;
18   import java.util.concurrent.locks.StampedLock;
19   import java.util.function.BiConsumer;
18 import java.util.function.Consumer;
20   import java.util.function.Function;
21  
22   import junit.framework.Test;
# Line 30 | Line 31 | public class StampedLockTest extends JSR
31      }
32  
33      /**
33     * A runnable calling writeLockInterruptibly
34     */
35    class InterruptibleLockRunnable extends CheckedRunnable {
36        final StampedLock lock;
37        InterruptibleLockRunnable(StampedLock l) { lock = l; }
38        public void realRun() throws InterruptedException {
39            lock.writeLockInterruptibly();
40        }
41    }
42
43    /**
44     * A runnable calling writeLockInterruptibly that expects to be
45     * interrupted
46     */
47    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
48        final StampedLock lock;
49        InterruptedLockRunnable(StampedLock l) { lock = l; }
50        public void realRun() throws InterruptedException {
51            lock.writeLockInterruptibly();
52        }
53    }
54
55    /**
34       * Releases write lock, checking isWriteLocked before and after
35       */
36 <    void releaseWriteLock(StampedLock lock, long s) {
36 >    void releaseWriteLock(StampedLock lock, long stamp) {
37          assertTrue(lock.isWriteLocked());
38 <        lock.unlockWrite(s);
38 >        assertValid(lock, stamp);
39 >        lock.unlockWrite(stamp);
40          assertFalse(lock.isWriteLocked());
41 +        assertFalse(lock.validate(stamp));
42      }
43  
44      /**
45 <     * Constructed StampedLock is in unlocked state
45 >     * Releases read lock, checking isReadLocked before and after
46       */
47 <    public void testConstructor() {
48 <        StampedLock lock;
49 <        lock = new StampedLock();
50 <        assertFalse(lock.isWriteLocked());
47 >    void releaseReadLock(StampedLock lock, long stamp) {
48 >        assertTrue(lock.isReadLocked());
49 >        assertValid(lock, stamp);
50 >        lock.unlockRead(stamp);
51          assertFalse(lock.isReadLocked());
52 <        assertEquals(lock.getReadLockCount(), 0);
52 >        assertTrue(lock.validate(stamp));
53      }
54  
55 <    /**
56 <     * write-locking and read-locking an unlocked lock succeed
57 <     */
78 <    public void testLock() {
79 <        StampedLock lock = new StampedLock();
80 <        assertFalse(lock.isWriteLocked());
81 <        assertFalse(lock.isReadLocked());
82 <        assertEquals(lock.getReadLockCount(), 0);
83 <        long s = lock.writeLock();
84 <        assertTrue(lock.isWriteLocked());
85 <        assertFalse(lock.isReadLocked());
86 <        assertEquals(lock.getReadLockCount(), 0);
87 <        lock.unlockWrite(s);
88 <        assertFalse(lock.isWriteLocked());
89 <        assertFalse(lock.isReadLocked());
90 <        assertEquals(lock.getReadLockCount(), 0);
91 <        long rs = lock.readLock();
92 <        assertFalse(lock.isWriteLocked());
93 <        assertTrue(lock.isReadLocked());
94 <        assertEquals(lock.getReadLockCount(), 1);
95 <        lock.unlockRead(rs);
96 <        assertFalse(lock.isWriteLocked());
97 <        assertFalse(lock.isReadLocked());
98 <        assertEquals(lock.getReadLockCount(), 0);
55 >    long assertNonZero(long v) {
56 >        assertTrue(v != 0L);
57 >        return v;
58      }
59  
60 <    /**
61 <     * unlock releases either a read or write lock
62 <     */
63 <    public void testUnlock() {
105 <        StampedLock lock = new StampedLock();
106 <        assertFalse(lock.isWriteLocked());
107 <        assertFalse(lock.isReadLocked());
108 <        assertEquals(lock.getReadLockCount(), 0);
109 <        long s = lock.writeLock();
110 <        assertTrue(lock.isWriteLocked());
111 <        assertFalse(lock.isReadLocked());
112 <        assertEquals(lock.getReadLockCount(), 0);
113 <        lock.unlock(s);
114 <        assertFalse(lock.isWriteLocked());
115 <        assertFalse(lock.isReadLocked());
116 <        assertEquals(lock.getReadLockCount(), 0);
117 <        long rs = lock.readLock();
118 <        assertFalse(lock.isWriteLocked());
119 <        assertTrue(lock.isReadLocked());
120 <        assertEquals(lock.getReadLockCount(), 1);
121 <        lock.unlock(rs);
122 <        assertFalse(lock.isWriteLocked());
123 <        assertFalse(lock.isReadLocked());
124 <        assertEquals(lock.getReadLockCount(), 0);
60 >    long assertValid(StampedLock lock, long stamp) {
61 >        assertTrue(stamp != 0L);
62 >        assertTrue(lock.validate(stamp));
63 >        return stamp;
64      }
65  
66 <    /**
128 <     * tryUnlockRead/Write succeeds if locked in associated mode else
129 <     * returns false
130 <     */
131 <    public void testTryUnlock() {
132 <        StampedLock lock = new StampedLock();
133 <        assertFalse(lock.isWriteLocked());
66 >    void assertUnlocked(StampedLock lock) {
67          assertFalse(lock.isReadLocked());
135        assertEquals(lock.getReadLockCount(), 0);
136        long s = lock.writeLock();
137        assertTrue(lock.isWriteLocked());
138        assertFalse(lock.isReadLocked());
139        assertEquals(lock.getReadLockCount(), 0);
140        assertFalse(lock.tryUnlockRead());
141        assertTrue(lock.tryUnlockWrite());
142        assertFalse(lock.tryUnlockWrite());
143        assertFalse(lock.tryUnlockRead());
144        assertFalse(lock.isWriteLocked());
145        assertFalse(lock.isReadLocked());
146        assertEquals(lock.getReadLockCount(), 0);
147        long rs = lock.readLock();
68          assertFalse(lock.isWriteLocked());
69 <        assertTrue(lock.isReadLocked());
70 <        assertEquals(lock.getReadLockCount(), 1);
151 <        assertFalse(lock.tryUnlockWrite());
152 <        assertTrue(lock.tryUnlockRead());
153 <        assertFalse(lock.tryUnlockRead());
154 <        assertFalse(lock.tryUnlockWrite());
155 <        assertFalse(lock.isWriteLocked());
156 <        assertFalse(lock.isReadLocked());
157 <        assertEquals(lock.getReadLockCount(), 0);
69 >        assertEquals(0, lock.getReadLockCount());
70 >        assertValid(lock, lock.tryOptimisticRead());
71      }
72  
73 <    /**
74 <     * write-unlocking an unlocked lock throws IllegalMonitorStateException
75 <     */
76 <    public void testWriteUnlock_IMSE() {
77 <        StampedLock lock = new StampedLock();
78 <        try {
79 <            lock.unlockWrite(0L);
80 <            shouldThrow();
81 <        } catch (IllegalMonitorStateException success) {}
73 >    List<Action> lockLockers(Lock lock) {
74 >        List<Action> lockers = new ArrayList<>();
75 >        lockers.add(() -> lock.lock());
76 >        lockers.add(() -> lock.lockInterruptibly());
77 >        lockers.add(() -> lock.tryLock());
78 >        lockers.add(() -> lock.tryLock(Long.MIN_VALUE, DAYS));
79 >        lockers.add(() -> lock.tryLock(0L, DAYS));
80 >        lockers.add(() -> lock.tryLock(Long.MAX_VALUE, DAYS));
81 >        return lockers;
82 >    }
83 >
84 >    List<Function<StampedLock, Long>> readLockers() {
85 >        List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
86 >        readLockers.add(sl -> sl.readLock());
87 >        readLockers.add(sl -> sl.tryReadLock());
88 >        readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl));
89 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
90 >        readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS));
91 >        readLockers.add(sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
92 >        return readLockers;
93 >    }
94 >
95 >    List<BiConsumer<StampedLock, Long>> readUnlockers() {
96 >        List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>();
97 >        readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp));
98 >        readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead()));
99 >        readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock());
100 >        readUnlockers.add((sl, stamp) -> sl.unlock(stamp));
101 >        readUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
102 >        return readUnlockers;
103 >    }
104 >
105 >    List<Function<StampedLock, Long>> writeLockers() {
106 >        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
107 >        writeLockers.add(sl -> sl.writeLock());
108 >        writeLockers.add(sl -> sl.tryWriteLock());
109 >        writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl));
110 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
111 >        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS));
112 >        writeLockers.add(sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
113 >        return writeLockers;
114 >    }
115 >
116 >    List<BiConsumer<StampedLock, Long>> writeUnlockers() {
117 >        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
118 >        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
119 >        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
120 >        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
121 >        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
122 >        writeUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
123 >        return writeUnlockers;
124      }
125  
126      /**
127 <     * write-unlocking an unlocked lock throws IllegalMonitorStateException
127 >     * Constructed StampedLock is in unlocked state
128       */
129 <    public void testWriteUnlock_IMSE2() {
130 <        StampedLock lock = new StampedLock();
176 <        long s = lock.writeLock();
177 <        lock.unlockWrite(s);
178 <        try {
179 <            lock.unlockWrite(s);
180 <            shouldThrow();
181 <        } catch (IllegalMonitorStateException success) {}
129 >    public void testConstructor() {
130 >        assertUnlocked(new StampedLock());
131      }
132  
133      /**
134 <     * write-unlocking after readlock throws IllegalMonitorStateException
134 >     * write-locking, then unlocking, an unlocked lock succeed
135       */
136 <    public void testWriteUnlock_IMSE3() {
136 >    public void testWriteLock_lockUnlock() {
137          StampedLock lock = new StampedLock();
138 <        long s = lock.readLock();
139 <        try {
140 <            lock.unlockWrite(s);
141 <            shouldThrow();
142 <        } catch (IllegalMonitorStateException success) {}
138 >
139 >        for (Function<StampedLock, Long> writeLocker : writeLockers())
140 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
141 >            assertFalse(lock.isWriteLocked());
142 >            assertFalse(lock.isReadLocked());
143 >            assertEquals(0, lock.getReadLockCount());
144 >
145 >            long s = writeLocker.apply(lock);
146 >            assertValid(lock, s);
147 >            assertTrue(lock.isWriteLocked());
148 >            assertFalse(lock.isReadLocked());
149 >            assertEquals(0, lock.getReadLockCount());
150 >            writeUnlocker.accept(lock, s);
151 >            assertUnlocked(lock);
152 >        }
153      }
154  
155      /**
156 <     * read-unlocking an unlocked lock throws IllegalMonitorStateException
156 >     * read-locking, then unlocking, an unlocked lock succeed
157       */
158 <    public void testReadUnlock_IMSE() {
158 >    public void testReadLock_lockUnlock() {
159          StampedLock lock = new StampedLock();
160 <        long s = lock.readLock();
161 <        lock.unlockRead(s);
162 <        try {
163 <            lock.unlockRead(s);
164 <            shouldThrow();
165 <        } catch (IllegalMonitorStateException success) {}
160 >
161 >        for (Function<StampedLock, Long> readLocker : readLockers())
162 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
163 >            long s = 42;
164 >            for (int i = 0; i < 2; i++) {
165 >                s = assertValid(lock, readLocker.apply(lock));
166 >                assertFalse(lock.isWriteLocked());
167 >                assertTrue(lock.isReadLocked());
168 >                assertEquals(i + 1, lock.getReadLockCount());
169 >            }
170 >            for (int i = 0; i < 2; i++) {
171 >                assertFalse(lock.isWriteLocked());
172 >                assertTrue(lock.isReadLocked());
173 >                assertEquals(2 - i, lock.getReadLockCount());
174 >                readUnlocker.accept(lock, s);
175 >            }
176 >            assertUnlocked(lock);
177 >        }
178      }
179  
180      /**
181 <     * read-unlocking an unlocked lock throws IllegalMonitorStateException
181 >     * tryUnlockWrite fails if not write locked
182       */
183 <    public void testReadUnlock_IMSE2() {
183 >    public void testTryUnlockWrite_failure() {
184          StampedLock lock = new StampedLock();
185 <        try {
186 <            lock.unlockRead(0L);
187 <            shouldThrow();
188 <        } catch (IllegalMonitorStateException success) {}
185 >        assertFalse(lock.tryUnlockWrite());
186 >
187 >        for (Function<StampedLock, Long> readLocker : readLockers())
188 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
189 >            long s = assertValid(lock, readLocker.apply(lock));
190 >            assertFalse(lock.tryUnlockWrite());
191 >            assertTrue(lock.isReadLocked());
192 >            readUnlocker.accept(lock, s);
193 >            assertUnlocked(lock);
194 >        }
195      }
196  
197      /**
198 <     * read-unlocking after writeLock throws IllegalMonitorStateException
198 >     * tryUnlockRead fails if not read locked
199       */
200 <    public void testReadUnlock_IMSE3() {
200 >    public void testTryUnlockRead_failure() {
201          StampedLock lock = new StampedLock();
202 <        long s = lock.writeLock();
203 <        try {
204 <            lock.unlockRead(s);
205 <            shouldThrow();
206 <        } catch (IllegalMonitorStateException success) {}
202 >        assertFalse(lock.tryUnlockRead());
203 >
204 >        for (Function<StampedLock, Long> writeLocker : writeLockers())
205 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
206 >            long s = writeLocker.apply(lock);
207 >            assertFalse(lock.tryUnlockRead());
208 >            assertTrue(lock.isWriteLocked());
209 >            writeUnlocker.accept(lock, s);
210 >            assertUnlocked(lock);
211 >        }
212      }
213  
214      /**
215 <     * validate(0) fails
215 >     * validate(0L) fails
216       */
217      public void testValidate0() {
218          StampedLock lock = new StampedLock();
# Line 238 | Line 220 | public class StampedLockTest extends JSR
220      }
221  
222      /**
223 <     * A stamp obtained from a successful lock operation validates
223 >     * A stamp obtained from a successful lock operation validates while the lock is held
224       */
225      public void testValidate() throws InterruptedException {
226          StampedLock lock = new StampedLock();
227 <        long s = lock.writeLock();
228 <        assertTrue(lock.validate(s));
229 <        lock.unlockWrite(s);
230 <        s = lock.readLock();
231 <        assertTrue(lock.validate(s));
232 <        lock.unlockRead(s);
233 <        assertTrue((s = lock.tryWriteLock()) != 0L);
234 <        assertTrue(lock.validate(s));
235 <        lock.unlockWrite(s);
236 <        assertTrue((s = lock.tryReadLock()) != 0L);
237 <        assertTrue(lock.validate(s));
238 <        lock.unlockRead(s);
239 <        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
240 <        assertTrue(lock.validate(s));
259 <        lock.unlockWrite(s);
260 <        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
261 <        assertTrue(lock.validate(s));
262 <        lock.unlockRead(s);
263 <        assertTrue((s = lock.tryOptimisticRead()) != 0L);
227 >
228 >        for (Function<StampedLock, Long> readLocker : readLockers())
229 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
230 >            long s = assertNonZero(readLocker.apply(lock));
231 >            assertTrue(lock.validate(s));
232 >            readUnlocker.accept(lock, s);
233 >        }
234 >
235 >        for (Function<StampedLock, Long> writeLocker : writeLockers())
236 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
237 >            long s = assertNonZero(writeLocker.apply(lock));
238 >            assertTrue(lock.validate(s));
239 >            writeUnlocker.accept(lock, s);
240 >        }
241      }
242  
243      /**
# Line 268 | Line 245 | public class StampedLockTest extends JSR
245       */
246      public void testValidate2() throws InterruptedException {
247          StampedLock lock = new StampedLock();
248 <        long s;
272 <        assertTrue((s = lock.writeLock()) != 0L);
248 >        long s = assertNonZero(lock.writeLock());
249          assertTrue(lock.validate(s));
250          assertFalse(lock.validate(lock.tryWriteLock()));
251 <        assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
251 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
252 >                                                    randomTimeUnit())));
253          assertFalse(lock.validate(lock.tryReadLock()));
254 <        assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
254 >        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
255 >                                                    randomTimeUnit())));
256          assertFalse(lock.validate(lock.tryOptimisticRead()));
257          lock.unlockWrite(s);
258      }
259  
260 +    void assertThrowInterruptedExceptionWhenPreInterrupted(Action[] actions) {
261 +        for (Action action : actions) {
262 +            Thread.currentThread().interrupt();
263 +            try {
264 +                action.run();
265 +                shouldThrow();
266 +            }
267 +            catch (InterruptedException success) {}
268 +            catch (Throwable fail) { threadUnexpectedException(fail); }
269 +            assertFalse(Thread.interrupted());
270 +        }
271 +    }
272 +
273      /**
274 <     * writeLockInterruptibly is interruptible
274 >     * interruptible operations throw InterruptedException when pre-interrupted
275       */
276 <    public void testWriteLockInterruptibly_Interruptible()
286 <            throws InterruptedException {
287 <        final CountDownLatch running = new CountDownLatch(1);
276 >    public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
277          final StampedLock lock = new StampedLock();
289        long s = lock.writeLock();
290        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
291            public void realRun() throws InterruptedException {
292                running.countDown();
293                lock.writeLockInterruptibly();
294            }});
278  
279 <        running.await();
280 <        waitForThreadToEnterWaitState(t, 100);
281 <        t.interrupt();
282 <        awaitTermination(t);
283 <        releaseWriteLock(lock, s);
279 >        Action[] interruptibleLockActions = {
280 >            () -> lock.writeLockInterruptibly(),
281 >            () -> lock.tryWriteLock(Long.MIN_VALUE, DAYS),
282 >            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
283 >            () -> lock.readLockInterruptibly(),
284 >            () -> lock.tryReadLock(Long.MIN_VALUE, DAYS),
285 >            () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
286 >            () -> lock.asWriteLock().lockInterruptibly(),
287 >            () -> lock.asWriteLock().tryLock(0L, DAYS),
288 >            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
289 >            () -> lock.asReadLock().lockInterruptibly(),
290 >            () -> lock.asReadLock().tryLock(0L, DAYS),
291 >            () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
292 >        };
293 >        shuffle(interruptibleLockActions);
294 >
295 >        assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
296 >        {
297 >            long s = lock.writeLock();
298 >            assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
299 >            lock.unlockWrite(s);
300 >        }
301 >        {
302 >            long s = lock.readLock();
303 >            assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
304 >            lock.unlockRead(s);
305 >        }
306 >    }
307 >
308 >    void assertThrowInterruptedExceptionWhenInterrupted(Action[] actions) {
309 >        int n = actions.length;
310 >        Future<?>[] futures = new Future<?>[n];
311 >        CountDownLatch threadsStarted = new CountDownLatch(n);
312 >        CountDownLatch done = new CountDownLatch(n);
313 >
314 >        for (int i = 0; i < n; i++) {
315 >            Action action = actions[i];
316 >            futures[i] = cachedThreadPool.submit(new CheckedRunnable() {
317 >                public void realRun() throws Throwable {
318 >                    threadsStarted.countDown();
319 >                    try {
320 >                        action.run();
321 >                        shouldThrow();
322 >                    }
323 >                    catch (InterruptedException success) {}
324 >                    catch (Throwable fail) { threadUnexpectedException(fail); }
325 >                    assertFalse(Thread.interrupted());
326 >                    done.countDown();
327 >                }});
328 >        }
329 >
330 >        await(threadsStarted);
331 >        assertEquals(n, done.getCount());
332 >        for (Future<?> future : futures) // Interrupt all the tasks
333 >            future.cancel(true);
334 >        await(done);
335      }
336  
337      /**
338 <     * timed tryWriteLock is interruptible
338 >     * interruptible operations throw InterruptedException when write locked and interrupted
339       */
340 <    public void testWriteTryLock_Interruptible() throws InterruptedException {
307 <        final CountDownLatch running = new CountDownLatch(1);
340 >    public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
341          final StampedLock lock = new StampedLock();
342          long s = lock.writeLock();
310        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
311            public void realRun() throws InterruptedException {
312                running.countDown();
313                lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
314            }});
343  
344 <        running.await();
345 <        waitForThreadToEnterWaitState(t, 100);
346 <        t.interrupt();
347 <        awaitTermination(t);
348 <        releaseWriteLock(lock, s);
344 >        Action[] interruptibleLockBlockingActions = {
345 >            () -> lock.writeLockInterruptibly(),
346 >            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
347 >            () -> lock.readLockInterruptibly(),
348 >            () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
349 >            () -> lock.asWriteLock().lockInterruptibly(),
350 >            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
351 >            () -> lock.asReadLock().lockInterruptibly(),
352 >            () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
353 >        };
354 >        shuffle(interruptibleLockBlockingActions);
355 >
356 >        assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
357      }
358  
359      /**
360 <     * readLockInterruptibly is interruptible
360 >     * interruptible operations throw InterruptedException when read locked and interrupted
361       */
362 <    public void testReadLockInterruptibly_Interruptible()
327 <            throws InterruptedException {
328 <        final CountDownLatch running = new CountDownLatch(1);
362 >    public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
363          final StampedLock lock = new StampedLock();
364 <        long s = lock.writeLock();
331 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
332 <            public void realRun() throws InterruptedException {
333 <                running.countDown();
334 <                lock.readLockInterruptibly();
335 <            }});
364 >        long s = lock.readLock();
365  
366 <        running.await();
367 <        waitForThreadToEnterWaitState(t, 100);
368 <        t.interrupt();
369 <        awaitTermination(t);
370 <        releaseWriteLock(lock, s);
366 >        Action[] interruptibleLockBlockingActions = {
367 >            () -> lock.writeLockInterruptibly(),
368 >            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
369 >            () -> lock.asWriteLock().lockInterruptibly(),
370 >            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
371 >        };
372 >        shuffle(interruptibleLockBlockingActions);
373 >
374 >        assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
375      }
376  
377      /**
378 <     * timed tryReadLock is interruptible
378 >     * Non-interruptible operations ignore and preserve interrupt status
379       */
380 <    public void testReadTryLock_Interruptible() throws InterruptedException {
348 <        final CountDownLatch running = new CountDownLatch(1);
380 >    public void testNonInterruptibleOperationsIgnoreInterrupts() {
381          final StampedLock lock = new StampedLock();
382 <        long s = lock.writeLock();
351 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
352 <            public void realRun() throws InterruptedException {
353 <                running.countDown();
354 <                lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
355 <            }});
382 >        Thread.currentThread().interrupt();
383  
384 <        running.await();
385 <        waitForThreadToEnterWaitState(t, 100);
386 <        t.interrupt();
387 <        awaitTermination(t);
388 <        releaseWriteLock(lock, s);
384 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
385 >            long s = assertValid(lock, lock.readLock());
386 >            readUnlocker.accept(lock, s);
387 >            s = assertValid(lock, lock.tryReadLock());
388 >            readUnlocker.accept(lock, s);
389 >        }
390 >
391 >        lock.asReadLock().lock();
392 >        lock.asReadLock().unlock();
393 >
394 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
395 >            long s = assertValid(lock, lock.writeLock());
396 >            writeUnlocker.accept(lock, s);
397 >            s = assertValid(lock, lock.tryWriteLock());
398 >            writeUnlocker.accept(lock, s);
399 >        }
400 >
401 >        lock.asWriteLock().lock();
402 >        lock.asWriteLock().unlock();
403 >
404 >        assertTrue(Thread.interrupted());
405      }
406  
407      /**
408       * tryWriteLock on an unlocked lock succeeds
409       */
410 <    public void testWriteTryLock() {
410 >    public void testTryWriteLock() {
411          final StampedLock lock = new StampedLock();
412          long s = lock.tryWriteLock();
413          assertTrue(s != 0L);
414          assertTrue(lock.isWriteLocked());
415 <        long s2 = lock.tryWriteLock();
373 <        assertEquals(s2, 0L);
415 >        assertEquals(0L, lock.tryWriteLock());
416          releaseWriteLock(lock, s);
417      }
418  
419      /**
420       * tryWriteLock fails if locked
421       */
422 <    public void testWriteTryLockWhenLocked() {
422 >    public void testTryWriteLockWhenLocked() {
423          final StampedLock lock = new StampedLock();
424          long s = lock.writeLock();
425          Thread t = newStartedThread(new CheckedRunnable() {
426              public void realRun() {
427 <                long ws = lock.tryWriteLock();
386 <                assertTrue(ws == 0L);
427 >                assertEquals(0L, lock.tryWriteLock());
428              }});
429  
430 +        assertEquals(0L, lock.tryWriteLock());
431          awaitTermination(t);
432          releaseWriteLock(lock, s);
433      }
# Line 393 | Line 435 | public class StampedLockTest extends JSR
435      /**
436       * tryReadLock fails if write-locked
437       */
438 <    public void testReadTryLockWhenLocked() {
438 >    public void testTryReadLockWhenLocked() {
439          final StampedLock lock = new StampedLock();
440          long s = lock.writeLock();
441          Thread t = newStartedThread(new CheckedRunnable() {
442              public void realRun() {
443 <                long rs = lock.tryReadLock();
402 <                assertEquals(rs, 0L);
443 >                assertEquals(0L, lock.tryReadLock());
444              }});
445  
446 +        assertEquals(0L, lock.tryReadLock());
447          awaitTermination(t);
448          releaseWriteLock(lock, s);
449      }
# Line 415 | Line 457 | public class StampedLockTest extends JSR
457          Thread t = newStartedThread(new CheckedRunnable() {
458              public void realRun() throws InterruptedException {
459                  long s2 = lock.tryReadLock();
460 <                assertTrue(s2 != 0L);
460 >                assertValid(lock, s2);
461                  lock.unlockRead(s2);
462                  long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
463 <                assertTrue(s3 != 0L);
463 >                assertValid(lock, s3);
464                  lock.unlockRead(s3);
465                  long s4 = lock.readLock();
466 +                assertValid(lock, s4);
467                  lock.unlockRead(s4);
468 +                lock.asReadLock().lock();
469 +                lock.asReadLock().unlock();
470 +                lock.asReadLock().lockInterruptibly();
471 +                lock.asReadLock().unlock();
472 +                lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS);
473 +                lock.asReadLock().unlock();
474              }});
475  
476          awaitTermination(t);
# Line 429 | Line 478 | public class StampedLockTest extends JSR
478      }
479  
480      /**
481 <     * A writelock succeeds only after a reading thread unlocks
481 >     * writeLock() succeeds only after a reading thread unlocks
482       */
483      public void testWriteAfterReadLock() throws InterruptedException {
484 <        final CountDownLatch running = new CountDownLatch(1);
484 >        final CountDownLatch aboutToLock = new CountDownLatch(1);
485          final StampedLock lock = new StampedLock();
486          long rs = lock.readLock();
487          Thread t = newStartedThread(new CheckedRunnable() {
488              public void realRun() {
489 <                running.countDown();
489 >                aboutToLock.countDown();
490                  long s = lock.writeLock();
491 +                assertTrue(lock.isWriteLocked());
492 +                assertFalse(lock.isReadLocked());
493                  lock.unlockWrite(s);
494              }});
495  
496 <        running.await();
497 <        waitForThreadToEnterWaitState(t, 100);
496 >        await(aboutToLock);
497 >        assertThreadBlocks(t, Thread.State.WAITING);
498          assertFalse(lock.isWriteLocked());
499 +        assertTrue(lock.isReadLocked());
500          lock.unlockRead(rs);
501          awaitTermination(t);
502 <        assertFalse(lock.isWriteLocked());
502 >        assertUnlocked(lock);
503      }
504  
505      /**
506 <     * A writelock succeeds only after reading threads unlock
506 >     * writeLock() succeeds only after reading threads unlock
507       */
508      public void testWriteAfterMultipleReadLocks() {
509          final StampedLock lock = new StampedLock();
# Line 470 | Line 522 | public class StampedLockTest extends JSR
522                  lock.unlockWrite(ws);
523              }});
524  
525 +        assertTrue(lock.isReadLocked());
526          assertFalse(lock.isWriteLocked());
527          lock.unlockRead(s);
528          awaitTermination(t2);
529 <        assertFalse(lock.isWriteLocked());
529 >        assertUnlocked(lock);
530      }
531  
532      /**
533 <     * Readlocks succeed only after a writing thread unlocks
533 >     * readLock() succeed only after a writing thread unlocks
534       */
535      public void testReadAfterWriteLock() {
536          final StampedLock lock = new StampedLock();
537 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
538          final long s = lock.writeLock();
539 <        Thread t1 = newStartedThread(new CheckedRunnable() {
486 <            public void realRun() {
487 <                long rs = lock.readLock();
488 <                lock.unlockRead(rs);
489 <            }});
490 <        Thread t2 = newStartedThread(new CheckedRunnable() {
539 >        final Runnable acquireReleaseReadLock = new CheckedRunnable() {
540              public void realRun() {
541 +                threadsStarted.countDown();
542                  long rs = lock.readLock();
543 +                assertTrue(lock.isReadLocked());
544 +                assertFalse(lock.isWriteLocked());
545                  lock.unlockRead(rs);
546 <            }});
547 <
546 >            }};
547 >        Thread t1 = newStartedThread(acquireReleaseReadLock);
548 >        Thread t2 = newStartedThread(acquireReleaseReadLock);
549 >
550 >        await(threadsStarted);
551 >        assertThreadBlocks(t1, Thread.State.WAITING);
552 >        assertThreadBlocks(t2, Thread.State.WAITING);
553 >        assertTrue(lock.isWriteLocked());
554 >        assertFalse(lock.isReadLocked());
555          releaseWriteLock(lock, s);
556          awaitTermination(t1);
557          awaitTermination(t2);
558 +        assertUnlocked(lock);
559      }
560  
561      /**
562 <     * tryReadLock succeeds if readlocked but not writelocked
562 >     * tryReadLock succeeds if read locked but not write locked
563       */
564      public void testTryLockWhenReadLocked() {
565          final StampedLock lock = new StampedLock();
# Line 507 | Line 567 | public class StampedLockTest extends JSR
567          Thread t = newStartedThread(new CheckedRunnable() {
568              public void realRun() {
569                  long rs = lock.tryReadLock();
570 <                threadAssertTrue(rs != 0L);
570 >                assertValid(lock, rs);
571                  lock.unlockRead(rs);
572              }});
573  
# Line 516 | Line 576 | public class StampedLockTest extends JSR
576      }
577  
578      /**
579 <     * tryWriteLock fails when readlocked
579 >     * tryWriteLock fails when read locked
580       */
581 <    public void testWriteTryLockWhenReadLocked() {
581 >    public void testTryWriteLockWhenReadLocked() {
582          final StampedLock lock = new StampedLock();
583          long s = lock.readLock();
584          Thread t = newStartedThread(new CheckedRunnable() {
585              public void realRun() {
586 <                long ws = lock.tryWriteLock();
527 <                threadAssertEquals(ws, 0L);
586 >                threadAssertEquals(0L, lock.tryWriteLock());
587              }});
588  
589          awaitTermination(t);
# Line 532 | Line 591 | public class StampedLockTest extends JSR
591      }
592  
593      /**
594 <     * timed tryWriteLock times out if locked
594 >     * timed lock operations time out if lock not available
595       */
596 <    public void testWriteTryLock_Timeout() {
596 >    public void testTimedLock_Timeout() throws Exception {
597 >        ArrayList<Future<?>> futures = new ArrayList<>();
598 >
599 >        // Write locked
600          final StampedLock lock = new StampedLock();
601 <        long s = lock.writeLock();
602 <        Thread t = newStartedThread(new CheckedRunnable() {
601 >        long stamp = lock.writeLock();
602 >        assertEquals(0L, lock.tryReadLock(0L, DAYS));
603 >        assertEquals(0L, lock.tryReadLock(Long.MIN_VALUE, DAYS));
604 >        assertFalse(lock.asReadLock().tryLock(0L, DAYS));
605 >        assertFalse(lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS));
606 >        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
607 >        assertEquals(0L, lock.tryWriteLock(Long.MIN_VALUE, DAYS));
608 >        assertFalse(lock.asWriteLock().tryLock(0L, DAYS));
609 >        assertFalse(lock.asWriteLock().tryLock(Long.MIN_VALUE, DAYS));
610 >
611 >        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
612              public void realRun() throws InterruptedException {
613                  long startTime = System.nanoTime();
614 <                long timeoutMillis = 10;
615 <                long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
616 <                assertEquals(ws, 0L);
546 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
547 <            }});
614 >                assertEquals(0L, lock.tryWriteLock(timeoutMillis(), MILLISECONDS));
615 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
616 >            }}));
617  
618 <        awaitTermination(t);
619 <        releaseWriteLock(lock, s);
620 <    }
618 >        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
619 >            public void realRun() throws InterruptedException {
620 >                long startTime = System.nanoTime();
621 >                assertEquals(0L, lock.tryReadLock(timeoutMillis(), MILLISECONDS));
622 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
623 >            }}));
624 >
625 >        // Read locked
626 >        final StampedLock lock2 = new StampedLock();
627 >        long stamp2 = lock2.readLock();
628 >        assertEquals(0L, lock2.tryWriteLock(0L, DAYS));
629 >        assertEquals(0L, lock2.tryWriteLock(Long.MIN_VALUE, DAYS));
630 >        assertFalse(lock2.asWriteLock().tryLock(0L, DAYS));
631 >        assertFalse(lock2.asWriteLock().tryLock(Long.MIN_VALUE, DAYS));
632  
633 <    /**
554 <     * timed tryReadLock times out if write-locked
555 <     */
556 <    public void testReadTryLock_Timeout() {
557 <        final StampedLock lock = new StampedLock();
558 <        long s = lock.writeLock();
559 <        Thread t = newStartedThread(new CheckedRunnable() {
633 >        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
634              public void realRun() throws InterruptedException {
635                  long startTime = System.nanoTime();
636 <                long timeoutMillis = 10;
637 <                long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
638 <                assertEquals(rs, 0L);
565 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
566 <            }});
636 >                assertEquals(0L, lock2.tryWriteLock(timeoutMillis(), MILLISECONDS));
637 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
638 >            }}));
639  
640 <        awaitTermination(t);
641 <        assertTrue(lock.isWriteLocked());
642 <        lock.unlockWrite(s);
640 >        for (Future<?> future : futures)
641 >            assertNull(future.get());
642 >
643 >        releaseWriteLock(lock, stamp);
644 >        releaseReadLock(lock2, stamp2);
645      }
646  
647      /**
648 <     * writeLockInterruptibly succeeds if unlocked, else is interruptible
648 >     * writeLockInterruptibly succeeds if unlocked
649       */
650      public void testWriteLockInterruptibly() throws InterruptedException {
577        final CountDownLatch running = new CountDownLatch(1);
651          final StampedLock lock = new StampedLock();
652          long s = lock.writeLockInterruptibly();
580        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
581            public void realRun() throws InterruptedException {
582                running.countDown();
583                lock.writeLockInterruptibly();
584            }});
585
586        running.await();
587        waitForThreadToEnterWaitState(t, 100);
588        t.interrupt();
653          assertTrue(lock.isWriteLocked());
590        awaitTermination(t);
654          releaseWriteLock(lock, s);
655      }
656  
657      /**
658 <     * readLockInterruptibly succeeds if lock free else is interruptible
658 >     * readLockInterruptibly succeeds if lock free
659       */
660      public void testReadLockInterruptibly() throws InterruptedException {
598        final CountDownLatch running = new CountDownLatch(1);
661          final StampedLock lock = new StampedLock();
662 <        long s;
663 <        s = lock.readLockInterruptibly();
662 >
663 >        long s = assertValid(lock, lock.readLockInterruptibly());
664 >        assertTrue(lock.isReadLocked());
665          lock.unlockRead(s);
603        s = lock.writeLockInterruptibly();
604        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
605            public void realRun() throws InterruptedException {
606                running.countDown();
607                lock.readLockInterruptibly();
608            }});
666  
667 <        running.await();
668 <        waitForThreadToEnterWaitState(t, 100);
669 <        t.interrupt();
613 <        awaitTermination(t);
614 <        releaseWriteLock(lock, s);
667 >        lock.asReadLock().lockInterruptibly();
668 >        assertTrue(lock.isReadLocked());
669 >        lock.asReadLock().unlock();
670      }
671  
672      /**
# Line 643 | Line 698 | public class StampedLockTest extends JSR
698      }
699  
700      /**
701 <     * tryOptimisticRead succeeds and validates if unlocked, fails if locked
701 >     * tryOptimisticRead succeeds and validates if unlocked, fails if
702 >     * exclusively locked
703       */
704      public void testValidateOptimistic() throws InterruptedException {
705          StampedLock lock = new StampedLock();
706 <        long s, p;
707 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
708 <        assertTrue(lock.validate(p));
709 <        assertTrue((s = lock.writeLock()) != 0L);
710 <        assertFalse((p = lock.tryOptimisticRead()) != 0L);
711 <        assertTrue(lock.validate(s));
712 <        lock.unlockWrite(s);
713 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
714 <        assertTrue(lock.validate(p));
715 <        assertTrue((s = lock.readLock()) != 0L);
716 <        assertTrue(lock.validate(s));
717 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
718 <        assertTrue(lock.validate(p));
719 <        lock.unlockRead(s);
720 <        assertTrue((s = lock.tryWriteLock()) != 0L);
721 <        assertTrue(lock.validate(s));
722 <        assertFalse((p = lock.tryOptimisticRead()) != 0L);
667 <        lock.unlockWrite(s);
668 <        assertTrue((s = lock.tryReadLock()) != 0L);
669 <        assertTrue(lock.validate(s));
670 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
671 <        lock.unlockRead(s);
672 <        assertTrue(lock.validate(p));
673 <        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
674 <        assertFalse((p = lock.tryOptimisticRead()) != 0L);
675 <        assertTrue(lock.validate(s));
676 <        lock.unlockWrite(s);
677 <        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
678 <        assertTrue(lock.validate(s));
679 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
680 <        lock.unlockRead(s);
681 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
706 >
707 >        assertValid(lock, lock.tryOptimisticRead());
708 >
709 >        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
710 >            long s = assertValid(lock, writeLocker.apply(lock));
711 >            assertEquals(0L, lock.tryOptimisticRead());
712 >            releaseWriteLock(lock, s);
713 >        }
714 >
715 >        for (Function<StampedLock, Long> readLocker : readLockers()) {
716 >            long s = assertValid(lock, readLocker.apply(lock));
717 >            long p = assertValid(lock, lock.tryOptimisticRead());
718 >            releaseReadLock(lock, s);
719 >            assertTrue(lock.validate(p));
720 >        }
721 >
722 >        assertValid(lock, lock.tryOptimisticRead());
723      }
724  
725      /**
726       * tryOptimisticRead stamp does not validate if a write lock intervenes
727       */
728      public void testValidateOptimisticWriteLocked() {
729 <        StampedLock lock = new StampedLock();
730 <        long s, p;
731 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
691 <        assertTrue((s = lock.writeLock()) != 0L);
729 >        final StampedLock lock = new StampedLock();
730 >        final long p = assertValid(lock, lock.tryOptimisticRead());
731 >        final long s = assertValid(lock, lock.writeLock());
732          assertFalse(lock.validate(p));
733 <        assertFalse((p = lock.tryOptimisticRead()) != 0L);
733 >        assertEquals(0L, lock.tryOptimisticRead());
734          assertTrue(lock.validate(s));
735          lock.unlockWrite(s);
736      }
# Line 701 | Line 741 | public class StampedLockTest extends JSR
741       */
742      public void testValidateOptimisticWriteLocked2()
743              throws InterruptedException {
744 <        final CountDownLatch running = new CountDownLatch(1);
744 >        final CountDownLatch locked = new CountDownLatch(1);
745          final StampedLock lock = new StampedLock();
746 <        long s, p;
747 <        assertTrue((p = lock.tryOptimisticRead()) != 0L);
746 >        final long p = assertValid(lock, lock.tryOptimisticRead());
747 >
748          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
749              public void realRun() throws InterruptedException {
750                  lock.writeLockInterruptibly();
751 <                running.countDown();
751 >                locked.countDown();
752                  lock.writeLockInterruptibly();
753              }});
754  
755 <        running.await();
755 >        await(locked);
756          assertFalse(lock.validate(p));
757 <        assertFalse((p = lock.tryOptimisticRead()) != 0L);
757 >        assertEquals(0L, lock.tryOptimisticRead());
758 >        assertThreadBlocks(t, Thread.State.WAITING);
759          t.interrupt();
760          awaitTermination(t);
761 +        assertTrue(lock.isWriteLocked());
762      }
763  
764      /**
765 <     * tryConvertToOptimisticRead succeeds and validates if successfully locked,
765 >     * tryConvertToOptimisticRead succeeds and validates if successfully locked
766       */
767      public void testTryConvertToOptimisticRead() throws InterruptedException {
768          StampedLock lock = new StampedLock();
769 <        long s, p;
769 >        long s, p, q;
770          assertEquals(0L, lock.tryConvertToOptimisticRead(0L));
771  
772 <        assertTrue((s = lock.tryOptimisticRead()) != 0L);
731 <        assertEquals(s, lock.tryConvertToOptimisticRead(s));
732 <        assertTrue(lock.validate(s));
733 <
734 <        assertTrue((p = lock.readLock()) != 0L);
735 <        assertTrue((s = lock.tryOptimisticRead()) != 0L);
772 >        s = assertValid(lock, lock.tryOptimisticRead());
773          assertEquals(s, lock.tryConvertToOptimisticRead(s));
774          assertTrue(lock.validate(s));
738        lock.unlockRead(p);
739
740        assertTrue((s = lock.writeLock()) != 0L);
741        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
742        assertTrue(lock.validate(p));
775  
776 <        assertTrue((s = lock.readLock()) != 0L);
777 <        assertTrue(lock.validate(s));
778 <        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
779 <        assertTrue(lock.validate(p));
780 <
781 <        assertTrue((s = lock.tryWriteLock()) != 0L);
782 <        assertTrue(lock.validate(s));
751 <        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
752 <        assertTrue(lock.validate(p));
753 <
754 <        assertTrue((s = lock.tryReadLock()) != 0L);
755 <        assertTrue(lock.validate(s));
756 <        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
757 <        assertTrue(lock.validate(p));
758 <
759 <        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
760 <        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
761 <        assertTrue(lock.validate(p));
776 >        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
777 >            s = assertValid(lock, writeLocker.apply(lock));
778 >            p = assertValid(lock, lock.tryConvertToOptimisticRead(s));
779 >            assertFalse(lock.validate(s));
780 >            assertTrue(lock.validate(p));
781 >            assertUnlocked(lock);
782 >        }
783  
784 <        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
785 <        assertTrue(lock.validate(s));
786 <        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
787 <        assertTrue(lock.validate(p));
784 >        for (Function<StampedLock, Long> readLocker : readLockers()) {
785 >            s = assertValid(lock, readLocker.apply(lock));
786 >            q = assertValid(lock, lock.tryOptimisticRead());
787 >            assertEquals(q, lock.tryConvertToOptimisticRead(q));
788 >            assertTrue(lock.validate(q));
789 >            assertTrue(lock.isReadLocked());
790 >            p = assertValid(lock, lock.tryConvertToOptimisticRead(s));
791 >            assertTrue(lock.validate(p));
792 >            assertTrue(lock.validate(s));
793 >            assertUnlocked(lock);
794 >            assertEquals(q, lock.tryConvertToOptimisticRead(q));
795 >            assertTrue(lock.validate(q));
796 >        }
797      }
798  
799      /**
800 <     * tryConvertToReadLock succeeds and validates if successfully locked
771 <     * or lock free;
800 >     * tryConvertToReadLock succeeds for valid stamps
801       */
802      public void testTryConvertToReadLock() throws InterruptedException {
803          StampedLock lock = new StampedLock();
804          long s, p;
805  
806 <        assertFalse((p = lock.tryConvertToReadLock(0L)) != 0L);
806 >        assertEquals(0L, lock.tryConvertToReadLock(0L));
807  
808 <        assertTrue((s = lock.tryOptimisticRead()) != 0L);
809 <        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
808 >        s = assertValid(lock, lock.tryOptimisticRead());
809 >        p = assertValid(lock, lock.tryConvertToReadLock(s));
810          assertTrue(lock.isReadLocked());
811          assertEquals(1, lock.getReadLockCount());
812 +        assertTrue(lock.validate(s));
813          lock.unlockRead(p);
814  
815 <        assertTrue((s = lock.tryOptimisticRead()) != 0L);
815 >        s = assertValid(lock, lock.tryOptimisticRead());
816          lock.readLock();
817 <        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
817 >        p = assertValid(lock, lock.tryConvertToReadLock(s));
818          assertTrue(lock.isReadLocked());
819          assertEquals(2, lock.getReadLockCount());
820          lock.unlockRead(p);
821          lock.unlockRead(p);
822 +        assertUnlocked(lock);
823  
824 <        assertTrue((s = lock.writeLock()) != 0L);
825 <        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
826 <        assertTrue(lock.validate(p));
827 <        assertTrue(lock.isReadLocked());
828 <        assertEquals(1, lock.getReadLockCount());
829 <        lock.unlockRead(p);
830 <
831 <        assertTrue((s = lock.readLock()) != 0L);
832 <        assertTrue(lock.validate(s));
833 <        assertEquals(s, lock.tryConvertToReadLock(s));
834 <        assertTrue(lock.validate(s));
835 <        assertTrue(lock.isReadLocked());
836 <        assertEquals(1, lock.getReadLockCount());
837 <        lock.unlockRead(s);
838 <
839 <        assertTrue((s = lock.tryWriteLock()) != 0L);
840 <        assertTrue(lock.validate(s));
841 <        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
842 <        assertTrue(lock.validate(p));
812 <        assertEquals(1, lock.getReadLockCount());
813 <        lock.unlockRead(p);
814 <
815 <        assertTrue((s = lock.tryReadLock()) != 0L);
816 <        assertTrue(lock.validate(s));
817 <        assertEquals(s, lock.tryConvertToReadLock(s));
818 <        assertTrue(lock.validate(s));
819 <        assertTrue(lock.isReadLocked());
820 <        assertEquals(1, lock.getReadLockCount());
821 <        lock.unlockRead(s);
822 <
823 <        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
824 <        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
825 <        assertTrue(lock.validate(p));
826 <        assertTrue(lock.isReadLocked());
827 <        assertEquals(1, lock.getReadLockCount());
828 <        lock.unlockRead(p);
829 <
830 <        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
831 <        assertTrue(lock.validate(s));
832 <        assertEquals(s, lock.tryConvertToReadLock(s));
833 <        assertTrue(lock.validate(s));
834 <        assertTrue(lock.isReadLocked());
835 <        assertEquals(1, lock.getReadLockCount());
836 <        lock.unlockRead(s);
824 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
825 >            for (Function<StampedLock, Long> writeLocker : writeLockers()) {
826 >                s = assertValid(lock, writeLocker.apply(lock));
827 >                p = assertValid(lock, lock.tryConvertToReadLock(s));
828 >                assertFalse(lock.validate(s));
829 >                assertTrue(lock.isReadLocked());
830 >                assertEquals(1, lock.getReadLockCount());
831 >                readUnlocker.accept(lock, p);
832 >            }
833 >
834 >            for (Function<StampedLock, Long> readLocker : readLockers()) {
835 >                s = assertValid(lock, readLocker.apply(lock));
836 >                assertEquals(s, lock.tryConvertToReadLock(s));
837 >                assertTrue(lock.validate(s));
838 >                assertTrue(lock.isReadLocked());
839 >                assertEquals(1, lock.getReadLockCount());
840 >                readUnlocker.accept(lock, s);
841 >            }
842 >        }
843      }
844  
845      /**
846 <     * tryConvertToWriteLock succeeds and validates if successfully locked
841 <     * or lock free;
846 >     * tryConvertToWriteLock succeeds if lock available; fails if multiply read locked
847       */
848      public void testTryConvertToWriteLock() throws InterruptedException {
849          StampedLock lock = new StampedLock();
850          long s, p;
851  
852 <        assertFalse((p = lock.tryConvertToWriteLock(0L)) != 0L);
852 >        assertEquals(0L, lock.tryConvertToWriteLock(0L));
853  
854          assertTrue((s = lock.tryOptimisticRead()) != 0L);
855          assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
856          assertTrue(lock.isWriteLocked());
857          lock.unlockWrite(p);
858  
859 <        assertTrue((s = lock.writeLock()) != 0L);
860 <        assertEquals(s, lock.tryConvertToWriteLock(s));
861 <        assertTrue(lock.validate(s));
862 <        assertTrue(lock.isWriteLocked());
863 <        lock.unlockWrite(s);
864 <
865 <        assertTrue((s = lock.readLock()) != 0L);
866 <        assertTrue(lock.validate(s));
867 <        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
868 <        assertTrue(lock.validate(p));
869 <        assertTrue(lock.isWriteLocked());
870 <        lock.unlockWrite(p);
871 <
872 <        assertTrue((s = lock.tryWriteLock()) != 0L);
873 <        assertTrue(lock.validate(s));
874 <        assertEquals(s, lock.tryConvertToWriteLock(s));
875 <        assertTrue(lock.validate(s));
876 <        assertTrue(lock.isWriteLocked());
872 <        lock.unlockWrite(s);
873 <
874 <        assertTrue((s = lock.tryReadLock()) != 0L);
875 <        assertTrue(lock.validate(s));
876 <        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
877 <        assertTrue(lock.validate(p));
878 <        assertTrue(lock.isWriteLocked());
879 <        lock.unlockWrite(p);
880 <
881 <        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
882 <        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
883 <        assertTrue(lock.validate(p));
884 <        assertTrue(lock.isWriteLocked());
885 <        lock.unlockWrite(p);
859 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
860 >            for (Function<StampedLock, Long> writeLocker : writeLockers()) {
861 >                s = assertValid(lock, writeLocker.apply(lock));
862 >                assertEquals(s, lock.tryConvertToWriteLock(s));
863 >                assertTrue(lock.validate(s));
864 >                assertTrue(lock.isWriteLocked());
865 >                writeUnlocker.accept(lock, s);
866 >            }
867 >
868 >            for (Function<StampedLock, Long> readLocker : readLockers()) {
869 >                s = assertValid(lock, readLocker.apply(lock));
870 >                p = assertValid(lock, lock.tryConvertToWriteLock(s));
871 >                assertFalse(lock.validate(s));
872 >                assertTrue(lock.validate(p));
873 >                assertTrue(lock.isWriteLocked());
874 >                writeUnlocker.accept(lock, p);
875 >            }
876 >        }
877  
878 <        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
879 <        assertTrue(lock.validate(s));
880 <        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
881 <        assertTrue(lock.validate(p));
882 <        assertTrue(lock.isWriteLocked());
883 <        lock.unlockWrite(p);
878 >        // failure if multiply read locked
879 >        for (Function<StampedLock, Long> readLocker : readLockers()) {
880 >            s = assertValid(lock, readLocker.apply(lock));
881 >            p = assertValid(lock, readLocker.apply(lock));
882 >            assertEquals(0L, lock.tryConvertToWriteLock(s));
883 >            assertTrue(lock.validate(s));
884 >            assertTrue(lock.validate(p));
885 >            assertEquals(2, lock.getReadLockCount());
886 >            lock.unlock(p);
887 >            lock.unlock(s);
888 >            assertUnlocked(lock);
889 >        }
890      }
891  
892      /**
893       * asWriteLock can be locked and unlocked
894       */
895 <    public void testAsWriteLock() {
895 >    public void testAsWriteLock() throws Throwable {
896          StampedLock sl = new StampedLock();
897          Lock lock = sl.asWriteLock();
898 <        lock.lock();
899 <        assertFalse(lock.tryLock());
900 <        lock.unlock();
901 <        assertTrue(lock.tryLock());
898 >        for (Action locker : lockLockers(lock)) {
899 >            locker.run();
900 >            assertTrue(sl.isWriteLocked());
901 >            assertFalse(sl.isReadLocked());
902 >            assertFalse(lock.tryLock());
903 >            lock.unlock();
904 >            assertUnlocked(sl);
905 >        }
906      }
907  
908      /**
909       * asReadLock can be locked and unlocked
910       */
911 <    public void testAsReadLock() {
911 >    public void testAsReadLock() throws Throwable {
912          StampedLock sl = new StampedLock();
913          Lock lock = sl.asReadLock();
914 <        lock.lock();
915 <        lock.unlock();
916 <        assertTrue(lock.tryLock());
914 >        for (Action locker : lockLockers(lock)) {
915 >            locker.run();
916 >            assertTrue(sl.isReadLocked());
917 >            assertFalse(sl.isWriteLocked());
918 >            assertEquals(1, sl.getReadLockCount());
919 >            locker.run();
920 >            assertTrue(sl.isReadLocked());
921 >            assertEquals(2, sl.getReadLockCount());
922 >            lock.unlock();
923 >            lock.unlock();
924 >            assertUnlocked(sl);
925 >        }
926      }
927  
928      /**
929       * asReadWriteLock.writeLock can be locked and unlocked
930       */
931 <    public void testAsReadWriteLockWriteLock() {
931 >    public void testAsReadWriteLockWriteLock() throws Throwable {
932          StampedLock sl = new StampedLock();
933          Lock lock = sl.asReadWriteLock().writeLock();
934 <        lock.lock();
935 <        assertFalse(lock.tryLock());
936 <        lock.unlock();
937 <        assertTrue(lock.tryLock());
934 >        for (Action locker : lockLockers(lock)) {
935 >            locker.run();
936 >            assertTrue(sl.isWriteLocked());
937 >            assertFalse(sl.isReadLocked());
938 >            assertFalse(lock.tryLock());
939 >            lock.unlock();
940 >            assertUnlocked(sl);
941 >        }
942      }
943  
944      /**
945       * asReadWriteLock.readLock can be locked and unlocked
946       */
947 <    public void testAsReadWriteLockReadLock() {
947 >    public void testAsReadWriteLockReadLock() throws Throwable {
948          StampedLock sl = new StampedLock();
949          Lock lock = sl.asReadWriteLock().readLock();
950 <        lock.lock();
951 <        lock.unlock();
952 <        assertTrue(lock.tryLock());
950 >        for (Action locker : lockLockers(lock)) {
951 >            locker.run();
952 >            assertTrue(sl.isReadLocked());
953 >            assertFalse(sl.isWriteLocked());
954 >            assertEquals(1, sl.getReadLockCount());
955 >            locker.run();
956 >            assertTrue(sl.isReadLocked());
957 >            assertEquals(2, sl.getReadLockCount());
958 >            lock.unlock();
959 >            lock.unlock();
960 >            assertUnlocked(sl);
961 >        }
962      }
963  
964      /**
# Line 958 | Line 981 | public class StampedLockTest extends JSR
981          Runnable[] actions = {
982              () -> {
983                  StampedLock sl = new StampedLock();
984 <                long stamp = sl.tryOptimisticRead();
962 <                assertTrue(stamp != 0);
984 >                long stamp = assertValid(sl, sl.tryOptimisticRead());
985                  sl.unlockRead(stamp);
986              },
987              () -> {
# Line 976 | Line 998 | public class StampedLockTest extends JSR
998              },
999              () -> {
1000                  StampedLock sl = new StampedLock();
979                long stamp = sl.tryOptimisticRead();
1001                  sl.readLock();
1002 +                long stamp = assertValid(sl, sl.tryOptimisticRead());
1003                  sl.unlockRead(stamp);
1004              },
1005              () -> {
1006                  StampedLock sl = new StampedLock();
985                long stamp = sl.tryOptimisticRead();
1007                  sl.readLock();
1008 +                long stamp = assertValid(sl, sl.tryOptimisticRead());
1009                  sl.unlock(stamp);
1010              },
1011  
1012              () -> {
1013                  StampedLock sl = new StampedLock();
1014                  long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1015 <                assertTrue(stamp != 0);
1015 >                assertValid(sl, stamp);
1016                  sl.writeLock();
1017                  sl.unlockWrite(stamp);
1018              },
# Line 1016 | Line 1038 | public class StampedLockTest extends JSR
1038              () -> {
1039                  StampedLock sl = new StampedLock();
1040                  long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1041 <                assertTrue(stamp != 0);
1041 >                assertValid(sl, stamp);
1042                  sl.writeLock();
1043                  sl.unlockWrite(stamp);
1044              },
# Line 1036 | Line 1058 | public class StampedLockTest extends JSR
1058                  StampedLock sl = new StampedLock();
1059                  sl.readLock();
1060                  long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1061 <                assertTrue(stamp != 0);
1061 >                assertValid(sl, stamp);
1062                  sl.readLock();
1063                  sl.unlockRead(stamp);
1064              },
# Line 1079 | Line 1101 | public class StampedLockTest extends JSR
1101      }
1102  
1103      /**
1104 <     * Invalid write stamps result in IllegalMonitorStateException
1104 >     * Invalid stamps result in IllegalMonitorStateException
1105       */
1106 <    public void testInvalidWriteStampsThrowIllegalMonitorStateException() {
1107 <        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1086 <        writeLockers.add((sl) -> sl.writeLock());
1087 <        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1088 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1089 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1106 >    public void testInvalidStampsThrowIllegalMonitorStateException() {
1107 >        final StampedLock sl = new StampedLock();
1108  
1109 <        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1110 <        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1111 <        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1112 <        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1113 <        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1109 >        assertThrows(IllegalMonitorStateException.class,
1110 >                     () -> sl.unlockWrite(0L),
1111 >                     () -> sl.unlockRead(0L),
1112 >                     () -> sl.unlock(0L));
1113 >
1114 >        final long optimisticStamp = sl.tryOptimisticRead();
1115 >        final long readStamp = sl.readLock();
1116 >        sl.unlockRead(readStamp);
1117 >        final long writeStamp = sl.writeLock();
1118 >        sl.unlockWrite(writeStamp);
1119 >        assertTrue(optimisticStamp != 0L && readStamp != 0L && writeStamp != 0L);
1120 >        final long[] noLongerValidStamps = { optimisticStamp, readStamp, writeStamp };
1121 >        final Runnable assertNoLongerValidStampsThrow = () -> {
1122 >            for (long noLongerValidStamp : noLongerValidStamps)
1123 >                assertThrows(IllegalMonitorStateException.class,
1124 >                             () -> sl.unlockWrite(noLongerValidStamp),
1125 >                             () -> sl.unlockRead(noLongerValidStamp),
1126 >                             () -> sl.unlock(noLongerValidStamp));
1127 >        };
1128 >        assertNoLongerValidStampsThrow.run();
1129  
1130 <        List<Consumer<StampedLock>> mutaters = new ArrayList<>();
1131 <        mutaters.add((sl) -> {});
1132 <        mutaters.add((sl) -> sl.readLock());
1133 <        for (Function<StampedLock, Long> writeLocker : writeLockers)
1134 <            mutaters.add((sl) -> writeLocker.apply(sl));
1102 <
1103 <        for (Function<StampedLock, Long> writeLocker : writeLockers)
1104 <        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1105 <        for (Consumer<StampedLock> mutater : mutaters) {
1106 <            final StampedLock sl = new StampedLock();
1107 <            final long stamp = writeLocker.apply(sl);
1108 <            assertTrue(stamp != 0L);
1130 >        for (Function<StampedLock, Long> readLocker : readLockers())
1131 >        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
1132 >            final long stamp = readLocker.apply(sl);
1133 >            assertValid(sl, stamp);
1134 >            assertNoLongerValidStampsThrow.run();
1135              assertThrows(IllegalMonitorStateException.class,
1136 <                         () -> sl.unlockRead(stamp));
1137 <            writeUnlocker.accept(sl, stamp);
1138 <            mutater.accept(sl);
1136 >                         () -> sl.unlockWrite(stamp),
1137 >                         () -> sl.unlockRead(sl.tryOptimisticRead()),
1138 >                         () -> sl.unlockRead(0L));
1139 >            readUnlocker.accept(sl, stamp);
1140 >            assertUnlocked(sl);
1141 >            assertNoLongerValidStampsThrow.run();
1142 >        }
1143 >
1144 >        for (Function<StampedLock, Long> writeLocker : writeLockers())
1145 >        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
1146 >            final long stamp = writeLocker.apply(sl);
1147 >            assertValid(sl, stamp);
1148 >            assertNoLongerValidStampsThrow.run();
1149              assertThrows(IllegalMonitorStateException.class,
1114                         () -> sl.unlock(stamp),
1150                           () -> sl.unlockRead(stamp),
1151 <                         () -> sl.unlockWrite(stamp));
1151 >                         () -> sl.unlockWrite(0L));
1152 >            writeUnlocker.accept(sl, stamp);
1153 >            assertUnlocked(sl);
1154 >            assertNoLongerValidStampsThrow.run();
1155          }
1156      }
1157  
1158      /**
1159 <     * Invalid read stamps result in IllegalMonitorStateException
1159 >     * Read locks can be very deeply nested
1160       */
1161 <    public void testInvalidReadStampsThrowIllegalMonitorStateException() {
1162 <        List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
1163 <        readLockers.add((sl) -> sl.readLock());
1164 <        readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl));
1165 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1166 <        readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0, DAYS));
1167 <
1168 <        List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>();
1169 <        readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp));
1170 <        readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead()));
1171 <        readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock());
1172 <        readUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1173 <
1174 <        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1175 <        writeLockers.add((sl) -> sl.writeLock());
1176 <        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1177 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1178 <        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1179 <
1180 <        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1143 <        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1144 <        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1145 <        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1146 <        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1147 <
1148 <
1149 <        for (Function<StampedLock, Long> readLocker : readLockers)
1150 <        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers)
1151 <        for (Function<StampedLock, Long> writeLocker : writeLockers)
1152 <        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers) {
1153 <            final StampedLock sl = new StampedLock();
1154 <            final long stamp = readLocker.apply(sl);
1155 <            assertTrue(stamp != 0L);
1156 <            assertThrows(IllegalMonitorStateException.class,
1157 <                         () -> sl.unlockWrite(stamp));
1158 <            readUnlocker.accept(sl, stamp);
1159 <            assertThrows(IllegalMonitorStateException.class,
1160 <                         () -> sl.unlock(stamp),
1161 <                         () -> sl.unlockRead(stamp),
1162 <                         () -> sl.unlockWrite(stamp));
1163 <            final long writeStamp = writeLocker.apply(sl);
1164 <            assertTrue(writeStamp != 0L);
1165 <            assertTrue(writeStamp != stamp);
1166 <            assertThrows(IllegalMonitorStateException.class,
1167 <                         () -> sl.unlock(stamp),
1168 <                         () -> sl.unlockRead(stamp),
1169 <                         () -> sl.unlockWrite(stamp));
1170 <            writeUnlocker.accept(sl, writeStamp);
1171 <            assertThrows(IllegalMonitorStateException.class,
1172 <                         () -> sl.unlock(stamp),
1173 <                         () -> sl.unlockRead(stamp),
1174 <                         () -> sl.unlockWrite(stamp));
1161 >    public void testDeeplyNestedReadLocks() {
1162 >        final StampedLock lock = new StampedLock();
1163 >        final int depth = 300;
1164 >        final long[] stamps = new long[depth];
1165 >        final List<Function<StampedLock, Long>> readLockers = readLockers();
1166 >        final List<BiConsumer<StampedLock, Long>> readUnlockers = readUnlockers();
1167 >        for (int i = 0; i < depth; i++) {
1168 >            Function<StampedLock, Long> readLocker
1169 >                = readLockers.get(i % readLockers.size());
1170 >            long stamp = readLocker.apply(lock);
1171 >            assertEquals(i + 1, lock.getReadLockCount());
1172 >            assertTrue(lock.isReadLocked());
1173 >            stamps[i] = stamp;
1174 >        }
1175 >        for (int i = 0; i < depth; i++) {
1176 >            BiConsumer<StampedLock, Long> readUnlocker
1177 >                = readUnlockers.get(i % readUnlockers.size());
1178 >            assertEquals(depth - i, lock.getReadLockCount());
1179 >            assertTrue(lock.isReadLocked());
1180 >            readUnlocker.accept(lock, stamps[depth - 1 - i]);
1181          }
1182 +        assertUnlocked(lock);
1183      }
1177
1184   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines