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.6 by dl, Thu Mar 21 19:06:54 2013 UTC vs.
Revision 1.20 by jsr166, Tue Jun 7 07:20:09 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();
290 <            waitForThreadToEnterWaitState(t, 100);
291 <            t.interrupt();
292 <            awaitTermination(t);
293 <            releaseWriteLock(lock, s);
301 <        } catch (InterruptedException ie) {
302 <            threadUnexpectedException(ie);
303 <        }
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 315 | Line 305 | public class StampedLockTest extends JSR
305                  running.countDown();
306                  lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
307              }});
308 <        try {
309 <            running.await();
310 <            waitForThreadToEnterWaitState(t, 100);
311 <            t.interrupt();
312 <            awaitTermination(t);
313 <            releaseWriteLock(lock, s);
324 <        } catch (InterruptedException ie) {
325 <            threadUnexpectedException(ie);
326 <        }
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 338 | Line 326 | public class StampedLockTest extends JSR
326                  running.countDown();
327                  lock.readLockInterruptibly();
328              }});
329 <        try {
330 <            running.await();
331 <            waitForThreadToEnterWaitState(t, 100);
332 <            t.interrupt();
333 <            awaitTermination(t);
334 <            releaseWriteLock(lock, s);
347 <        } catch (InterruptedException ie) {
348 <            threadUnexpectedException(ie);
349 <        }
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 361 | Line 346 | public class StampedLockTest extends JSR
346                  running.countDown();
347                  lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
348              }});
349 <        try {
350 <            running.await();
351 <            waitForThreadToEnterWaitState(t, 100);
352 <            t.interrupt();
353 <            awaitTermination(t);
354 <            releaseWriteLock(lock, s);
370 <        } catch (InterruptedException ie) {
371 <            threadUnexpectedException(ie);
372 <        }
349 >
350 >        running.await();
351 >        waitForThreadToEnterWaitState(t, 100);
352 >        t.interrupt();
353 >        awaitTermination(t);
354 >        releaseWriteLock(lock, s);
355      }
356  
357      /**
# Line 442 | 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 452 | Line 434 | public class StampedLockTest extends JSR
434                  long s = lock.writeLock();
435                  lock.unlockWrite(s);
436              }});
437 <        try {
438 <            running.await();
439 <            waitForThreadToEnterWaitState(t, 100);
440 <            assertFalse(lock.isWriteLocked());
441 <            lock.unlockRead(rs);
442 <            awaitTermination(t);
443 <            assertFalse(lock.isWriteLocked());
462 <        } catch (InterruptedException ie) {
463 <            threadUnexpectedException(ie);
464 <        }
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 475 | 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 482 | 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 576 | 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 585 | 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;
592 <        try {
593 <            s = lock.writeLockInterruptibly();
594 <        } catch (InterruptedException ie) {
595 <            threadUnexpectedException(ie);
596 <        }
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();
581 <            waitForThreadToEnterWaitState(t, 100);
582 <            t.interrupt();
583 <            assertTrue(lock.isWriteLocked());
584 <            awaitTermination(t);
609 <            releaseWriteLock(lock, s);
610 <        } catch (InterruptedException ie) {
611 <            threadUnexpectedException(ie);
612 <        }
613 <
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);
626 <            s = lock.writeLockInterruptibly();
627 <        } catch (InterruptedException ie) {
628 <            threadUnexpectedException(ie);
629 <        }
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();
604 <            waitForThreadToEnterWaitState(t, 100);
605 <            t.interrupt();
606 <            awaitTermination(t);
607 <            releaseWriteLock(lock, s);
641 <        } catch (InterruptedException ie) {
642 <            threadUnexpectedException(ie);
643 <        }
602 >
603 >        running.await();
604 >        waitForThreadToEnterWaitState(t, 100);
605 >        t.interrupt();
606 >        awaitTermination(t);
607 >        releaseWriteLock(lock, s);
608      }
609  
610      /**
# Line 674 | 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);
711 <            assertTrue((p = lock.tryOptimisticRead()) != 0L);
712 <        } catch (InterruptedException ie) {
713 <            threadUnexpectedException(ie);
714 <        }
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 732 | 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);
752 <        } catch (InterruptedException ie) {
753 <            threadUnexpectedException(ie);
754 <        }
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((p = lock.readLock()) != 0L);
728 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
729 >        assertEquals(s, lock.tryConvertToOptimisticRead(s));
730 >        assertTrue(lock.validate(s));
731 >        lock.unlockRead(p);
732 >
733 >        assertTrue((s = lock.writeLock()) != 0L);
734 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
735 >        assertTrue(lock.validate(p));
736 >
737 >        assertTrue((s = lock.readLock()) != 0L);
738 >        assertTrue(lock.validate(s));
739 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
740 >        assertTrue(lock.validate(p));
741 >
742 >        assertTrue((s = lock.tryWriteLock()) != 0L);
743 >        assertTrue(lock.validate(s));
744 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
745 >        assertTrue(lock.validate(p));
746 >
747 >        assertTrue((s = lock.tryReadLock()) != 0L);
748 >        assertTrue(lock.validate(s));
749 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
750 >        assertTrue(lock.validate(p));
751 >
752 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
753 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
754 >        assertTrue(lock.validate(p));
755 >
756 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
757 >        assertTrue(lock.validate(s));
758 >        assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
759 >        assertTrue(lock.validate(p));
760      }
761  
762      /**
763       * tryConvertToReadLock succeeds and validates if successfully locked
764       * or lock free;
765       */
766 <    public void testTryConvertToReadLock() {
767 <        try {
768 <            StampedLock lock = new StampedLock();
769 <            long s, p;
770 <            s = 0L;
771 <            assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
772 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
773 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
774 <            lock.unlockRead(p);
775 <            assertTrue((s = lock.writeLock()) != 0L);
776 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
777 <            assertTrue(lock.validate(p));
778 <            lock.unlockRead(p);
779 <            assertTrue((s = lock.readLock()) != 0L);
780 <            assertTrue(lock.validate(s));
781 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
782 <            assertTrue(lock.validate(p));
783 <            lock.unlockRead(p);
784 <            assertTrue((s = lock.tryWriteLock()) != 0L);
785 <            assertTrue(lock.validate(s));
786 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
787 <            assertTrue(lock.validate(p));
788 <            lock.unlockRead(p);
789 <            assertTrue((s = lock.tryReadLock()) != 0L);
790 <            assertTrue(lock.validate(s));
791 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
792 <            assertTrue(lock.validate(p));
793 <            lock.unlockRead(p);
794 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
795 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
796 <            assertTrue(lock.validate(p));
797 <            lock.unlockRead(p);
798 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
799 <            assertTrue(lock.validate(s));
800 <            assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
801 <            assertTrue(lock.validate(p));
835 <            lock.unlockRead(p);
836 <        } catch (InterruptedException ie) {
837 <            threadUnexpectedException(ie);
838 <        }
766 >    public void testTryConvertToReadLock() throws InterruptedException {
767 >        StampedLock lock = new StampedLock();
768 >        long s, p;
769 >        s = 0L;
770 >        assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
771 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
772 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
773 >        lock.unlockRead(p);
774 >        assertTrue((s = lock.writeLock()) != 0L);
775 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
776 >        assertTrue(lock.validate(p));
777 >        lock.unlockRead(p);
778 >        assertTrue((s = lock.readLock()) != 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.tryWriteLock()) != 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.tryReadLock()) != 0L);
789 >        assertTrue(lock.validate(s));
790 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
791 >        assertTrue(lock.validate(p));
792 >        lock.unlockRead(p);
793 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
794 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
795 >        assertTrue(lock.validate(p));
796 >        lock.unlockRead(p);
797 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
798 >        assertTrue(lock.validate(s));
799 >        assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
800 >        assertTrue(lock.validate(p));
801 >        lock.unlockRead(p);
802      }
803  
804      /**
805       * tryConvertToWriteLock succeeds and validates if successfully locked
806       * or lock free;
807       */
808 <    public void testTryConvertToWriteLock() {
809 <        try {
810 <            StampedLock lock = new StampedLock();
811 <            long s, p;
812 <            s = 0L;
813 <            assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
814 <            assertTrue((s = lock.tryOptimisticRead()) != 0L);
815 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
816 <            lock.unlockWrite(p);
817 <            assertTrue((s = lock.writeLock()) != 0L);
818 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
819 <            assertTrue(lock.validate(p));
820 <            lock.unlockWrite(p);
821 <            assertTrue((s = lock.readLock()) != 0L);
822 <            assertTrue(lock.validate(s));
823 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
824 <            assertTrue(lock.validate(p));
825 <            lock.unlockWrite(p);
826 <            assertTrue((s = lock.tryWriteLock()) != 0L);
827 <            assertTrue(lock.validate(s));
828 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
829 <            assertTrue(lock.validate(p));
830 <            lock.unlockWrite(p);
831 <            assertTrue((s = lock.tryReadLock()) != 0L);
832 <            assertTrue(lock.validate(s));
833 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
834 <            assertTrue(lock.validate(p));
835 <            lock.unlockWrite(p);
836 <            assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
837 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
838 <            assertTrue(lock.validate(p));
839 <            lock.unlockWrite(p);
840 <            assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
841 <            assertTrue(lock.validate(s));
842 <            assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
843 <            assertTrue(lock.validate(p));
881 <            lock.unlockWrite(p);
882 <        } catch (InterruptedException ie) {
883 <            threadUnexpectedException(ie);
884 <        }
808 >    public void testTryConvertToWriteLock() throws InterruptedException {
809 >        StampedLock lock = new StampedLock();
810 >        long s, p;
811 >        s = 0L;
812 >        assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
813 >        assertTrue((s = lock.tryOptimisticRead()) != 0L);
814 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
815 >        lock.unlockWrite(p);
816 >        assertTrue((s = lock.writeLock()) != 0L);
817 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
818 >        assertTrue(lock.validate(p));
819 >        lock.unlockWrite(p);
820 >        assertTrue((s = lock.readLock()) != 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.tryWriteLock()) != 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.tryReadLock()) != 0L);
831 >        assertTrue(lock.validate(s));
832 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
833 >        assertTrue(lock.validate(p));
834 >        lock.unlockWrite(p);
835 >        assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
836 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
837 >        assertTrue(lock.validate(p));
838 >        lock.unlockWrite(p);
839 >        assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
840 >        assertTrue(lock.validate(s));
841 >        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
842 >        assertTrue(lock.validate(p));
843 >        lock.unlockWrite(p);
844      }
845  
846      /**
# Line 930 | Line 889 | public class StampedLockTest extends JSR
889          assertTrue(lock.tryLock());
890      }
891  
892 +    /**
893 +     * Lock.newCondition throws UnsupportedOperationException
894 +     */
895 +    public void testLockViewsDoNotSupportConditions() {
896 +        StampedLock sl = new StampedLock();
897 +        assertThrows(UnsupportedOperationException.class,
898 +                     () -> sl.asWriteLock().newCondition(),
899 +                     () -> sl.asReadLock().newCondition(),
900 +                     () -> sl.asReadWriteLock().writeLock().newCondition(),
901 +                     () -> sl.asReadWriteLock().readLock().newCondition());
902 +    }
903 +
904 +    /**
905 +     * Passing optimistic read stamps to unlock operations result in
906 +     * IllegalMonitorStateException
907 +     */
908 +    public void testCannotUnlockOptimisticReadStamps() {
909 +        Runnable[] actions = {
910 +            () -> {
911 +                StampedLock sl = new StampedLock();
912 +                long stamp = sl.tryOptimisticRead();
913 +                assertTrue(stamp != 0);
914 +                sl.unlockRead(stamp);
915 +            },
916 +            () -> {
917 +                StampedLock sl = new StampedLock();
918 +                long stamp = sl.tryOptimisticRead();
919 +                sl.unlock(stamp);
920 +            },
921 +
922 +            () -> {
923 +                StampedLock sl = new StampedLock();
924 +                long stamp = sl.tryOptimisticRead();
925 +                sl.writeLock();
926 +                sl.unlock(stamp);
927 +            },
928 +            () -> {
929 +                StampedLock sl = new StampedLock();
930 +                long stamp = sl.tryOptimisticRead();
931 +                sl.readLock();
932 +                sl.unlockRead(stamp);
933 +            },
934 +            () -> {
935 +                StampedLock sl = new StampedLock();
936 +                long stamp = sl.tryOptimisticRead();
937 +                sl.readLock();
938 +                sl.unlock(stamp);
939 +            },
940 +
941 +            () -> {
942 +                StampedLock sl = new StampedLock();
943 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
944 +                assertTrue(stamp != 0);
945 +                sl.writeLock();
946 +                sl.unlockWrite(stamp);
947 +            },
948 +            () -> {
949 +                StampedLock sl = new StampedLock();
950 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
951 +                sl.writeLock();
952 +                sl.unlock(stamp);
953 +            },
954 +            () -> {
955 +                StampedLock sl = new StampedLock();
956 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
957 +                sl.readLock();
958 +                sl.unlockRead(stamp);
959 +            },
960 +            () -> {
961 +                StampedLock sl = new StampedLock();
962 +                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
963 +                sl.readLock();
964 +                sl.unlock(stamp);
965 +            },
966 +
967 +            () -> {
968 +                StampedLock sl = new StampedLock();
969 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
970 +                assertTrue(stamp != 0);
971 +                sl.writeLock();
972 +                sl.unlockWrite(stamp);
973 +            },
974 +            () -> {
975 +                StampedLock sl = new StampedLock();
976 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
977 +                sl.writeLock();
978 +                sl.unlock(stamp);
979 +            },
980 +            () -> {
981 +                StampedLock sl = new StampedLock();
982 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
983 +                sl.readLock();
984 +                sl.unlockRead(stamp);
985 +            },
986 +            () -> {
987 +                StampedLock sl = new StampedLock();
988 +                sl.readLock();
989 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
990 +                assertTrue(stamp != 0);
991 +                sl.readLock();
992 +                sl.unlockRead(stamp);
993 +            },
994 +            () -> {
995 +                StampedLock sl = new StampedLock();
996 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
997 +                sl.readLock();
998 +                sl.unlock(stamp);
999 +            },
1000 +            () -> {
1001 +                StampedLock sl = new StampedLock();
1002 +                sl.readLock();
1003 +                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
1004 +                sl.readLock();
1005 +                sl.unlock(stamp);
1006 +            },
1007 +        };
1008 +
1009 +        assertThrows(IllegalMonitorStateException.class, actions);
1010 +    }
1011 +
1012   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines