ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/StampedLockTest.java
Revision: 1.9
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +0 -1 lines
Log Message:
remove unused imports

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Doug Lea and Martin Buchholz
3     * with assistance from members of JCP JSR-166 Expert Group and
4     * released to the public domain, as explained at
5     * http://creativecommons.org/publicdomain/zero/1.0/
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.locks.Lock;
10 jsr166 1.2 import java.util.concurrent.locks.StampedLock;
11 jsr166 1.1 import java.util.concurrent.CountDownLatch;
12     import static java.util.concurrent.TimeUnit.MILLISECONDS;
13     import java.util.*;
14 dl 1.4 import java.util.concurrent.*;
15 jsr166 1.1
16     public class StampedLockTest extends JSR166TestCase {
17     public static void main(String[] args) {
18     junit.textui.TestRunner.run(suite());
19     }
20     public static Test suite() {
21     return new TestSuite(StampedLockTest.class);
22     }
23    
24 dl 1.4 /**
25     * A runnable calling writeLockInterruptibly
26     */
27     class InterruptibleLockRunnable extends CheckedRunnable {
28     final StampedLock lock;
29     InterruptibleLockRunnable(StampedLock l) { lock = l; }
30     public void realRun() throws InterruptedException {
31     lock.writeLockInterruptibly();
32     }
33     }
34    
35     /**
36     * A runnable calling writeLockInterruptibly that expects to be
37     * interrupted
38     */
39     class InterruptedLockRunnable extends CheckedInterruptedRunnable {
40     final StampedLock lock;
41     InterruptedLockRunnable(StampedLock l) { lock = l; }
42     public void realRun() throws InterruptedException {
43     lock.writeLockInterruptibly();
44     }
45     }
46    
47     /**
48     * Releases write lock, checking isWriteLocked before and after
49     */
50     void releaseWriteLock(StampedLock lock, long s) {
51     assertTrue(lock.isWriteLocked());
52     lock.unlockWrite(s);
53     assertFalse(lock.isWriteLocked());
54     }
55    
56     /**
57     * Constructed StampedLock is in unlocked state
58     */
59     public void testConstructor() {
60     StampedLock lock;
61     lock = new StampedLock();
62     assertFalse(lock.isWriteLocked());
63     assertFalse(lock.isReadLocked());
64     assertEquals(lock.getReadLockCount(), 0);
65     }
66    
67     /**
68     * write-locking and read-locking an unlocked lock succeed
69     */
70     public void testLock() {
71     StampedLock lock = new StampedLock();
72     assertFalse(lock.isWriteLocked());
73     assertFalse(lock.isReadLocked());
74     assertEquals(lock.getReadLockCount(), 0);
75     long s = lock.writeLock();
76     assertTrue(lock.isWriteLocked());
77     assertFalse(lock.isReadLocked());
78     assertEquals(lock.getReadLockCount(), 0);
79     lock.unlockWrite(s);
80     assertFalse(lock.isWriteLocked());
81     assertFalse(lock.isReadLocked());
82     assertEquals(lock.getReadLockCount(), 0);
83     long rs = lock.readLock();
84     assertFalse(lock.isWriteLocked());
85     assertTrue(lock.isReadLocked());
86     assertEquals(lock.getReadLockCount(), 1);
87     lock.unlockRead(rs);
88     assertFalse(lock.isWriteLocked());
89     assertFalse(lock.isReadLocked());
90     assertEquals(lock.getReadLockCount(), 0);
91     }
92    
93     /**
94     * unlock releases either a read or write lock
95     */
96     public void testUnlock() {
97     StampedLock lock = new StampedLock();
98     assertFalse(lock.isWriteLocked());
99     assertFalse(lock.isReadLocked());
100     assertEquals(lock.getReadLockCount(), 0);
101     long s = lock.writeLock();
102     assertTrue(lock.isWriteLocked());
103     assertFalse(lock.isReadLocked());
104     assertEquals(lock.getReadLockCount(), 0);
105     lock.unlock(s);
106     assertFalse(lock.isWriteLocked());
107     assertFalse(lock.isReadLocked());
108     assertEquals(lock.getReadLockCount(), 0);
109     long rs = lock.readLock();
110     assertFalse(lock.isWriteLocked());
111     assertTrue(lock.isReadLocked());
112     assertEquals(lock.getReadLockCount(), 1);
113     lock.unlock(rs);
114     assertFalse(lock.isWriteLocked());
115     assertFalse(lock.isReadLocked());
116     assertEquals(lock.getReadLockCount(), 0);
117     }
118    
119     /**
120     * tryUnlockRead/Write succeeds if locked in associated mode else
121     * returns false
122     */
123     public void testTryUnlock() {
124     StampedLock lock = new StampedLock();
125     assertFalse(lock.isWriteLocked());
126     assertFalse(lock.isReadLocked());
127     assertEquals(lock.getReadLockCount(), 0);
128     long s = lock.writeLock();
129     assertTrue(lock.isWriteLocked());
130     assertFalse(lock.isReadLocked());
131     assertEquals(lock.getReadLockCount(), 0);
132     assertFalse(lock.tryUnlockRead());
133     assertTrue(lock.tryUnlockWrite());
134     assertFalse(lock.tryUnlockWrite());
135     assertFalse(lock.tryUnlockRead());
136     assertFalse(lock.isWriteLocked());
137     assertFalse(lock.isReadLocked());
138     assertEquals(lock.getReadLockCount(), 0);
139     long rs = lock.readLock();
140     assertFalse(lock.isWriteLocked());
141     assertTrue(lock.isReadLocked());
142     assertEquals(lock.getReadLockCount(), 1);
143     assertFalse(lock.tryUnlockWrite());
144     assertTrue(lock.tryUnlockRead());
145     assertFalse(lock.tryUnlockRead());
146     assertFalse(lock.tryUnlockWrite());
147     assertFalse(lock.isWriteLocked());
148     assertFalse(lock.isReadLocked());
149     assertEquals(lock.getReadLockCount(), 0);
150     }
151    
152     /**
153     * write-unlocking an unlocked lock throws IllegalMonitorStateException
154     */
155     public void testWriteUnlock_IMSE() {
156     StampedLock lock = new StampedLock();
157     try {
158     lock.unlockWrite(0L);
159     shouldThrow();
160     } catch (IllegalMonitorStateException success) {}
161     }
162    
163     /**
164     * write-unlocking an unlocked lock throws IllegalMonitorStateException
165     */
166     public void testWriteUnlock_IMSE2() {
167     StampedLock lock = new StampedLock();
168     try {
169     long s = lock.writeLock();
170     lock.unlockWrite(s);
171     lock.unlockWrite(s);
172     shouldThrow();
173     } catch (IllegalMonitorStateException success) {}
174     }
175    
176     /**
177     * write-unlocking after readlock throws IllegalMonitorStateException
178     */
179     public void testWriteUnlock_IMSE3() {
180     StampedLock lock = new StampedLock();
181     try {
182     long s = lock.readLock();
183     lock.unlockWrite(s);
184     shouldThrow();
185     } catch (IllegalMonitorStateException success) {}
186     }
187    
188     /**
189     * read-unlocking an unlocked lock throws IllegalMonitorStateException
190     */
191     public void testReadUnlock_IMSE() {
192     StampedLock lock = new StampedLock();
193     try {
194     long s = lock.readLock();
195     lock.unlockRead(s);
196     lock.unlockRead(s);
197     shouldThrow();
198     } catch (IllegalMonitorStateException success) {}
199     }
200    
201     /**
202     * read-unlocking an unlocked lock throws IllegalMonitorStateException
203     */
204     public void testReadUnlock_IMSE2() {
205     StampedLock lock = new StampedLock();
206     try {
207     lock.unlockRead(0L);
208     shouldThrow();
209     } catch (IllegalMonitorStateException success) {}
210     }
211    
212     /**
213     * read-unlocking after writeLock throws IllegalMonitorStateException
214     */
215     public void testReadUnlock_IMSE3() {
216     StampedLock lock = new StampedLock();
217     try {
218     long s = lock.writeLock();
219     lock.unlockRead(s);
220     shouldThrow();
221     } catch (IllegalMonitorStateException success) {}
222     }
223    
224     /**
225     * validate(0) fails
226     */
227     public void testValidate0() {
228     StampedLock lock = new StampedLock();
229     assertFalse(lock.validate(0L));
230     }
231    
232     /**
233     * A stamp obtained from a successful lock operation validates
234     */
235     public void testValidate() {
236     try {
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     } catch (InterruptedException ie) {
258     threadUnexpectedException(ie);
259     }
260     }
261    
262     /**
263     * A stamp obtained from an unsuccessful lock operation does not validate
264     */
265     public void testValidate2() {
266     try {
267     StampedLock lock = new StampedLock();
268     long s;
269     assertTrue((s = lock.writeLock()) != 0L);
270     assertTrue(lock.validate(s));
271     assertFalse(lock.validate(lock.tryWriteLock()));
272 jsr166 1.8 assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
273 dl 1.4 assertFalse(lock.validate(lock.tryReadLock()));
274 jsr166 1.8 assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
275 dl 1.4 assertFalse(lock.validate(lock.tryOptimisticRead()));
276     lock.unlockWrite(s);
277     } catch (InterruptedException ie) {
278     threadUnexpectedException(ie);
279     }
280     }
281    
282     /**
283     * writeLockInterruptibly is interruptible
284     */
285     public void testWriteLockInterruptibly_Interruptible() {
286     final CountDownLatch running = new CountDownLatch(1);
287     final StampedLock lock = new StampedLock();
288     long s = lock.writeLock();
289     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
290     public void realRun() throws InterruptedException {
291     running.countDown();
292     lock.writeLockInterruptibly();
293     }});
294 jsr166 1.7 try {
295     running.await();
296 dl 1.6 waitForThreadToEnterWaitState(t, 100);
297 dl 1.4 t.interrupt();
298     awaitTermination(t);
299     releaseWriteLock(lock, s);
300     } catch (InterruptedException ie) {
301     threadUnexpectedException(ie);
302     }
303     }
304    
305     /**
306     * timed tryWriteLock is interruptible
307     */
308     public void testWriteTryLock_Interruptible() {
309     final CountDownLatch running = new CountDownLatch(1);
310     final StampedLock lock = new StampedLock();
311     long s = lock.writeLock();
312     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
313     public void realRun() throws InterruptedException {
314     running.countDown();
315     lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
316     }});
317 jsr166 1.7 try {
318 dl 1.6 running.await();
319     waitForThreadToEnterWaitState(t, 100);
320 dl 1.4 t.interrupt();
321     awaitTermination(t);
322     releaseWriteLock(lock, s);
323     } catch (InterruptedException ie) {
324     threadUnexpectedException(ie);
325     }
326     }
327    
328     /**
329     * readLockInterruptibly is interruptible
330     */
331     public void testReadLockInterruptibly_Interruptible() {
332     final CountDownLatch running = new CountDownLatch(1);
333     final StampedLock lock = new StampedLock();
334     long s = lock.writeLock();
335     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
336     public void realRun() throws InterruptedException {
337     running.countDown();
338     lock.readLockInterruptibly();
339     }});
340 jsr166 1.7 try {
341     running.await();
342 dl 1.6 waitForThreadToEnterWaitState(t, 100);
343 dl 1.4 t.interrupt();
344     awaitTermination(t);
345     releaseWriteLock(lock, s);
346     } catch (InterruptedException ie) {
347     threadUnexpectedException(ie);
348     }
349     }
350    
351     /**
352     * timed tryReadLock is interruptible
353     */
354     public void testReadTryLock_Interruptible() {
355     final CountDownLatch running = new CountDownLatch(1);
356     final StampedLock lock = new StampedLock();
357     long s = lock.writeLock();
358     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
359     public void realRun() throws InterruptedException {
360     running.countDown();
361     lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
362     }});
363 jsr166 1.7 try {
364     running.await();
365 dl 1.6 waitForThreadToEnterWaitState(t, 100);
366 dl 1.4 t.interrupt();
367     awaitTermination(t);
368     releaseWriteLock(lock, s);
369     } catch (InterruptedException ie) {
370     threadUnexpectedException(ie);
371     }
372     }
373    
374     /**
375     * tryWriteLock on an unlocked lock succeeds
376     */
377     public void testWriteTryLock() {
378     final StampedLock lock = new StampedLock();
379     long s = lock.tryWriteLock();
380     assertTrue(s != 0L);
381     assertTrue(lock.isWriteLocked());
382     long s2 = lock.tryWriteLock();
383     assertEquals(s2, 0L);
384     releaseWriteLock(lock, s);
385     }
386    
387     /**
388     * tryWriteLock fails if locked
389     */
390     public void testWriteTryLockWhenLocked() {
391     final StampedLock lock = new StampedLock();
392     long s = lock.writeLock();
393     Thread t = newStartedThread(new CheckedRunnable() {
394     public void realRun() {
395     long ws = lock.tryWriteLock();
396     assertTrue(ws == 0L);
397     }});
398    
399     awaitTermination(t);
400     releaseWriteLock(lock, s);
401     }
402    
403     /**
404     * tryReadLock fails if write-locked
405     */
406     public void testReadTryLockWhenLocked() {
407     final StampedLock lock = new StampedLock();
408     long s = lock.writeLock();
409     Thread t = newStartedThread(new CheckedRunnable() {
410     public void realRun() {
411     long rs = lock.tryReadLock();
412     assertEquals(rs, 0L);
413     }});
414    
415     awaitTermination(t);
416     releaseWriteLock(lock, s);
417     }
418    
419     /**
420     * Multiple threads can hold a read lock when not write-locked
421     */
422     public void testMultipleReadLocks() {
423     final StampedLock lock = new StampedLock();
424     final long s = lock.readLock();
425     Thread t = newStartedThread(new CheckedRunnable() {
426     public void realRun() throws InterruptedException {
427     long s2 = lock.tryReadLock();
428     assertTrue(s2 != 0L);
429     lock.unlockRead(s2);
430     long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
431     assertTrue(s3 != 0L);
432     lock.unlockRead(s3);
433     long s4 = lock.readLock();
434     lock.unlockRead(s4);
435     }});
436    
437     awaitTermination(t);
438     lock.unlockRead(s);
439     }
440    
441     /**
442     * A writelock succeeds only after a reading thread unlocks
443     */
444     public void testWriteAfterReadLock() {
445     final CountDownLatch running = new CountDownLatch(1);
446     final StampedLock lock = new StampedLock();
447     long rs = lock.readLock();
448     Thread t = newStartedThread(new CheckedRunnable() {
449     public void realRun() {
450     running.countDown();
451     long s = lock.writeLock();
452     lock.unlockWrite(s);
453     }});
454 jsr166 1.7 try {
455     running.await();
456 dl 1.6 waitForThreadToEnterWaitState(t, 100);
457 dl 1.4 assertFalse(lock.isWriteLocked());
458     lock.unlockRead(rs);
459     awaitTermination(t);
460     assertFalse(lock.isWriteLocked());
461     } catch (InterruptedException ie) {
462     threadUnexpectedException(ie);
463     }
464     }
465    
466     /**
467     * A writelock succeeds only after reading threads unlock
468     */
469     public void testWriteAfterMultipleReadLocks() {
470     final StampedLock lock = new StampedLock();
471     long s = lock.readLock();
472     Thread t1 = newStartedThread(new CheckedRunnable() {
473     public void realRun() {
474     long rs = lock.readLock();
475     lock.unlockRead(rs);
476     }});
477     awaitTermination(t1);
478    
479     Thread t2 = newStartedThread(new CheckedRunnable() {
480     public void realRun() {
481     long ws = lock.writeLock();
482     lock.unlockWrite(ws);
483     }});
484     assertFalse(lock.isWriteLocked());
485     lock.unlockRead(s);
486     awaitTermination(t2);
487     assertFalse(lock.isWriteLocked());
488     }
489    
490     /**
491     * Readlocks succeed only after a writing thread unlocks
492     */
493     public void testReadAfterWriteLock() {
494     final StampedLock lock = new StampedLock();
495     final long s = lock.writeLock();
496     Thread t1 = newStartedThread(new CheckedRunnable() {
497     public void realRun() {
498     long rs = lock.readLock();
499     lock.unlockRead(rs);
500     }});
501     Thread t2 = newStartedThread(new CheckedRunnable() {
502     public void realRun() {
503     long rs = lock.readLock();
504     lock.unlockRead(rs);
505     }});
506    
507     releaseWriteLock(lock, s);
508     awaitTermination(t1);
509     awaitTermination(t2);
510     }
511    
512     /**
513     * tryReadLock succeeds if readlocked but not writelocked
514     */
515     public void testTryLockWhenReadLocked() {
516     final StampedLock lock = new StampedLock();
517     long s = lock.readLock();
518     Thread t = newStartedThread(new CheckedRunnable() {
519     public void realRun() {
520     long rs = lock.tryReadLock();
521     threadAssertTrue(rs != 0L);
522     lock.unlockRead(rs);
523     }});
524    
525     awaitTermination(t);
526     lock.unlockRead(s);
527     }
528    
529     /**
530     * tryWriteLock fails when readlocked
531     */
532     public void testWriteTryLockWhenReadLocked() {
533     final StampedLock lock = new StampedLock();
534     long s = lock.readLock();
535     Thread t = newStartedThread(new CheckedRunnable() {
536     public void realRun() {
537     long ws = lock.tryWriteLock();
538     threadAssertEquals(ws, 0L);
539     }});
540    
541     awaitTermination(t);
542     lock.unlockRead(s);
543     }
544    
545     /**
546     * timed tryWriteLock times out if locked
547     */
548     public void testWriteTryLock_Timeout() {
549     final StampedLock lock = new StampedLock();
550     long s = lock.writeLock();
551     Thread t = newStartedThread(new CheckedRunnable() {
552     public void realRun() throws InterruptedException {
553     long startTime = System.nanoTime();
554     long timeoutMillis = 10;
555     long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
556     assertEquals(ws, 0L);
557     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
558     }});
559    
560     awaitTermination(t);
561     releaseWriteLock(lock, s);
562     }
563    
564     /**
565     * timed tryReadLock times out if write-locked
566     */
567     public void testReadTryLock_Timeout() {
568     final StampedLock lock = new StampedLock();
569     long s = lock.writeLock();
570     Thread t = newStartedThread(new CheckedRunnable() {
571     public void realRun() throws InterruptedException {
572     long startTime = System.nanoTime();
573     long timeoutMillis = 10;
574     long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
575     assertEquals(rs, 0L);
576     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
577     }});
578 jsr166 1.7
579 dl 1.4 awaitTermination(t);
580     assertTrue(lock.isWriteLocked());
581     lock.unlockWrite(s);
582     }
583    
584     /**
585     * writeLockInterruptibly succeeds if unlocked, else is interruptible
586     */
587     public void testWriteLockInterruptibly() {
588     final CountDownLatch running = new CountDownLatch(1);
589     final StampedLock lock = new StampedLock();
590     long s = 0L;
591     try {
592     s = lock.writeLockInterruptibly();
593     } catch (InterruptedException ie) {
594     threadUnexpectedException(ie);
595     }
596     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
597     public void realRun() throws InterruptedException {
598     running.countDown();
599     lock.writeLockInterruptibly();
600     }});
601    
602 jsr166 1.7 try {
603     running.await();
604 dl 1.6 waitForThreadToEnterWaitState(t, 100);
605 dl 1.4 t.interrupt();
606     assertTrue(lock.isWriteLocked());
607     awaitTermination(t);
608     releaseWriteLock(lock, s);
609     } catch (InterruptedException ie) {
610     threadUnexpectedException(ie);
611     }
612    
613     }
614    
615     /**
616     * readLockInterruptibly succeeds if lock free else is interruptible
617     */
618     public void testReadLockInterruptibly() {
619     final CountDownLatch running = new CountDownLatch(1);
620     final StampedLock lock = new StampedLock();
621     long s = 0L;
622     try {
623     s = lock.readLockInterruptibly();
624     lock.unlockRead(s);
625     s = lock.writeLockInterruptibly();
626     } catch (InterruptedException ie) {
627     threadUnexpectedException(ie);
628     }
629     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
630     public void realRun() throws InterruptedException {
631     running.countDown();
632     lock.readLockInterruptibly();
633     }});
634 jsr166 1.7 try {
635 dl 1.6 running.await();
636     waitForThreadToEnterWaitState(t, 100);
637 dl 1.4 t.interrupt();
638     awaitTermination(t);
639     releaseWriteLock(lock, s);
640     } catch (InterruptedException ie) {
641     threadUnexpectedException(ie);
642     }
643     }
644    
645     /**
646     * A serialized lock deserializes as unlocked
647     */
648     public void testSerialization() {
649     StampedLock lock = new StampedLock();
650     lock.writeLock();
651     StampedLock clone = serialClone(lock);
652     assertTrue(lock.isWriteLocked());
653     assertFalse(clone.isWriteLocked());
654     long s = clone.writeLock();
655     assertTrue(clone.isWriteLocked());
656     clone.unlockWrite(s);
657     assertFalse(clone.isWriteLocked());
658     }
659 dl 1.5
660 dl 1.4 /**
661     * toString indicates current lock state
662     */
663     public void testToString() {
664     StampedLock lock = new StampedLock();
665     assertTrue(lock.toString().contains("Unlocked"));
666     long s = lock.writeLock();
667     assertTrue(lock.toString().contains("Write-locked"));
668     lock.unlockWrite(s);
669     s = lock.readLock();
670     assertTrue(lock.toString().contains("Read-locks"));
671     }
672    
673     /**
674     * tryOptimisticRead succeeds and validates if unlocked, fails if locked
675     */
676     public void testValidateOptimistic() {
677     try {
678     StampedLock lock = new StampedLock();
679     long s, p;
680     assertTrue((p = lock.tryOptimisticRead()) != 0L);
681     assertTrue(lock.validate(p));
682     assertTrue((s = lock.writeLock()) != 0L);
683     assertFalse((p = lock.tryOptimisticRead()) != 0L);
684     assertTrue(lock.validate(s));
685     lock.unlockWrite(s);
686     assertTrue((p = lock.tryOptimisticRead()) != 0L);
687     assertTrue(lock.validate(p));
688     assertTrue((s = lock.readLock()) != 0L);
689     assertTrue(lock.validate(s));
690     assertTrue((p = lock.tryOptimisticRead()) != 0L);
691     assertTrue(lock.validate(p));
692     lock.unlockRead(s);
693     assertTrue((s = lock.tryWriteLock()) != 0L);
694     assertTrue(lock.validate(s));
695     assertFalse((p = lock.tryOptimisticRead()) != 0L);
696     lock.unlockWrite(s);
697     assertTrue((s = lock.tryReadLock()) != 0L);
698     assertTrue(lock.validate(s));
699     assertTrue((p = lock.tryOptimisticRead()) != 0L);
700     lock.unlockRead(s);
701     assertTrue(lock.validate(p));
702     assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
703     assertFalse((p = lock.tryOptimisticRead()) != 0L);
704     assertTrue(lock.validate(s));
705     lock.unlockWrite(s);
706     assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
707     assertTrue(lock.validate(s));
708     assertTrue((p = lock.tryOptimisticRead()) != 0L);
709     lock.unlockRead(s);
710     assertTrue((p = lock.tryOptimisticRead()) != 0L);
711     } catch (InterruptedException ie) {
712     threadUnexpectedException(ie);
713     }
714     }
715    
716     /**
717     * tryOptimisticRead stamp does not validate if a write lock intervenes
718     */
719     public void testValidateOptimisticWriteLocked() {
720     StampedLock lock = new StampedLock();
721     long s, p;
722     assertTrue((p = lock.tryOptimisticRead()) != 0L);
723     assertTrue((s = lock.writeLock()) != 0L);
724     assertFalse(lock.validate(p));
725     assertFalse((p = lock.tryOptimisticRead()) != 0L);
726     assertTrue(lock.validate(s));
727     lock.unlockWrite(s);
728     }
729    
730     /**
731     * tryOptimisticRead stamp does not validate if a write lock
732     * intervenes in another thread
733     */
734     public void testValidateOptimisticWriteLocked2() {
735     final CountDownLatch running = new CountDownLatch(1);
736     final StampedLock lock = new StampedLock();
737     long s, p;
738     assertTrue((p = lock.tryOptimisticRead()) != 0L);
739     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
740     public void realRun() throws InterruptedException {
741     lock.writeLockInterruptibly();
742     running.countDown();
743     lock.writeLockInterruptibly();
744     }});
745 jsr166 1.7 try {
746 dl 1.4 running.await();
747     assertFalse(lock.validate(p));
748     assertFalse((p = lock.tryOptimisticRead()) != 0L);
749     t.interrupt();
750     awaitTermination(t);
751     } catch (InterruptedException ie) {
752     threadUnexpectedException(ie);
753     }
754     }
755    
756     /**
757     * tryConvertToOptimisticRead succeeds and validates if successfully locked,
758     */
759     public void testTryConvertToOptimisticRead() {
760     try {
761     StampedLock lock = new StampedLock();
762     long s, p;
763     s = 0L;
764     assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
765     assertTrue((s = lock.tryOptimisticRead()) != 0L);
766     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
767     assertTrue((s = lock.writeLock()) != 0L);
768     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
769     assertTrue(lock.validate(p));
770     assertTrue((s = lock.readLock()) != 0L);
771     assertTrue(lock.validate(s));
772     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
773     assertTrue(lock.validate(p));
774     assertTrue((s = lock.tryWriteLock()) != 0L);
775     assertTrue(lock.validate(s));
776     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
777     assertTrue(lock.validate(p));
778     assertTrue((s = lock.tryReadLock()) != 0L);
779     assertTrue(lock.validate(s));
780     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
781     assertTrue(lock.validate(p));
782     assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
783     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
784     assertTrue(lock.validate(p));
785     assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
786     assertTrue(lock.validate(s));
787     assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
788     assertTrue(lock.validate(p));
789     } catch (InterruptedException ie) {
790     threadUnexpectedException(ie);
791     }
792     }
793    
794     /**
795     * tryConvertToReadLock succeeds and validates if successfully locked
796     * or lock free;
797     */
798     public void testTryConvertToReadLock() {
799     try {
800     StampedLock lock = new StampedLock();
801     long s, p;
802     s = 0L;
803     assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
804     assertTrue((s = lock.tryOptimisticRead()) != 0L);
805     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
806     lock.unlockRead(p);
807     assertTrue((s = lock.writeLock()) != 0L);
808     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
809     assertTrue(lock.validate(p));
810     lock.unlockRead(p);
811     assertTrue((s = lock.readLock()) != 0L);
812     assertTrue(lock.validate(s));
813     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
814     assertTrue(lock.validate(p));
815     lock.unlockRead(p);
816     assertTrue((s = lock.tryWriteLock()) != 0L);
817     assertTrue(lock.validate(s));
818     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
819     assertTrue(lock.validate(p));
820     lock.unlockRead(p);
821     assertTrue((s = lock.tryReadLock()) != 0L);
822     assertTrue(lock.validate(s));
823     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
824     assertTrue(lock.validate(p));
825     lock.unlockRead(p);
826     assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
827     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
828     assertTrue(lock.validate(p));
829     lock.unlockRead(p);
830     assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
831     assertTrue(lock.validate(s));
832     assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
833     assertTrue(lock.validate(p));
834     lock.unlockRead(p);
835     } catch (InterruptedException ie) {
836     threadUnexpectedException(ie);
837     }
838     }
839    
840     /**
841     * tryConvertToWriteLock succeeds and validates if successfully locked
842     * or lock free;
843     */
844     public void testTryConvertToWriteLock() {
845     try {
846     StampedLock lock = new StampedLock();
847     long s, p;
848     s = 0L;
849     assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
850     assertTrue((s = lock.tryOptimisticRead()) != 0L);
851     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
852     lock.unlockWrite(p);
853     assertTrue((s = lock.writeLock()) != 0L);
854     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
855     assertTrue(lock.validate(p));
856     lock.unlockWrite(p);
857     assertTrue((s = lock.readLock()) != 0L);
858     assertTrue(lock.validate(s));
859     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
860     assertTrue(lock.validate(p));
861     lock.unlockWrite(p);
862     assertTrue((s = lock.tryWriteLock()) != 0L);
863     assertTrue(lock.validate(s));
864     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
865     assertTrue(lock.validate(p));
866     lock.unlockWrite(p);
867     assertTrue((s = lock.tryReadLock()) != 0L);
868     assertTrue(lock.validate(s));
869     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
870     assertTrue(lock.validate(p));
871     lock.unlockWrite(p);
872     assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
873     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
874     assertTrue(lock.validate(p));
875     lock.unlockWrite(p);
876     assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
877     assertTrue(lock.validate(s));
878     assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
879     assertTrue(lock.validate(p));
880     lock.unlockWrite(p);
881     } catch (InterruptedException ie) {
882     threadUnexpectedException(ie);
883     }
884     }
885    
886     /**
887     * asWriteLock can be locked and unlocked
888     */
889     public void testAsWriteLock() {
890     StampedLock sl = new StampedLock();
891     Lock lock = sl.asWriteLock();
892     lock.lock();
893     assertFalse(lock.tryLock());
894     lock.unlock();
895     assertTrue(lock.tryLock());
896     }
897    
898     /**
899     * asReadLock can be locked and unlocked
900     */
901     public void testAsReadLock() {
902     StampedLock sl = new StampedLock();
903     Lock lock = sl.asReadLock();
904     lock.lock();
905     lock.unlock();
906     assertTrue(lock.tryLock());
907     }
908    
909     /**
910     * asReadWriteLock.writeLock can be locked and unlocked
911     */
912     public void testAsReadWriteLockWriteLock() {
913     StampedLock sl = new StampedLock();
914     Lock lock = sl.asReadWriteLock().writeLock();
915     lock.lock();
916     assertFalse(lock.tryLock());
917     lock.unlock();
918     assertTrue(lock.tryLock());
919     }
920    
921     /**
922     * asReadWriteLock.readLock can be locked and unlocked
923     */
924     public void testAsReadWriteLockReadLock() {
925     StampedLock sl = new StampedLock();
926     Lock lock = sl.asReadWriteLock().readLock();
927     lock.lock();
928     lock.unlock();
929     assertTrue(lock.tryLock());
930     }
931 jsr166 1.1
932     }