8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
11 |
< |
import java.util.concurrent.locks.*; |
12 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.concurrent.locks.Condition; |
12 |
> |
import java.util.concurrent.locks.Lock; |
13 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.io.*; |
16 |
|
import java.util.*; |
17 |
|
|
18 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
68 |
|
*/ |
69 |
|
void releaseWriteLock(PublicReentrantReadWriteLock lock) { |
70 |
|
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); |
71 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
71 |
> |
assertWriteLockedByMoi(lock); |
72 |
|
assertEquals(1, lock.getWriteHoldCount()); |
73 |
|
writeLock.unlock(); |
74 |
|
assertNotWriteLocked(lock); |
81 |
|
long startTime = System.nanoTime(); |
82 |
|
while (!lock.hasQueuedThread(t)) { |
83 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
84 |
< |
throw new AssertionError("timed out"); |
84 |
> |
throw new AssertionFailedError("timed out"); |
85 |
|
Thread.yield(); |
86 |
|
} |
87 |
|
assertTrue(t.isAlive()); |
95 |
|
assertFalse(lock.isWriteLocked()); |
96 |
|
assertFalse(lock.isWriteLockedByCurrentThread()); |
97 |
|
assertFalse(lock.writeLock().isHeldByCurrentThread()); |
97 |
– |
assertNull(lock.getOwner()); |
98 |
|
assertEquals(0, lock.getWriteHoldCount()); |
99 |
+ |
assertEquals(0, lock.writeLock().getHoldCount()); |
100 |
+ |
assertNull(lock.getOwner()); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
112 |
|
lock.writeLock().isHeldByCurrentThread()); |
113 |
|
assertEquals(t == Thread.currentThread(), |
114 |
|
lock.getWriteHoldCount() > 0); |
115 |
+ |
assertEquals(t == Thread.currentThread(), |
116 |
+ |
lock.writeLock().getHoldCount() > 0); |
117 |
|
assertEquals(0, lock.getReadLockCount()); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
+ |
* Checks that lock is write-locked by the current thread. |
122 |
+ |
*/ |
123 |
+ |
void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) { |
124 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
/** |
128 |
|
* Checks that condition c has no waiters. |
129 |
|
*/ |
130 |
|
void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { |
146 |
|
lock.writeLock().unlock(); |
147 |
|
} |
148 |
|
|
149 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
149 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
150 |
|
|
151 |
|
/** |
152 |
< |
* Awaits condition using the specified AwaitMethod |
152 |
> |
* Awaits condition using the specified AwaitMethod. |
153 |
|
*/ |
154 |
|
void await(Condition c, AwaitMethod awaitMethod) |
155 |
|
throws InterruptedException { |
157 |
|
case await: |
158 |
|
c.await(); |
159 |
|
break; |
160 |
+ |
case awaitTimed: |
161 |
+ |
assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
162 |
+ |
break; |
163 |
|
case awaitNanos: |
164 |
|
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
165 |
|
assertTrue(nanosRemaining > 0); |
203 |
|
new PublicReentrantReadWriteLock(fair); |
204 |
|
assertNotWriteLocked(lock); |
205 |
|
lock.writeLock().lock(); |
206 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
206 |
> |
assertWriteLockedByMoi(lock); |
207 |
|
lock.writeLock().unlock(); |
208 |
|
assertNotWriteLocked(lock); |
209 |
|
assertEquals(0, lock.getReadLockCount()); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
+ |
* write-tryLock on an unlocked lock succeeds |
377 |
+ |
*/ |
378 |
+ |
public void testWriteTryLock() { testWriteTryLock(false); } |
379 |
+ |
public void testWriteTryLock_fair() { testWriteTryLock(true); } |
380 |
+ |
public void testWriteTryLock(boolean fair) { |
381 |
+ |
final PublicReentrantReadWriteLock lock = |
382 |
+ |
new PublicReentrantReadWriteLock(fair); |
383 |
+ |
assertTrue(lock.writeLock().tryLock()); |
384 |
+ |
assertWriteLockedByMoi(lock); |
385 |
+ |
assertTrue(lock.writeLock().tryLock()); |
386 |
+ |
assertWriteLockedByMoi(lock); |
387 |
+ |
lock.writeLock().unlock(); |
388 |
+ |
releaseWriteLock(lock); |
389 |
+ |
} |
390 |
+ |
|
391 |
+ |
/** |
392 |
|
* write-tryLock fails if locked |
393 |
|
*/ |
394 |
|
public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } |
508 |
|
|
509 |
|
/** |
510 |
|
* A thread that tries to acquire a fair read lock (non-reentrantly) |
511 |
< |
* will block if there is a waiting writer thread. |
511 |
> |
* will block if there is a waiting writer thread |
512 |
|
*/ |
513 |
|
public void testReaderWriterReaderFairFifo() { |
514 |
|
final PublicReentrantReadWriteLock lock = |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* Read trylock succeeds (barging) even in the presence of waiting readers and/or writers. |
588 |
> |
* Read trylock succeeds (barging) even in the presence of waiting |
589 |
> |
* readers and/or writers |
590 |
|
*/ |
591 |
|
public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
592 |
|
public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
653 |
|
|
654 |
|
waitForQueuedThread(lock, t1); |
655 |
|
waitForQueuedThread(lock, t2); |
656 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
656 |
> |
assertWriteLockedByMoi(lock); |
657 |
|
lock.readLock().lock(); |
658 |
|
lock.readLock().unlock(); |
659 |
|
releaseWriteLock(lock); |
687 |
|
|
688 |
|
waitForQueuedThread(lock, t1); |
689 |
|
waitForQueuedThread(lock, t2); |
690 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
690 |
> |
assertWriteLockedByMoi(lock); |
691 |
|
lock.readLock().lock(); |
692 |
|
lock.readLock().unlock(); |
693 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
693 |
> |
assertWriteLockedByMoi(lock); |
694 |
|
lock.writeLock().unlock(); |
695 |
|
awaitTermination(t1); |
696 |
|
awaitTermination(t2); |
722 |
|
|
723 |
|
waitForQueuedThread(lock, t1); |
724 |
|
waitForQueuedThread(lock, t2); |
725 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
725 |
> |
assertWriteLockedByMoi(lock); |
726 |
|
assertEquals(1, lock.getWriteHoldCount()); |
727 |
|
lock.writeLock().lock(); |
728 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
728 |
> |
assertWriteLockedByMoi(lock); |
729 |
|
assertEquals(2, lock.getWriteHoldCount()); |
730 |
|
lock.writeLock().unlock(); |
731 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
731 |
> |
assertWriteLockedByMoi(lock); |
732 |
|
assertEquals(1, lock.getWriteHoldCount()); |
733 |
|
lock.writeLock().unlock(); |
734 |
|
awaitTermination(t1); |
776 |
|
public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } |
777 |
|
public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } |
778 |
|
public void testWriteTryLock_Timeout(boolean fair) { |
779 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
779 |
> |
final PublicReentrantReadWriteLock lock = |
780 |
> |
new PublicReentrantReadWriteLock(fair); |
781 |
|
lock.writeLock().lock(); |
782 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
783 |
|
public void realRun() throws InterruptedException { |
788 |
|
}}); |
789 |
|
|
790 |
|
awaitTermination(t); |
791 |
< |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
760 |
< |
lock.writeLock().unlock(); |
791 |
> |
releaseWriteLock(lock); |
792 |
|
} |
793 |
|
|
794 |
|
/** |
813 |
|
} |
814 |
|
|
815 |
|
/** |
816 |
< |
* write lockInterruptibly succeeds if lock free else is interruptible |
816 |
> |
* write lockInterruptibly succeeds if unlocked, else is interruptible |
817 |
|
*/ |
818 |
|
public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } |
819 |
|
public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } |
822 |
|
new PublicReentrantReadWriteLock(fair); |
823 |
|
try { |
824 |
|
lock.writeLock().lockInterruptibly(); |
825 |
< |
} catch (Throwable t) { |
826 |
< |
threadUnexpectedException(t); |
825 |
> |
} catch (InterruptedException ie) { |
826 |
> |
threadUnexpectedException(ie); |
827 |
|
} |
828 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
829 |
|
public void realRun() throws InterruptedException { |
832 |
|
|
833 |
|
waitForQueuedThread(lock, t); |
834 |
|
t.interrupt(); |
835 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
836 |
|
awaitTermination(t); |
837 |
|
releaseWriteLock(lock); |
838 |
|
} |
849 |
|
lock.readLock().lockInterruptibly(); |
850 |
|
lock.readLock().unlock(); |
851 |
|
lock.writeLock().lockInterruptibly(); |
852 |
< |
} catch (Throwable t) { |
853 |
< |
threadUnexpectedException(t); |
852 |
> |
} catch (InterruptedException ie) { |
853 |
> |
threadUnexpectedException(ie); |
854 |
|
} |
855 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
856 |
|
public void realRun() throws InterruptedException { |
871 |
|
public void testAwait_IMSE(boolean fair) { |
872 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
873 |
|
final Condition c = lock.writeLock().newCondition(); |
874 |
< |
long startTime = System.nanoTime(); |
875 |
< |
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) {} |
874 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
875 |
> |
long startTime = System.nanoTime(); |
876 |
|
try { |
877 |
< |
c.awaitUninterruptibly(); |
877 |
> |
await(c, awaitMethod); |
878 |
|
shouldThrow(); |
879 |
< |
} catch (IllegalMonitorStateException success) {} |
880 |
< |
} catch (Throwable t) { |
881 |
< |
threadUnexpectedException(t); |
879 |
> |
} catch (IllegalMonitorStateException success) { |
880 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
881 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
882 |
|
} |
863 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
1027 |
|
lock.writeLock().unlock(); |
1028 |
|
t.interrupt(); |
1029 |
|
long timeoutMillis = 10; |
1030 |
< |
assertThreadJoinTimesOut(t, timeoutMillis); |
1030 |
> |
assertThreadStaysAlive(t, timeoutMillis); |
1031 |
|
lock.writeLock().lock(); |
1032 |
|
c.signal(); |
1033 |
|
lock.writeLock().unlock(); |
1039 |
|
*/ |
1040 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1041 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1042 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1043 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1044 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1045 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1046 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1053 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1054 |
|
public void realRun() throws InterruptedException { |
1055 |
|
lock.writeLock().lock(); |
1056 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1056 |
> |
assertWriteLockedByMoi(lock); |
1057 |
|
assertHasNoWaiters(lock, c); |
1058 |
|
locked.countDown(); |
1059 |
|
try { |
1060 |
|
await(c, awaitMethod); |
1061 |
|
} finally { |
1062 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1062 |
> |
assertWriteLockedByMoi(lock); |
1063 |
|
assertHasNoWaiters(lock, c); |
1064 |
|
lock.writeLock().unlock(); |
1065 |
|
assertFalse(Thread.interrupted()); |
1078 |
|
*/ |
1079 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1080 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1081 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1082 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1083 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1084 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1085 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1116 |
< |
* signal wakes up waiting threads in FIFO order. |
1116 |
> |
* signal wakes up waiting threads in FIFO order |
1117 |
|
*/ |
1118 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1119 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1173 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1174 |
|
public void realRun() throws InterruptedException { |
1175 |
|
lock.writeLock().lock(); |
1176 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1176 |
> |
assertWriteLockedByMoi(lock); |
1177 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1178 |
|
locked.countDown(); |
1179 |
|
c.await(); |
1180 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1180 |
> |
assertWriteLockedByMoi(lock); |
1181 |
|
assertEquals(1, lock.writeLock().getHoldCount()); |
1182 |
|
lock.writeLock().unlock(); |
1183 |
|
}}); |
1186 |
|
public void realRun() throws InterruptedException { |
1187 |
|
lock.writeLock().lock(); |
1188 |
|
lock.writeLock().lock(); |
1189 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1189 |
> |
assertWriteLockedByMoi(lock); |
1190 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1191 |
|
locked.countDown(); |
1192 |
|
c.await(); |
1193 |
< |
assertWriteLockedBy(lock, Thread.currentThread()); |
1193 |
> |
assertWriteLockedByMoi(lock); |
1194 |
|
assertEquals(2, lock.writeLock().getHoldCount()); |
1195 |
|
lock.writeLock().unlock(); |
1196 |
|
lock.writeLock().unlock(); |
1228 |
|
assertEquals(1, clone.getReadLockCount()); |
1229 |
|
clone.readLock().unlock(); |
1230 |
|
clone.writeLock().unlock(); |
1231 |
+ |
assertFalse(clone.isWriteLocked()); |
1232 |
+ |
assertEquals(1, lock.getReadLockCount()); |
1233 |
+ |
assertEquals(0, clone.getReadLockCount()); |
1234 |
|
} |
1235 |
|
|
1236 |
|
/** |
1274 |
|
} |
1275 |
|
|
1276 |
|
/** |
1277 |
< |
* hasQueuedThread reports whether a thread is queued. |
1277 |
> |
* hasQueuedThread reports whether a thread is queued |
1278 |
|
*/ |
1279 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1280 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1615 |
|
public void testToString_fair() { testToString(true); } |
1616 |
|
public void testToString(boolean fair) { |
1617 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1618 |
< |
String us = lock.toString(); |
1619 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1620 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1621 |
< |
lock.writeLock().lock(); |
1622 |
< |
String ws = lock.toString(); |
1597 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1598 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1618 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1619 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1620 |
> |
lock.writeLock().lock(); |
1621 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1622 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1623 |
|
lock.writeLock().unlock(); |
1624 |
|
lock.readLock().lock(); |
1625 |
|
lock.readLock().lock(); |
1626 |
< |
String rs = lock.toString(); |
1627 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1604 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1626 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1627 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1628 |
|
} |
1629 |
|
|
1630 |
|
/** |
1634 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1635 |
|
public void testReadLockToString(boolean fair) { |
1636 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1637 |
< |
String us = lock.readLock().toString(); |
1615 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1637 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1638 |
|
lock.readLock().lock(); |
1639 |
|
lock.readLock().lock(); |
1640 |
< |
String rs = lock.readLock().toString(); |
1619 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1640 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1641 |
|
} |
1642 |
|
|
1643 |
|
/** |
1647 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1648 |
|
public void testWriteLockToString(boolean fair) { |
1649 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1650 |
< |
String us = lock.writeLock().toString(); |
1630 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1650 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1651 |
|
lock.writeLock().lock(); |
1652 |
< |
String ls = lock.writeLock().toString(); |
1653 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1652 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1653 |
> |
lock.writeLock().unlock(); |
1654 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1655 |
|
} |
1656 |
|
|
1657 |
|
} |