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); |
80 |
|
long startTime = System.nanoTime(); |
81 |
|
while (!lock.hasQueuedThread(t)) { |
82 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
83 |
< |
throw new AssertionError("timed out"); |
83 |
> |
throw new AssertionFailedError("timed out"); |
84 |
|
Thread.yield(); |
85 |
|
} |
86 |
|
assertTrue(t.isAlive()); |
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) { |
145 |
|
lock.writeLock().unlock(); |
146 |
|
} |
147 |
|
|
148 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
148 |
> |
enum AwaitMethod { await, awaitTimed, 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 { |
156 |
|
case await: |
157 |
|
c.await(); |
158 |
|
break; |
159 |
+ |
case awaitTimed: |
160 |
+ |
assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
161 |
+ |
break; |
162 |
|
case awaitNanos: |
163 |
|
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
164 |
|
assertTrue(nanosRemaining > 0); |
202 |
|
new PublicReentrantReadWriteLock(fair); |
203 |
|
assertNotWriteLocked(lock); |
204 |
|
lock.writeLock().lock(); |
205 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
205 |
> |
assertWriteLockedByMoi(lock); |
206 |
|
lock.writeLock().unlock(); |
207 |
|
assertNotWriteLocked(lock); |
208 |
|
assertEquals(0, lock.getReadLockCount()); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
+ |
* write-tryLock on an unlocked lock succeeds |
376 |
+ |
*/ |
377 |
+ |
public void testWriteTryLock() { testWriteTryLock(false); } |
378 |
+ |
public void testWriteTryLock_fair() { testWriteTryLock(true); } |
379 |
+ |
public void testWriteTryLock(boolean fair) { |
380 |
+ |
final PublicReentrantReadWriteLock lock = |
381 |
+ |
new PublicReentrantReadWriteLock(fair); |
382 |
+ |
assertTrue(lock.writeLock().tryLock()); |
383 |
+ |
assertWriteLockedByMoi(lock); |
384 |
+ |
assertTrue(lock.writeLock().tryLock()); |
385 |
+ |
assertWriteLockedByMoi(lock); |
386 |
+ |
lock.writeLock().unlock(); |
387 |
+ |
releaseWriteLock(lock); |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
|
* write-tryLock fails if locked |
392 |
|
*/ |
393 |
|
public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } |
651 |
|
|
652 |
|
waitForQueuedThread(lock, t1); |
653 |
|
waitForQueuedThread(lock, t2); |
654 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
654 |
> |
assertWriteLockedByMoi(lock); |
655 |
|
lock.readLock().lock(); |
656 |
|
lock.readLock().unlock(); |
657 |
|
releaseWriteLock(lock); |
685 |
|
|
686 |
|
waitForQueuedThread(lock, t1); |
687 |
|
waitForQueuedThread(lock, t2); |
688 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
688 |
> |
assertWriteLockedByMoi(lock); |
689 |
|
lock.readLock().lock(); |
690 |
|
lock.readLock().unlock(); |
691 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
691 |
> |
assertWriteLockedByMoi(lock); |
692 |
|
lock.writeLock().unlock(); |
693 |
|
awaitTermination(t1); |
694 |
|
awaitTermination(t2); |
720 |
|
|
721 |
|
waitForQueuedThread(lock, t1); |
722 |
|
waitForQueuedThread(lock, t2); |
723 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
723 |
> |
assertWriteLockedByMoi(lock); |
724 |
|
assertEquals(1, lock.getWriteHoldCount()); |
725 |
|
lock.writeLock().lock(); |
726 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
726 |
> |
assertWriteLockedByMoi(lock); |
727 |
|
assertEquals(2, lock.getWriteHoldCount()); |
728 |
|
lock.writeLock().unlock(); |
729 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
729 |
> |
assertWriteLockedByMoi(lock); |
730 |
|
assertEquals(1, lock.getWriteHoldCount()); |
731 |
|
lock.writeLock().unlock(); |
732 |
|
awaitTermination(t1); |
774 |
|
public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } |
775 |
|
public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } |
776 |
|
public void testWriteTryLock_Timeout(boolean fair) { |
777 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
777 |
> |
final PublicReentrantReadWriteLock lock = |
778 |
> |
new PublicReentrantReadWriteLock(fair); |
779 |
|
lock.writeLock().lock(); |
780 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
781 |
|
public void realRun() throws InterruptedException { |
786 |
|
}}); |
787 |
|
|
788 |
|
awaitTermination(t); |
789 |
< |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
760 |
< |
lock.writeLock().unlock(); |
789 |
> |
releaseWriteLock(lock); |
790 |
|
} |
791 |
|
|
792 |
|
/** |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
< |
* write lockInterruptibly succeeds if lock free else is interruptible |
814 |
> |
* write lockInterruptibly succeeds if unlocked, else is interruptible |
815 |
|
*/ |
816 |
|
public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } |
817 |
|
public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } |
820 |
|
new PublicReentrantReadWriteLock(fair); |
821 |
|
try { |
822 |
|
lock.writeLock().lockInterruptibly(); |
823 |
< |
} catch (Throwable t) { |
824 |
< |
threadUnexpectedException(t); |
823 |
> |
} catch (InterruptedException ie) { |
824 |
> |
threadUnexpectedException(ie); |
825 |
|
} |
826 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
827 |
|
public void realRun() throws InterruptedException { |
830 |
|
|
831 |
|
waitForQueuedThread(lock, t); |
832 |
|
t.interrupt(); |
833 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
834 |
|
awaitTermination(t); |
835 |
|
releaseWriteLock(lock); |
836 |
|
} |
847 |
|
lock.readLock().lockInterruptibly(); |
848 |
|
lock.readLock().unlock(); |
849 |
|
lock.writeLock().lockInterruptibly(); |
850 |
< |
} catch (Throwable t) { |
851 |
< |
threadUnexpectedException(t); |
850 |
> |
} catch (InterruptedException ie) { |
851 |
> |
threadUnexpectedException(ie); |
852 |
|
} |
853 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
854 |
|
public void realRun() throws InterruptedException { |
869 |
|
public void testAwait_IMSE(boolean fair) { |
870 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
871 |
|
final Condition c = lock.writeLock().newCondition(); |
872 |
< |
long startTime = System.nanoTime(); |
873 |
< |
try { |
844 |
< |
try { |
845 |
< |
c.await(); |
846 |
< |
shouldThrow(); |
847 |
< |
} catch (IllegalMonitorStateException success) {} |
848 |
< |
try { |
849 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
850 |
< |
shouldThrow(); |
851 |
< |
} catch (IllegalMonitorStateException success) {} |
852 |
< |
try { |
853 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
854 |
< |
shouldThrow(); |
855 |
< |
} catch (IllegalMonitorStateException success) {} |
872 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
873 |
> |
long startTime = System.nanoTime(); |
874 |
|
try { |
875 |
< |
c.awaitUninterruptibly(); |
875 |
> |
await(c, awaitMethod); |
876 |
|
shouldThrow(); |
877 |
< |
} catch (IllegalMonitorStateException success) {} |
878 |
< |
} catch (Throwable t) { |
879 |
< |
threadUnexpectedException(t); |
877 |
> |
} catch (IllegalMonitorStateException success) { |
878 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
879 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
880 |
|
} |
863 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
1025 |
|
lock.writeLock().unlock(); |
1026 |
|
t.interrupt(); |
1027 |
|
long timeoutMillis = 10; |
1028 |
< |
assertThreadJoinTimesOut(t, timeoutMillis); |
1028 |
> |
assertThreadStaysAlive(t, timeoutMillis); |
1029 |
|
lock.writeLock().lock(); |
1030 |
|
c.signal(); |
1031 |
|
lock.writeLock().unlock(); |
1037 |
|
*/ |
1038 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1039 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1040 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1041 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1042 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1043 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1044 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1051 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1052 |
|
public void realRun() throws InterruptedException { |
1053 |
|
lock.writeLock().lock(); |
1054 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1054 |
> |
assertWriteLockedByMoi(lock); |
1055 |
|
assertHasNoWaiters(lock, c); |
1056 |
|
locked.countDown(); |
1057 |
|
try { |
1058 |
|
await(c, awaitMethod); |
1059 |
|
} finally { |
1060 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1060 |
> |
assertWriteLockedByMoi(lock); |
1061 |
|
assertHasNoWaiters(lock, c); |
1062 |
|
lock.writeLock().unlock(); |
1063 |
|
assertFalse(Thread.interrupted()); |
1076 |
|
*/ |
1077 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1078 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1079 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1080 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1081 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1082 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1083 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1171 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1172 |
|
public void realRun() throws InterruptedException { |
1173 |
|
lock.writeLock().lock(); |
1174 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1174 |
> |
assertWriteLockedByMoi(lock); |
1175 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1176 |
|
locked.countDown(); |
1177 |
|
c.await(); |
1178 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1178 |
> |
assertWriteLockedByMoi(lock); |
1179 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1180 |
|
lock.writeLock().unlock(); |
1181 |
|
}}); |
1184 |
|
public void realRun() throws InterruptedException { |
1185 |
|
lock.writeLock().lock(); |
1186 |
|
lock.writeLock().lock(); |
1187 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1187 |
> |
assertWriteLockedByMoi(lock); |
1188 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1189 |
|
locked.countDown(); |
1190 |
|
c.await(); |
1191 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1191 |
> |
assertWriteLockedByMoi(lock); |
1192 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1193 |
|
lock.writeLock().unlock(); |
1194 |
|
lock.writeLock().unlock(); |
1226 |
|
assertEquals(1, clone.getReadLockCount()); |
1227 |
|
clone.readLock().unlock(); |
1228 |
|
clone.writeLock().unlock(); |
1229 |
+ |
assertFalse(clone.isWriteLocked()); |
1230 |
+ |
assertEquals(1, lock.getReadLockCount()); |
1231 |
+ |
assertEquals(0, clone.getReadLockCount()); |
1232 |
|
} |
1233 |
|
|
1234 |
|
/** |