ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/StampedLockTest.java
Revision: 1.5
Committed: Thu Mar 21 16:26:43 2013 UTC (11 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
CompletableFuture coverage

File Contents

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