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.9 by jsr166, Wed Dec 31 16:44:02 2014 UTC vs.
Revision 1.25 by jsr166, Sun Jul 17 02:30:53 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.DAYS;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.concurrent.CountDownLatch;
14 > import java.util.concurrent.TimeUnit;
15   import java.util.concurrent.locks.Lock;
16   import java.util.concurrent.locks.StampedLock;
17 < import java.util.concurrent.CountDownLatch;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import java.util.*;
20 < import java.util.concurrent.*;
17 > import java.util.function.BiConsumer;
18 > import java.util.function.Consumer;
19 > import java.util.function.Function;
20 >
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class StampedLockTest extends JSR166TestCase {
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28      public static Test suite() {
29          return new TestSuite(StampedLockTest.class);
# Line 165 | Line 173 | public class StampedLockTest extends JSR
173       */
174      public void testWriteUnlock_IMSE2() {
175          StampedLock lock = new StampedLock();
176 +        long s = lock.writeLock();
177 +        lock.unlockWrite(s);
178          try {
169            long s = lock.writeLock();
170            lock.unlockWrite(s);
179              lock.unlockWrite(s);
180              shouldThrow();
181          } catch (IllegalMonitorStateException success) {}
# Line 178 | Line 186 | public class StampedLockTest extends JSR
186       */
187      public void testWriteUnlock_IMSE3() {
188          StampedLock lock = new StampedLock();
189 +        long s = lock.readLock();
190          try {
182            long s = lock.readLock();
191              lock.unlockWrite(s);
192              shouldThrow();
193          } catch (IllegalMonitorStateException success) {}
# Line 190 | Line 198 | public class StampedLockTest extends JSR
198       */
199      public void testReadUnlock_IMSE() {
200          StampedLock lock = new StampedLock();
201 +        long s = lock.readLock();
202 +        lock.unlockRead(s);
203          try {
194            long s = lock.readLock();
195            lock.unlockRead(s);
204              lock.unlockRead(s);
205              shouldThrow();
206          } catch (IllegalMonitorStateException success) {}
# Line 214 | Line 222 | public class StampedLockTest extends JSR
222       */
223      public void testReadUnlock_IMSE3() {
224          StampedLock lock = new StampedLock();
225 +        long s = lock.writeLock();
226          try {
218            long s = lock.writeLock();
227              lock.unlockRead(s);
228              shouldThrow();
229          } catch (IllegalMonitorStateException success) {}
# Line 232 | Line 240 | public class StampedLockTest extends JSR
240      /**
241       * A stamp obtained from a successful lock operation validates
242       */
243 <    public void testValidate() {
244 <        try {
245 <            StampedLock lock = new StampedLock();
246 <            long s = lock.writeLock();
247 <            assertTrue(lock.validate(s));
248 <            lock.unlockWrite(s);
249 <            s = lock.readLock();
250 <            assertTrue(lock.validate(s));
251 <            lock.unlockRead(s);
252 <            assertTrue((s = lock.tryWriteLock()) != 0L);
253 <            assertTrue(lock.validate(s));
254 <            lock.unlockWrite(s);
255 <            assertTrue((s = lock.tryReadLock()) != 0L);
256 <            assertTrue(lock.validate(s));
257 <            lock.unlockRead(s);
258 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
259 <            assertTrue(lock.validate(s));
260 <            lock.unlockWrite(s);
261 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
262 <            assertTrue(lock.validate(s));
263 <            lock.unlockRead(s);
256 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
257 <        } catch (InterruptedException ie) {
258 <            threadUnexpectedException(ie);
259 <        }
243 >    public void testValidate() throws InterruptedException {
244 >        StampedLock lock = new StampedLock();
245 >        long s = lock.writeLock();
246 >        assertTrue(lock.validate(s));
247 >        lock.unlockWrite(s);
248 >        s = lock.readLock();
249 >        assertTrue(lock.validate(s));
250 >        lock.unlockRead(s);
251 >        assertTrue((s = lock.tryWriteLock()) != 0L);
252 >        assertTrue(lock.validate(s));
253 >        lock.unlockWrite(s);
254 >        assertTrue((s = lock.tryReadLock()) != 0L);
255 >        assertTrue(lock.validate(s));
256 >        lock.unlockRead(s);
257 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
258 >        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);
264      }
265  
266      /**
267       * A stamp obtained from an unsuccessful lock operation does not validate
268       */
269 <    public void testValidate2() {
270 <        try {
271 <            StampedLock lock = new StampedLock();
272 <            long s;
273 <            assertTrue((s = lock.writeLock()) != 0L);
274 <            assertTrue(lock.validate(s));
275 <            assertFalse(lock.validate(lock.tryWriteLock()));
276 <            assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
277 <            assertFalse(lock.validate(lock.tryReadLock()));
278 <            assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
279 <            assertFalse(lock.validate(lock.tryOptimisticRead()));
276 <            lock.unlockWrite(s);
277 <        } catch (InterruptedException ie) {
278 <            threadUnexpectedException(ie);
279 <        }
269 >    public void testValidate2() throws InterruptedException {
270 >        StampedLock lock = new StampedLock();
271 >        long s;
272 >        assertTrue((s = lock.writeLock()) != 0L);
273 >        assertTrue(lock.validate(s));
274 >        assertFalse(lock.validate(lock.tryWriteLock()));
275 >        assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
276 >        assertFalse(lock.validate(lock.tryReadLock()));
277 >        assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
278 >        assertFalse(lock.validate(lock.tryOptimisticRead()));
279 >        lock.unlockWrite(s);
280      }
281  
282      /**
283       * writeLockInterruptibly is interruptible
284       */
285 <    public void testWriteLockInterruptibly_Interruptible() {
285 >    public void testWriteLockInterruptibly_Interruptible()
286 >            throws InterruptedException {
287          final CountDownLatch running = new CountDownLatch(1);
288          final StampedLock lock = new StampedLock();
289          long s = lock.writeLock();
# Line 291 | Line 292 | public class StampedLockTest extends JSR
292                  running.countDown();
293                  lock.writeLockInterruptibly();
294              }});
295 <        try {
296 <            running.await();
297 <            waitForThreadToEnterWaitState(t, 100);
298 <            t.interrupt();
299 <            awaitTermination(t);
300 <            releaseWriteLock(lock, s);
300 <        } catch (InterruptedException ie) {
301 <            threadUnexpectedException(ie);
302 <        }
295 >
296 >        running.await();
297 >        waitForThreadToEnterWaitState(t, 100);
298 >        t.interrupt();
299 >        awaitTermination(t);
300 >        releaseWriteLock(lock, s);
301      }
302  
303      /**
304       * timed tryWriteLock is interruptible
305       */
306 <    public void testWriteTryLock_Interruptible() {
306 >    public void testWriteTryLock_Interruptible() throws InterruptedException {
307          final CountDownLatch running = new CountDownLatch(1);
308          final StampedLock lock = new StampedLock();
309          long s = lock.writeLock();
# Line 314 | Line 312 | public class StampedLockTest extends JSR
312                  running.countDown();
313                  lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
314              }});
315 <        try {
316 <            running.await();
317 <            waitForThreadToEnterWaitState(t, 100);
318 <            t.interrupt();
319 <            awaitTermination(t);
320 <            releaseWriteLock(lock, s);
323 <        } catch (InterruptedException ie) {
324 <            threadUnexpectedException(ie);
325 <        }
315 >
316 >        running.await();
317 >        waitForThreadToEnterWaitState(t, 100);
318 >        t.interrupt();
319 >        awaitTermination(t);
320 >        releaseWriteLock(lock, s);
321      }
322  
323      /**
324       * readLockInterruptibly is interruptible
325       */
326 <    public void testReadLockInterruptibly_Interruptible() {
326 >    public void testReadLockInterruptibly_Interruptible()
327 >            throws InterruptedException {
328          final CountDownLatch running = new CountDownLatch(1);
329          final StampedLock lock = new StampedLock();
330          long s = lock.writeLock();
# Line 337 | Line 333 | public class StampedLockTest extends JSR
333                  running.countDown();
334                  lock.readLockInterruptibly();
335              }});
336 <        try {
337 <            running.await();
338 <            waitForThreadToEnterWaitState(t, 100);
339 <            t.interrupt();
340 <            awaitTermination(t);
341 <            releaseWriteLock(lock, s);
346 <        } catch (InterruptedException ie) {
347 <            threadUnexpectedException(ie);
348 <        }
336 >
337 >        running.await();
338 >        waitForThreadToEnterWaitState(t, 100);
339 >        t.interrupt();
340 >        awaitTermination(t);
341 >        releaseWriteLock(lock, s);
342      }
343  
344      /**
345       * timed tryReadLock is interruptible
346       */
347 <    public void testReadTryLock_Interruptible() {
347 >    public void testReadTryLock_Interruptible() throws InterruptedException {
348          final CountDownLatch running = new CountDownLatch(1);
349          final StampedLock lock = new StampedLock();
350          long s = lock.writeLock();
# Line 360 | Line 353 | public class StampedLockTest extends JSR
353                  running.countDown();
354                  lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
355              }});
356 <        try {
357 <            running.await();
358 <            waitForThreadToEnterWaitState(t, 100);
359 <            t.interrupt();
360 <            awaitTermination(t);
361 <            releaseWriteLock(lock, s);
369 <        } catch (InterruptedException ie) {
370 <            threadUnexpectedException(ie);
371 <        }
356 >
357 >        running.await();
358 >        waitForThreadToEnterWaitState(t, 100);
359 >        t.interrupt();
360 >        awaitTermination(t);
361 >        releaseWriteLock(lock, s);
362      }
363  
364      /**
# Line 441 | Line 431 | public class StampedLockTest extends JSR
431      /**
432       * A writelock succeeds only after a reading thread unlocks
433       */
434 <    public void testWriteAfterReadLock() {
434 >    public void testWriteAfterReadLock() throws InterruptedException {
435          final CountDownLatch running = new CountDownLatch(1);
436          final StampedLock lock = new StampedLock();
437          long rs = lock.readLock();
# Line 451 | Line 441 | public class StampedLockTest extends JSR
441                  long s = lock.writeLock();
442                  lock.unlockWrite(s);
443              }});
444 <        try {
445 <            running.await();
446 <            waitForThreadToEnterWaitState(t, 100);
447 <            assertFalse(lock.isWriteLocked());
448 <            lock.unlockRead(rs);
449 <            awaitTermination(t);
450 <            assertFalse(lock.isWriteLocked());
461 <        } catch (InterruptedException ie) {
462 <            threadUnexpectedException(ie);
463 <        }
444 >
445 >        running.await();
446 >        waitForThreadToEnterWaitState(t, 100);
447 >        assertFalse(lock.isWriteLocked());
448 >        lock.unlockRead(rs);
449 >        awaitTermination(t);
450 >        assertFalse(lock.isWriteLocked());
451      }
452  
453      /**
# Line 474 | Line 461 | public class StampedLockTest extends JSR
461                  long rs = lock.readLock();
462                  lock.unlockRead(rs);
463              }});
464 +
465          awaitTermination(t1);
466  
467          Thread t2 = newStartedThread(new CheckedRunnable() {
# Line 481 | Line 469 | public class StampedLockTest extends JSR
469                  long ws = lock.writeLock();
470                  lock.unlockWrite(ws);
471              }});
472 +
473          assertFalse(lock.isWriteLocked());
474          lock.unlockRead(s);
475          awaitTermination(t2);
# Line 584 | Line 573 | public class StampedLockTest extends JSR
573      /**
574       * writeLockInterruptibly succeeds if unlocked, else is interruptible
575       */
576 <    public void testWriteLockInterruptibly() {
576 >    public void testWriteLockInterruptibly() throws InterruptedException {
577          final CountDownLatch running = new CountDownLatch(1);
578          final StampedLock lock = new StampedLock();
579 <        long s = 0L;
591 <        try {
592 <            s = lock.writeLockInterruptibly();
593 <        } catch (InterruptedException ie) {
594 <            threadUnexpectedException(ie);
595 <        }
579 >        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 <        try {
587 <            running.await();
588 <            waitForThreadToEnterWaitState(t, 100);
589 <            t.interrupt();
590 <            assertTrue(lock.isWriteLocked());
591 <            awaitTermination(t);
608 <            releaseWriteLock(lock, s);
609 <        } catch (InterruptedException ie) {
610 <            threadUnexpectedException(ie);
611 <        }
612 <
586 >        running.await();
587 >        waitForThreadToEnterWaitState(t, 100);
588 >        t.interrupt();
589 >        assertTrue(lock.isWriteLocked());
590 >        awaitTermination(t);
591 >        releaseWriteLock(lock, s);
592      }
593  
594      /**
595       * readLockInterruptibly succeeds if lock free else is interruptible
596       */
597 <    public void testReadLockInterruptibly() {
597 >    public void testReadLockInterruptibly() throws InterruptedException {
598          final CountDownLatch running = new CountDownLatch(1);
599          final StampedLock lock = new StampedLock();
600 <        long s = 0L;
601 <        try {
602 <            s = lock.readLockInterruptibly();
603 <            lock.unlockRead(s);
625 <            s = lock.writeLockInterruptibly();
626 <        } catch (InterruptedException ie) {
627 <            threadUnexpectedException(ie);
628 <        }
600 >        long s;
601 >        s = lock.readLockInterruptibly();
602 >        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              }});
609 <        try {
610 <            running.await();
611 <            waitForThreadToEnterWaitState(t, 100);
612 <            t.interrupt();
613 <            awaitTermination(t);
614 <            releaseWriteLock(lock, s);
640 <        } catch (InterruptedException ie) {
641 <            threadUnexpectedException(ie);
642 <        }
609 >
610 >        running.await();
611 >        waitForThreadToEnterWaitState(t, 100);
612 >        t.interrupt();
613 >        awaitTermination(t);
614 >        releaseWriteLock(lock, s);
615      }
616  
617      /**
# Line 673 | Line 645 | public class StampedLockTest extends JSR
645      /**
646       * tryOptimisticRead succeeds and validates if unlocked, fails if locked
647       */
648 <    public void testValidateOptimistic() {
649 <        try {
650 <            StampedLock lock = new StampedLock();
651 <            long s, p;
652 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
653 <            assertTrue(lock.validate(p));
654 <            assertTrue((s = lock.writeLock()) != 0L);
655 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
656 <            assertTrue(lock.validate(s));
657 <            lock.unlockWrite(s);
658 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
659 <            assertTrue(lock.validate(p));
660 <            assertTrue((s = lock.readLock()) != 0L);
661 <            assertTrue(lock.validate(s));
662 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
663 <            assertTrue(lock.validate(p));
664 <            lock.unlockRead(s);
665 <            assertTrue((s = lock.tryWriteLock()) != 0L);
666 <            assertTrue(lock.validate(s));
667 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
668 <            lock.unlockWrite(s);
669 <            assertTrue((s = lock.tryReadLock()) != 0L);
670 <            assertTrue(lock.validate(s));
671 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
672 <            lock.unlockRead(s);
673 <            assertTrue(lock.validate(p));
674 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
675 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
676 <            assertTrue(lock.validate(s));
677 <            lock.unlockWrite(s);
678 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
679 <            assertTrue(lock.validate(s));
680 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
681 <            lock.unlockRead(s);
710 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
711 <        } catch (InterruptedException ie) {
712 <            threadUnexpectedException(ie);
713 <        }
648 >    public void testValidateOptimistic() throws InterruptedException {
649 >        StampedLock lock = new StampedLock();
650 >        long s, p;
651 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
652 >        assertTrue(lock.validate(p));
653 >        assertTrue((s = lock.writeLock()) != 0L);
654 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
655 >        assertTrue(lock.validate(s));
656 >        lock.unlockWrite(s);
657 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
658 >        assertTrue(lock.validate(p));
659 >        assertTrue((s = lock.readLock()) != 0L);
660 >        assertTrue(lock.validate(s));
661 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
662 >        assertTrue(lock.validate(p));
663 >        lock.unlockRead(s);
664 >        assertTrue((s = lock.tryWriteLock()) != 0L);
665 >        assertTrue(lock.validate(s));
666 >        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);
682      }
683  
684      /**
# Line 731 | Line 699 | public class StampedLockTest extends JSR
699       * tryOptimisticRead stamp does not validate if a write lock
700       * intervenes in another thread
701       */
702 <    public void testValidateOptimisticWriteLocked2() {
702 >    public void testValidateOptimisticWriteLocked2()
703 >            throws InterruptedException {
704          final CountDownLatch running = new CountDownLatch(1);
705          final StampedLock lock = new StampedLock();
706          long s, p;
707          assertTrue((p = lock.tryOptimisticRead()) != 0L);
708          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
709 <                public void realRun() throws InterruptedException {
710 <                    lock.writeLockInterruptibly();
711 <                    running.countDown();
712 <                    lock.writeLockInterruptibly();
713 <                }});
714 <        try {
715 <            running.await();
716 <            assertFalse(lock.validate(p));
717 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
718 <            t.interrupt();
719 <            awaitTermination(t);
751 <        } catch (InterruptedException ie) {
752 <            threadUnexpectedException(ie);
753 <        }
709 >            public void realRun() throws InterruptedException {
710 >                lock.writeLockInterruptibly();
711 >                running.countDown();
712 >                lock.writeLockInterruptibly();
713 >            }});
714 >
715 >        running.await();
716 >        assertFalse(lock.validate(p));
717 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
718 >        t.interrupt();
719 >        awaitTermination(t);
720      }
721  
722      /**
723       * tryConvertToOptimisticRead succeeds and validates if successfully locked,
724       */
725 <    public void testTryConvertToOptimisticRead() {
726 <        try {
727 <            StampedLock lock = new StampedLock();
728 <            long s, p;
729 <            s = 0L;
730 <            assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
731 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
732 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
733 <            assertTrue((s = lock.writeLock()) != 0L);
734 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
735 <            assertTrue(lock.validate(p));
736 <            assertTrue((s = lock.readLock()) != 0L);
737 <            assertTrue(lock.validate(s));
738 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
739 <            assertTrue(lock.validate(p));
740 <            assertTrue((s = lock.tryWriteLock()) != 0L);
741 <            assertTrue(lock.validate(s));
742 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
743 <            assertTrue(lock.validate(p));
744 <            assertTrue((s = lock.tryReadLock()) != 0L);
745 <            assertTrue(lock.validate(s));
746 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
747 <            assertTrue(lock.validate(p));
748 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
749 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
750 <            assertTrue(lock.validate(p));
751 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
752 <            assertTrue(lock.validate(s));
753 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
754 <            assertTrue(lock.validate(p));
755 <        } catch (InterruptedException ie) {
756 <            threadUnexpectedException(ie);
757 <        }
725 >    public void testTryConvertToOptimisticRead() throws InterruptedException {
726 >        StampedLock lock = new StampedLock();
727 >        long s, p;
728 >        assertEquals(0L, lock.tryConvertToOptimisticRead(0L));
729 >
730 >        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);
736 >        assertEquals(s, lock.tryConvertToOptimisticRead(s));
737 >        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));
743 >
744 >        assertTrue((s = lock.readLock()) != 0L);
745 >        assertTrue(lock.validate(s));
746 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
747 >        assertTrue(lock.validate(p));
748 >
749 >        assertTrue((s = lock.tryWriteLock()) != 0L);
750 >        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));
762 >
763 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
764 >        assertTrue(lock.validate(s));
765 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
766 >        assertTrue(lock.validate(p));
767      }
768  
769      /**
770       * tryConvertToReadLock succeeds and validates if successfully locked
771       * or lock free;
772       */
773 <    public void testTryConvertToReadLock() {
774 <        try {
775 <            StampedLock lock = new StampedLock();
776 <            long s, p;
777 <            s = 0L;
778 <            assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
779 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
780 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
781 <            lock.unlockRead(p);
782 <            assertTrue((s = lock.writeLock()) != 0L);
783 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
784 <            assertTrue(lock.validate(p));
785 <            lock.unlockRead(p);
786 <            assertTrue((s = lock.readLock()) != 0L);
787 <            assertTrue(lock.validate(s));
788 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
789 <            assertTrue(lock.validate(p));
790 <            lock.unlockRead(p);
791 <            assertTrue((s = lock.tryWriteLock()) != 0L);
792 <            assertTrue(lock.validate(s));
793 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
794 <            assertTrue(lock.validate(p));
795 <            lock.unlockRead(p);
796 <            assertTrue((s = lock.tryReadLock()) != 0L);
797 <            assertTrue(lock.validate(s));
798 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
799 <            assertTrue(lock.validate(p));
800 <            lock.unlockRead(p);
801 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
802 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
803 <            assertTrue(lock.validate(p));
804 <            lock.unlockRead(p);
805 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
806 <            assertTrue(lock.validate(s));
807 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
808 <            assertTrue(lock.validate(p));
809 <            lock.unlockRead(p);
810 <        } catch (InterruptedException ie) {
811 <            threadUnexpectedException(ie);
812 <        }
773 >    public void testTryConvertToReadLock() throws InterruptedException {
774 >        StampedLock lock = new StampedLock();
775 >        long s, p;
776 >
777 >        assertFalse((p = lock.tryConvertToReadLock(0L)) != 0L);
778 >
779 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
780 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
781 >        assertTrue(lock.isReadLocked());
782 >        assertEquals(1, lock.getReadLockCount());
783 >        lock.unlockRead(p);
784 >
785 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
786 >        lock.readLock();
787 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
788 >        assertTrue(lock.isReadLocked());
789 >        assertEquals(2, lock.getReadLockCount());
790 >        lock.unlockRead(p);
791 >        lock.unlockRead(p);
792 >
793 >        assertTrue((s = lock.writeLock()) != 0L);
794 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
795 >        assertTrue(lock.validate(p));
796 >        assertTrue(lock.isReadLocked());
797 >        assertEquals(1, lock.getReadLockCount());
798 >        lock.unlockRead(p);
799 >
800 >        assertTrue((s = lock.readLock()) != 0L);
801 >        assertTrue(lock.validate(s));
802 >        assertEquals(s, lock.tryConvertToReadLock(s));
803 >        assertTrue(lock.validate(s));
804 >        assertTrue(lock.isReadLocked());
805 >        assertEquals(1, lock.getReadLockCount());
806 >        lock.unlockRead(s);
807 >
808 >        assertTrue((s = lock.tryWriteLock()) != 0L);
809 >        assertTrue(lock.validate(s));
810 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
811 >        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);
837      }
838  
839      /**
840       * tryConvertToWriteLock succeeds and validates if successfully locked
841       * or lock free;
842       */
843 <    public void testTryConvertToWriteLock() {
844 <        try {
845 <            StampedLock lock = new StampedLock();
846 <            long s, p;
847 <            s = 0L;
848 <            assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
849 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
850 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
851 <            lock.unlockWrite(p);
852 <            assertTrue((s = lock.writeLock()) != 0L);
853 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
854 <            assertTrue(lock.validate(p));
855 <            lock.unlockWrite(p);
856 <            assertTrue((s = lock.readLock()) != 0L);
857 <            assertTrue(lock.validate(s));
858 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
859 <            assertTrue(lock.validate(p));
860 <            lock.unlockWrite(p);
861 <            assertTrue((s = lock.tryWriteLock()) != 0L);
862 <            assertTrue(lock.validate(s));
863 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
864 <            assertTrue(lock.validate(p));
865 <            lock.unlockWrite(p);
866 <            assertTrue((s = lock.tryReadLock()) != 0L);
867 <            assertTrue(lock.validate(s));
868 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
869 <            assertTrue(lock.validate(p));
870 <            lock.unlockWrite(p);
871 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
872 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
873 <            assertTrue(lock.validate(p));
874 <            lock.unlockWrite(p);
875 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
876 <            assertTrue(lock.validate(s));
877 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
878 <            assertTrue(lock.validate(p));
879 <            lock.unlockWrite(p);
880 <        } catch (InterruptedException ie) {
881 <            threadUnexpectedException(ie);
882 <        }
843 >    public void testTryConvertToWriteLock() throws InterruptedException {
844 >        StampedLock lock = new StampedLock();
845 >        long s, p;
846 >
847 >        assertFalse((p = lock.tryConvertToWriteLock(0L)) != 0L);
848 >
849 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
850 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
851 >        assertTrue(lock.isWriteLocked());
852 >        lock.unlockWrite(p);
853 >
854 >        assertTrue((s = lock.writeLock()) != 0L);
855 >        assertEquals(s, lock.tryConvertToWriteLock(s));
856 >        assertTrue(lock.validate(s));
857 >        assertTrue(lock.isWriteLocked());
858 >        lock.unlockWrite(s);
859 >
860 >        assertTrue((s = lock.readLock()) != 0L);
861 >        assertTrue(lock.validate(s));
862 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
863 >        assertTrue(lock.validate(p));
864 >        assertTrue(lock.isWriteLocked());
865 >        lock.unlockWrite(p);
866 >
867 >        assertTrue((s = lock.tryWriteLock()) != 0L);
868 >        assertTrue(lock.validate(s));
869 >        assertEquals(s, lock.tryConvertToWriteLock(s));
870 >        assertTrue(lock.validate(s));
871 >        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);
886 >
887 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
888 >        assertTrue(lock.validate(s));
889 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
890 >        assertTrue(lock.validate(p));
891 >        assertTrue(lock.isWriteLocked());
892 >        lock.unlockWrite(p);
893      }
894  
895      /**
# Line 929 | Line 938 | public class StampedLockTest extends JSR
938          assertTrue(lock.tryLock());
939      }
940  
941 +    /**
942 +     * Lock.newCondition throws UnsupportedOperationException
943 +     */
944 +    public void testLockViewsDoNotSupportConditions() {
945 +        StampedLock sl = new StampedLock();
946 +        assertThrows(UnsupportedOperationException.class,
947 +                     () -> sl.asWriteLock().newCondition(),
948 +                     () -> sl.asReadLock().newCondition(),
949 +                     () -> sl.asReadWriteLock().writeLock().newCondition(),
950 +                     () -> sl.asReadWriteLock().readLock().newCondition());
951 +    }
952 +
953 +    /**
954 +     * Passing optimistic read stamps to unlock operations result in
955 +     * IllegalMonitorStateException
956 +     */
957 +    public void testCannotUnlockOptimisticReadStamps() {
958 +        Runnable[] actions = {
959 +            () -> {
960 +                StampedLock sl = new StampedLock();
961 +                long stamp = sl.tryOptimisticRead();
962 +                assertTrue(stamp != 0);
963 +                sl.unlockRead(stamp);
964 +            },
965 +            () -> {
966 +                StampedLock sl = new StampedLock();
967 +                long stamp = sl.tryOptimisticRead();
968 +                sl.unlock(stamp);
969 +            },
970 +
971 +            () -> {
972 +                StampedLock sl = new StampedLock();
973 +                long stamp = sl.tryOptimisticRead();
974 +                sl.writeLock();
975 +                sl.unlock(stamp);
976 +            },
977 +            () -> {
978 +                StampedLock sl = new StampedLock();
979 +                long stamp = sl.tryOptimisticRead();
980 +                sl.readLock();
981 +                sl.unlockRead(stamp);
982 +            },
983 +            () -> {
984 +                StampedLock sl = new StampedLock();
985 +                long stamp = sl.tryOptimisticRead();
986 +                sl.readLock();
987 +                sl.unlock(stamp);
988 +            },
989 +
990 +            () -> {
991 +                StampedLock sl = new StampedLock();
992 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
993 +                assertTrue(stamp != 0);
994 +                sl.writeLock();
995 +                sl.unlockWrite(stamp);
996 +            },
997 +            () -> {
998 +                StampedLock sl = new StampedLock();
999 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1000 +                sl.writeLock();
1001 +                sl.unlock(stamp);
1002 +            },
1003 +            () -> {
1004 +                StampedLock sl = new StampedLock();
1005 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1006 +                sl.readLock();
1007 +                sl.unlockRead(stamp);
1008 +            },
1009 +            () -> {
1010 +                StampedLock sl = new StampedLock();
1011 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
1012 +                sl.readLock();
1013 +                sl.unlock(stamp);
1014 +            },
1015 +
1016 +            () -> {
1017 +                StampedLock sl = new StampedLock();
1018 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1019 +                assertTrue(stamp != 0);
1020 +                sl.writeLock();
1021 +                sl.unlockWrite(stamp);
1022 +            },
1023 +            () -> {
1024 +                StampedLock sl = new StampedLock();
1025 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1026 +                sl.writeLock();
1027 +                sl.unlock(stamp);
1028 +            },
1029 +            () -> {
1030 +                StampedLock sl = new StampedLock();
1031 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1032 +                sl.readLock();
1033 +                sl.unlockRead(stamp);
1034 +            },
1035 +            () -> {
1036 +                StampedLock sl = new StampedLock();
1037 +                sl.readLock();
1038 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1039 +                assertTrue(stamp != 0);
1040 +                sl.readLock();
1041 +                sl.unlockRead(stamp);
1042 +            },
1043 +            () -> {
1044 +                StampedLock sl = new StampedLock();
1045 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1046 +                sl.readLock();
1047 +                sl.unlock(stamp);
1048 +            },
1049 +            () -> {
1050 +                StampedLock sl = new StampedLock();
1051 +                sl.readLock();
1052 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1053 +                sl.readLock();
1054 +                sl.unlock(stamp);
1055 +            },
1056 +        };
1057 +
1058 +        assertThrows(IllegalMonitorStateException.class, actions);
1059 +    }
1060 +
1061 +    static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
1062 +        try { return sl.writeLockInterruptibly(); }
1063 +        catch (InterruptedException ex) { throw new AssertionError(ex); }
1064 +    }
1065 +
1066 +    static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
1067 +        try { return sl.tryWriteLock(time, unit); }
1068 +        catch (InterruptedException ex) { throw new AssertionError(ex); }
1069 +    }
1070 +
1071 +    /**
1072 +     * Invalid write stamps result in IllegalMonitorStateException
1073 +     */
1074 +    public void testInvalidWriteStampsThrowIllegalMonitorStateException() {
1075 +        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
1076 +        writeLockers.add((sl) -> sl.writeLock());
1077 +        writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl));
1078 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
1079 +        writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0, DAYS));
1080 +
1081 +        List<Consumer<StampedLock>> mutaters = new ArrayList<>();
1082 +        mutaters.add((sl) -> {});
1083 +        mutaters.add((sl) -> sl.readLock());
1084 +        for (Function<StampedLock, Long> writeLocker : writeLockers)
1085 +            mutaters.add((sl) -> writeLocker.apply(sl));
1086 +
1087 +        List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>();
1088 +        writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp));
1089 +        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
1090 +        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
1091 +        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
1092 +
1093 +        for (Function<StampedLock, Long> writeLocker : writeLockers)
1094 +        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers)
1095 +        for (Consumer<StampedLock> mutater : mutaters) {
1096 +            StampedLock sl = new StampedLock();
1097 +            long stamp = writeLocker.apply(sl);
1098 +            assertTrue(stamp != 0L);
1099 +            assertThrows(IllegalMonitorStateException.class,
1100 +                         () -> sl.unlockRead(stamp));
1101 +            writeUnlocker.accept(sl, stamp);
1102 +            mutater.accept(sl);
1103 +            assertThrows(IllegalMonitorStateException.class,
1104 +                         () -> sl.unlock(stamp),
1105 +                         () -> sl.unlockRead(stamp),
1106 +                         () -> sl.unlockWrite(stamp));
1107 +        }
1108 +    }
1109 +
1110   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines