83 |
|
|
84 |
|
List<Function<StampedLock, Long>> readLockers() { |
85 |
|
List<Function<StampedLock, Long>> readLockers = new ArrayList<>(); |
86 |
< |
readLockers.add((sl) -> sl.readLock()); |
87 |
< |
readLockers.add((sl) -> sl.tryReadLock()); |
88 |
< |
readLockers.add((sl) -> readLockInterruptiblyUninterrupted(sl)); |
89 |
< |
readLockers.add((sl) -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
90 |
< |
readLockers.add((sl) -> tryReadLockUninterrupted(sl, 0L, DAYS)); |
91 |
< |
readLockers.add((sl) -> sl.tryConvertToReadLock(sl.tryOptimisticRead())); |
86 |
> |
readLockers.add(sl -> sl.readLock()); |
87 |
> |
readLockers.add(sl -> sl.tryReadLock()); |
88 |
> |
readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl)); |
89 |
> |
readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
90 |
> |
readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS)); |
91 |
> |
readLockers.add(sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead())); |
92 |
|
return readLockers; |
93 |
|
} |
94 |
|
|
104 |
|
|
105 |
|
List<Function<StampedLock, Long>> writeLockers() { |
106 |
|
List<Function<StampedLock, Long>> writeLockers = new ArrayList<>(); |
107 |
< |
writeLockers.add((sl) -> sl.writeLock()); |
108 |
< |
writeLockers.add((sl) -> sl.tryWriteLock()); |
109 |
< |
writeLockers.add((sl) -> writeLockInterruptiblyUninterrupted(sl)); |
110 |
< |
writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
111 |
< |
writeLockers.add((sl) -> tryWriteLockUninterrupted(sl, 0L, DAYS)); |
112 |
< |
writeLockers.add((sl) -> sl.tryConvertToWriteLock(sl.tryOptimisticRead())); |
107 |
> |
writeLockers.add(sl -> sl.writeLock()); |
108 |
> |
writeLockers.add(sl -> sl.tryWriteLock()); |
109 |
> |
writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl)); |
110 |
> |
writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
111 |
> |
writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS)); |
112 |
> |
writeLockers.add(sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead())); |
113 |
|
return writeLockers; |
114 |
|
} |
115 |
|
|
248 |
|
long s = assertNonZero(lock.writeLock()); |
249 |
|
assertTrue(lock.validate(s)); |
250 |
|
assertFalse(lock.validate(lock.tryWriteLock())); |
251 |
< |
assertFalse(lock.validate(lock.tryWriteLock(0L, SECONDS))); |
251 |
> |
assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(), |
252 |
> |
randomTimeUnit()))); |
253 |
|
assertFalse(lock.validate(lock.tryReadLock())); |
254 |
< |
assertFalse(lock.validate(lock.tryReadLock(0L, SECONDS))); |
254 |
> |
assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(), |
255 |
> |
randomTimeUnit()))); |
256 |
|
assertFalse(lock.validate(lock.tryOptimisticRead())); |
257 |
|
lock.unlockWrite(s); |
258 |
|
} |
274 |
|
* interruptible operations throw InterruptedException when pre-interrupted |
275 |
|
*/ |
276 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() { |
275 |
– |
final CountDownLatch running = new CountDownLatch(1); |
277 |
|
final StampedLock lock = new StampedLock(); |
278 |
|
|
279 |
|
Action[] interruptibleLockActions = { |
338 |
|
* interruptible operations throw InterruptedException when write locked and interrupted |
339 |
|
*/ |
340 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() { |
340 |
– |
final CountDownLatch running = new CountDownLatch(1); |
341 |
|
final StampedLock lock = new StampedLock(); |
342 |
|
long s = lock.writeLock(); |
343 |
|
|
360 |
|
* interruptible operations throw InterruptedException when read locked and interrupted |
361 |
|
*/ |
362 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() { |
363 |
– |
final CountDownLatch running = new CountDownLatch(1); |
363 |
|
final StampedLock lock = new StampedLock(); |
364 |
|
long s = lock.readLock(); |
365 |
|
|
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
< |
* A writelock succeeds only after a reading thread unlocks |
481 |
> |
* writeLock() succeeds only after a reading thread unlocks |
482 |
|
*/ |
483 |
|
public void testWriteAfterReadLock() throws InterruptedException { |
484 |
< |
final CountDownLatch running = new CountDownLatch(1); |
484 |
> |
final CountDownLatch aboutToLock = new CountDownLatch(1); |
485 |
|
final StampedLock lock = new StampedLock(); |
486 |
|
long rs = lock.readLock(); |
487 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
488 |
|
public void realRun() { |
489 |
< |
running.countDown(); |
489 |
> |
aboutToLock.countDown(); |
490 |
|
long s = lock.writeLock(); |
491 |
+ |
assertTrue(lock.isWriteLocked()); |
492 |
+ |
assertFalse(lock.isReadLocked()); |
493 |
|
lock.unlockWrite(s); |
494 |
|
}}); |
495 |
|
|
496 |
< |
running.await(); |
497 |
< |
waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS); |
496 |
> |
await(aboutToLock); |
497 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
498 |
|
assertFalse(lock.isWriteLocked()); |
499 |
+ |
assertTrue(lock.isReadLocked()); |
500 |
|
lock.unlockRead(rs); |
501 |
|
awaitTermination(t); |
502 |
< |
assertFalse(lock.isWriteLocked()); |
502 |
> |
assertUnlocked(lock); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
< |
* A writelock succeeds only after reading threads unlock |
506 |
> |
* writeLock() succeeds only after reading threads unlock |
507 |
|
*/ |
508 |
|
public void testWriteAfterMultipleReadLocks() { |
509 |
|
final StampedLock lock = new StampedLock(); |
526 |
|
assertFalse(lock.isWriteLocked()); |
527 |
|
lock.unlockRead(s); |
528 |
|
awaitTermination(t2); |
529 |
< |
assertFalse(lock.isWriteLocked()); |
529 |
> |
assertUnlocked(lock); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
< |
* Readlocks succeed only after a writing thread unlocks |
533 |
> |
* readLock() succeed only after a writing thread unlocks |
534 |
|
*/ |
535 |
|
public void testReadAfterWriteLock() { |
536 |
|
final StampedLock lock = new StampedLock(); |
537 |
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
538 |
|
final long s = lock.writeLock(); |
539 |
< |
Thread t1 = newStartedThread(new CheckedRunnable() { |
538 |
< |
public void realRun() { |
539 |
< |
threadsStarted.countDown(); |
540 |
< |
long rs = lock.readLock(); |
541 |
< |
lock.unlockRead(rs); |
542 |
< |
}}); |
543 |
< |
Thread t2 = newStartedThread(new CheckedRunnable() { |
539 |
> |
final Runnable acquireReleaseReadLock = new CheckedRunnable() { |
540 |
|
public void realRun() { |
541 |
|
threadsStarted.countDown(); |
542 |
|
long rs = lock.readLock(); |
543 |
+ |
assertTrue(lock.isReadLocked()); |
544 |
+ |
assertFalse(lock.isWriteLocked()); |
545 |
|
lock.unlockRead(rs); |
546 |
< |
}}); |
546 |
> |
}}; |
547 |
> |
Thread t1 = newStartedThread(acquireReleaseReadLock); |
548 |
> |
Thread t2 = newStartedThread(acquireReleaseReadLock); |
549 |
|
|
550 |
|
await(threadsStarted); |
551 |
< |
waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS); |
552 |
< |
waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS); |
551 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
552 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
553 |
> |
assertTrue(lock.isWriteLocked()); |
554 |
> |
assertFalse(lock.isReadLocked()); |
555 |
|
releaseWriteLock(lock, s); |
556 |
|
awaitTermination(t1); |
557 |
|
awaitTermination(t2); |
558 |
+ |
assertUnlocked(lock); |
559 |
|
} |
560 |
|
|
561 |
|
/** |
741 |
|
*/ |
742 |
|
public void testValidateOptimisticWriteLocked2() |
743 |
|
throws InterruptedException { |
744 |
< |
final CountDownLatch running = new CountDownLatch(1); |
744 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
745 |
|
final StampedLock lock = new StampedLock(); |
746 |
|
final long p = assertValid(lock, lock.tryOptimisticRead()); |
747 |
|
|
748 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
749 |
|
public void realRun() throws InterruptedException { |
750 |
|
lock.writeLockInterruptibly(); |
751 |
< |
running.countDown(); |
751 |
> |
locked.countDown(); |
752 |
|
lock.writeLockInterruptibly(); |
753 |
|
}}); |
754 |
|
|
755 |
< |
running.await(); |
755 |
> |
await(locked); |
756 |
|
assertFalse(lock.validate(p)); |
757 |
|
assertEquals(0L, lock.tryOptimisticRead()); |
758 |
+ |
assertThreadBlocks(t, Thread.State.WAITING); |
759 |
|
t.interrupt(); |
760 |
|
awaitTermination(t); |
761 |
+ |
assertTrue(lock.isWriteLocked()); |
762 |
|
} |
763 |
|
|
764 |
|
/** |