ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/StampedLockTest.java
Revision: 1.4
Committed: Wed Mar 20 16:51:11 2013 UTC (11 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.3: +900 -1658 lines
Log Message:
Basic 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 * toString indicates current lock state
655 */
656 public void testToString() {
657 StampedLock lock = new StampedLock();
658 assertTrue(lock.toString().contains("Unlocked"));
659 long s = lock.writeLock();
660 assertTrue(lock.toString().contains("Write-locked"));
661 lock.unlockWrite(s);
662 s = lock.readLock();
663 assertTrue(lock.toString().contains("Read-locks"));
664 }
665
666 /**
667 * tryOptimisticRead succeeds and validates if unlocked, fails if locked
668 */
669 public void testValidateOptimistic() {
670 try {
671 StampedLock lock = new StampedLock();
672 long s, p;
673 assertTrue((p = lock.tryOptimisticRead()) != 0L);
674 assertTrue(lock.validate(p));
675 assertTrue((s = lock.writeLock()) != 0L);
676 assertFalse((p = lock.tryOptimisticRead()) != 0L);
677 assertTrue(lock.validate(s));
678 lock.unlockWrite(s);
679 assertTrue((p = lock.tryOptimisticRead()) != 0L);
680 assertTrue(lock.validate(p));
681 assertTrue((s = lock.readLock()) != 0L);
682 assertTrue(lock.validate(s));
683 assertTrue((p = lock.tryOptimisticRead()) != 0L);
684 assertTrue(lock.validate(p));
685 lock.unlockRead(s);
686 assertTrue((s = lock.tryWriteLock()) != 0L);
687 assertTrue(lock.validate(s));
688 assertFalse((p = lock.tryOptimisticRead()) != 0L);
689 lock.unlockWrite(s);
690 assertTrue((s = lock.tryReadLock()) != 0L);
691 assertTrue(lock.validate(s));
692 assertTrue((p = lock.tryOptimisticRead()) != 0L);
693 lock.unlockRead(s);
694 assertTrue(lock.validate(p));
695 assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
696 assertFalse((p = lock.tryOptimisticRead()) != 0L);
697 assertTrue(lock.validate(s));
698 lock.unlockWrite(s);
699 assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
700 assertTrue(lock.validate(s));
701 assertTrue((p = lock.tryOptimisticRead()) != 0L);
702 lock.unlockRead(s);
703 assertTrue((p = lock.tryOptimisticRead()) != 0L);
704 } catch (InterruptedException ie) {
705 threadUnexpectedException(ie);
706 }
707 }
708
709 /**
710 * tryOptimisticRead stamp does not validate if a write lock intervenes
711 */
712 public void testValidateOptimisticWriteLocked() {
713 StampedLock lock = new StampedLock();
714 long s, p;
715 assertTrue((p = lock.tryOptimisticRead()) != 0L);
716 assertTrue((s = lock.writeLock()) != 0L);
717 assertFalse(lock.validate(p));
718 assertFalse((p = lock.tryOptimisticRead()) != 0L);
719 assertTrue(lock.validate(s));
720 lock.unlockWrite(s);
721 }
722
723 /**
724 * tryOptimisticRead stamp does not validate if a write lock
725 * intervenes in another thread
726 */
727 public void testValidateOptimisticWriteLocked2() {
728 final CountDownLatch running = new CountDownLatch(1);
729 final StampedLock lock = new StampedLock();
730 long s, p;
731 assertTrue((p = lock.tryOptimisticRead()) != 0L);
732 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
733 public void realRun() throws InterruptedException {
734 lock.writeLockInterruptibly();
735 running.countDown();
736 lock.writeLockInterruptibly();
737 }});
738 try {
739 running.await();
740 assertFalse(lock.validate(p));
741 assertFalse((p = lock.tryOptimisticRead()) != 0L);
742 t.interrupt();
743 awaitTermination(t);
744 } catch (InterruptedException ie) {
745 threadUnexpectedException(ie);
746 }
747 }
748
749 /**
750 * tryConvertToOptimisticRead succeeds and validates if successfully locked,
751 */
752 public void testTryConvertToOptimisticRead() {
753 try {
754 StampedLock lock = new StampedLock();
755 long s, p;
756 s = 0L;
757 assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
758 assertTrue((s = lock.tryOptimisticRead()) != 0L);
759 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
760 assertTrue((s = lock.writeLock()) != 0L);
761 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
762 assertTrue(lock.validate(p));
763 assertTrue((s = lock.readLock()) != 0L);
764 assertTrue(lock.validate(s));
765 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
766 assertTrue(lock.validate(p));
767 assertTrue((s = lock.tryWriteLock()) != 0L);
768 assertTrue(lock.validate(s));
769 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
770 assertTrue(lock.validate(p));
771 assertTrue((s = lock.tryReadLock()) != 0L);
772 assertTrue(lock.validate(s));
773 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
774 assertTrue(lock.validate(p));
775 assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
776 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
777 assertTrue(lock.validate(p));
778 assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
779 assertTrue(lock.validate(s));
780 assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
781 assertTrue(lock.validate(p));
782 } catch (InterruptedException ie) {
783 threadUnexpectedException(ie);
784 }
785 }
786
787 /**
788 * tryConvertToReadLock succeeds and validates if successfully locked
789 * or lock free;
790 */
791 public void testTryConvertToReadLock() {
792 try {
793 StampedLock lock = new StampedLock();
794 long s, p;
795 s = 0L;
796 assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
797 assertTrue((s = lock.tryOptimisticRead()) != 0L);
798 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
799 lock.unlockRead(p);
800 assertTrue((s = lock.writeLock()) != 0L);
801 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
802 assertTrue(lock.validate(p));
803 lock.unlockRead(p);
804 assertTrue((s = lock.readLock()) != 0L);
805 assertTrue(lock.validate(s));
806 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
807 assertTrue(lock.validate(p));
808 lock.unlockRead(p);
809 assertTrue((s = lock.tryWriteLock()) != 0L);
810 assertTrue(lock.validate(s));
811 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
812 assertTrue(lock.validate(p));
813 lock.unlockRead(p);
814 assertTrue((s = lock.tryReadLock()) != 0L);
815 assertTrue(lock.validate(s));
816 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
817 assertTrue(lock.validate(p));
818 lock.unlockRead(p);
819 assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
820 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
821 assertTrue(lock.validate(p));
822 lock.unlockRead(p);
823 assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
824 assertTrue(lock.validate(s));
825 assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
826 assertTrue(lock.validate(p));
827 lock.unlockRead(p);
828 } catch (InterruptedException ie) {
829 threadUnexpectedException(ie);
830 }
831 }
832
833 /**
834 * tryConvertToWriteLock succeeds and validates if successfully locked
835 * or lock free;
836 */
837 public void testTryConvertToWriteLock() {
838 try {
839 StampedLock lock = new StampedLock();
840 long s, p;
841 s = 0L;
842 assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
843 assertTrue((s = lock.tryOptimisticRead()) != 0L);
844 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
845 lock.unlockWrite(p);
846 assertTrue((s = lock.writeLock()) != 0L);
847 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
848 assertTrue(lock.validate(p));
849 lock.unlockWrite(p);
850 assertTrue((s = lock.readLock()) != 0L);
851 assertTrue(lock.validate(s));
852 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
853 assertTrue(lock.validate(p));
854 lock.unlockWrite(p);
855 assertTrue((s = lock.tryWriteLock()) != 0L);
856 assertTrue(lock.validate(s));
857 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
858 assertTrue(lock.validate(p));
859 lock.unlockWrite(p);
860 assertTrue((s = lock.tryReadLock()) != 0L);
861 assertTrue(lock.validate(s));
862 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
863 assertTrue(lock.validate(p));
864 lock.unlockWrite(p);
865 assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
866 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
867 assertTrue(lock.validate(p));
868 lock.unlockWrite(p);
869 assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
870 assertTrue(lock.validate(s));
871 assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
872 assertTrue(lock.validate(p));
873 lock.unlockWrite(p);
874 } catch (InterruptedException ie) {
875 threadUnexpectedException(ie);
876 }
877 }
878
879 /**
880 * asWriteLock can be locked and unlocked
881 */
882 public void testAsWriteLock() {
883 StampedLock sl = new StampedLock();
884 Lock lock = sl.asWriteLock();
885 lock.lock();
886 assertFalse(lock.tryLock());
887 lock.unlock();
888 assertTrue(lock.tryLock());
889 }
890
891 /**
892 * asReadLock can be locked and unlocked
893 */
894 public void testAsReadLock() {
895 StampedLock sl = new StampedLock();
896 Lock lock = sl.asReadLock();
897 lock.lock();
898 lock.unlock();
899 assertTrue(lock.tryLock());
900 }
901
902 /**
903 * asReadWriteLock.writeLock can be locked and unlocked
904 */
905 public void testAsReadWriteLockWriteLock() {
906 StampedLock sl = new StampedLock();
907 Lock lock = sl.asReadWriteLock().writeLock();
908 lock.lock();
909 assertFalse(lock.tryLock());
910 lock.unlock();
911 assertTrue(lock.tryLock());
912 }
913
914 /**
915 * asReadWriteLock.readLock can be locked and unlocked
916 */
917 public void testAsReadWriteLockReadLock() {
918 StampedLock sl = new StampedLock();
919 Lock lock = sl.asReadWriteLock().readLock();
920 lock.lock();
921 lock.unlock();
922 assertTrue(lock.tryLock());
923 }
924
925 }