67 |
|
*/ |
68 |
|
void releaseWriteLock(PublicReentrantReadWriteLock lock) { |
69 |
|
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); |
70 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
70 |
> |
assertWriteLockedByMoi(lock); |
71 |
|
assertEquals(1, lock.getWriteHoldCount()); |
72 |
|
writeLock.unlock(); |
73 |
|
assertNotWriteLocked(lock); |
94 |
|
assertFalse(lock.isWriteLocked()); |
95 |
|
assertFalse(lock.isWriteLockedByCurrentThread()); |
96 |
|
assertFalse(lock.writeLock().isHeldByCurrentThread()); |
97 |
– |
assertNull(lock.getOwner()); |
97 |
|
assertEquals(0, lock.getWriteHoldCount()); |
98 |
+ |
assertEquals(0, lock.writeLock().getHoldCount()); |
99 |
+ |
assertNull(lock.getOwner()); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
111 |
|
lock.writeLock().isHeldByCurrentThread()); |
112 |
|
assertEquals(t == Thread.currentThread(), |
113 |
|
lock.getWriteHoldCount() > 0); |
114 |
+ |
assertEquals(t == Thread.currentThread(), |
115 |
+ |
lock.writeLock().getHoldCount() > 0); |
116 |
|
assertEquals(0, lock.getReadLockCount()); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
+ |
* Checks that lock is write-locked by the current thread. |
121 |
+ |
*/ |
122 |
+ |
void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) { |
123 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
/** |
127 |
|
* Checks that condition c has no waiters. |
128 |
|
*/ |
129 |
|
void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { |
148 |
|
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
149 |
|
|
150 |
|
/** |
151 |
< |
* Awaits condition using the specified AwaitMethod |
151 |
> |
* Awaits condition using the specified AwaitMethod. |
152 |
|
*/ |
153 |
|
void await(Condition c, AwaitMethod awaitMethod) |
154 |
|
throws InterruptedException { |
199 |
|
new PublicReentrantReadWriteLock(fair); |
200 |
|
assertNotWriteLocked(lock); |
201 |
|
lock.writeLock().lock(); |
202 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
202 |
> |
assertWriteLockedByMoi(lock); |
203 |
|
lock.writeLock().unlock(); |
204 |
|
assertNotWriteLocked(lock); |
205 |
|
assertEquals(0, lock.getReadLockCount()); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
+ |
* write-tryLock on an unlocked lock succeeds |
373 |
+ |
*/ |
374 |
+ |
public void testWriteTryLock() { testWriteTryLock(false); } |
375 |
+ |
public void testWriteTryLock_fair() { testWriteTryLock(true); } |
376 |
+ |
public void testWriteTryLock(boolean fair) { |
377 |
+ |
final PublicReentrantReadWriteLock lock = |
378 |
+ |
new PublicReentrantReadWriteLock(fair); |
379 |
+ |
assertTrue(lock.writeLock().tryLock()); |
380 |
+ |
assertWriteLockedByMoi(lock); |
381 |
+ |
assertTrue(lock.writeLock().tryLock()); |
382 |
+ |
assertWriteLockedByMoi(lock); |
383 |
+ |
lock.writeLock().unlock(); |
384 |
+ |
releaseWriteLock(lock); |
385 |
+ |
} |
386 |
+ |
|
387 |
+ |
/** |
388 |
|
* write-tryLock fails if locked |
389 |
|
*/ |
390 |
|
public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } |
648 |
|
|
649 |
|
waitForQueuedThread(lock, t1); |
650 |
|
waitForQueuedThread(lock, t2); |
651 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
651 |
> |
assertWriteLockedByMoi(lock); |
652 |
|
lock.readLock().lock(); |
653 |
|
lock.readLock().unlock(); |
654 |
|
releaseWriteLock(lock); |
682 |
|
|
683 |
|
waitForQueuedThread(lock, t1); |
684 |
|
waitForQueuedThread(lock, t2); |
685 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
685 |
> |
assertWriteLockedByMoi(lock); |
686 |
|
lock.readLock().lock(); |
687 |
|
lock.readLock().unlock(); |
688 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
688 |
> |
assertWriteLockedByMoi(lock); |
689 |
|
lock.writeLock().unlock(); |
690 |
|
awaitTermination(t1); |
691 |
|
awaitTermination(t2); |
717 |
|
|
718 |
|
waitForQueuedThread(lock, t1); |
719 |
|
waitForQueuedThread(lock, t2); |
720 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
720 |
> |
assertWriteLockedByMoi(lock); |
721 |
|
assertEquals(1, lock.getWriteHoldCount()); |
722 |
|
lock.writeLock().lock(); |
723 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
723 |
> |
assertWriteLockedByMoi(lock); |
724 |
|
assertEquals(2, lock.getWriteHoldCount()); |
725 |
|
lock.writeLock().unlock(); |
726 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
726 |
> |
assertWriteLockedByMoi(lock); |
727 |
|
assertEquals(1, lock.getWriteHoldCount()); |
728 |
|
lock.writeLock().unlock(); |
729 |
|
awaitTermination(t1); |
771 |
|
public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } |
772 |
|
public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } |
773 |
|
public void testWriteTryLock_Timeout(boolean fair) { |
774 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
774 |
> |
final PublicReentrantReadWriteLock lock = |
775 |
> |
new PublicReentrantReadWriteLock(fair); |
776 |
|
lock.writeLock().lock(); |
777 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
778 |
|
public void realRun() throws InterruptedException { |
783 |
|
}}); |
784 |
|
|
785 |
|
awaitTermination(t); |
786 |
< |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
760 |
< |
lock.writeLock().unlock(); |
786 |
> |
releaseWriteLock(lock); |
787 |
|
} |
788 |
|
|
789 |
|
/** |
808 |
|
} |
809 |
|
|
810 |
|
/** |
811 |
< |
* write lockInterruptibly succeeds if lock free else is interruptible |
811 |
> |
* write lockInterruptibly succeeds if unlocked, else is interruptible |
812 |
|
*/ |
813 |
|
public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } |
814 |
|
public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } |
817 |
|
new PublicReentrantReadWriteLock(fair); |
818 |
|
try { |
819 |
|
lock.writeLock().lockInterruptibly(); |
820 |
< |
} catch (Throwable t) { |
821 |
< |
threadUnexpectedException(t); |
820 |
> |
} catch (InterruptedException ie) { |
821 |
> |
threadUnexpectedException(ie); |
822 |
|
} |
823 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
824 |
|
public void realRun() throws InterruptedException { |
827 |
|
|
828 |
|
waitForQueuedThread(lock, t); |
829 |
|
t.interrupt(); |
830 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
831 |
|
awaitTermination(t); |
832 |
|
releaseWriteLock(lock); |
833 |
|
} |
844 |
|
lock.readLock().lockInterruptibly(); |
845 |
|
lock.readLock().unlock(); |
846 |
|
lock.writeLock().lockInterruptibly(); |
847 |
< |
} catch (Throwable t) { |
848 |
< |
threadUnexpectedException(t); |
847 |
> |
} catch (InterruptedException ie) { |
848 |
> |
threadUnexpectedException(ie); |
849 |
|
} |
850 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
851 |
|
public void realRun() throws InterruptedException { |
884 |
|
c.awaitUninterruptibly(); |
885 |
|
shouldThrow(); |
886 |
|
} catch (IllegalMonitorStateException success) {} |
887 |
< |
} catch (Throwable t) { |
888 |
< |
threadUnexpectedException(t); |
887 |
> |
} catch (InterruptedException ie) { |
888 |
> |
threadUnexpectedException(ie); |
889 |
|
} |
890 |
|
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
891 |
|
} |
1035 |
|
lock.writeLock().unlock(); |
1036 |
|
t.interrupt(); |
1037 |
|
long timeoutMillis = 10; |
1038 |
< |
assertThreadJoinTimesOut(t, timeoutMillis); |
1038 |
> |
assertThreadStaysAlive(t, timeoutMillis); |
1039 |
|
lock.writeLock().lock(); |
1040 |
|
c.signal(); |
1041 |
|
lock.writeLock().unlock(); |
1059 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
|
lock.writeLock().lock(); |
1062 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1062 |
> |
assertWriteLockedByMoi(lock); |
1063 |
|
assertHasNoWaiters(lock, c); |
1064 |
|
locked.countDown(); |
1065 |
|
try { |
1066 |
|
await(c, awaitMethod); |
1067 |
|
} finally { |
1068 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1068 |
> |
assertWriteLockedByMoi(lock); |
1069 |
|
assertHasNoWaiters(lock, c); |
1070 |
|
lock.writeLock().unlock(); |
1071 |
|
assertFalse(Thread.interrupted()); |
1177 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1178 |
|
public void realRun() throws InterruptedException { |
1179 |
|
lock.writeLock().lock(); |
1180 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1180 |
> |
assertWriteLockedByMoi(lock); |
1181 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1182 |
|
locked.countDown(); |
1183 |
|
c.await(); |
1184 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1184 |
> |
assertWriteLockedByMoi(lock); |
1185 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1186 |
|
lock.writeLock().unlock(); |
1187 |
|
}}); |
1190 |
|
public void realRun() throws InterruptedException { |
1191 |
|
lock.writeLock().lock(); |
1192 |
|
lock.writeLock().lock(); |
1193 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1193 |
> |
assertWriteLockedByMoi(lock); |
1194 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1195 |
|
locked.countDown(); |
1196 |
|
c.await(); |
1197 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1197 |
> |
assertWriteLockedByMoi(lock); |
1198 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1199 |
|
lock.writeLock().unlock(); |
1200 |
|
lock.writeLock().unlock(); |
1232 |
|
assertEquals(1, clone.getReadLockCount()); |
1233 |
|
clone.readLock().unlock(); |
1234 |
|
clone.writeLock().unlock(); |
1235 |
+ |
assertFalse(clone.isWriteLocked()); |
1236 |
+ |
assertEquals(1, lock.getReadLockCount()); |
1237 |
+ |
assertEquals(0, clone.getReadLockCount()); |
1238 |
|
} |
1239 |
|
|
1240 |
|
/** |