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 |
|
|
272 |
|
* interruptible operations throw InterruptedException when pre-interrupted |
273 |
|
*/ |
274 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() { |
275 |
– |
final CountDownLatch running = new CountDownLatch(1); |
275 |
|
final StampedLock lock = new StampedLock(); |
276 |
|
|
277 |
|
Action[] interruptibleLockActions = { |
336 |
|
* interruptible operations throw InterruptedException when write locked and interrupted |
337 |
|
*/ |
338 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() { |
340 |
– |
final CountDownLatch running = new CountDownLatch(1); |
339 |
|
final StampedLock lock = new StampedLock(); |
340 |
|
long s = lock.writeLock(); |
341 |
|
|
358 |
|
* interruptible operations throw InterruptedException when read locked and interrupted |
359 |
|
*/ |
360 |
|
public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() { |
363 |
– |
final CountDownLatch running = new CountDownLatch(1); |
361 |
|
final StampedLock lock = new StampedLock(); |
362 |
|
long s = lock.readLock(); |
363 |
|
|
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* A writelock succeeds only after a reading thread unlocks |
479 |
> |
* writeLock() succeeds only after a reading thread unlocks |
480 |
|
*/ |
481 |
|
public void testWriteAfterReadLock() throws InterruptedException { |
482 |
< |
final CountDownLatch running = new CountDownLatch(1); |
482 |
> |
final CountDownLatch aboutToLock = new CountDownLatch(1); |
483 |
|
final StampedLock lock = new StampedLock(); |
484 |
|
long rs = lock.readLock(); |
485 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
486 |
|
public void realRun() { |
487 |
< |
running.countDown(); |
487 |
> |
aboutToLock.countDown(); |
488 |
|
long s = lock.writeLock(); |
489 |
+ |
assertTrue(lock.isWriteLocked()); |
490 |
+ |
assertFalse(lock.isReadLocked()); |
491 |
|
lock.unlockWrite(s); |
492 |
|
}}); |
493 |
|
|
494 |
< |
running.await(); |
495 |
< |
waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS); |
494 |
> |
await(aboutToLock); |
495 |
> |
waitForThreadToEnterWaitState(t); |
496 |
|
assertFalse(lock.isWriteLocked()); |
497 |
+ |
assertTrue(lock.isReadLocked()); |
498 |
|
lock.unlockRead(rs); |
499 |
|
awaitTermination(t); |
500 |
< |
assertFalse(lock.isWriteLocked()); |
500 |
> |
assertUnlocked(lock); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* A writelock succeeds only after reading threads unlock |
504 |
> |
* writeLock() succeeds only after reading threads unlock |
505 |
|
*/ |
506 |
|
public void testWriteAfterMultipleReadLocks() { |
507 |
|
final StampedLock lock = new StampedLock(); |
524 |
|
assertFalse(lock.isWriteLocked()); |
525 |
|
lock.unlockRead(s); |
526 |
|
awaitTermination(t2); |
527 |
< |
assertFalse(lock.isWriteLocked()); |
527 |
> |
assertUnlocked(lock); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* Readlocks succeed only after a writing thread unlocks |
531 |
> |
* readLock() succeed only after a writing thread unlocks |
532 |
|
*/ |
533 |
|
public void testReadAfterWriteLock() { |
534 |
|
final StampedLock lock = new StampedLock(); |
535 |
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
536 |
|
final long s = lock.writeLock(); |
537 |
< |
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() { |
537 |
> |
final Runnable acquireReleaseReadLock = new CheckedRunnable() { |
538 |
|
public void realRun() { |
539 |
|
threadsStarted.countDown(); |
540 |
|
long rs = lock.readLock(); |
541 |
+ |
assertTrue(lock.isReadLocked()); |
542 |
+ |
assertFalse(lock.isWriteLocked()); |
543 |
|
lock.unlockRead(rs); |
544 |
< |
}}); |
544 |
> |
}}; |
545 |
> |
Thread t1 = newStartedThread(acquireReleaseReadLock); |
546 |
> |
Thread t2 = newStartedThread(acquireReleaseReadLock); |
547 |
|
|
548 |
|
await(threadsStarted); |
549 |
< |
waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS); |
550 |
< |
waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS); |
549 |
> |
waitForThreadToEnterWaitState(t1); |
550 |
> |
waitForThreadToEnterWaitState(t2); |
551 |
> |
assertTrue(lock.isWriteLocked()); |
552 |
> |
assertFalse(lock.isReadLocked()); |
553 |
|
releaseWriteLock(lock, s); |
554 |
|
awaitTermination(t1); |
555 |
|
awaitTermination(t2); |
556 |
+ |
assertUnlocked(lock); |
557 |
|
} |
558 |
|
|
559 |
|
/** |
739 |
|
*/ |
740 |
|
public void testValidateOptimisticWriteLocked2() |
741 |
|
throws InterruptedException { |
742 |
< |
final CountDownLatch running = new CountDownLatch(1); |
742 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
743 |
|
final StampedLock lock = new StampedLock(); |
744 |
|
final long p = assertValid(lock, lock.tryOptimisticRead()); |
745 |
|
|
746 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
747 |
|
public void realRun() throws InterruptedException { |
748 |
|
lock.writeLockInterruptibly(); |
749 |
< |
running.countDown(); |
749 |
> |
locked.countDown(); |
750 |
|
lock.writeLockInterruptibly(); |
751 |
|
}}); |
752 |
|
|
753 |
< |
running.await(); |
753 |
> |
await(locked); |
754 |
|
assertFalse(lock.validate(p)); |
755 |
|
assertEquals(0L, lock.tryOptimisticRead()); |
756 |
+ |
waitForThreadToEnterWaitState(t); |
757 |
|
t.interrupt(); |
758 |
|
awaitTermination(t); |
759 |
+ |
assertTrue(lock.isWriteLocked()); |
760 |
|
} |
761 |
|
|
762 |
|
/** |