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.4 by dl, Wed Mar 20 16:51:11 2013 UTC vs.
Revision 1.19 by jsr166, Tue Jun 7 06:25:29 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
9 < import java.util.concurrent.atomic.AtomicBoolean;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10 > import java.util.concurrent.CountDownLatch;
11   import java.util.concurrent.locks.Lock;
12   import java.util.concurrent.locks.StampedLock;
13 < import java.util.concurrent.CountDownLatch;
14 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 < import java.util.*;
15 < import java.util.concurrent.*;
13 >
14 > import junit.framework.Test;
15 > import junit.framework.TestSuite;
16  
17   public class StampedLockTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run(suite());
19 >        main(suite(), args);
20      }
21      public static Test suite() {
22          return new TestSuite(StampedLockTest.class);
# Line 166 | Line 166 | public class StampedLockTest extends JSR
166       */
167      public void testWriteUnlock_IMSE2() {
168          StampedLock lock = new StampedLock();
169 +        long s = lock.writeLock();
170 +        lock.unlockWrite(s);
171          try {
170            long s = lock.writeLock();
171            lock.unlockWrite(s);
172              lock.unlockWrite(s);
173              shouldThrow();
174          } catch (IllegalMonitorStateException success) {}
# Line 179 | Line 179 | public class StampedLockTest extends JSR
179       */
180      public void testWriteUnlock_IMSE3() {
181          StampedLock lock = new StampedLock();
182 +        long s = lock.readLock();
183          try {
183            long s = lock.readLock();
184              lock.unlockWrite(s);
185              shouldThrow();
186          } catch (IllegalMonitorStateException success) {}
# Line 191 | Line 191 | public class StampedLockTest extends JSR
191       */
192      public void testReadUnlock_IMSE() {
193          StampedLock lock = new StampedLock();
194 +        long s = lock.readLock();
195 +        lock.unlockRead(s);
196          try {
195            long s = lock.readLock();
196            lock.unlockRead(s);
197              lock.unlockRead(s);
198              shouldThrow();
199          } catch (IllegalMonitorStateException success) {}
# Line 215 | Line 215 | public class StampedLockTest extends JSR
215       */
216      public void testReadUnlock_IMSE3() {
217          StampedLock lock = new StampedLock();
218 +        long s = lock.writeLock();
219          try {
219            long s = lock.writeLock();
220              lock.unlockRead(s);
221              shouldThrow();
222          } catch (IllegalMonitorStateException success) {}
# Line 233 | Line 233 | public class StampedLockTest extends JSR
233      /**
234       * A stamp obtained from a successful lock operation validates
235       */
236 <    public void testValidate() {
237 <        try {
238 <            StampedLock lock = new StampedLock();
239 <            long s = lock.writeLock();
240 <            assertTrue(lock.validate(s));
241 <            lock.unlockWrite(s);
242 <            s = lock.readLock();
243 <            assertTrue(lock.validate(s));
244 <            lock.unlockRead(s);
245 <            assertTrue((s = lock.tryWriteLock()) != 0L);
246 <            assertTrue(lock.validate(s));
247 <            lock.unlockWrite(s);
248 <            assertTrue((s = lock.tryReadLock()) != 0L);
249 <            assertTrue(lock.validate(s));
250 <            lock.unlockRead(s);
251 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
252 <            assertTrue(lock.validate(s));
253 <            lock.unlockWrite(s);
254 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
255 <            assertTrue(lock.validate(s));
256 <            lock.unlockRead(s);
257 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
258 <        } catch (InterruptedException ie) {
259 <            threadUnexpectedException(ie);
260 <        }
236 >    public void testValidate() throws InterruptedException {
237 >        StampedLock lock = new StampedLock();
238 >        long s = lock.writeLock();
239 >        assertTrue(lock.validate(s));
240 >        lock.unlockWrite(s);
241 >        s = lock.readLock();
242 >        assertTrue(lock.validate(s));
243 >        lock.unlockRead(s);
244 >        assertTrue((s = lock.tryWriteLock()) != 0L);
245 >        assertTrue(lock.validate(s));
246 >        lock.unlockWrite(s);
247 >        assertTrue((s = lock.tryReadLock()) != 0L);
248 >        assertTrue(lock.validate(s));
249 >        lock.unlockRead(s);
250 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
251 >        assertTrue(lock.validate(s));
252 >        lock.unlockWrite(s);
253 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
254 >        assertTrue(lock.validate(s));
255 >        lock.unlockRead(s);
256 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
257      }
258  
259      /**
260       * A stamp obtained from an unsuccessful lock operation does not validate
261       */
262 <    public void testValidate2() {
263 <        try {
264 <            StampedLock lock = new StampedLock();
265 <            long s;
266 <            assertTrue((s = lock.writeLock()) != 0L);
267 <            assertTrue(lock.validate(s));
268 <            assertFalse(lock.validate(lock.tryWriteLock()));
269 <            assertFalse(lock.validate(lock.tryWriteLock(100L, MILLISECONDS)));
270 <            assertFalse(lock.validate(lock.tryReadLock()));
271 <            assertFalse(lock.validate(lock.tryReadLock(100L, MILLISECONDS)));
272 <            assertFalse(lock.validate(lock.tryOptimisticRead()));
277 <            lock.unlockWrite(s);
278 <        } catch (InterruptedException ie) {
279 <            threadUnexpectedException(ie);
280 <        }
262 >    public void testValidate2() throws InterruptedException {
263 >        StampedLock lock = new StampedLock();
264 >        long s;
265 >        assertTrue((s = lock.writeLock()) != 0L);
266 >        assertTrue(lock.validate(s));
267 >        assertFalse(lock.validate(lock.tryWriteLock()));
268 >        assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
269 >        assertFalse(lock.validate(lock.tryReadLock()));
270 >        assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
271 >        assertFalse(lock.validate(lock.tryOptimisticRead()));
272 >        lock.unlockWrite(s);
273      }
274  
275      /**
276       * writeLockInterruptibly is interruptible
277       */
278 <    public void testWriteLockInterruptibly_Interruptible() {
278 >    public void testWriteLockInterruptibly_Interruptible()
279 >            throws InterruptedException {
280          final CountDownLatch running = new CountDownLatch(1);
281          final StampedLock lock = new StampedLock();
282          long s = lock.writeLock();
# Line 292 | Line 285 | public class StampedLockTest extends JSR
285                  running.countDown();
286                  lock.writeLockInterruptibly();
287              }});
288 <        try {
289 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
290 <            t.interrupt();
291 <            awaitTermination(t);
292 <            releaseWriteLock(lock, s);
293 <        } catch (InterruptedException ie) {
301 <            threadUnexpectedException(ie);
302 <        }
288 >
289 >        running.await();
290 >        waitForThreadToEnterWaitState(t, 100);
291 >        t.interrupt();
292 >        awaitTermination(t);
293 >        releaseWriteLock(lock, s);
294      }
295  
296      /**
297       * timed tryWriteLock is interruptible
298       */
299 <    public void testWriteTryLock_Interruptible() {
299 >    public void testWriteTryLock_Interruptible() throws InterruptedException {
300          final CountDownLatch running = new CountDownLatch(1);
301          final StampedLock lock = new StampedLock();
302          long s = lock.writeLock();
# Line 314 | Line 305 | public class StampedLockTest extends JSR
305                  running.countDown();
306                  lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
307              }});
308 <        try {
309 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
310 <            t.interrupt();
311 <            awaitTermination(t);
312 <            releaseWriteLock(lock, s);
313 <        } catch (InterruptedException ie) {
323 <            threadUnexpectedException(ie);
324 <        }
308 >
309 >        running.await();
310 >        waitForThreadToEnterWaitState(t, 100);
311 >        t.interrupt();
312 >        awaitTermination(t);
313 >        releaseWriteLock(lock, s);
314      }
315  
316      /**
317       * readLockInterruptibly is interruptible
318       */
319 <    public void testReadLockInterruptibly_Interruptible() {
319 >    public void testReadLockInterruptibly_Interruptible()
320 >            throws InterruptedException {
321          final CountDownLatch running = new CountDownLatch(1);
322          final StampedLock lock = new StampedLock();
323          long s = lock.writeLock();
# Line 336 | Line 326 | public class StampedLockTest extends JSR
326                  running.countDown();
327                  lock.readLockInterruptibly();
328              }});
329 <        try {
330 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
331 <            t.interrupt();
332 <            awaitTermination(t);
333 <            releaseWriteLock(lock, s);
334 <        } catch (InterruptedException ie) {
345 <            threadUnexpectedException(ie);
346 <        }
329 >
330 >        running.await();
331 >        waitForThreadToEnterWaitState(t, 100);
332 >        t.interrupt();
333 >        awaitTermination(t);
334 >        releaseWriteLock(lock, s);
335      }
336  
337      /**
338       * timed tryReadLock is interruptible
339       */
340 <    public void testReadTryLock_Interruptible() {
340 >    public void testReadTryLock_Interruptible() throws InterruptedException {
341          final CountDownLatch running = new CountDownLatch(1);
342          final StampedLock lock = new StampedLock();
343          long s = lock.writeLock();
# Line 358 | Line 346 | public class StampedLockTest extends JSR
346                  running.countDown();
347                  lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
348              }});
349 <        try {
350 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
351 <            t.interrupt();
352 <            awaitTermination(t);
353 <            releaseWriteLock(lock, s);
354 <        } catch (InterruptedException ie) {
367 <            threadUnexpectedException(ie);
368 <        }
349 >
350 >        running.await();
351 >        waitForThreadToEnterWaitState(t, 100);
352 >        t.interrupt();
353 >        awaitTermination(t);
354 >        releaseWriteLock(lock, s);
355      }
356  
357      /**
# Line 438 | Line 424 | public class StampedLockTest extends JSR
424      /**
425       * A writelock succeeds only after a reading thread unlocks
426       */
427 <    public void testWriteAfterReadLock() {
427 >    public void testWriteAfterReadLock() throws InterruptedException {
428          final CountDownLatch running = new CountDownLatch(1);
429          final StampedLock lock = new StampedLock();
430          long rs = lock.readLock();
# Line 448 | Line 434 | public class StampedLockTest extends JSR
434                  long s = lock.writeLock();
435                  lock.unlockWrite(s);
436              }});
437 <        try {
438 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
439 <            assertFalse(lock.isWriteLocked());
440 <            lock.unlockRead(rs);
441 <            awaitTermination(t);
442 <            assertFalse(lock.isWriteLocked());
443 <        } catch (InterruptedException ie) {
458 <            threadUnexpectedException(ie);
459 <        }
437 >
438 >        running.await();
439 >        waitForThreadToEnterWaitState(t, 100);
440 >        assertFalse(lock.isWriteLocked());
441 >        lock.unlockRead(rs);
442 >        awaitTermination(t);
443 >        assertFalse(lock.isWriteLocked());
444      }
445  
446      /**
# Line 470 | Line 454 | public class StampedLockTest extends JSR
454                  long rs = lock.readLock();
455                  lock.unlockRead(rs);
456              }});
457 +
458          awaitTermination(t1);
459  
460          Thread t2 = newStartedThread(new CheckedRunnable() {
# Line 477 | Line 462 | public class StampedLockTest extends JSR
462                  long ws = lock.writeLock();
463                  lock.unlockWrite(ws);
464              }});
465 +
466          assertFalse(lock.isWriteLocked());
467          lock.unlockRead(s);
468          awaitTermination(t2);
# Line 571 | Line 557 | public class StampedLockTest extends JSR
557                  assertEquals(rs, 0L);
558                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
559              }});
560 <        
560 >
561          awaitTermination(t);
562          assertTrue(lock.isWriteLocked());
563          lock.unlockWrite(s);
# Line 580 | Line 566 | public class StampedLockTest extends JSR
566      /**
567       * writeLockInterruptibly succeeds if unlocked, else is interruptible
568       */
569 <    public void testWriteLockInterruptibly() {
569 >    public void testWriteLockInterruptibly() throws InterruptedException {
570          final CountDownLatch running = new CountDownLatch(1);
571          final StampedLock lock = new StampedLock();
572 <        long s = 0L;
587 <        try {
588 <            s = lock.writeLockInterruptibly();
589 <        } catch (InterruptedException ie) {
590 <            threadUnexpectedException(ie);
591 <        }
572 >        long s = lock.writeLockInterruptibly();
573          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
574              public void realRun() throws InterruptedException {
575                  running.countDown();
576                  lock.writeLockInterruptibly();
577              }});
578  
579 <        try {
580 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
581 <            t.interrupt();
582 <            assertTrue(lock.isWriteLocked());
583 <            awaitTermination(t);
584 <            releaseWriteLock(lock, s);
604 <        } catch (InterruptedException ie) {
605 <            threadUnexpectedException(ie);
606 <        }
607 <
579 >        running.await();
580 >        waitForThreadToEnterWaitState(t, 100);
581 >        t.interrupt();
582 >        assertTrue(lock.isWriteLocked());
583 >        awaitTermination(t);
584 >        releaseWriteLock(lock, s);
585      }
586  
587      /**
588       * readLockInterruptibly succeeds if lock free else is interruptible
589       */
590 <    public void testReadLockInterruptibly() {
590 >    public void testReadLockInterruptibly() throws InterruptedException {
591          final CountDownLatch running = new CountDownLatch(1);
592          final StampedLock lock = new StampedLock();
593 <        long s = 0L;
594 <        try {
595 <            s = lock.readLockInterruptibly();
596 <            lock.unlockRead(s);
620 <            s = lock.writeLockInterruptibly();
621 <        } catch (InterruptedException ie) {
622 <            threadUnexpectedException(ie);
623 <        }
593 >        long s;
594 >        s = lock.readLockInterruptibly();
595 >        lock.unlockRead(s);
596 >        s = lock.writeLockInterruptibly();
597          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
598              public void realRun() throws InterruptedException {
599                  running.countDown();
600                  lock.readLockInterruptibly();
601              }});
602 <        try {
603 <            running.await(); Thread.sleep(SHORT_DELAY_MS);
604 <            t.interrupt();
605 <            awaitTermination(t);
606 <            releaseWriteLock(lock, s);
607 <        } catch (InterruptedException ie) {
635 <            threadUnexpectedException(ie);
636 <        }
602 >
603 >        running.await();
604 >        waitForThreadToEnterWaitState(t, 100);
605 >        t.interrupt();
606 >        awaitTermination(t);
607 >        releaseWriteLock(lock, s);
608      }
609  
610      /**
# Line 650 | Line 621 | public class StampedLockTest extends JSR
621          clone.unlockWrite(s);
622          assertFalse(clone.isWriteLocked());
623      }
624 +
625      /**
626       * toString indicates current lock state
627       */
# Line 666 | Line 638 | public class StampedLockTest extends JSR
638      /**
639       * tryOptimisticRead succeeds and validates if unlocked, fails if locked
640       */
641 <    public void testValidateOptimistic() {
642 <        try {
643 <            StampedLock lock = new StampedLock();
644 <            long s, p;
645 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
646 <            assertTrue(lock.validate(p));
647 <            assertTrue((s = lock.writeLock()) != 0L);
648 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
649 <            assertTrue(lock.validate(s));
650 <            lock.unlockWrite(s);
651 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
652 <            assertTrue(lock.validate(p));
653 <            assertTrue((s = lock.readLock()) != 0L);
654 <            assertTrue(lock.validate(s));
655 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
656 <            assertTrue(lock.validate(p));
657 <            lock.unlockRead(s);
658 <            assertTrue((s = lock.tryWriteLock()) != 0L);
659 <            assertTrue(lock.validate(s));
660 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
661 <            lock.unlockWrite(s);
662 <            assertTrue((s = lock.tryReadLock()) != 0L);
663 <            assertTrue(lock.validate(s));
664 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
665 <            lock.unlockRead(s);
666 <            assertTrue(lock.validate(p));
667 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
668 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
669 <            assertTrue(lock.validate(s));
670 <            lock.unlockWrite(s);
671 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
672 <            assertTrue(lock.validate(s));
673 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
674 <            lock.unlockRead(s);
703 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
704 <        } catch (InterruptedException ie) {
705 <            threadUnexpectedException(ie);
706 <        }
641 >    public void testValidateOptimistic() throws InterruptedException {
642 >        StampedLock lock = new StampedLock();
643 >        long s, p;
644 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
645 >        assertTrue(lock.validate(p));
646 >        assertTrue((s = lock.writeLock()) != 0L);
647 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
648 >        assertTrue(lock.validate(s));
649 >        lock.unlockWrite(s);
650 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
651 >        assertTrue(lock.validate(p));
652 >        assertTrue((s = lock.readLock()) != 0L);
653 >        assertTrue(lock.validate(s));
654 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
655 >        assertTrue(lock.validate(p));
656 >        lock.unlockRead(s);
657 >        assertTrue((s = lock.tryWriteLock()) != 0L);
658 >        assertTrue(lock.validate(s));
659 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
660 >        lock.unlockWrite(s);
661 >        assertTrue((s = lock.tryReadLock()) != 0L);
662 >        assertTrue(lock.validate(s));
663 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
664 >        lock.unlockRead(s);
665 >        assertTrue(lock.validate(p));
666 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
667 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
668 >        assertTrue(lock.validate(s));
669 >        lock.unlockWrite(s);
670 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
671 >        assertTrue(lock.validate(s));
672 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
673 >        lock.unlockRead(s);
674 >        assertTrue((p = lock.tryOptimisticRead()) != 0L);
675      }
676  
677      /**
# Line 724 | Line 692 | public class StampedLockTest extends JSR
692       * tryOptimisticRead stamp does not validate if a write lock
693       * intervenes in another thread
694       */
695 <    public void testValidateOptimisticWriteLocked2() {
695 >    public void testValidateOptimisticWriteLocked2()
696 >            throws InterruptedException {
697          final CountDownLatch running = new CountDownLatch(1);
698          final StampedLock lock = new StampedLock();
699          long s, p;
700          assertTrue((p = lock.tryOptimisticRead()) != 0L);
701          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
702 <                public void realRun() throws InterruptedException {
703 <                    lock.writeLockInterruptibly();
704 <                    running.countDown();
705 <                    lock.writeLockInterruptibly();
706 <                }});
707 <        try {
708 <            running.await();
709 <            assertFalse(lock.validate(p));
710 <            assertFalse((p = lock.tryOptimisticRead()) != 0L);
711 <            t.interrupt();
712 <            awaitTermination(t);
744 <        } catch (InterruptedException ie) {
745 <            threadUnexpectedException(ie);
746 <        }
702 >            public void realRun() throws InterruptedException {
703 >                lock.writeLockInterruptibly();
704 >                running.countDown();
705 >                lock.writeLockInterruptibly();
706 >            }});
707 >
708 >        running.await();
709 >        assertFalse(lock.validate(p));
710 >        assertFalse((p = lock.tryOptimisticRead()) != 0L);
711 >        t.interrupt();
712 >        awaitTermination(t);
713      }
714  
715      /**
716       * tryConvertToOptimisticRead succeeds and validates if successfully locked,
717       */
718 <    public void testTryConvertToOptimisticRead() {
719 <        try {
720 <            StampedLock lock = new StampedLock();
721 <            long s, p;
722 <            s = 0L;
723 <            assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
724 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
725 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
726 <            assertTrue((s = lock.writeLock()) != 0L);
727 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
728 <            assertTrue(lock.validate(p));
729 <            assertTrue((s = lock.readLock()) != 0L);
730 <            assertTrue(lock.validate(s));
731 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
732 <            assertTrue(lock.validate(p));
733 <            assertTrue((s = lock.tryWriteLock()) != 0L);
734 <            assertTrue(lock.validate(s));
735 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
736 <            assertTrue(lock.validate(p));
737 <            assertTrue((s = lock.tryReadLock()) != 0L);
738 <            assertTrue(lock.validate(s));
739 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
740 <            assertTrue(lock.validate(p));
741 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
742 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
743 <            assertTrue(lock.validate(p));
744 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
745 <            assertTrue(lock.validate(s));
746 <            assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
747 <            assertTrue(lock.validate(p));
748 <        } catch (InterruptedException ie) {
749 <            threadUnexpectedException(ie);
750 <        }
718 >    public void testTryConvertToOptimisticRead() throws InterruptedException {
719 >        StampedLock lock = new StampedLock();
720 >        long s, p;
721 >        assertEquals(0L, lock.tryConvertToOptimisticRead(0L));
722 >
723 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
724 >        assertEquals(s, lock.tryConvertToOptimisticRead(s));
725 >        assertTrue(lock.validate(s));
726 >
727 >        assertTrue((s = lock.writeLock()) != 0L);
728 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
729 >        assertTrue(lock.validate(p));
730 >
731 >        assertTrue((s = lock.readLock()) != 0L);
732 >        assertTrue(lock.validate(s));
733 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
734 >        assertTrue(lock.validate(p));
735 >
736 >        assertTrue((s = lock.tryWriteLock()) != 0L);
737 >        assertTrue(lock.validate(s));
738 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
739 >        assertTrue(lock.validate(p));
740 >
741 >        assertTrue((s = lock.tryReadLock()) != 0L);
742 >        assertTrue(lock.validate(s));
743 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
744 >        assertTrue(lock.validate(p));
745 >
746 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
747 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
748 >        assertTrue(lock.validate(p));
749 >
750 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
751 >        assertTrue(lock.validate(s));
752 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
753 >        assertTrue(lock.validate(p));
754      }
755  
756      /**
757       * tryConvertToReadLock succeeds and validates if successfully locked
758       * or lock free;
759       */
760 <    public void testTryConvertToReadLock() {
761 <        try {
762 <            StampedLock lock = new StampedLock();
763 <            long s, p;
764 <            s = 0L;
765 <            assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
766 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
767 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
768 <            lock.unlockRead(p);
769 <            assertTrue((s = lock.writeLock()) != 0L);
770 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
771 <            assertTrue(lock.validate(p));
772 <            lock.unlockRead(p);
773 <            assertTrue((s = lock.readLock()) != 0L);
774 <            assertTrue(lock.validate(s));
775 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
776 <            assertTrue(lock.validate(p));
777 <            lock.unlockRead(p);
778 <            assertTrue((s = lock.tryWriteLock()) != 0L);
779 <            assertTrue(lock.validate(s));
780 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
781 <            assertTrue(lock.validate(p));
782 <            lock.unlockRead(p);
783 <            assertTrue((s = lock.tryReadLock()) != 0L);
784 <            assertTrue(lock.validate(s));
785 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
786 <            assertTrue(lock.validate(p));
787 <            lock.unlockRead(p);
788 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
789 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
790 <            assertTrue(lock.validate(p));
791 <            lock.unlockRead(p);
792 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
793 <            assertTrue(lock.validate(s));
794 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
795 <            assertTrue(lock.validate(p));
827 <            lock.unlockRead(p);
828 <        } catch (InterruptedException ie) {
829 <            threadUnexpectedException(ie);
830 <        }
760 >    public void testTryConvertToReadLock() throws InterruptedException {
761 >        StampedLock lock = new StampedLock();
762 >        long s, p;
763 >        s = 0L;
764 >        assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
765 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
766 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
767 >        lock.unlockRead(p);
768 >        assertTrue((s = lock.writeLock()) != 0L);
769 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
770 >        assertTrue(lock.validate(p));
771 >        lock.unlockRead(p);
772 >        assertTrue((s = lock.readLock()) != 0L);
773 >        assertTrue(lock.validate(s));
774 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
775 >        assertTrue(lock.validate(p));
776 >        lock.unlockRead(p);
777 >        assertTrue((s = lock.tryWriteLock()) != 0L);
778 >        assertTrue(lock.validate(s));
779 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
780 >        assertTrue(lock.validate(p));
781 >        lock.unlockRead(p);
782 >        assertTrue((s = lock.tryReadLock()) != 0L);
783 >        assertTrue(lock.validate(s));
784 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
785 >        assertTrue(lock.validate(p));
786 >        lock.unlockRead(p);
787 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
788 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
789 >        assertTrue(lock.validate(p));
790 >        lock.unlockRead(p);
791 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
792 >        assertTrue(lock.validate(s));
793 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
794 >        assertTrue(lock.validate(p));
795 >        lock.unlockRead(p);
796      }
797  
798      /**
799       * tryConvertToWriteLock succeeds and validates if successfully locked
800       * or lock free;
801       */
802 <    public void testTryConvertToWriteLock() {
803 <        try {
804 <            StampedLock lock = new StampedLock();
805 <            long s, p;
806 <            s = 0L;
807 <            assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
808 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
809 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
810 <            lock.unlockWrite(p);
811 <            assertTrue((s = lock.writeLock()) != 0L);
812 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
813 <            assertTrue(lock.validate(p));
814 <            lock.unlockWrite(p);
815 <            assertTrue((s = lock.readLock()) != 0L);
816 <            assertTrue(lock.validate(s));
817 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
818 <            assertTrue(lock.validate(p));
819 <            lock.unlockWrite(p);
820 <            assertTrue((s = lock.tryWriteLock()) != 0L);
821 <            assertTrue(lock.validate(s));
822 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
823 <            assertTrue(lock.validate(p));
824 <            lock.unlockWrite(p);
825 <            assertTrue((s = lock.tryReadLock()) != 0L);
826 <            assertTrue(lock.validate(s));
827 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
828 <            assertTrue(lock.validate(p));
829 <            lock.unlockWrite(p);
830 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
831 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
832 <            assertTrue(lock.validate(p));
833 <            lock.unlockWrite(p);
834 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
835 <            assertTrue(lock.validate(s));
836 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
837 <            assertTrue(lock.validate(p));
873 <            lock.unlockWrite(p);
874 <        } catch (InterruptedException ie) {
875 <            threadUnexpectedException(ie);
876 <        }
802 >    public void testTryConvertToWriteLock() throws InterruptedException {
803 >        StampedLock lock = new StampedLock();
804 >        long s, p;
805 >        s = 0L;
806 >        assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
807 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
808 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
809 >        lock.unlockWrite(p);
810 >        assertTrue((s = lock.writeLock()) != 0L);
811 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
812 >        assertTrue(lock.validate(p));
813 >        lock.unlockWrite(p);
814 >        assertTrue((s = lock.readLock()) != 0L);
815 >        assertTrue(lock.validate(s));
816 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
817 >        assertTrue(lock.validate(p));
818 >        lock.unlockWrite(p);
819 >        assertTrue((s = lock.tryWriteLock()) != 0L);
820 >        assertTrue(lock.validate(s));
821 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
822 >        assertTrue(lock.validate(p));
823 >        lock.unlockWrite(p);
824 >        assertTrue((s = lock.tryReadLock()) != 0L);
825 >        assertTrue(lock.validate(s));
826 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
827 >        assertTrue(lock.validate(p));
828 >        lock.unlockWrite(p);
829 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
830 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
831 >        assertTrue(lock.validate(p));
832 >        lock.unlockWrite(p);
833 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
834 >        assertTrue(lock.validate(s));
835 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
836 >        assertTrue(lock.validate(p));
837 >        lock.unlockWrite(p);
838      }
839  
840      /**
# Line 922 | Line 883 | public class StampedLockTest extends JSR
883          assertTrue(lock.tryLock());
884      }
885  
886 +    /**
887 +     * Lock.newCondition throws UnsupportedOperationException
888 +     */
889 +    public void testLockViewsDoNotSupportConditions() {
890 +        StampedLock sl = new StampedLock();
891 +        assertThrows(UnsupportedOperationException.class,
892 +                     () -> sl.asWriteLock().newCondition(),
893 +                     () -> sl.asReadLock().newCondition(),
894 +                     () -> sl.asReadWriteLock().writeLock().newCondition(),
895 +                     () -> sl.asReadWriteLock().readLock().newCondition());
896 +    }
897 +
898 +    /**
899 +     * Passing optimistic read stamps to unlock operations result in
900 +     * IllegalMonitorStateException
901 +     */
902 +    public void testCannotUnlockOptimisticReadStamps() {
903 +        Runnable[] actions = {
904 +            () -> {
905 +                StampedLock sl = new StampedLock();
906 +                long stamp = sl.tryOptimisticRead();
907 +                assertTrue(stamp != 0);
908 +                sl.unlockRead(stamp);
909 +            },
910 +            () -> {
911 +                StampedLock sl = new StampedLock();
912 +                long stamp = sl.tryOptimisticRead();
913 +                sl.unlock(stamp);
914 +            },
915 +
916 +            () -> {
917 +                StampedLock sl = new StampedLock();
918 +                long stamp = sl.tryOptimisticRead();
919 +                sl.writeLock();
920 +                sl.unlock(stamp);
921 +            },
922 +            () -> {
923 +                StampedLock sl = new StampedLock();
924 +                long stamp = sl.tryOptimisticRead();
925 +                sl.readLock();
926 +                sl.unlockRead(stamp);
927 +            },
928 +            () -> {
929 +                StampedLock sl = new StampedLock();
930 +                long stamp = sl.tryOptimisticRead();
931 +                sl.readLock();
932 +                sl.unlock(stamp);
933 +            },
934 +
935 +            () -> {
936 +                StampedLock sl = new StampedLock();
937 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
938 +                assertTrue(stamp != 0);
939 +                sl.writeLock();
940 +                sl.unlockWrite(stamp);
941 +            },
942 +            () -> {
943 +                StampedLock sl = new StampedLock();
944 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
945 +                sl.writeLock();
946 +                sl.unlock(stamp);
947 +            },
948 +            () -> {
949 +                StampedLock sl = new StampedLock();
950 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
951 +                sl.readLock();
952 +                sl.unlockRead(stamp);
953 +            },
954 +            () -> {
955 +                StampedLock sl = new StampedLock();
956 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
957 +                sl.readLock();
958 +                sl.unlock(stamp);
959 +            },
960 +
961 +            () -> {
962 +                StampedLock sl = new StampedLock();
963 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
964 +                assertTrue(stamp != 0);
965 +                sl.writeLock();
966 +                sl.unlockWrite(stamp);
967 +            },
968 +            () -> {
969 +                StampedLock sl = new StampedLock();
970 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
971 +                sl.writeLock();
972 +                sl.unlock(stamp);
973 +            },
974 +            () -> {
975 +                StampedLock sl = new StampedLock();
976 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
977 +                sl.readLock();
978 +                sl.unlockRead(stamp);
979 +            },
980 +            () -> {
981 +                StampedLock sl = new StampedLock();
982 +                sl.readLock();
983 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
984 +                assertTrue(stamp != 0);
985 +                sl.readLock();
986 +                sl.unlockRead(stamp);
987 +            },
988 +            () -> {
989 +                StampedLock sl = new StampedLock();
990 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
991 +                sl.readLock();
992 +                sl.unlock(stamp);
993 +            },
994 +            () -> {
995 +                StampedLock sl = new StampedLock();
996 +                sl.readLock();
997 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
998 +                sl.readLock();
999 +                sl.unlock(stamp);
1000 +            },
1001 +        };
1002 +
1003 +        assertThrows(IllegalMonitorStateException.class, actions);
1004 +    }
1005 +
1006   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines