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 { |
85 |
|
Thread.yield(); |
86 |
|
} |
87 |
|
assertTrue(t.isAlive()); |
88 |
< |
assertTrue(lock.getOwner() != t); |
88 |
> |
assertNotSame(t, lock.getOwner()); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
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. |
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); |
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); } |
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 { |
871 |
< |
try { |
872 |
< |
c.await(); |
873 |
< |
shouldThrow(); |
874 |
< |
} catch (IllegalMonitorStateException success) {} |
875 |
< |
try { |
876 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
877 |
< |
shouldThrow(); |
878 |
< |
} catch (IllegalMonitorStateException success) {} |
879 |
< |
try { |
880 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
881 |
< |
shouldThrow(); |
882 |
< |
} 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 (InterruptedException ie) { |
881 |
< |
threadUnexpectedException(ie); |
879 |
> |
} catch (IllegalMonitorStateException success) { |
880 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
881 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
882 |
|
} |
890 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1008 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1008 |
> |
* awaitUninterruptibly is uninterruptible |
1009 |
|
*/ |
1010 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1011 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1012 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1013 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1014 |
|
final Condition c = lock.writeLock().newCondition(); |
1015 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1016 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1015 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1016 |
> |
|
1017 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1018 |
|
public void realRun() { |
1019 |
+ |
// Interrupt before awaitUninterruptibly |
1020 |
|
lock.writeLock().lock(); |
1021 |
< |
locked.countDown(); |
1021 |
> |
pleaseInterrupt.countDown(); |
1022 |
> |
Thread.currentThread().interrupt(); |
1023 |
|
c.awaitUninterruptibly(); |
1024 |
|
assertTrue(Thread.interrupted()); |
1025 |
|
lock.writeLock().unlock(); |
1026 |
|
}}); |
1027 |
|
|
1028 |
< |
await(locked); |
1028 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1029 |
> |
public void realRun() { |
1030 |
> |
// Interrupt during awaitUninterruptibly |
1031 |
> |
lock.writeLock().lock(); |
1032 |
> |
pleaseInterrupt.countDown(); |
1033 |
> |
c.awaitUninterruptibly(); |
1034 |
> |
assertTrue(Thread.interrupted()); |
1035 |
> |
lock.writeLock().unlock(); |
1036 |
> |
}}); |
1037 |
> |
|
1038 |
> |
await(pleaseInterrupt); |
1039 |
|
lock.writeLock().lock(); |
1040 |
|
lock.writeLock().unlock(); |
1041 |
< |
t.interrupt(); |
1042 |
< |
long timeoutMillis = 10; |
1043 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1041 |
> |
t2.interrupt(); |
1042 |
> |
|
1043 |
> |
assertThreadStaysAlive(t1); |
1044 |
> |
assertTrue(t2.isAlive()); |
1045 |
> |
|
1046 |
|
lock.writeLock().lock(); |
1047 |
< |
c.signal(); |
1047 |
> |
c.signalAll(); |
1048 |
|
lock.writeLock().unlock(); |
1049 |
< |
awaitTermination(t); |
1049 |
> |
|
1050 |
> |
awaitTermination(t1); |
1051 |
> |
awaitTermination(t2); |
1052 |
|
} |
1053 |
|
|
1054 |
|
/** |
1056 |
|
*/ |
1057 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1058 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1059 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1060 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1061 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1062 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1063 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1095 |
|
*/ |
1096 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1097 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1098 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1099 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1100 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1101 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1102 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* signal wakes up waiting threads in FIFO order. |
1133 |
> |
* signal wakes up waiting threads in FIFO order |
1134 |
|
*/ |
1135 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1136 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1291 |
|
} |
1292 |
|
|
1293 |
|
/** |
1294 |
< |
* hasQueuedThread reports whether a thread is queued. |
1294 |
> |
* hasQueuedThread reports whether a thread is queued |
1295 |
|
*/ |
1296 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1297 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1632 |
|
public void testToString_fair() { testToString(true); } |
1633 |
|
public void testToString(boolean fair) { |
1634 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1635 |
< |
String us = lock.toString(); |
1636 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1637 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1638 |
< |
lock.writeLock().lock(); |
1639 |
< |
String ws = lock.toString(); |
1627 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1628 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1635 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1636 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1637 |
> |
lock.writeLock().lock(); |
1638 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1639 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1640 |
|
lock.writeLock().unlock(); |
1641 |
|
lock.readLock().lock(); |
1642 |
|
lock.readLock().lock(); |
1643 |
< |
String rs = lock.toString(); |
1644 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1634 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1643 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1644 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1645 |
|
} |
1646 |
|
|
1647 |
|
/** |
1651 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1652 |
|
public void testReadLockToString(boolean fair) { |
1653 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1654 |
< |
String us = lock.readLock().toString(); |
1645 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1654 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1655 |
|
lock.readLock().lock(); |
1656 |
|
lock.readLock().lock(); |
1657 |
< |
String rs = lock.readLock().toString(); |
1649 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1657 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1658 |
|
} |
1659 |
|
|
1660 |
|
/** |
1664 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1665 |
|
public void testWriteLockToString(boolean fair) { |
1666 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1667 |
< |
String us = lock.writeLock().toString(); |
1660 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1667 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1668 |
|
lock.writeLock().lock(); |
1669 |
< |
String ls = lock.writeLock().toString(); |
1670 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1669 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1670 |
> |
lock.writeLock().unlock(); |
1671 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1672 |
|
} |
1673 |
|
|
1674 |
|
} |