1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.locks.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
13 |
< |
import java.util.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.Arrays; |
12 |
+ |
import java.util.Collection; |
13 |
+ |
import java.util.HashSet; |
14 |
+ |
import java.util.concurrent.Callable; |
15 |
+ |
import java.util.concurrent.CountDownLatch; |
16 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
17 |
+ |
import java.util.concurrent.locks.Condition; |
18 |
+ |
import java.util.concurrent.locks.Lock; |
19 |
+ |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
20 |
+ |
|
21 |
+ |
import junit.framework.Test; |
22 |
+ |
import junit.framework.TestSuite; |
23 |
+ |
|
24 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
25 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run (suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
< |
return new TestSuite(ReentrantReadWriteLockTest.class); |
30 |
> |
return new TestSuite(ReentrantReadWriteLockTest.class); |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
|
* A runnable calling lockInterruptibly |
35 |
|
*/ |
36 |
< |
class InterruptibleLockRunnable implements Runnable { |
36 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
37 |
|
final ReentrantReadWriteLock lock; |
38 |
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
39 |
< |
public void run() { |
40 |
< |
try { |
31 |
< |
lock.writeLock().lockInterruptibly(); |
32 |
< |
} catch(InterruptedException success){} |
39 |
> |
public void realRun() throws InterruptedException { |
40 |
> |
lock.writeLock().lockInterruptibly(); |
41 |
|
} |
42 |
|
} |
43 |
|
|
36 |
– |
|
44 |
|
/** |
45 |
|
* A runnable calling lockInterruptibly that expects to be |
46 |
|
* interrupted |
47 |
|
*/ |
48 |
< |
class InterruptedLockRunnable implements Runnable { |
48 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
49 |
|
final ReentrantReadWriteLock lock; |
50 |
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
51 |
< |
public void run() { |
52 |
< |
try { |
46 |
< |
lock.writeLock().lockInterruptibly(); |
47 |
< |
threadShouldThrow(); |
48 |
< |
} catch(InterruptedException success){} |
51 |
> |
public void realRun() throws InterruptedException { |
52 |
> |
lock.writeLock().lockInterruptibly(); |
53 |
|
} |
54 |
|
} |
55 |
|
|
58 |
|
*/ |
59 |
|
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
60 |
|
PublicReentrantReadWriteLock() { super(); } |
61 |
< |
public Collection<Thread> getQueuedThreads() { |
62 |
< |
return super.getQueuedThreads(); |
61 |
> |
PublicReentrantReadWriteLock(boolean fair) { super(fair); } |
62 |
> |
public Thread getOwner() { |
63 |
> |
return super.getOwner(); |
64 |
> |
} |
65 |
> |
public Collection<Thread> getQueuedThreads() { |
66 |
> |
return super.getQueuedThreads(); |
67 |
|
} |
68 |
< |
public Collection<Thread> getWaitingThreads(Condition c) { |
69 |
< |
return super.getWaitingThreads(c); |
68 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
69 |
> |
return super.getWaitingThreads(c); |
70 |
|
} |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* Constructor sets given fairness, and is in unlocked state |
74 |
> |
* Releases write lock, checking that it had a hold count of 1. |
75 |
|
*/ |
76 |
< |
public void testConstructor() { |
77 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
78 |
< |
assertFalse(rl.isFair()); |
79 |
< |
assertFalse(rl.isWriteLocked()); |
80 |
< |
assertEquals(0, rl.getReadLockCount()); |
81 |
< |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
74 |
< |
assertTrue(r2.isFair()); |
75 |
< |
assertFalse(r2.isWriteLocked()); |
76 |
< |
assertEquals(0, r2.getReadLockCount()); |
76 |
> |
void releaseWriteLock(PublicReentrantReadWriteLock lock) { |
77 |
> |
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); |
78 |
> |
assertWriteLockedByMoi(lock); |
79 |
> |
assertEquals(1, lock.getWriteHoldCount()); |
80 |
> |
writeLock.unlock(); |
81 |
> |
assertNotWriteLocked(lock); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* write-locking and read-locking an unlocked lock succeed |
85 |
> |
* Spin-waits until lock.hasQueuedThread(t) becomes true. |
86 |
|
*/ |
87 |
< |
public void testLock() { |
88 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
89 |
< |
rl.writeLock().lock(); |
90 |
< |
assertTrue(rl.isWriteLocked()); |
91 |
< |
assertTrue(rl.isWriteLockedByCurrentThread()); |
92 |
< |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
93 |
< |
assertEquals(0, rl.getReadLockCount()); |
94 |
< |
rl.writeLock().unlock(); |
95 |
< |
assertFalse(rl.isWriteLocked()); |
91 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
92 |
< |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
93 |
< |
assertEquals(0, rl.getReadLockCount()); |
94 |
< |
rl.readLock().lock(); |
95 |
< |
assertFalse(rl.isWriteLocked()); |
96 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
97 |
< |
assertEquals(1, rl.getReadLockCount()); |
98 |
< |
rl.readLock().unlock(); |
99 |
< |
assertFalse(rl.isWriteLocked()); |
100 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
101 |
< |
assertEquals(0, rl.getReadLockCount()); |
87 |
> |
void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) { |
88 |
> |
long startTime = System.nanoTime(); |
89 |
> |
while (!lock.hasQueuedThread(t)) { |
90 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
91 |
> |
throw new AssertionError("timed out"); |
92 |
> |
Thread.yield(); |
93 |
> |
} |
94 |
> |
assertTrue(t.isAlive()); |
95 |
> |
assertNotSame(t, lock.getOwner()); |
96 |
|
} |
97 |
|
|
104 |
– |
|
98 |
|
/** |
99 |
< |
* locking an unlocked fair lock succeeds |
100 |
< |
*/ |
101 |
< |
public void testFairLock() { |
102 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
103 |
< |
rl.writeLock().lock(); |
104 |
< |
assertTrue(rl.isWriteLocked()); |
105 |
< |
assertTrue(rl.isWriteLockedByCurrentThread()); |
106 |
< |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
107 |
< |
assertEquals(0, rl.getReadLockCount()); |
115 |
< |
rl.writeLock().unlock(); |
116 |
< |
assertFalse(rl.isWriteLocked()); |
117 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
118 |
< |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
119 |
< |
assertEquals(0, rl.getReadLockCount()); |
120 |
< |
rl.readLock().lock(); |
121 |
< |
assertFalse(rl.isWriteLocked()); |
122 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
123 |
< |
assertEquals(1, rl.getReadLockCount()); |
124 |
< |
rl.readLock().unlock(); |
125 |
< |
assertFalse(rl.isWriteLocked()); |
126 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
127 |
< |
assertEquals(0, rl.getReadLockCount()); |
99 |
> |
* Checks that lock is not write-locked. |
100 |
> |
*/ |
101 |
> |
void assertNotWriteLocked(PublicReentrantReadWriteLock lock) { |
102 |
> |
assertFalse(lock.isWriteLocked()); |
103 |
> |
assertFalse(lock.isWriteLockedByCurrentThread()); |
104 |
> |
assertFalse(lock.writeLock().isHeldByCurrentThread()); |
105 |
> |
assertEquals(0, lock.getWriteHoldCount()); |
106 |
> |
assertEquals(0, lock.writeLock().getHoldCount()); |
107 |
> |
assertNull(lock.getOwner()); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* getWriteHoldCount returns number of recursive holds |
111 |
> |
* Checks that lock is write-locked by the given thread. |
112 |
|
*/ |
113 |
< |
public void testGetWriteHoldCount() { |
114 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
115 |
< |
for(int i = 1; i <= SIZE; i++) { |
116 |
< |
lock.writeLock().lock(); |
117 |
< |
assertEquals(i,lock.getWriteHoldCount()); |
118 |
< |
} |
119 |
< |
for(int i = SIZE; i > 0; i--) { |
120 |
< |
lock.writeLock().unlock(); |
121 |
< |
assertEquals(i-1,lock.getWriteHoldCount()); |
122 |
< |
} |
113 |
> |
void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) { |
114 |
> |
assertTrue(lock.isWriteLocked()); |
115 |
> |
assertSame(t, lock.getOwner()); |
116 |
> |
assertEquals(t == Thread.currentThread(), |
117 |
> |
lock.isWriteLockedByCurrentThread()); |
118 |
> |
assertEquals(t == Thread.currentThread(), |
119 |
> |
lock.writeLock().isHeldByCurrentThread()); |
120 |
> |
assertEquals(t == Thread.currentThread(), |
121 |
> |
lock.getWriteHoldCount() > 0); |
122 |
> |
assertEquals(t == Thread.currentThread(), |
123 |
> |
lock.writeLock().getHoldCount() > 0); |
124 |
> |
assertEquals(0, lock.getReadLockCount()); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* WriteLock.getHoldCount returns number of recursive holds |
129 |
< |
*/ |
130 |
< |
public void testGetHoldCount() { |
131 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
150 |
< |
for(int i = 1; i <= SIZE; i++) { |
151 |
< |
lock.writeLock().lock(); |
152 |
< |
assertEquals(i,lock.writeLock().getHoldCount()); |
153 |
< |
} |
154 |
< |
for(int i = SIZE; i > 0; i--) { |
155 |
< |
lock.writeLock().unlock(); |
156 |
< |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
157 |
< |
} |
128 |
> |
* Checks that lock is write-locked by the current thread. |
129 |
> |
*/ |
130 |
> |
void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) { |
131 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* getReadHoldCount returns number of recursive holds |
135 |
> |
* Checks that condition c has no waiters. |
136 |
|
*/ |
137 |
< |
public void testGetReadHoldCount() { |
138 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
165 |
< |
for(int i = 1; i <= SIZE; i++) { |
166 |
< |
lock.readLock().lock(); |
167 |
< |
assertEquals(i,lock.getReadHoldCount()); |
168 |
< |
} |
169 |
< |
for(int i = SIZE; i > 0; i--) { |
170 |
< |
lock.readLock().unlock(); |
171 |
< |
assertEquals(i-1,lock.getReadHoldCount()); |
172 |
< |
} |
137 |
> |
void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { |
138 |
> |
assertHasWaiters(lock, c, new Thread[] {}); |
139 |
|
} |
174 |
– |
|
140 |
|
|
141 |
|
/** |
142 |
< |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
142 |
> |
* Checks that condition c has exactly the given waiter threads. |
143 |
|
*/ |
144 |
< |
public void testUnlock_IllegalMonitorStateException() { |
145 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
146 |
< |
try { |
147 |
< |
rl.writeLock().unlock(); |
148 |
< |
shouldThrow(); |
149 |
< |
} catch(IllegalMonitorStateException success){} |
144 |
> |
void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c, |
145 |
> |
Thread... threads) { |
146 |
> |
lock.writeLock().lock(); |
147 |
> |
assertEquals(threads.length > 0, lock.hasWaiters(c)); |
148 |
> |
assertEquals(threads.length, lock.getWaitQueueLength(c)); |
149 |
> |
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); |
150 |
> |
assertEquals(threads.length, lock.getWaitingThreads(c).size()); |
151 |
> |
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)), |
152 |
> |
new HashSet<Thread>(Arrays.asList(threads))); |
153 |
> |
lock.writeLock().unlock(); |
154 |
|
} |
155 |
|
|
156 |
+ |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
157 |
|
|
158 |
|
/** |
159 |
< |
* write-lockInterruptibly is interruptible |
159 |
> |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
160 |
|
*/ |
161 |
< |
public void testWriteLockInterruptibly_Interrupted() { |
162 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
163 |
< |
Thread t = new Thread(new Runnable() { |
164 |
< |
public void run() { |
165 |
< |
try { |
166 |
< |
lock.writeLock().lockInterruptibly(); |
167 |
< |
lock.writeLock().unlock(); |
168 |
< |
lock.writeLock().lockInterruptibly(); |
169 |
< |
lock.writeLock().unlock(); |
170 |
< |
} catch(InterruptedException success){} |
171 |
< |
} |
172 |
< |
}); |
173 |
< |
try { |
174 |
< |
lock.writeLock().lock(); |
175 |
< |
t.start(); |
176 |
< |
t.interrupt(); |
177 |
< |
lock.writeLock().unlock(); |
178 |
< |
t.join(); |
179 |
< |
} catch(Exception e){ |
180 |
< |
unexpectedException(); |
161 |
> |
void await(Condition c, AwaitMethod awaitMethod) |
162 |
> |
throws InterruptedException { |
163 |
> |
long timeoutMillis = 2 * LONG_DELAY_MS; |
164 |
> |
switch (awaitMethod) { |
165 |
> |
case await: |
166 |
> |
c.await(); |
167 |
> |
break; |
168 |
> |
case awaitTimed: |
169 |
> |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
170 |
> |
break; |
171 |
> |
case awaitNanos: |
172 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
173 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
174 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
175 |
> |
assertTrue(nanosRemaining <= timeoutNanos); |
176 |
> |
break; |
177 |
> |
case awaitUntil: |
178 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
179 |
> |
break; |
180 |
> |
default: |
181 |
> |
throw new AssertionError(); |
182 |
|
} |
183 |
< |
} |
183 |
> |
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* timed write-tryLock is interruptible |
186 |
> |
* Constructor sets given fairness, and is in unlocked state |
187 |
|
*/ |
188 |
< |
public void testWriteTryLock_Interrupted() { |
189 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
190 |
< |
lock.writeLock().lock(); |
191 |
< |
Thread t = new Thread(new Runnable() { |
192 |
< |
public void run() { |
193 |
< |
try { |
194 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
195 |
< |
} catch(InterruptedException success){} |
196 |
< |
} |
197 |
< |
}); |
198 |
< |
try { |
199 |
< |
t.start(); |
200 |
< |
t.interrupt(); |
201 |
< |
lock.writeLock().unlock(); |
202 |
< |
t.join(); |
203 |
< |
} catch(Exception e){ |
204 |
< |
unexpectedException(); |
234 |
< |
} |
188 |
> |
public void testConstructor() { |
189 |
> |
PublicReentrantReadWriteLock lock; |
190 |
> |
|
191 |
> |
lock = new PublicReentrantReadWriteLock(); |
192 |
> |
assertFalse(lock.isFair()); |
193 |
> |
assertNotWriteLocked(lock); |
194 |
> |
assertEquals(0, lock.getReadLockCount()); |
195 |
> |
|
196 |
> |
lock = new PublicReentrantReadWriteLock(true); |
197 |
> |
assertTrue(lock.isFair()); |
198 |
> |
assertNotWriteLocked(lock); |
199 |
> |
assertEquals(0, lock.getReadLockCount()); |
200 |
> |
|
201 |
> |
lock = new PublicReentrantReadWriteLock(false); |
202 |
> |
assertFalse(lock.isFair()); |
203 |
> |
assertNotWriteLocked(lock); |
204 |
> |
assertEquals(0, lock.getReadLockCount()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* read-lockInterruptibly is interruptible |
208 |
> |
* write-locking and read-locking an unlocked lock succeed |
209 |
|
*/ |
210 |
< |
public void testReadLockInterruptibly_Interrupted() { |
211 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
212 |
< |
lock.writeLock().lock(); |
213 |
< |
Thread t = new Thread(new Runnable() { |
214 |
< |
public void run() { |
215 |
< |
try { |
216 |
< |
lock.readLock().lockInterruptibly(); |
217 |
< |
} catch(InterruptedException success){} |
218 |
< |
} |
219 |
< |
}); |
220 |
< |
try { |
221 |
< |
t.start(); |
222 |
< |
t.interrupt(); |
210 |
> |
public void testLock() { testLock(false); } |
211 |
> |
public void testLock_fair() { testLock(true); } |
212 |
> |
public void testLock(boolean fair) { |
213 |
> |
PublicReentrantReadWriteLock lock = |
214 |
> |
new PublicReentrantReadWriteLock(fair); |
215 |
> |
assertNotWriteLocked(lock); |
216 |
> |
lock.writeLock().lock(); |
217 |
> |
assertWriteLockedByMoi(lock); |
218 |
> |
lock.writeLock().unlock(); |
219 |
> |
assertNotWriteLocked(lock); |
220 |
> |
assertEquals(0, lock.getReadLockCount()); |
221 |
> |
lock.readLock().lock(); |
222 |
> |
assertNotWriteLocked(lock); |
223 |
> |
assertEquals(1, lock.getReadLockCount()); |
224 |
> |
lock.readLock().unlock(); |
225 |
> |
assertNotWriteLocked(lock); |
226 |
> |
assertEquals(0, lock.getReadLockCount()); |
227 |
> |
} |
228 |
> |
|
229 |
> |
/** |
230 |
> |
* getWriteHoldCount returns number of recursive holds |
231 |
> |
*/ |
232 |
> |
public void testGetWriteHoldCount() { testGetWriteHoldCount(false); } |
233 |
> |
public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); } |
234 |
> |
public void testGetWriteHoldCount(boolean fair) { |
235 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
236 |
> |
for (int i = 1; i <= SIZE; i++) { |
237 |
> |
lock.writeLock().lock(); |
238 |
> |
assertEquals(i,lock.getWriteHoldCount()); |
239 |
> |
} |
240 |
> |
for (int i = SIZE; i > 0; i--) { |
241 |
|
lock.writeLock().unlock(); |
242 |
< |
t.join(); |
255 |
< |
} catch(Exception e){ |
256 |
< |
unexpectedException(); |
242 |
> |
assertEquals(i - 1,lock.getWriteHoldCount()); |
243 |
|
} |
244 |
< |
} |
244 |
> |
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* timed read-tryLock is interruptible |
247 |
> |
* writelock.getHoldCount returns number of recursive holds |
248 |
|
*/ |
249 |
< |
public void testReadTryLock_Interrupted() { |
250 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
251 |
< |
lock.writeLock().lock(); |
252 |
< |
Thread t = new Thread(new Runnable() { |
253 |
< |
public void run() { |
254 |
< |
try { |
255 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
256 |
< |
threadShouldThrow(); |
257 |
< |
} catch(InterruptedException success){} |
258 |
< |
} |
259 |
< |
}); |
274 |
< |
try { |
275 |
< |
t.start(); |
276 |
< |
t.interrupt(); |
277 |
< |
t.join(); |
278 |
< |
} catch(Exception e){ |
279 |
< |
unexpectedException(); |
249 |
> |
public void testGetHoldCount() { testGetHoldCount(false); } |
250 |
> |
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
251 |
> |
public void testGetHoldCount(boolean fair) { |
252 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
253 |
> |
for (int i = 1; i <= SIZE; i++) { |
254 |
> |
lock.writeLock().lock(); |
255 |
> |
assertEquals(i,lock.writeLock().getHoldCount()); |
256 |
> |
} |
257 |
> |
for (int i = SIZE; i > 0; i--) { |
258 |
> |
lock.writeLock().unlock(); |
259 |
> |
assertEquals(i - 1,lock.writeLock().getHoldCount()); |
260 |
|
} |
261 |
|
} |
262 |
|
|
283 |
– |
|
263 |
|
/** |
264 |
< |
* write-tryLock fails if locked |
264 |
> |
* getReadHoldCount returns number of recursive holds |
265 |
|
*/ |
266 |
< |
public void testWriteTryLockWhenLocked() { |
267 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
268 |
< |
lock.writeLock().lock(); |
269 |
< |
Thread t = new Thread(new Runnable() { |
270 |
< |
public void run() { |
271 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
272 |
< |
} |
294 |
< |
}); |
295 |
< |
try { |
296 |
< |
t.start(); |
297 |
< |
t.join(); |
298 |
< |
lock.writeLock().unlock(); |
299 |
< |
} catch(Exception e){ |
300 |
< |
unexpectedException(); |
266 |
> |
public void testGetReadHoldCount() { testGetReadHoldCount(false); } |
267 |
> |
public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); } |
268 |
> |
public void testGetReadHoldCount(boolean fair) { |
269 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
270 |
> |
for (int i = 1; i <= SIZE; i++) { |
271 |
> |
lock.readLock().lock(); |
272 |
> |
assertEquals(i,lock.getReadHoldCount()); |
273 |
|
} |
274 |
< |
} |
274 |
> |
for (int i = SIZE; i > 0; i--) { |
275 |
> |
lock.readLock().unlock(); |
276 |
> |
assertEquals(i - 1,lock.getReadHoldCount()); |
277 |
> |
} |
278 |
> |
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* read-tryLock fails if locked |
281 |
> |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
282 |
|
*/ |
283 |
< |
public void testReadTryLockWhenLocked() { |
284 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
285 |
< |
lock.writeLock().lock(); |
286 |
< |
Thread t = new Thread(new Runnable() { |
311 |
< |
public void run() { |
312 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
313 |
< |
} |
314 |
< |
}); |
283 |
> |
public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); } |
284 |
> |
public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); } |
285 |
> |
public void testWriteUnlock_IMSE(boolean fair) { |
286 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
287 |
|
try { |
316 |
– |
t.start(); |
317 |
– |
t.join(); |
288 |
|
lock.writeLock().unlock(); |
289 |
< |
} catch(Exception e){ |
290 |
< |
unexpectedException(); |
291 |
< |
} |
322 |
< |
} |
289 |
> |
shouldThrow(); |
290 |
> |
} catch (IllegalMonitorStateException success) {} |
291 |
> |
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* Multiple threads can hold a read lock when not write-locked |
294 |
> |
* read-unlocking an unlocked lock throws IllegalMonitorStateException |
295 |
|
*/ |
296 |
< |
public void testMultipleReadLocks() { |
297 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
298 |
< |
lock.readLock().lock(); |
299 |
< |
Thread t = new Thread(new Runnable() { |
331 |
< |
public void run() { |
332 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
333 |
< |
lock.readLock().unlock(); |
334 |
< |
} |
335 |
< |
}); |
296 |
> |
public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); } |
297 |
> |
public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); } |
298 |
> |
public void testReadUnlock_IMSE(boolean fair) { |
299 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
300 |
|
try { |
337 |
– |
t.start(); |
338 |
– |
t.join(); |
301 |
|
lock.readLock().unlock(); |
302 |
< |
} catch(Exception e){ |
303 |
< |
unexpectedException(); |
304 |
< |
} |
343 |
< |
} |
302 |
> |
shouldThrow(); |
303 |
> |
} catch (IllegalMonitorStateException success) {} |
304 |
> |
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* A writelock succeeds after reading threads unlock |
307 |
> |
* write-lockInterruptibly is interruptible |
308 |
|
*/ |
309 |
< |
public void testWriteAfterMultipleReadLocks() { |
310 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
311 |
< |
lock.readLock().lock(); |
312 |
< |
Thread t1 = new Thread(new Runnable() { |
313 |
< |
public void run() { |
314 |
< |
lock.readLock().lock(); |
315 |
< |
lock.readLock().unlock(); |
316 |
< |
} |
317 |
< |
}); |
318 |
< |
Thread t2 = new Thread(new Runnable() { |
358 |
< |
public void run() { |
359 |
< |
lock.writeLock().lock(); |
360 |
< |
lock.writeLock().unlock(); |
361 |
< |
} |
362 |
< |
}); |
309 |
> |
public void testWriteLockInterruptibly_Interruptible() { testWriteLockInterruptibly_Interruptible(false); } |
310 |
> |
public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); } |
311 |
> |
public void testWriteLockInterruptibly_Interruptible(boolean fair) { |
312 |
> |
final PublicReentrantReadWriteLock lock = |
313 |
> |
new PublicReentrantReadWriteLock(fair); |
314 |
> |
lock.writeLock().lock(); |
315 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
316 |
> |
public void realRun() throws InterruptedException { |
317 |
> |
lock.writeLock().lockInterruptibly(); |
318 |
> |
}}); |
319 |
|
|
320 |
< |
try { |
321 |
< |
t1.start(); |
322 |
< |
t2.start(); |
323 |
< |
Thread.sleep(SHORT_DELAY_MS); |
324 |
< |
lock.readLock().unlock(); |
369 |
< |
t1.join(MEDIUM_DELAY_MS); |
370 |
< |
t2.join(MEDIUM_DELAY_MS); |
371 |
< |
assertTrue(!t1.isAlive()); |
372 |
< |
assertTrue(!t2.isAlive()); |
373 |
< |
|
374 |
< |
} catch(Exception e){ |
375 |
< |
unexpectedException(); |
376 |
< |
} |
377 |
< |
} |
320 |
> |
waitForQueuedThread(lock, t); |
321 |
> |
t.interrupt(); |
322 |
> |
awaitTermination(t); |
323 |
> |
releaseWriteLock(lock); |
324 |
> |
} |
325 |
|
|
326 |
|
/** |
327 |
< |
* Readlocks succeed after a writing thread unlocks |
327 |
> |
* timed write-tryLock is interruptible |
328 |
|
*/ |
329 |
< |
public void testReadAfterWriteLock() { |
330 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
331 |
< |
lock.writeLock().lock(); |
332 |
< |
Thread t1 = new Thread(new Runnable() { |
333 |
< |
public void run() { |
334 |
< |
lock.readLock().lock(); |
335 |
< |
lock.readLock().unlock(); |
336 |
< |
} |
337 |
< |
}); |
338 |
< |
Thread t2 = new Thread(new Runnable() { |
339 |
< |
public void run() { |
340 |
< |
lock.readLock().lock(); |
341 |
< |
lock.readLock().unlock(); |
342 |
< |
} |
343 |
< |
}); |
329 |
> |
public void testWriteTryLock_Interruptible() { testWriteTryLock_Interruptible(false); } |
330 |
> |
public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); } |
331 |
> |
public void testWriteTryLock_Interruptible(boolean fair) { |
332 |
> |
final PublicReentrantReadWriteLock lock = |
333 |
> |
new PublicReentrantReadWriteLock(fair); |
334 |
> |
lock.writeLock().lock(); |
335 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
336 |
> |
public void realRun() throws InterruptedException { |
337 |
> |
lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
338 |
> |
}}); |
339 |
> |
|
340 |
> |
waitForQueuedThread(lock, t); |
341 |
> |
t.interrupt(); |
342 |
> |
awaitTermination(t); |
343 |
> |
releaseWriteLock(lock); |
344 |
> |
} |
345 |
|
|
346 |
< |
try { |
347 |
< |
t1.start(); |
348 |
< |
t2.start(); |
349 |
< |
Thread.sleep(SHORT_DELAY_MS); |
350 |
< |
lock.writeLock().unlock(); |
351 |
< |
t1.join(MEDIUM_DELAY_MS); |
352 |
< |
t2.join(MEDIUM_DELAY_MS); |
353 |
< |
assertTrue(!t1.isAlive()); |
354 |
< |
assertTrue(!t2.isAlive()); |
355 |
< |
|
356 |
< |
} catch(Exception e){ |
357 |
< |
unexpectedException(); |
358 |
< |
} |
359 |
< |
} |
346 |
> |
/** |
347 |
> |
* read-lockInterruptibly is interruptible |
348 |
> |
*/ |
349 |
> |
public void testReadLockInterruptibly_Interruptible() { testReadLockInterruptibly_Interruptible(false); } |
350 |
> |
public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); } |
351 |
> |
public void testReadLockInterruptibly_Interruptible(boolean fair) { |
352 |
> |
final PublicReentrantReadWriteLock lock = |
353 |
> |
new PublicReentrantReadWriteLock(fair); |
354 |
> |
lock.writeLock().lock(); |
355 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
356 |
> |
public void realRun() throws InterruptedException { |
357 |
> |
lock.readLock().lockInterruptibly(); |
358 |
> |
}}); |
359 |
> |
|
360 |
> |
waitForQueuedThread(lock, t); |
361 |
> |
t.interrupt(); |
362 |
> |
awaitTermination(t); |
363 |
> |
releaseWriteLock(lock); |
364 |
> |
} |
365 |
|
|
366 |
|
/** |
367 |
< |
* Read trylock succeeds if write locked by current thread |
367 |
> |
* timed read-tryLock is interruptible |
368 |
|
*/ |
369 |
< |
public void testReadHoldingWriteLock() { |
370 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
371 |
< |
lock.writeLock().lock(); |
372 |
< |
assertTrue(lock.readLock().tryLock()); |
373 |
< |
lock.readLock().unlock(); |
369 |
> |
public void testReadTryLock_Interruptible() { testReadTryLock_Interruptible(false); } |
370 |
> |
public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); } |
371 |
> |
public void testReadTryLock_Interruptible(boolean fair) { |
372 |
> |
final PublicReentrantReadWriteLock lock = |
373 |
> |
new PublicReentrantReadWriteLock(fair); |
374 |
> |
lock.writeLock().lock(); |
375 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
376 |
> |
public void realRun() throws InterruptedException { |
377 |
> |
lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
378 |
> |
}}); |
379 |
> |
|
380 |
> |
waitForQueuedThread(lock, t); |
381 |
> |
t.interrupt(); |
382 |
> |
awaitTermination(t); |
383 |
> |
releaseWriteLock(lock); |
384 |
> |
} |
385 |
> |
|
386 |
> |
/** |
387 |
> |
* write-tryLock on an unlocked lock succeeds |
388 |
> |
*/ |
389 |
> |
public void testWriteTryLock() { testWriteTryLock(false); } |
390 |
> |
public void testWriteTryLock_fair() { testWriteTryLock(true); } |
391 |
> |
public void testWriteTryLock(boolean fair) { |
392 |
> |
final PublicReentrantReadWriteLock lock = |
393 |
> |
new PublicReentrantReadWriteLock(fair); |
394 |
> |
assertTrue(lock.writeLock().tryLock()); |
395 |
> |
assertWriteLockedByMoi(lock); |
396 |
> |
assertTrue(lock.writeLock().tryLock()); |
397 |
> |
assertWriteLockedByMoi(lock); |
398 |
|
lock.writeLock().unlock(); |
399 |
< |
} |
399 |
> |
releaseWriteLock(lock); |
400 |
> |
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* Read lock succeeds if write locked by current thread even if |
426 |
< |
* other threads are waiting for readlock |
403 |
> |
* write-tryLock fails if locked |
404 |
|
*/ |
405 |
< |
public void testReadHoldingWriteLock2() { |
406 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
407 |
< |
lock.writeLock().lock(); |
408 |
< |
Thread t1 = new Thread(new Runnable() { |
409 |
< |
public void run() { |
410 |
< |
lock.readLock().lock(); |
411 |
< |
lock.readLock().unlock(); |
412 |
< |
} |
413 |
< |
}); |
414 |
< |
Thread t2 = new Thread(new Runnable() { |
438 |
< |
public void run() { |
439 |
< |
lock.readLock().lock(); |
440 |
< |
lock.readLock().unlock(); |
441 |
< |
} |
442 |
< |
}); |
405 |
> |
public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } |
406 |
> |
public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); } |
407 |
> |
public void testWriteTryLockWhenLocked(boolean fair) { |
408 |
> |
final PublicReentrantReadWriteLock lock = |
409 |
> |
new PublicReentrantReadWriteLock(fair); |
410 |
> |
lock.writeLock().lock(); |
411 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
412 |
> |
public void realRun() { |
413 |
> |
assertFalse(lock.writeLock().tryLock()); |
414 |
> |
}}); |
415 |
|
|
416 |
< |
try { |
417 |
< |
t1.start(); |
418 |
< |
t2.start(); |
447 |
< |
lock.readLock().lock(); |
448 |
< |
lock.readLock().unlock(); |
449 |
< |
Thread.sleep(SHORT_DELAY_MS); |
450 |
< |
lock.readLock().lock(); |
451 |
< |
lock.readLock().unlock(); |
452 |
< |
lock.writeLock().unlock(); |
453 |
< |
t1.join(MEDIUM_DELAY_MS); |
454 |
< |
t2.join(MEDIUM_DELAY_MS); |
455 |
< |
assertTrue(!t1.isAlive()); |
456 |
< |
assertTrue(!t2.isAlive()); |
457 |
< |
|
458 |
< |
} catch(Exception e){ |
459 |
< |
unexpectedException(); |
460 |
< |
} |
461 |
< |
} |
416 |
> |
awaitTermination(t); |
417 |
> |
releaseWriteLock(lock); |
418 |
> |
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* Read lock succeeds if write locked by current thread even if |
465 |
< |
* other threads are waiting for writelock |
421 |
> |
* read-tryLock fails if locked |
422 |
|
*/ |
423 |
< |
public void testReadHoldingWriteLock3() { |
424 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
425 |
< |
lock.writeLock().lock(); |
426 |
< |
Thread t1 = new Thread(new Runnable() { |
427 |
< |
public void run() { |
428 |
< |
lock.writeLock().lock(); |
429 |
< |
lock.writeLock().unlock(); |
430 |
< |
} |
431 |
< |
}); |
432 |
< |
Thread t2 = new Thread(new Runnable() { |
477 |
< |
public void run() { |
478 |
< |
lock.writeLock().lock(); |
479 |
< |
lock.writeLock().unlock(); |
480 |
< |
} |
481 |
< |
}); |
423 |
> |
public void testReadTryLockWhenLocked() { testReadTryLockWhenLocked(false); } |
424 |
> |
public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); } |
425 |
> |
public void testReadTryLockWhenLocked(boolean fair) { |
426 |
> |
final PublicReentrantReadWriteLock lock = |
427 |
> |
new PublicReentrantReadWriteLock(fair); |
428 |
> |
lock.writeLock().lock(); |
429 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
430 |
> |
public void realRun() { |
431 |
> |
assertFalse(lock.readLock().tryLock()); |
432 |
> |
}}); |
433 |
|
|
434 |
< |
try { |
435 |
< |
t1.start(); |
436 |
< |
t2.start(); |
437 |
< |
lock.readLock().lock(); |
438 |
< |
lock.readLock().unlock(); |
439 |
< |
Thread.sleep(SHORT_DELAY_MS); |
440 |
< |
lock.readLock().lock(); |
441 |
< |
lock.readLock().unlock(); |
442 |
< |
lock.writeLock().unlock(); |
443 |
< |
t1.join(MEDIUM_DELAY_MS); |
444 |
< |
t2.join(MEDIUM_DELAY_MS); |
445 |
< |
assertTrue(!t1.isAlive()); |
446 |
< |
assertTrue(!t2.isAlive()); |
447 |
< |
|
448 |
< |
} catch(Exception e){ |
449 |
< |
unexpectedException(); |
450 |
< |
} |
451 |
< |
} |
434 |
> |
awaitTermination(t); |
435 |
> |
releaseWriteLock(lock); |
436 |
> |
} |
437 |
> |
|
438 |
> |
/** |
439 |
> |
* Multiple threads can hold a read lock when not write-locked |
440 |
> |
*/ |
441 |
> |
public void testMultipleReadLocks() { testMultipleReadLocks(false); } |
442 |
> |
public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); } |
443 |
> |
public void testMultipleReadLocks(boolean fair) { |
444 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
445 |
> |
lock.readLock().lock(); |
446 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
447 |
> |
public void realRun() throws InterruptedException { |
448 |
> |
assertTrue(lock.readLock().tryLock()); |
449 |
> |
lock.readLock().unlock(); |
450 |
> |
assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS)); |
451 |
> |
lock.readLock().unlock(); |
452 |
> |
lock.readLock().lock(); |
453 |
> |
lock.readLock().unlock(); |
454 |
> |
}}); |
455 |
|
|
456 |
+ |
awaitTermination(t); |
457 |
+ |
lock.readLock().unlock(); |
458 |
+ |
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* Write lock succeeds if write locked by current thread even if |
505 |
< |
* other threads are waiting for writelock |
461 |
> |
* A writelock succeeds only after a reading thread unlocks |
462 |
|
*/ |
463 |
< |
public void testWriteHoldingWriteLock4() { |
464 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
465 |
< |
lock.writeLock().lock(); |
466 |
< |
Thread t1 = new Thread(new Runnable() { |
467 |
< |
public void run() { |
468 |
< |
lock.writeLock().lock(); |
469 |
< |
lock.writeLock().unlock(); |
470 |
< |
} |
471 |
< |
}); |
472 |
< |
Thread t2 = new Thread(new Runnable() { |
473 |
< |
public void run() { |
474 |
< |
lock.writeLock().lock(); |
475 |
< |
lock.writeLock().unlock(); |
476 |
< |
} |
477 |
< |
}); |
463 |
> |
public void testWriteAfterReadLock() { testWriteAfterReadLock(false); } |
464 |
> |
public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); } |
465 |
> |
public void testWriteAfterReadLock(boolean fair) { |
466 |
> |
final PublicReentrantReadWriteLock lock = |
467 |
> |
new PublicReentrantReadWriteLock(fair); |
468 |
> |
lock.readLock().lock(); |
469 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
470 |
> |
public void realRun() { |
471 |
> |
assertEquals(1, lock.getReadLockCount()); |
472 |
> |
lock.writeLock().lock(); |
473 |
> |
assertEquals(0, lock.getReadLockCount()); |
474 |
> |
lock.writeLock().unlock(); |
475 |
> |
}}); |
476 |
> |
waitForQueuedThread(lock, t); |
477 |
> |
assertNotWriteLocked(lock); |
478 |
> |
assertEquals(1, lock.getReadLockCount()); |
479 |
> |
lock.readLock().unlock(); |
480 |
> |
assertEquals(0, lock.getReadLockCount()); |
481 |
> |
awaitTermination(t); |
482 |
> |
assertNotWriteLocked(lock); |
483 |
> |
} |
484 |
|
|
485 |
< |
try { |
486 |
< |
t1.start(); |
487 |
< |
t2.start(); |
488 |
< |
lock.writeLock().lock(); |
489 |
< |
lock.writeLock().unlock(); |
490 |
< |
Thread.sleep(SHORT_DELAY_MS); |
491 |
< |
lock.writeLock().lock(); |
492 |
< |
lock.writeLock().unlock(); |
493 |
< |
lock.writeLock().unlock(); |
494 |
< |
t1.join(MEDIUM_DELAY_MS); |
495 |
< |
t2.join(MEDIUM_DELAY_MS); |
496 |
< |
assertTrue(!t1.isAlive()); |
497 |
< |
assertTrue(!t2.isAlive()); |
498 |
< |
|
499 |
< |
} catch(Exception e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
502 |
< |
} |
485 |
> |
/** |
486 |
> |
* A writelock succeeds only after reading threads unlock |
487 |
> |
*/ |
488 |
> |
public void testWriteAfterMultipleReadLocks() { testWriteAfterMultipleReadLocks(false); } |
489 |
> |
public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); } |
490 |
> |
public void testWriteAfterMultipleReadLocks(boolean fair) { |
491 |
> |
final PublicReentrantReadWriteLock lock = |
492 |
> |
new PublicReentrantReadWriteLock(fair); |
493 |
> |
lock.readLock().lock(); |
494 |
> |
lock.readLock().lock(); |
495 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
496 |
> |
public void realRun() { |
497 |
> |
lock.readLock().lock(); |
498 |
> |
assertEquals(3, lock.getReadLockCount()); |
499 |
> |
lock.readLock().unlock(); |
500 |
> |
}}); |
501 |
> |
awaitTermination(t1); |
502 |
> |
|
503 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
504 |
> |
public void realRun() { |
505 |
> |
assertEquals(2, lock.getReadLockCount()); |
506 |
> |
lock.writeLock().lock(); |
507 |
> |
assertEquals(0, lock.getReadLockCount()); |
508 |
> |
lock.writeLock().unlock(); |
509 |
> |
}}); |
510 |
> |
waitForQueuedThread(lock, t2); |
511 |
> |
assertNotWriteLocked(lock); |
512 |
> |
assertEquals(2, lock.getReadLockCount()); |
513 |
> |
lock.readLock().unlock(); |
514 |
> |
lock.readLock().unlock(); |
515 |
> |
assertEquals(0, lock.getReadLockCount()); |
516 |
> |
awaitTermination(t2); |
517 |
> |
assertNotWriteLocked(lock); |
518 |
> |
} |
519 |
|
|
520 |
+ |
/** |
521 |
+ |
* A thread that tries to acquire a fair read lock (non-reentrantly) |
522 |
+ |
* will block if there is a waiting writer thread |
523 |
+ |
*/ |
524 |
+ |
public void testReaderWriterReaderFairFifo() { |
525 |
+ |
final PublicReentrantReadWriteLock lock = |
526 |
+ |
new PublicReentrantReadWriteLock(true); |
527 |
+ |
final AtomicBoolean t1GotLock = new AtomicBoolean(false); |
528 |
+ |
|
529 |
+ |
lock.readLock().lock(); |
530 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
531 |
+ |
public void realRun() { |
532 |
+ |
assertEquals(1, lock.getReadLockCount()); |
533 |
+ |
lock.writeLock().lock(); |
534 |
+ |
assertEquals(0, lock.getReadLockCount()); |
535 |
+ |
t1GotLock.set(true); |
536 |
+ |
lock.writeLock().unlock(); |
537 |
+ |
}}); |
538 |
+ |
waitForQueuedThread(lock, t1); |
539 |
+ |
|
540 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
541 |
+ |
public void realRun() { |
542 |
+ |
assertEquals(1, lock.getReadLockCount()); |
543 |
+ |
lock.readLock().lock(); |
544 |
+ |
assertEquals(1, lock.getReadLockCount()); |
545 |
+ |
assertTrue(t1GotLock.get()); |
546 |
+ |
lock.readLock().unlock(); |
547 |
+ |
}}); |
548 |
+ |
waitForQueuedThread(lock, t2); |
549 |
+ |
assertTrue(t1.isAlive()); |
550 |
+ |
assertNotWriteLocked(lock); |
551 |
+ |
assertEquals(1, lock.getReadLockCount()); |
552 |
+ |
lock.readLock().unlock(); |
553 |
+ |
awaitTermination(t1); |
554 |
+ |
awaitTermination(t2); |
555 |
+ |
assertNotWriteLocked(lock); |
556 |
+ |
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* Fair Read trylock succeeds if write locked by current thread |
559 |
> |
* Readlocks succeed only after a writing thread unlocks |
560 |
|
*/ |
561 |
< |
public void testReadHoldingWriteLockFair() { |
562 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
563 |
< |
lock.writeLock().lock(); |
561 |
> |
public void testReadAfterWriteLock() { testReadAfterWriteLock(false); } |
562 |
> |
public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); } |
563 |
> |
public void testReadAfterWriteLock(boolean fair) { |
564 |
> |
final PublicReentrantReadWriteLock lock = |
565 |
> |
new PublicReentrantReadWriteLock(fair); |
566 |
> |
lock.writeLock().lock(); |
567 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
568 |
> |
public void realRun() { |
569 |
> |
lock.readLock().lock(); |
570 |
> |
lock.readLock().unlock(); |
571 |
> |
}}); |
572 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
573 |
> |
public void realRun() { |
574 |
> |
lock.readLock().lock(); |
575 |
> |
lock.readLock().unlock(); |
576 |
> |
}}); |
577 |
> |
|
578 |
> |
waitForQueuedThread(lock, t1); |
579 |
> |
waitForQueuedThread(lock, t2); |
580 |
> |
releaseWriteLock(lock); |
581 |
> |
awaitTermination(t1); |
582 |
> |
awaitTermination(t2); |
583 |
> |
} |
584 |
> |
|
585 |
> |
/** |
586 |
> |
* Read trylock succeeds if write locked by current thread |
587 |
> |
*/ |
588 |
> |
public void testReadHoldingWriteLock() { testReadHoldingWriteLock(false); } |
589 |
> |
public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); } |
590 |
> |
public void testReadHoldingWriteLock(boolean fair) { |
591 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
592 |
> |
lock.writeLock().lock(); |
593 |
|
assertTrue(lock.readLock().tryLock()); |
594 |
|
lock.readLock().unlock(); |
595 |
|
lock.writeLock().unlock(); |
596 |
< |
} |
596 |
> |
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* Fair Read lock succeeds if write locked by current thread even if |
600 |
< |
* other threads are waiting for readlock |
599 |
> |
* Read trylock succeeds (barging) even in the presence of waiting |
600 |
> |
* readers and/or writers |
601 |
|
*/ |
602 |
< |
public void testReadHoldingWriteLockFair2() { |
603 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
604 |
< |
lock.writeLock().lock(); |
605 |
< |
Thread t1 = new Thread(new Runnable() { |
606 |
< |
public void run() { |
607 |
< |
lock.readLock().lock(); |
564 |
< |
lock.readLock().unlock(); |
565 |
< |
} |
566 |
< |
}); |
567 |
< |
Thread t2 = new Thread(new Runnable() { |
568 |
< |
public void run() { |
569 |
< |
lock.readLock().lock(); |
570 |
< |
lock.readLock().unlock(); |
571 |
< |
} |
572 |
< |
}); |
602 |
> |
public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
603 |
> |
public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
604 |
> |
public void testReadTryLockBarging(boolean fair) { |
605 |
> |
final PublicReentrantReadWriteLock lock = |
606 |
> |
new PublicReentrantReadWriteLock(fair); |
607 |
> |
lock.readLock().lock(); |
608 |
|
|
609 |
< |
try { |
610 |
< |
t1.start(); |
611 |
< |
t2.start(); |
612 |
< |
lock.readLock().lock(); |
613 |
< |
lock.readLock().unlock(); |
579 |
< |
Thread.sleep(SHORT_DELAY_MS); |
580 |
< |
lock.readLock().lock(); |
581 |
< |
lock.readLock().unlock(); |
582 |
< |
lock.writeLock().unlock(); |
583 |
< |
t1.join(MEDIUM_DELAY_MS); |
584 |
< |
t2.join(MEDIUM_DELAY_MS); |
585 |
< |
assertTrue(!t1.isAlive()); |
586 |
< |
assertTrue(!t2.isAlive()); |
587 |
< |
|
588 |
< |
} catch(Exception e){ |
589 |
< |
unexpectedException(); |
590 |
< |
} |
591 |
< |
} |
609 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
610 |
> |
public void realRun() { |
611 |
> |
lock.writeLock().lock(); |
612 |
> |
lock.writeLock().unlock(); |
613 |
> |
}}); |
614 |
|
|
615 |
+ |
waitForQueuedThread(lock, t1); |
616 |
+ |
|
617 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
618 |
+ |
public void realRun() { |
619 |
+ |
lock.readLock().lock(); |
620 |
+ |
lock.readLock().unlock(); |
621 |
+ |
}}); |
622 |
+ |
|
623 |
+ |
if (fair) |
624 |
+ |
waitForQueuedThread(lock, t2); |
625 |
+ |
|
626 |
+ |
Thread t3 = newStartedThread(new CheckedRunnable() { |
627 |
+ |
public void realRun() { |
628 |
+ |
lock.readLock().tryLock(); |
629 |
+ |
lock.readLock().unlock(); |
630 |
+ |
}}); |
631 |
+ |
|
632 |
+ |
assertTrue(lock.getReadLockCount() > 0); |
633 |
+ |
awaitTermination(t3); |
634 |
+ |
assertTrue(t1.isAlive()); |
635 |
+ |
if (fair) assertTrue(t2.isAlive()); |
636 |
+ |
lock.readLock().unlock(); |
637 |
+ |
awaitTermination(t1); |
638 |
+ |
awaitTermination(t2); |
639 |
+ |
} |
640 |
|
|
641 |
|
/** |
642 |
< |
* Fair Read lock succeeds if write locked by current thread even if |
643 |
< |
* other threads are waiting for writelock |
642 |
> |
* Read lock succeeds if write locked by current thread even if |
643 |
> |
* other threads are waiting for readlock |
644 |
|
*/ |
645 |
< |
public void testReadHoldingWriteLockFair3() { |
646 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
647 |
< |
lock.writeLock().lock(); |
648 |
< |
Thread t1 = new Thread(new Runnable() { |
649 |
< |
public void run() { |
650 |
< |
lock.writeLock().lock(); |
651 |
< |
lock.writeLock().unlock(); |
652 |
< |
} |
606 |
< |
}); |
607 |
< |
Thread t2 = new Thread(new Runnable() { |
608 |
< |
public void run() { |
609 |
< |
lock.writeLock().lock(); |
610 |
< |
lock.writeLock().unlock(); |
611 |
< |
} |
612 |
< |
}); |
613 |
< |
|
614 |
< |
try { |
615 |
< |
t1.start(); |
616 |
< |
t2.start(); |
617 |
< |
lock.readLock().lock(); |
618 |
< |
lock.readLock().unlock(); |
619 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
< |
lock.readLock().lock(); |
621 |
< |
lock.readLock().unlock(); |
622 |
< |
lock.writeLock().unlock(); |
623 |
< |
t1.join(MEDIUM_DELAY_MS); |
624 |
< |
t2.join(MEDIUM_DELAY_MS); |
625 |
< |
assertTrue(!t1.isAlive()); |
626 |
< |
assertTrue(!t2.isAlive()); |
627 |
< |
|
628 |
< |
} catch(Exception e){ |
629 |
< |
unexpectedException(); |
630 |
< |
} |
631 |
< |
} |
645 |
> |
public void testReadHoldingWriteLock2() { testReadHoldingWriteLock2(false); } |
646 |
> |
public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); } |
647 |
> |
public void testReadHoldingWriteLock2(boolean fair) { |
648 |
> |
final PublicReentrantReadWriteLock lock = |
649 |
> |
new PublicReentrantReadWriteLock(fair); |
650 |
> |
lock.writeLock().lock(); |
651 |
> |
lock.readLock().lock(); |
652 |
> |
lock.readLock().unlock(); |
653 |
|
|
654 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
655 |
+ |
public void realRun() { |
656 |
+ |
lock.readLock().lock(); |
657 |
+ |
lock.readLock().unlock(); |
658 |
+ |
}}); |
659 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
660 |
+ |
public void realRun() { |
661 |
+ |
lock.readLock().lock(); |
662 |
+ |
lock.readLock().unlock(); |
663 |
+ |
}}); |
664 |
+ |
|
665 |
+ |
waitForQueuedThread(lock, t1); |
666 |
+ |
waitForQueuedThread(lock, t2); |
667 |
+ |
assertWriteLockedByMoi(lock); |
668 |
+ |
lock.readLock().lock(); |
669 |
+ |
lock.readLock().unlock(); |
670 |
+ |
releaseWriteLock(lock); |
671 |
+ |
awaitTermination(t1); |
672 |
+ |
awaitTermination(t2); |
673 |
+ |
} |
674 |
|
|
675 |
|
/** |
676 |
< |
* Fair Write lock succeeds if write locked by current thread even if |
676 |
> |
* Read lock succeeds if write locked by current thread even if |
677 |
|
* other threads are waiting for writelock |
678 |
|
*/ |
679 |
< |
public void testWriteHoldingWriteLockFair4() { |
680 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
681 |
< |
lock.writeLock().lock(); |
682 |
< |
Thread t1 = new Thread(new Runnable() { |
683 |
< |
public void run() { |
684 |
< |
lock.writeLock().lock(); |
685 |
< |
lock.writeLock().unlock(); |
686 |
< |
} |
646 |
< |
}); |
647 |
< |
Thread t2 = new Thread(new Runnable() { |
648 |
< |
public void run() { |
649 |
< |
lock.writeLock().lock(); |
650 |
< |
lock.writeLock().unlock(); |
651 |
< |
} |
652 |
< |
}); |
679 |
> |
public void testReadHoldingWriteLock3() { testReadHoldingWriteLock3(false); } |
680 |
> |
public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); } |
681 |
> |
public void testReadHoldingWriteLock3(boolean fair) { |
682 |
> |
final PublicReentrantReadWriteLock lock = |
683 |
> |
new PublicReentrantReadWriteLock(fair); |
684 |
> |
lock.writeLock().lock(); |
685 |
> |
lock.readLock().lock(); |
686 |
> |
lock.readLock().unlock(); |
687 |
|
|
688 |
< |
try { |
689 |
< |
t1.start(); |
690 |
< |
t2.start(); |
691 |
< |
Thread.sleep(SHORT_DELAY_MS); |
692 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
693 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
694 |
< |
lock.writeLock().lock(); |
695 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
696 |
< |
lock.writeLock().unlock(); |
697 |
< |
lock.writeLock().lock(); |
664 |
< |
lock.writeLock().unlock(); |
665 |
< |
lock.writeLock().unlock(); |
666 |
< |
t1.join(MEDIUM_DELAY_MS); |
667 |
< |
t2.join(MEDIUM_DELAY_MS); |
668 |
< |
assertTrue(!t1.isAlive()); |
669 |
< |
assertTrue(!t2.isAlive()); |
670 |
< |
|
671 |
< |
} catch(Exception e){ |
672 |
< |
unexpectedException(); |
673 |
< |
} |
674 |
< |
} |
688 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
689 |
> |
public void realRun() { |
690 |
> |
lock.writeLock().lock(); |
691 |
> |
lock.writeLock().unlock(); |
692 |
> |
}}); |
693 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
694 |
> |
public void realRun() { |
695 |
> |
lock.writeLock().lock(); |
696 |
> |
lock.writeLock().unlock(); |
697 |
> |
}}); |
698 |
|
|
699 |
+ |
waitForQueuedThread(lock, t1); |
700 |
+ |
waitForQueuedThread(lock, t2); |
701 |
+ |
assertWriteLockedByMoi(lock); |
702 |
+ |
lock.readLock().lock(); |
703 |
+ |
lock.readLock().unlock(); |
704 |
+ |
assertWriteLockedByMoi(lock); |
705 |
+ |
lock.writeLock().unlock(); |
706 |
+ |
awaitTermination(t1); |
707 |
+ |
awaitTermination(t2); |
708 |
+ |
} |
709 |
|
|
710 |
|
/** |
711 |
< |
* Read tryLock succeeds if readlocked but not writelocked |
711 |
> |
* Write lock succeeds if write locked by current thread even if |
712 |
> |
* other threads are waiting for writelock |
713 |
|
*/ |
714 |
< |
public void testTryLockWhenReadLocked() { |
715 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
716 |
< |
lock.readLock().lock(); |
717 |
< |
Thread t = new Thread(new Runnable() { |
718 |
< |
public void run() { |
719 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
720 |
< |
lock.readLock().unlock(); |
721 |
< |
} |
688 |
< |
}); |
689 |
< |
try { |
690 |
< |
t.start(); |
691 |
< |
t.join(); |
692 |
< |
lock.readLock().unlock(); |
693 |
< |
} catch(Exception e){ |
694 |
< |
unexpectedException(); |
695 |
< |
} |
696 |
< |
} |
697 |
< |
|
698 |
< |
|
714 |
> |
public void testWriteHoldingWriteLock4() { testWriteHoldingWriteLock4(false); } |
715 |
> |
public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); } |
716 |
> |
public void testWriteHoldingWriteLock4(boolean fair) { |
717 |
> |
final PublicReentrantReadWriteLock lock = |
718 |
> |
new PublicReentrantReadWriteLock(fair); |
719 |
> |
lock.writeLock().lock(); |
720 |
> |
lock.writeLock().lock(); |
721 |
> |
lock.writeLock().unlock(); |
722 |
|
|
723 |
< |
/** |
724 |
< |
* write tryLock fails when readlocked |
725 |
< |
*/ |
726 |
< |
public void testWriteTryLockWhenReadLocked() { |
727 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
728 |
< |
lock.readLock().lock(); |
729 |
< |
Thread t = new Thread(new Runnable() { |
730 |
< |
public void run() { |
731 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
732 |
< |
} |
710 |
< |
}); |
711 |
< |
try { |
712 |
< |
t.start(); |
713 |
< |
t.join(); |
714 |
< |
lock.readLock().unlock(); |
715 |
< |
} catch(Exception e){ |
716 |
< |
unexpectedException(); |
717 |
< |
} |
718 |
< |
} |
723 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
724 |
> |
public void realRun() { |
725 |
> |
lock.writeLock().lock(); |
726 |
> |
lock.writeLock().unlock(); |
727 |
> |
}}); |
728 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
729 |
> |
public void realRun() { |
730 |
> |
lock.writeLock().lock(); |
731 |
> |
lock.writeLock().unlock(); |
732 |
> |
}}); |
733 |
|
|
734 |
+ |
waitForQueuedThread(lock, t1); |
735 |
+ |
waitForQueuedThread(lock, t2); |
736 |
+ |
assertWriteLockedByMoi(lock); |
737 |
+ |
assertEquals(1, lock.getWriteHoldCount()); |
738 |
+ |
lock.writeLock().lock(); |
739 |
+ |
assertWriteLockedByMoi(lock); |
740 |
+ |
assertEquals(2, lock.getWriteHoldCount()); |
741 |
+ |
lock.writeLock().unlock(); |
742 |
+ |
assertWriteLockedByMoi(lock); |
743 |
+ |
assertEquals(1, lock.getWriteHoldCount()); |
744 |
+ |
lock.writeLock().unlock(); |
745 |
+ |
awaitTermination(t1); |
746 |
+ |
awaitTermination(t2); |
747 |
+ |
} |
748 |
|
|
749 |
|
/** |
750 |
< |
* Fair Read tryLock succeeds if readlocked but not writelocked |
750 |
> |
* Read tryLock succeeds if readlocked but not writelocked |
751 |
|
*/ |
752 |
< |
public void testTryLockWhenReadLockedFair() { |
753 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
754 |
< |
lock.readLock().lock(); |
755 |
< |
Thread t = new Thread(new Runnable() { |
756 |
< |
public void run() { |
757 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
758 |
< |
lock.readLock().unlock(); |
759 |
< |
} |
760 |
< |
}); |
761 |
< |
try { |
734 |
< |
t.start(); |
735 |
< |
t.join(); |
736 |
< |
lock.readLock().unlock(); |
737 |
< |
} catch(Exception e){ |
738 |
< |
unexpectedException(); |
739 |
< |
} |
740 |
< |
} |
752 |
> |
public void testTryLockWhenReadLocked() { testTryLockWhenReadLocked(false); } |
753 |
> |
public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); } |
754 |
> |
public void testTryLockWhenReadLocked(boolean fair) { |
755 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
756 |
> |
lock.readLock().lock(); |
757 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
758 |
> |
public void realRun() { |
759 |
> |
assertTrue(lock.readLock().tryLock()); |
760 |
> |
lock.readLock().unlock(); |
761 |
> |
}}); |
762 |
|
|
763 |
< |
|
763 |
> |
awaitTermination(t); |
764 |
> |
lock.readLock().unlock(); |
765 |
> |
} |
766 |
|
|
767 |
|
/** |
768 |
< |
* Fair write tryLock fails when readlocked |
768 |
> |
* write tryLock fails when readlocked |
769 |
|
*/ |
770 |
< |
public void testWriteTryLockWhenReadLockedFair() { |
771 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
772 |
< |
lock.readLock().lock(); |
773 |
< |
Thread t = new Thread(new Runnable() { |
774 |
< |
public void run() { |
775 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
776 |
< |
} |
777 |
< |
}); |
778 |
< |
try { |
756 |
< |
t.start(); |
757 |
< |
t.join(); |
758 |
< |
lock.readLock().unlock(); |
759 |
< |
} catch(Exception e){ |
760 |
< |
unexpectedException(); |
761 |
< |
} |
762 |
< |
} |
770 |
> |
public void testWriteTryLockWhenReadLocked() { testWriteTryLockWhenReadLocked(false); } |
771 |
> |
public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); } |
772 |
> |
public void testWriteTryLockWhenReadLocked(boolean fair) { |
773 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
774 |
> |
lock.readLock().lock(); |
775 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
776 |
> |
public void realRun() { |
777 |
> |
assertFalse(lock.writeLock().tryLock()); |
778 |
> |
}}); |
779 |
|
|
780 |
< |
|
780 |
> |
awaitTermination(t); |
781 |
> |
lock.readLock().unlock(); |
782 |
> |
} |
783 |
|
|
784 |
|
/** |
785 |
|
* write timed tryLock times out if locked |
786 |
|
*/ |
787 |
< |
public void testWriteTryLock_Timeout() { |
788 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
789 |
< |
lock.writeLock().lock(); |
790 |
< |
Thread t = new Thread(new Runnable() { |
791 |
< |
public void run() { |
792 |
< |
try { |
793 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
794 |
< |
} catch (Exception ex) { |
795 |
< |
threadUnexpectedException(); |
796 |
< |
} |
797 |
< |
} |
798 |
< |
}); |
799 |
< |
try { |
800 |
< |
t.start(); |
801 |
< |
t.join(); |
802 |
< |
lock.writeLock().unlock(); |
803 |
< |
} catch(Exception e){ |
786 |
< |
unexpectedException(); |
787 |
< |
} |
788 |
< |
} |
787 |
> |
public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } |
788 |
> |
public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } |
789 |
> |
public void testWriteTryLock_Timeout(boolean fair) { |
790 |
> |
final PublicReentrantReadWriteLock lock = |
791 |
> |
new PublicReentrantReadWriteLock(fair); |
792 |
> |
final long timeoutMillis = timeoutMillis(); |
793 |
> |
lock.writeLock().lock(); |
794 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
795 |
> |
public void realRun() throws InterruptedException { |
796 |
> |
long startTime = System.nanoTime(); |
797 |
> |
assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS)); |
798 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
799 |
> |
}}); |
800 |
> |
|
801 |
> |
awaitTermination(t); |
802 |
> |
releaseWriteLock(lock); |
803 |
> |
} |
804 |
|
|
805 |
|
/** |
806 |
|
* read timed tryLock times out if write-locked |
807 |
|
*/ |
808 |
< |
public void testReadTryLock_Timeout() { |
809 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
810 |
< |
lock.writeLock().lock(); |
811 |
< |
Thread t = new Thread(new Runnable() { |
812 |
< |
public void run() { |
813 |
< |
try { |
814 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
815 |
< |
} catch (Exception ex) { |
816 |
< |
threadUnexpectedException(); |
817 |
< |
} |
818 |
< |
} |
819 |
< |
}); |
805 |
< |
try { |
806 |
< |
t.start(); |
807 |
< |
t.join(); |
808 |
< |
lock.writeLock().unlock(); |
809 |
< |
} catch(Exception e){ |
810 |
< |
unexpectedException(); |
811 |
< |
} |
812 |
< |
} |
808 |
> |
public void testReadTryLock_Timeout() { testReadTryLock_Timeout(false); } |
809 |
> |
public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); } |
810 |
> |
public void testReadTryLock_Timeout(boolean fair) { |
811 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
812 |
> |
lock.writeLock().lock(); |
813 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
814 |
> |
public void realRun() throws InterruptedException { |
815 |
> |
long startTime = System.nanoTime(); |
816 |
> |
long timeoutMillis = timeoutMillis(); |
817 |
> |
assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS)); |
818 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
819 |
> |
}}); |
820 |
|
|
821 |
+ |
awaitTermination(t); |
822 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
823 |
+ |
lock.writeLock().unlock(); |
824 |
+ |
} |
825 |
|
|
826 |
|
/** |
827 |
< |
* write lockInterruptibly succeeds if lock free else is interruptible |
827 |
> |
* write lockInterruptibly succeeds if unlocked, else is interruptible |
828 |
|
*/ |
829 |
< |
public void testWriteLockInterruptibly() { |
830 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
831 |
< |
try { |
832 |
< |
lock.writeLock().lockInterruptibly(); |
833 |
< |
} catch(Exception e) { |
823 |
< |
unexpectedException(); |
824 |
< |
} |
825 |
< |
Thread t = new Thread(new Runnable() { |
826 |
< |
public void run() { |
827 |
< |
try { |
828 |
< |
lock.writeLock().lockInterruptibly(); |
829 |
< |
threadShouldThrow(); |
830 |
< |
} |
831 |
< |
catch(InterruptedException success) { |
832 |
< |
} |
833 |
< |
} |
834 |
< |
}); |
829 |
> |
public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } |
830 |
> |
public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } |
831 |
> |
public void testWriteLockInterruptibly(boolean fair) { |
832 |
> |
final PublicReentrantReadWriteLock lock = |
833 |
> |
new PublicReentrantReadWriteLock(fair); |
834 |
|
try { |
835 |
< |
t.start(); |
836 |
< |
t.interrupt(); |
837 |
< |
t.join(); |
838 |
< |
lock.writeLock().unlock(); |
839 |
< |
} catch(Exception e){ |
840 |
< |
unexpectedException(); |
841 |
< |
} |
835 |
> |
lock.writeLock().lockInterruptibly(); |
836 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
837 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
838 |
> |
public void realRun() throws InterruptedException { |
839 |
> |
lock.writeLock().lockInterruptibly(); |
840 |
> |
}}); |
841 |
> |
|
842 |
> |
waitForQueuedThread(lock, t); |
843 |
> |
t.interrupt(); |
844 |
> |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
845 |
> |
awaitTermination(t); |
846 |
> |
releaseWriteLock(lock); |
847 |
|
} |
848 |
|
|
849 |
|
/** |
850 |
< |
* read lockInterruptibly succeeds if lock free else is interruptible |
850 |
> |
* read lockInterruptibly succeeds if lock free else is interruptible |
851 |
|
*/ |
852 |
< |
public void testReadLockInterruptibly() { |
853 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
854 |
< |
try { |
855 |
< |
lock.writeLock().lockInterruptibly(); |
856 |
< |
} catch(Exception e) { |
853 |
< |
unexpectedException(); |
854 |
< |
} |
855 |
< |
Thread t = new Thread(new Runnable() { |
856 |
< |
public void run() { |
857 |
< |
try { |
858 |
< |
lock.readLock().lockInterruptibly(); |
859 |
< |
threadShouldThrow(); |
860 |
< |
} |
861 |
< |
catch(InterruptedException success) { |
862 |
< |
} |
863 |
< |
} |
864 |
< |
}); |
852 |
> |
public void testReadLockInterruptibly() { testReadLockInterruptibly(false); } |
853 |
> |
public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); } |
854 |
> |
public void testReadLockInterruptibly(boolean fair) { |
855 |
> |
final PublicReentrantReadWriteLock lock = |
856 |
> |
new PublicReentrantReadWriteLock(fair); |
857 |
|
try { |
858 |
< |
t.start(); |
859 |
< |
t.interrupt(); |
860 |
< |
t.join(); |
861 |
< |
lock.writeLock().unlock(); |
862 |
< |
} catch(Exception e){ |
863 |
< |
unexpectedException(); |
864 |
< |
} |
858 |
> |
lock.readLock().lockInterruptibly(); |
859 |
> |
lock.readLock().unlock(); |
860 |
> |
lock.writeLock().lockInterruptibly(); |
861 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
862 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
863 |
> |
public void realRun() throws InterruptedException { |
864 |
> |
lock.readLock().lockInterruptibly(); |
865 |
> |
}}); |
866 |
> |
|
867 |
> |
waitForQueuedThread(lock, t); |
868 |
> |
t.interrupt(); |
869 |
> |
awaitTermination(t); |
870 |
> |
releaseWriteLock(lock); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
874 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
875 |
|
*/ |
876 |
< |
public void testAwait_IllegalMonitor() { |
877 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
876 |
> |
public void testAwait_IMSE() { testAwait_IMSE(false); } |
877 |
> |
public void testAwait_IMSE_fair() { testAwait_IMSE(true); } |
878 |
> |
public void testAwait_IMSE(boolean fair) { |
879 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
880 |
|
final Condition c = lock.writeLock().newCondition(); |
881 |
< |
try { |
882 |
< |
c.await(); |
883 |
< |
shouldThrow(); |
884 |
< |
} |
885 |
< |
catch (IllegalMonitorStateException success) { |
886 |
< |
} |
887 |
< |
catch (Exception ex) { |
888 |
< |
shouldThrow(); |
881 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
882 |
> |
long startTime = System.nanoTime(); |
883 |
> |
try { |
884 |
> |
await(c, awaitMethod); |
885 |
> |
shouldThrow(); |
886 |
> |
} catch (IllegalMonitorStateException success) { |
887 |
> |
} catch (InterruptedException fail) { |
888 |
> |
threadUnexpectedException(fail); |
889 |
> |
} |
890 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
891 |
|
} |
892 |
|
} |
893 |
|
|
894 |
|
/** |
895 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
896 |
|
*/ |
897 |
< |
public void testSignal_IllegalMonitor() { |
898 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
897 |
> |
public void testSignal_IMSE() { testSignal_IMSE(false); } |
898 |
> |
public void testSignal_IMSE_fair() { testSignal_IMSE(true); } |
899 |
> |
public void testSignal_IMSE(boolean fair) { |
900 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
901 |
|
final Condition c = lock.writeLock().newCondition(); |
902 |
|
try { |
903 |
|
c.signal(); |
904 |
|
shouldThrow(); |
905 |
< |
} |
902 |
< |
catch (IllegalMonitorStateException success) { |
903 |
< |
} |
904 |
< |
catch (Exception ex) { |
905 |
< |
unexpectedException(); |
906 |
< |
} |
905 |
> |
} catch (IllegalMonitorStateException success) {} |
906 |
|
} |
907 |
|
|
908 |
|
/** |
909 |
< |
* awaitNanos without a signal times out |
909 |
> |
* Calling signalAll without holding lock throws IllegalMonitorStateException |
910 |
|
*/ |
911 |
< |
public void testAwaitNanos_Timeout() { |
912 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
911 |
> |
public void testSignalAll_IMSE() { testSignalAll_IMSE(false); } |
912 |
> |
public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); } |
913 |
> |
public void testSignalAll_IMSE(boolean fair) { |
914 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
915 |
|
final Condition c = lock.writeLock().newCondition(); |
916 |
|
try { |
917 |
< |
lock.writeLock().lock(); |
918 |
< |
long t = c.awaitNanos(100); |
919 |
< |
assertTrue(t <= 0); |
919 |
< |
lock.writeLock().unlock(); |
920 |
< |
} |
921 |
< |
catch (Exception ex) { |
922 |
< |
unexpectedException(); |
923 |
< |
} |
917 |
> |
c.signalAll(); |
918 |
> |
shouldThrow(); |
919 |
> |
} catch (IllegalMonitorStateException success) {} |
920 |
|
} |
921 |
|
|
926 |
– |
|
922 |
|
/** |
923 |
< |
* timed await without a signal times out |
923 |
> |
* awaitNanos without a signal times out |
924 |
|
*/ |
925 |
< |
public void testAwait_Timeout() { |
926 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
925 |
> |
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
926 |
> |
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
927 |
> |
public void testAwaitNanos_Timeout(boolean fair) { |
928 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
929 |
|
final Condition c = lock.writeLock().newCondition(); |
930 |
+ |
final long timeoutMillis = timeoutMillis(); |
931 |
+ |
lock.writeLock().lock(); |
932 |
+ |
final long startTime = System.nanoTime(); |
933 |
+ |
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
934 |
|
try { |
935 |
< |
lock.writeLock().lock(); |
936 |
< |
lock.writeLock().unlock(); |
937 |
< |
} |
938 |
< |
catch (Exception ex) { |
939 |
< |
unexpectedException(); |
939 |
< |
} |
935 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
936 |
> |
assertTrue(nanosRemaining <= 0); |
937 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
938 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
939 |
> |
lock.writeLock().unlock(); |
940 |
|
} |
941 |
|
|
942 |
|
/** |
943 |
< |
* awaitUntil without a signal times out |
943 |
> |
* timed await without a signal times out |
944 |
|
*/ |
945 |
< |
public void testAwaitUntil_Timeout() { |
946 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
945 |
> |
public void testAwait_Timeout() { testAwait_Timeout(false); } |
946 |
> |
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
947 |
> |
public void testAwait_Timeout(boolean fair) { |
948 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
949 |
|
final Condition c = lock.writeLock().newCondition(); |
950 |
+ |
final long timeoutMillis = timeoutMillis(); |
951 |
+ |
lock.writeLock().lock(); |
952 |
+ |
final long startTime = System.nanoTime(); |
953 |
|
try { |
954 |
< |
lock.writeLock().lock(); |
955 |
< |
java.util.Date d = new java.util.Date(); |
956 |
< |
lock.writeLock().unlock(); |
957 |
< |
} |
953 |
< |
catch (Exception ex) { |
954 |
< |
unexpectedException(); |
955 |
< |
} |
954 |
> |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
955 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
956 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
957 |
> |
lock.writeLock().unlock(); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
961 |
< |
* await returns when signalled |
961 |
> |
* awaitUntil without a signal times out |
962 |
|
*/ |
963 |
< |
public void testAwait() { |
964 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
963 |
> |
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
964 |
> |
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
965 |
> |
public void testAwaitUntil_Timeout(boolean fair) { |
966 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
967 |
|
final Condition c = lock.writeLock().newCondition(); |
968 |
< |
Thread t = new Thread(new Runnable() { |
969 |
< |
public void run() { |
970 |
< |
try { |
971 |
< |
lock.writeLock().lock(); |
972 |
< |
c.await(); |
973 |
< |
lock.writeLock().unlock(); |
974 |
< |
} |
975 |
< |
catch(InterruptedException e) { |
976 |
< |
threadUnexpectedException(); |
973 |
< |
} |
974 |
< |
} |
975 |
< |
}); |
976 |
< |
|
977 |
< |
try { |
978 |
< |
t.start(); |
979 |
< |
Thread.sleep(SHORT_DELAY_MS); |
980 |
< |
lock.writeLock().lock(); |
981 |
< |
c.signal(); |
982 |
< |
lock.writeLock().unlock(); |
983 |
< |
t.join(SHORT_DELAY_MS); |
984 |
< |
assertFalse(t.isAlive()); |
985 |
< |
} |
986 |
< |
catch (Exception ex) { |
987 |
< |
unexpectedException(); |
988 |
< |
} |
989 |
< |
} |
990 |
< |
|
991 |
< |
/** A helper class for uninterruptible wait tests */ |
992 |
< |
class UninterruptableThread extends Thread { |
993 |
< |
private Lock lock; |
994 |
< |
private Condition c; |
995 |
< |
|
996 |
< |
public volatile boolean canAwake = false; |
997 |
< |
public volatile boolean interrupted = false; |
998 |
< |
public volatile boolean lockStarted = false; |
999 |
< |
|
1000 |
< |
public UninterruptableThread(Lock lock, Condition c) { |
1001 |
< |
this.lock = lock; |
1002 |
< |
this.c = c; |
1003 |
< |
} |
1004 |
< |
|
1005 |
< |
public synchronized void run() { |
1006 |
< |
lock.lock(); |
1007 |
< |
lockStarted = true; |
1008 |
< |
|
1009 |
< |
while (!canAwake) { |
1010 |
< |
c.awaitUninterruptibly(); |
1011 |
< |
} |
1012 |
< |
|
1013 |
< |
interrupted = isInterrupted(); |
1014 |
< |
lock.unlock(); |
1015 |
< |
} |
968 |
> |
lock.writeLock().lock(); |
969 |
> |
// We shouldn't assume that nanoTime and currentTimeMillis |
970 |
> |
// use the same time source, so don't use nanoTime here. |
971 |
> |
final java.util.Date delayedDate = delayedDate(timeoutMillis()); |
972 |
> |
try { |
973 |
> |
assertFalse(c.awaitUntil(delayedDate)); |
974 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
975 |
> |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
976 |
> |
lock.writeLock().unlock(); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
980 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
980 |
> |
* await returns when signalled |
981 |
|
*/ |
982 |
< |
public void testAwaitUninterruptibly() { |
983 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
982 |
> |
public void testAwait() { testAwait(false); } |
983 |
> |
public void testAwait_fair() { testAwait(true); } |
984 |
> |
public void testAwait(boolean fair) { |
985 |
> |
final PublicReentrantReadWriteLock lock = |
986 |
> |
new PublicReentrantReadWriteLock(fair); |
987 |
|
final Condition c = lock.writeLock().newCondition(); |
988 |
< |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
989 |
< |
|
990 |
< |
try { |
991 |
< |
thread.start(); |
992 |
< |
|
993 |
< |
while (!thread.lockStarted) { |
1030 |
< |
Thread.sleep(100); |
1031 |
< |
} |
1032 |
< |
|
1033 |
< |
lock.writeLock().lock(); |
1034 |
< |
try { |
1035 |
< |
thread.interrupt(); |
1036 |
< |
thread.canAwake = true; |
1037 |
< |
c.signal(); |
1038 |
< |
} finally { |
988 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
989 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
990 |
> |
public void realRun() throws InterruptedException { |
991 |
> |
lock.writeLock().lock(); |
992 |
> |
locked.countDown(); |
993 |
> |
c.await(); |
994 |
|
lock.writeLock().unlock(); |
995 |
< |
} |
995 |
> |
}}); |
996 |
|
|
997 |
< |
thread.join(); |
998 |
< |
assertTrue(thread.interrupted); |
999 |
< |
assertFalse(thread.isAlive()); |
1000 |
< |
} catch (Exception ex) { |
1001 |
< |
unexpectedException(); |
1002 |
< |
} |
997 |
> |
await(locked); |
998 |
> |
lock.writeLock().lock(); |
999 |
> |
assertHasWaiters(lock, c, t); |
1000 |
> |
c.signal(); |
1001 |
> |
assertHasNoWaiters(lock, c); |
1002 |
> |
assertTrue(t.isAlive()); |
1003 |
> |
lock.writeLock().unlock(); |
1004 |
> |
awaitTermination(t); |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1008 |
< |
* await is interruptible |
1008 |
> |
* awaitUninterruptibly is uninterruptible |
1009 |
|
*/ |
1010 |
< |
public void testAwait_Interrupt() { |
1011 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1012 |
< |
final Condition c = lock.writeLock().newCondition(); |
1013 |
< |
Thread t = new Thread(new Runnable() { |
1014 |
< |
public void run() { |
1015 |
< |
try { |
1016 |
< |
lock.writeLock().lock(); |
1017 |
< |
c.await(); |
1018 |
< |
lock.writeLock().unlock(); |
1019 |
< |
threadShouldThrow(); |
1020 |
< |
} |
1021 |
< |
catch(InterruptedException success) { |
1022 |
< |
} |
1023 |
< |
} |
1024 |
< |
}); |
1010 |
> |
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1011 |
> |
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1012 |
> |
public void testAwaitUninterruptibly(boolean fair) { |
1013 |
> |
final Lock lock = new ReentrantReadWriteLock(fair).writeLock(); |
1014 |
> |
final Condition condition = lock.newCondition(); |
1015 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1016 |
> |
|
1017 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1018 |
> |
public void realRun() { |
1019 |
> |
// Interrupt before awaitUninterruptibly |
1020 |
> |
lock.lock(); |
1021 |
> |
pleaseInterrupt.countDown(); |
1022 |
> |
Thread.currentThread().interrupt(); |
1023 |
> |
condition.awaitUninterruptibly(); |
1024 |
> |
assertTrue(Thread.interrupted()); |
1025 |
> |
lock.unlock(); |
1026 |
> |
}}); |
1027 |
> |
|
1028 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1029 |
> |
public void realRun() { |
1030 |
> |
// Interrupt during awaitUninterruptibly |
1031 |
> |
lock.lock(); |
1032 |
> |
pleaseInterrupt.countDown(); |
1033 |
> |
condition.awaitUninterruptibly(); |
1034 |
> |
assertTrue(Thread.interrupted()); |
1035 |
> |
lock.unlock(); |
1036 |
> |
}}); |
1037 |
> |
|
1038 |
> |
await(pleaseInterrupt); |
1039 |
> |
t2.interrupt(); |
1040 |
> |
lock.lock(); |
1041 |
> |
lock.unlock(); |
1042 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
1043 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
1044 |
> |
|
1045 |
> |
lock.lock(); |
1046 |
> |
condition.signalAll(); |
1047 |
> |
lock.unlock(); |
1048 |
> |
|
1049 |
> |
awaitTermination(t1); |
1050 |
> |
awaitTermination(t2); |
1051 |
> |
} |
1052 |
|
|
1053 |
< |
try { |
1054 |
< |
t.start(); |
1055 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1056 |
< |
t.interrupt(); |
1057 |
< |
t.join(SHORT_DELAY_MS); |
1058 |
< |
assertFalse(t.isAlive()); |
1059 |
< |
} |
1060 |
< |
catch (Exception ex) { |
1061 |
< |
unexpectedException(); |
1062 |
< |
} |
1053 |
> |
/** |
1054 |
> |
* await/awaitNanos/awaitUntil is interruptible |
1055 |
> |
*/ |
1056 |
> |
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1057 |
> |
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1058 |
> |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1059 |
> |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1060 |
> |
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1061 |
> |
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1062 |
> |
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1063 |
> |
public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); } |
1064 |
> |
public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) { |
1065 |
> |
final PublicReentrantReadWriteLock lock = |
1066 |
> |
new PublicReentrantReadWriteLock(fair); |
1067 |
> |
final Condition c = lock.writeLock().newCondition(); |
1068 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
1069 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1070 |
> |
public void realRun() throws InterruptedException { |
1071 |
> |
lock.writeLock().lock(); |
1072 |
> |
assertWriteLockedByMoi(lock); |
1073 |
> |
assertHasNoWaiters(lock, c); |
1074 |
> |
locked.countDown(); |
1075 |
> |
try { |
1076 |
> |
await(c, awaitMethod); |
1077 |
> |
} finally { |
1078 |
> |
assertWriteLockedByMoi(lock); |
1079 |
> |
assertHasNoWaiters(lock, c); |
1080 |
> |
lock.writeLock().unlock(); |
1081 |
> |
assertFalse(Thread.interrupted()); |
1082 |
> |
} |
1083 |
> |
}}); |
1084 |
> |
|
1085 |
> |
await(locked); |
1086 |
> |
assertHasWaiters(lock, c, t); |
1087 |
> |
t.interrupt(); |
1088 |
> |
awaitTermination(t); |
1089 |
> |
assertNotWriteLocked(lock); |
1090 |
|
} |
1091 |
|
|
1092 |
|
/** |
1093 |
< |
* awaitNanos is interruptible |
1093 |
> |
* signalAll wakes up all threads |
1094 |
|
*/ |
1095 |
< |
public void testAwaitNanos_Interrupt() { |
1096 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1095 |
> |
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1096 |
> |
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1097 |
> |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1098 |
> |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1099 |
> |
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1100 |
> |
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1101 |
> |
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1102 |
> |
public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); } |
1103 |
> |
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
1104 |
> |
final PublicReentrantReadWriteLock lock = |
1105 |
> |
new PublicReentrantReadWriteLock(fair); |
1106 |
|
final Condition c = lock.writeLock().newCondition(); |
1107 |
< |
Thread t = new Thread(new Runnable() { |
1108 |
< |
public void run() { |
1109 |
< |
try { |
1110 |
< |
lock.writeLock().lock(); |
1111 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
1112 |
< |
lock.writeLock().unlock(); |
1113 |
< |
threadShouldThrow(); |
1114 |
< |
} |
1115 |
< |
catch(InterruptedException success) { |
1096 |
< |
} |
1097 |
< |
} |
1098 |
< |
}); |
1099 |
< |
|
1100 |
< |
try { |
1101 |
< |
t.start(); |
1102 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1103 |
< |
t.interrupt(); |
1104 |
< |
t.join(SHORT_DELAY_MS); |
1105 |
< |
assertFalse(t.isAlive()); |
1106 |
< |
} |
1107 |
< |
catch (Exception ex) { |
1108 |
< |
unexpectedException(); |
1107 |
> |
final CountDownLatch locked = new CountDownLatch(2); |
1108 |
> |
final Lock writeLock = lock.writeLock(); |
1109 |
> |
class Awaiter extends CheckedRunnable { |
1110 |
> |
public void realRun() throws InterruptedException { |
1111 |
> |
writeLock.lock(); |
1112 |
> |
locked.countDown(); |
1113 |
> |
await(c, awaitMethod); |
1114 |
> |
writeLock.unlock(); |
1115 |
> |
} |
1116 |
|
} |
1117 |
+ |
|
1118 |
+ |
Thread t1 = newStartedThread(new Awaiter()); |
1119 |
+ |
Thread t2 = newStartedThread(new Awaiter()); |
1120 |
+ |
|
1121 |
+ |
await(locked); |
1122 |
+ |
writeLock.lock(); |
1123 |
+ |
assertHasWaiters(lock, c, t1, t2); |
1124 |
+ |
c.signalAll(); |
1125 |
+ |
assertHasNoWaiters(lock, c); |
1126 |
+ |
writeLock.unlock(); |
1127 |
+ |
awaitTermination(t1); |
1128 |
+ |
awaitTermination(t2); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/** |
1132 |
< |
* awaitUntil is interruptible |
1133 |
< |
*/ |
1134 |
< |
public void testAwaitUntil_Interrupt() { |
1135 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1132 |
> |
* signal wakes up waiting threads in FIFO order |
1133 |
> |
*/ |
1134 |
> |
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1135 |
> |
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1136 |
> |
public void testSignalWakesFifo(boolean fair) { |
1137 |
> |
final PublicReentrantReadWriteLock lock = |
1138 |
> |
new PublicReentrantReadWriteLock(fair); |
1139 |
|
final Condition c = lock.writeLock().newCondition(); |
1140 |
< |
Thread t = new Thread(new Runnable() { |
1141 |
< |
public void run() { |
1142 |
< |
try { |
1143 |
< |
lock.writeLock().lock(); |
1144 |
< |
java.util.Date d = new java.util.Date(); |
1145 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1146 |
< |
lock.writeLock().unlock(); |
1147 |
< |
threadShouldThrow(); |
1148 |
< |
} |
1149 |
< |
catch(InterruptedException success) { |
1150 |
< |
} |
1151 |
< |
} |
1152 |
< |
}); |
1153 |
< |
|
1154 |
< |
try { |
1155 |
< |
t.start(); |
1156 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1157 |
< |
t.interrupt(); |
1158 |
< |
t.join(SHORT_DELAY_MS); |
1159 |
< |
assertFalse(t.isAlive()); |
1160 |
< |
} |
1161 |
< |
catch (Exception ex) { |
1162 |
< |
unexpectedException(); |
1163 |
< |
} |
1140 |
> |
final CountDownLatch locked1 = new CountDownLatch(1); |
1141 |
> |
final CountDownLatch locked2 = new CountDownLatch(1); |
1142 |
> |
final Lock writeLock = lock.writeLock(); |
1143 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1144 |
> |
public void realRun() throws InterruptedException { |
1145 |
> |
writeLock.lock(); |
1146 |
> |
locked1.countDown(); |
1147 |
> |
c.await(); |
1148 |
> |
writeLock.unlock(); |
1149 |
> |
}}); |
1150 |
> |
|
1151 |
> |
await(locked1); |
1152 |
> |
|
1153 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1154 |
> |
public void realRun() throws InterruptedException { |
1155 |
> |
writeLock.lock(); |
1156 |
> |
locked2.countDown(); |
1157 |
> |
c.await(); |
1158 |
> |
writeLock.unlock(); |
1159 |
> |
}}); |
1160 |
> |
|
1161 |
> |
await(locked2); |
1162 |
> |
|
1163 |
> |
writeLock.lock(); |
1164 |
> |
assertHasWaiters(lock, c, t1, t2); |
1165 |
> |
assertFalse(lock.hasQueuedThreads()); |
1166 |
> |
c.signal(); |
1167 |
> |
assertHasWaiters(lock, c, t2); |
1168 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1169 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1170 |
> |
c.signal(); |
1171 |
> |
assertHasNoWaiters(lock, c); |
1172 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1173 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
1174 |
> |
writeLock.unlock(); |
1175 |
> |
awaitTermination(t1); |
1176 |
> |
awaitTermination(t2); |
1177 |
|
} |
1178 |
|
|
1179 |
|
/** |
1180 |
< |
* signalAll wakes up all threads |
1181 |
< |
*/ |
1182 |
< |
public void testSignalAll() { |
1183 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1180 |
> |
* await after multiple reentrant locking preserves lock count |
1181 |
> |
*/ |
1182 |
> |
public void testAwaitLockCount() { testAwaitLockCount(false); } |
1183 |
> |
public void testAwaitLockCount_fair() { testAwaitLockCount(true); } |
1184 |
> |
public void testAwaitLockCount(boolean fair) { |
1185 |
> |
final PublicReentrantReadWriteLock lock = |
1186 |
> |
new PublicReentrantReadWriteLock(fair); |
1187 |
|
final Condition c = lock.writeLock().newCondition(); |
1188 |
< |
Thread t1 = new Thread(new Runnable() { |
1189 |
< |
public void run() { |
1190 |
< |
try { |
1191 |
< |
lock.writeLock().lock(); |
1192 |
< |
c.await(); |
1193 |
< |
lock.writeLock().unlock(); |
1194 |
< |
} |
1195 |
< |
catch(InterruptedException e) { |
1196 |
< |
threadUnexpectedException(); |
1197 |
< |
} |
1198 |
< |
} |
1199 |
< |
}); |
1162 |
< |
|
1163 |
< |
Thread t2 = new Thread(new Runnable() { |
1164 |
< |
public void run() { |
1165 |
< |
try { |
1166 |
< |
lock.writeLock().lock(); |
1167 |
< |
c.await(); |
1168 |
< |
lock.writeLock().unlock(); |
1169 |
< |
} |
1170 |
< |
catch(InterruptedException e) { |
1171 |
< |
threadUnexpectedException(); |
1172 |
< |
} |
1173 |
< |
} |
1174 |
< |
}); |
1188 |
> |
final CountDownLatch locked = new CountDownLatch(2); |
1189 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1190 |
> |
public void realRun() throws InterruptedException { |
1191 |
> |
lock.writeLock().lock(); |
1192 |
> |
assertWriteLockedByMoi(lock); |
1193 |
> |
assertEquals(1, lock.writeLock().getHoldCount()); |
1194 |
> |
locked.countDown(); |
1195 |
> |
c.await(); |
1196 |
> |
assertWriteLockedByMoi(lock); |
1197 |
> |
assertEquals(1, lock.writeLock().getHoldCount()); |
1198 |
> |
lock.writeLock().unlock(); |
1199 |
> |
}}); |
1200 |
|
|
1201 |
< |
try { |
1202 |
< |
t1.start(); |
1203 |
< |
t2.start(); |
1204 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1205 |
< |
lock.writeLock().lock(); |
1206 |
< |
c.signalAll(); |
1207 |
< |
lock.writeLock().unlock(); |
1208 |
< |
t1.join(SHORT_DELAY_MS); |
1209 |
< |
t2.join(SHORT_DELAY_MS); |
1210 |
< |
assertFalse(t1.isAlive()); |
1211 |
< |
assertFalse(t2.isAlive()); |
1212 |
< |
} |
1213 |
< |
catch (Exception ex) { |
1214 |
< |
unexpectedException(); |
1215 |
< |
} |
1201 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1202 |
> |
public void realRun() throws InterruptedException { |
1203 |
> |
lock.writeLock().lock(); |
1204 |
> |
lock.writeLock().lock(); |
1205 |
> |
assertWriteLockedByMoi(lock); |
1206 |
> |
assertEquals(2, lock.writeLock().getHoldCount()); |
1207 |
> |
locked.countDown(); |
1208 |
> |
c.await(); |
1209 |
> |
assertWriteLockedByMoi(lock); |
1210 |
> |
assertEquals(2, lock.writeLock().getHoldCount()); |
1211 |
> |
lock.writeLock().unlock(); |
1212 |
> |
lock.writeLock().unlock(); |
1213 |
> |
}}); |
1214 |
> |
|
1215 |
> |
await(locked); |
1216 |
> |
lock.writeLock().lock(); |
1217 |
> |
assertHasWaiters(lock, c, t1, t2); |
1218 |
> |
c.signalAll(); |
1219 |
> |
assertHasNoWaiters(lock, c); |
1220 |
> |
lock.writeLock().unlock(); |
1221 |
> |
awaitTermination(t1); |
1222 |
> |
awaitTermination(t2); |
1223 |
|
} |
1224 |
|
|
1225 |
|
/** |
1226 |
|
* A serialized lock deserializes as unlocked |
1227 |
|
*/ |
1228 |
< |
public void testSerialization() { |
1229 |
< |
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
1230 |
< |
l.readLock().lock(); |
1231 |
< |
l.readLock().unlock(); |
1228 |
> |
public void testSerialization() { testSerialization(false); } |
1229 |
> |
public void testSerialization_fair() { testSerialization(true); } |
1230 |
> |
public void testSerialization(boolean fair) { |
1231 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1232 |
> |
lock.writeLock().lock(); |
1233 |
> |
lock.readLock().lock(); |
1234 |
|
|
1235 |
< |
try { |
1236 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1237 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1238 |
< |
out.writeObject(l); |
1239 |
< |
out.close(); |
1240 |
< |
|
1241 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1242 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1243 |
< |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1244 |
< |
r.readLock().lock(); |
1245 |
< |
r.readLock().unlock(); |
1246 |
< |
} catch(Exception e){ |
1247 |
< |
e.printStackTrace(); |
1248 |
< |
unexpectedException(); |
1249 |
< |
} |
1235 |
> |
ReentrantReadWriteLock clone = serialClone(lock); |
1236 |
> |
assertEquals(lock.isFair(), clone.isFair()); |
1237 |
> |
assertTrue(lock.isWriteLocked()); |
1238 |
> |
assertFalse(clone.isWriteLocked()); |
1239 |
> |
assertEquals(1, lock.getReadLockCount()); |
1240 |
> |
assertEquals(0, clone.getReadLockCount()); |
1241 |
> |
clone.writeLock().lock(); |
1242 |
> |
clone.readLock().lock(); |
1243 |
> |
assertTrue(clone.isWriteLocked()); |
1244 |
> |
assertEquals(1, clone.getReadLockCount()); |
1245 |
> |
clone.readLock().unlock(); |
1246 |
> |
clone.writeLock().unlock(); |
1247 |
> |
assertFalse(clone.isWriteLocked()); |
1248 |
> |
assertEquals(1, lock.getReadLockCount()); |
1249 |
> |
assertEquals(0, clone.getReadLockCount()); |
1250 |
|
} |
1251 |
|
|
1252 |
|
/** |
1253 |
|
* hasQueuedThreads reports whether there are waiting threads |
1254 |
|
*/ |
1255 |
< |
public void testhasQueuedThreads() { |
1256 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1255 |
> |
public void testHasQueuedThreads() { testHasQueuedThreads(false); } |
1256 |
> |
public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); } |
1257 |
> |
public void testHasQueuedThreads(boolean fair) { |
1258 |
> |
final PublicReentrantReadWriteLock lock = |
1259 |
> |
new PublicReentrantReadWriteLock(fair); |
1260 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1261 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1262 |
< |
try { |
1263 |
< |
assertFalse(lock.hasQueuedThreads()); |
1264 |
< |
lock.writeLock().lock(); |
1265 |
< |
t1.start(); |
1266 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1267 |
< |
assertTrue(lock.hasQueuedThreads()); |
1268 |
< |
t2.start(); |
1269 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1270 |
< |
assertTrue(lock.hasQueuedThreads()); |
1271 |
< |
t1.interrupt(); |
1272 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1273 |
< |
assertTrue(lock.hasQueuedThreads()); |
1274 |
< |
lock.writeLock().unlock(); |
1275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1276 |
< |
assertFalse(lock.hasQueuedThreads()); |
1277 |
< |
t1.join(); |
1241 |
< |
t2.join(); |
1242 |
< |
} catch(Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1245 |
< |
} |
1262 |
> |
assertFalse(lock.hasQueuedThreads()); |
1263 |
> |
lock.writeLock().lock(); |
1264 |
> |
assertFalse(lock.hasQueuedThreads()); |
1265 |
> |
t1.start(); |
1266 |
> |
waitForQueuedThread(lock, t1); |
1267 |
> |
assertTrue(lock.hasQueuedThreads()); |
1268 |
> |
t2.start(); |
1269 |
> |
waitForQueuedThread(lock, t2); |
1270 |
> |
assertTrue(lock.hasQueuedThreads()); |
1271 |
> |
t1.interrupt(); |
1272 |
> |
awaitTermination(t1); |
1273 |
> |
assertTrue(lock.hasQueuedThreads()); |
1274 |
> |
lock.writeLock().unlock(); |
1275 |
> |
awaitTermination(t2); |
1276 |
> |
assertFalse(lock.hasQueuedThreads()); |
1277 |
> |
} |
1278 |
|
|
1279 |
|
/** |
1280 |
|
* hasQueuedThread(null) throws NPE |
1281 |
|
*/ |
1282 |
< |
public void testHasQueuedThreadNPE() { |
1283 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1282 |
> |
public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); } |
1283 |
> |
public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); } |
1284 |
> |
public void testHasQueuedThreadNPE(boolean fair) { |
1285 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1286 |
|
try { |
1287 |
< |
sync.hasQueuedThread(null); |
1287 |
> |
lock.hasQueuedThread(null); |
1288 |
|
shouldThrow(); |
1289 |
< |
} catch (NullPointerException success) { |
1256 |
< |
} |
1289 |
> |
} catch (NullPointerException success) {} |
1290 |
|
} |
1291 |
|
|
1292 |
|
/** |
1293 |
< |
* hasQueuedThread reports whether a thread is queued. |
1293 |
> |
* hasQueuedThread reports whether a thread is queued |
1294 |
|
*/ |
1295 |
< |
public void testHasQueuedThread() { |
1296 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1297 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1298 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1299 |
< |
try { |
1300 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1301 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1302 |
< |
sync.writeLock().lock(); |
1303 |
< |
t1.start(); |
1304 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1305 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1306 |
< |
t2.start(); |
1307 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1308 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1309 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1310 |
< |
t1.interrupt(); |
1311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1312 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1313 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1314 |
< |
sync.writeLock().unlock(); |
1315 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1316 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1318 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1319 |
< |
t1.join(); |
1320 |
< |
t2.join(); |
1321 |
< |
} catch(Exception e){ |
1289 |
< |
unexpectedException(); |
1290 |
< |
} |
1291 |
< |
} |
1292 |
< |
|
1295 |
> |
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1296 |
> |
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1297 |
> |
public void testHasQueuedThread(boolean fair) { |
1298 |
> |
final PublicReentrantReadWriteLock lock = |
1299 |
> |
new PublicReentrantReadWriteLock(fair); |
1300 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1301 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1302 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
1303 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1304 |
> |
lock.writeLock().lock(); |
1305 |
> |
t1.start(); |
1306 |
> |
waitForQueuedThread(lock, t1); |
1307 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1308 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1309 |
> |
t2.start(); |
1310 |
> |
waitForQueuedThread(lock, t2); |
1311 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1312 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
1313 |
> |
t1.interrupt(); |
1314 |
> |
awaitTermination(t1); |
1315 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
1316 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
1317 |
> |
lock.writeLock().unlock(); |
1318 |
> |
awaitTermination(t2); |
1319 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
1320 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1321 |
> |
} |
1322 |
|
|
1323 |
|
/** |
1324 |
|
* getQueueLength reports number of waiting threads |
1325 |
|
*/ |
1326 |
< |
public void testGetQueueLength() { |
1327 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1326 |
> |
public void testGetQueueLength() { testGetQueueLength(false); } |
1327 |
> |
public void testGetQueueLength_fair() { testGetQueueLength(true); } |
1328 |
> |
public void testGetQueueLength(boolean fair) { |
1329 |
> |
final PublicReentrantReadWriteLock lock = |
1330 |
> |
new PublicReentrantReadWriteLock(fair); |
1331 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1332 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1333 |
< |
try { |
1334 |
< |
assertEquals(0, lock.getQueueLength()); |
1335 |
< |
lock.writeLock().lock(); |
1336 |
< |
t1.start(); |
1337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1338 |
< |
assertEquals(1, lock.getQueueLength()); |
1339 |
< |
t2.start(); |
1340 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1341 |
< |
assertEquals(2, lock.getQueueLength()); |
1342 |
< |
t1.interrupt(); |
1343 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1344 |
< |
assertEquals(1, lock.getQueueLength()); |
1345 |
< |
lock.writeLock().unlock(); |
1346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1347 |
< |
assertEquals(0, lock.getQueueLength()); |
1316 |
< |
t1.join(); |
1317 |
< |
t2.join(); |
1318 |
< |
} catch(Exception e){ |
1319 |
< |
unexpectedException(); |
1320 |
< |
} |
1321 |
< |
} |
1333 |
> |
assertEquals(0, lock.getQueueLength()); |
1334 |
> |
lock.writeLock().lock(); |
1335 |
> |
t1.start(); |
1336 |
> |
waitForQueuedThread(lock, t1); |
1337 |
> |
assertEquals(1, lock.getQueueLength()); |
1338 |
> |
t2.start(); |
1339 |
> |
waitForQueuedThread(lock, t2); |
1340 |
> |
assertEquals(2, lock.getQueueLength()); |
1341 |
> |
t1.interrupt(); |
1342 |
> |
awaitTermination(t1); |
1343 |
> |
assertEquals(1, lock.getQueueLength()); |
1344 |
> |
lock.writeLock().unlock(); |
1345 |
> |
awaitTermination(t2); |
1346 |
> |
assertEquals(0, lock.getQueueLength()); |
1347 |
> |
} |
1348 |
|
|
1349 |
|
/** |
1350 |
|
* getQueuedThreads includes waiting threads |
1351 |
|
*/ |
1352 |
< |
public void testGetQueuedThreads() { |
1353 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1352 |
> |
public void testGetQueuedThreads() { testGetQueuedThreads(false); } |
1353 |
> |
public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); } |
1354 |
> |
public void testGetQueuedThreads(boolean fair) { |
1355 |
> |
final PublicReentrantReadWriteLock lock = |
1356 |
> |
new PublicReentrantReadWriteLock(fair); |
1357 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1358 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1359 |
< |
try { |
1360 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1361 |
< |
lock.writeLock().lock(); |
1362 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1363 |
< |
t1.start(); |
1364 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1365 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1366 |
< |
t2.start(); |
1367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1368 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1369 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1370 |
< |
t1.interrupt(); |
1371 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1372 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1373 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1374 |
< |
lock.writeLock().unlock(); |
1375 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1376 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1377 |
< |
t1.join(); |
1378 |
< |
t2.join(); |
1379 |
< |
} catch(Exception e){ |
1351 |
< |
unexpectedException(); |
1352 |
< |
} |
1353 |
< |
} |
1359 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1360 |
> |
lock.writeLock().lock(); |
1361 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1362 |
> |
t1.start(); |
1363 |
> |
waitForQueuedThread(lock, t1); |
1364 |
> |
assertEquals(1, lock.getQueuedThreads().size()); |
1365 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1366 |
> |
t2.start(); |
1367 |
> |
waitForQueuedThread(lock, t2); |
1368 |
> |
assertEquals(2, lock.getQueuedThreads().size()); |
1369 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1370 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1371 |
> |
t1.interrupt(); |
1372 |
> |
awaitTermination(t1); |
1373 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1374 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1375 |
> |
assertEquals(1, lock.getQueuedThreads().size()); |
1376 |
> |
lock.writeLock().unlock(); |
1377 |
> |
awaitTermination(t2); |
1378 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1379 |
> |
} |
1380 |
|
|
1381 |
|
/** |
1382 |
|
* hasWaiters throws NPE if null |
1383 |
|
*/ |
1384 |
< |
public void testHasWaitersNPE() { |
1385 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1384 |
> |
public void testHasWaitersNPE() { testHasWaitersNPE(false); } |
1385 |
> |
public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); } |
1386 |
> |
public void testHasWaitersNPE(boolean fair) { |
1387 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1388 |
|
try { |
1389 |
|
lock.hasWaiters(null); |
1390 |
|
shouldThrow(); |
1391 |
< |
} catch (NullPointerException success) { |
1364 |
< |
} catch (Exception ex) { |
1365 |
< |
unexpectedException(); |
1366 |
< |
} |
1391 |
> |
} catch (NullPointerException success) {} |
1392 |
|
} |
1393 |
|
|
1394 |
|
/** |
1395 |
|
* getWaitQueueLength throws NPE if null |
1396 |
|
*/ |
1397 |
< |
public void testGetWaitQueueLengthNPE() { |
1398 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1397 |
> |
public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); } |
1398 |
> |
public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); } |
1399 |
> |
public void testGetWaitQueueLengthNPE(boolean fair) { |
1400 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1401 |
|
try { |
1402 |
|
lock.getWaitQueueLength(null); |
1403 |
|
shouldThrow(); |
1404 |
< |
} catch (NullPointerException success) { |
1378 |
< |
} catch (Exception ex) { |
1379 |
< |
unexpectedException(); |
1380 |
< |
} |
1404 |
> |
} catch (NullPointerException success) {} |
1405 |
|
} |
1406 |
|
|
1383 |
– |
|
1407 |
|
/** |
1408 |
|
* getWaitingThreads throws NPE if null |
1409 |
|
*/ |
1410 |
< |
public void testGetWaitingThreadsNPE() { |
1411 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1410 |
> |
public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); } |
1411 |
> |
public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); } |
1412 |
> |
public void testGetWaitingThreadsNPE(boolean fair) { |
1413 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair); |
1414 |
|
try { |
1415 |
|
lock.getWaitingThreads(null); |
1416 |
|
shouldThrow(); |
1417 |
< |
} catch (NullPointerException success) { |
1393 |
< |
} catch (Exception ex) { |
1394 |
< |
unexpectedException(); |
1395 |
< |
} |
1417 |
> |
} catch (NullPointerException success) {} |
1418 |
|
} |
1419 |
|
|
1420 |
|
/** |
1421 |
< |
* hasWaiters throws IAE if not owned |
1421 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
1422 |
|
*/ |
1423 |
< |
public void testHasWaitersIAE() { |
1424 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1425 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1426 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1423 |
> |
public void testHasWaitersIAE() { testHasWaitersIAE(false); } |
1424 |
> |
public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); } |
1425 |
> |
public void testHasWaitersIAE(boolean fair) { |
1426 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1427 |
> |
final Condition c = lock.writeLock().newCondition(); |
1428 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair); |
1429 |
|
try { |
1430 |
|
lock2.hasWaiters(c); |
1431 |
|
shouldThrow(); |
1432 |
< |
} catch (IllegalArgumentException success) { |
1409 |
< |
} catch (Exception ex) { |
1410 |
< |
unexpectedException(); |
1411 |
< |
} |
1432 |
> |
} catch (IllegalArgumentException success) {} |
1433 |
|
} |
1434 |
|
|
1435 |
|
/** |
1436 |
< |
* hasWaiters throws IMSE if not locked |
1436 |
> |
* hasWaiters throws IllegalMonitorStateException if not locked |
1437 |
|
*/ |
1438 |
< |
public void testHasWaitersIMSE() { |
1439 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1440 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1438 |
> |
public void testHasWaitersIMSE() { testHasWaitersIMSE(false); } |
1439 |
> |
public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); } |
1440 |
> |
public void testHasWaitersIMSE(boolean fair) { |
1441 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1442 |
> |
final Condition c = lock.writeLock().newCondition(); |
1443 |
|
try { |
1444 |
|
lock.hasWaiters(c); |
1445 |
|
shouldThrow(); |
1446 |
< |
} catch (IllegalMonitorStateException success) { |
1424 |
< |
} catch (Exception ex) { |
1425 |
< |
unexpectedException(); |
1426 |
< |
} |
1446 |
> |
} catch (IllegalMonitorStateException success) {} |
1447 |
|
} |
1448 |
|
|
1429 |
– |
|
1449 |
|
/** |
1450 |
< |
* getWaitQueueLength throws IAE if not owned |
1450 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
1451 |
|
*/ |
1452 |
< |
public void testGetWaitQueueLengthIAE() { |
1453 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1454 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1455 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1452 |
> |
public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); } |
1453 |
> |
public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); } |
1454 |
> |
public void testGetWaitQueueLengthIAE(boolean fair) { |
1455 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1456 |
> |
final Condition c = lock.writeLock().newCondition(); |
1457 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair); |
1458 |
|
try { |
1459 |
|
lock2.getWaitQueueLength(c); |
1460 |
|
shouldThrow(); |
1461 |
< |
} catch (IllegalArgumentException success) { |
1441 |
< |
} catch (Exception ex) { |
1442 |
< |
unexpectedException(); |
1443 |
< |
} |
1461 |
> |
} catch (IllegalArgumentException success) {} |
1462 |
|
} |
1463 |
|
|
1464 |
|
/** |
1465 |
< |
* getWaitQueueLength throws IMSE if not locked |
1465 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not locked |
1466 |
|
*/ |
1467 |
< |
public void testGetWaitQueueLengthIMSE() { |
1468 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1469 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1467 |
> |
public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); } |
1468 |
> |
public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); } |
1469 |
> |
public void testGetWaitQueueLengthIMSE(boolean fair) { |
1470 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1471 |
> |
final Condition c = lock.writeLock().newCondition(); |
1472 |
|
try { |
1473 |
|
lock.getWaitQueueLength(c); |
1474 |
|
shouldThrow(); |
1475 |
< |
} catch (IllegalMonitorStateException success) { |
1456 |
< |
} catch (Exception ex) { |
1457 |
< |
unexpectedException(); |
1458 |
< |
} |
1475 |
> |
} catch (IllegalMonitorStateException success) {} |
1476 |
|
} |
1477 |
|
|
1461 |
– |
|
1478 |
|
/** |
1479 |
< |
* getWaitingThreads throws IAE if not owned |
1479 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
1480 |
|
*/ |
1481 |
< |
public void testGetWaitingThreadsIAE() { |
1482 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1483 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1484 |
< |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1481 |
> |
public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); } |
1482 |
> |
public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); } |
1483 |
> |
public void testGetWaitingThreadsIAE(boolean fair) { |
1484 |
> |
final PublicReentrantReadWriteLock lock = |
1485 |
> |
new PublicReentrantReadWriteLock(fair); |
1486 |
> |
final Condition c = lock.writeLock().newCondition(); |
1487 |
> |
final PublicReentrantReadWriteLock lock2 = |
1488 |
> |
new PublicReentrantReadWriteLock(fair); |
1489 |
|
try { |
1490 |
|
lock2.getWaitingThreads(c); |
1491 |
|
shouldThrow(); |
1492 |
< |
} catch (IllegalArgumentException success) { |
1473 |
< |
} catch (Exception ex) { |
1474 |
< |
unexpectedException(); |
1475 |
< |
} |
1492 |
> |
} catch (IllegalArgumentException success) {} |
1493 |
|
} |
1494 |
|
|
1495 |
|
/** |
1496 |
< |
* getWaitingThreads throws IMSE if not locked |
1496 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not locked |
1497 |
|
*/ |
1498 |
< |
public void testGetWaitingThreadsIMSE() { |
1499 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1500 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1498 |
> |
public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); } |
1499 |
> |
public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); } |
1500 |
> |
public void testGetWaitingThreadsIMSE(boolean fair) { |
1501 |
> |
final PublicReentrantReadWriteLock lock = |
1502 |
> |
new PublicReentrantReadWriteLock(fair); |
1503 |
> |
final Condition c = lock.writeLock().newCondition(); |
1504 |
|
try { |
1505 |
|
lock.getWaitingThreads(c); |
1506 |
|
shouldThrow(); |
1507 |
< |
} catch (IllegalMonitorStateException success) { |
1488 |
< |
} catch (Exception ex) { |
1489 |
< |
unexpectedException(); |
1490 |
< |
} |
1507 |
> |
} catch (IllegalMonitorStateException success) {} |
1508 |
|
} |
1509 |
|
|
1493 |
– |
|
1510 |
|
/** |
1511 |
|
* hasWaiters returns true when a thread is waiting, else false |
1512 |
|
*/ |
1513 |
< |
public void testHasWaiters() { |
1514 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1515 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1516 |
< |
Thread t = new Thread(new Runnable() { |
1517 |
< |
public void run() { |
1518 |
< |
try { |
1519 |
< |
lock.writeLock().lock(); |
1520 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1521 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1522 |
< |
c.await(); |
1523 |
< |
lock.writeLock().unlock(); |
1524 |
< |
} |
1525 |
< |
catch(InterruptedException e) { |
1526 |
< |
threadUnexpectedException(); |
1527 |
< |
} |
1528 |
< |
} |
1529 |
< |
}); |
1513 |
> |
public void testHasWaiters() { testHasWaiters(false); } |
1514 |
> |
public void testHasWaiters_fair() { testHasWaiters(true); } |
1515 |
> |
public void testHasWaiters(boolean fair) { |
1516 |
> |
final PublicReentrantReadWriteLock lock = |
1517 |
> |
new PublicReentrantReadWriteLock(fair); |
1518 |
> |
final Condition c = lock.writeLock().newCondition(); |
1519 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
1520 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1521 |
> |
public void realRun() throws InterruptedException { |
1522 |
> |
lock.writeLock().lock(); |
1523 |
> |
assertHasNoWaiters(lock, c); |
1524 |
> |
assertFalse(lock.hasWaiters(c)); |
1525 |
> |
locked.countDown(); |
1526 |
> |
c.await(); |
1527 |
> |
assertHasNoWaiters(lock, c); |
1528 |
> |
assertFalse(lock.hasWaiters(c)); |
1529 |
> |
lock.writeLock().unlock(); |
1530 |
> |
}}); |
1531 |
|
|
1532 |
< |
try { |
1533 |
< |
t.start(); |
1534 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1535 |
< |
lock.writeLock().lock(); |
1536 |
< |
assertTrue(lock.hasWaiters(c)); |
1537 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1538 |
< |
c.signal(); |
1539 |
< |
lock.writeLock().unlock(); |
1540 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1541 |
< |
lock.writeLock().lock(); |
1525 |
< |
assertFalse(lock.hasWaiters(c)); |
1526 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1527 |
< |
lock.writeLock().unlock(); |
1528 |
< |
t.join(SHORT_DELAY_MS); |
1529 |
< |
assertFalse(t.isAlive()); |
1530 |
< |
} |
1531 |
< |
catch (Exception ex) { |
1532 |
< |
unexpectedException(); |
1533 |
< |
} |
1532 |
> |
await(locked); |
1533 |
> |
lock.writeLock().lock(); |
1534 |
> |
assertHasWaiters(lock, c, t); |
1535 |
> |
assertTrue(lock.hasWaiters(c)); |
1536 |
> |
c.signal(); |
1537 |
> |
assertHasNoWaiters(lock, c); |
1538 |
> |
assertFalse(lock.hasWaiters(c)); |
1539 |
> |
lock.writeLock().unlock(); |
1540 |
> |
awaitTermination(t); |
1541 |
> |
assertHasNoWaiters(lock, c); |
1542 |
|
} |
1543 |
|
|
1544 |
|
/** |
1545 |
|
* getWaitQueueLength returns number of waiting threads |
1546 |
|
*/ |
1547 |
< |
public void testGetWaitQueueLength() { |
1548 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1549 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1550 |
< |
Thread t = new Thread(new Runnable() { |
1551 |
< |
public void run() { |
1552 |
< |
try { |
1553 |
< |
lock.writeLock().lock(); |
1554 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1555 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1556 |
< |
c.await(); |
1557 |
< |
lock.writeLock().unlock(); |
1558 |
< |
} |
1559 |
< |
catch(InterruptedException e) { |
1560 |
< |
threadUnexpectedException(); |
1561 |
< |
} |
1554 |
< |
} |
1555 |
< |
}); |
1547 |
> |
public void testGetWaitQueueLength() { testGetWaitQueueLength(false); } |
1548 |
> |
public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); } |
1549 |
> |
public void testGetWaitQueueLength(boolean fair) { |
1550 |
> |
final PublicReentrantReadWriteLock lock = |
1551 |
> |
new PublicReentrantReadWriteLock(fair); |
1552 |
> |
final Condition c = lock.writeLock().newCondition(); |
1553 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
1554 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1555 |
> |
public void realRun() throws InterruptedException { |
1556 |
> |
lock.writeLock().lock(); |
1557 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1558 |
> |
locked.countDown(); |
1559 |
> |
c.await(); |
1560 |
> |
lock.writeLock().unlock(); |
1561 |
> |
}}); |
1562 |
|
|
1563 |
< |
try { |
1564 |
< |
t.start(); |
1565 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1566 |
< |
lock.writeLock().lock(); |
1567 |
< |
assertTrue(lock.hasWaiters(c)); |
1568 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1569 |
< |
c.signal(); |
1570 |
< |
lock.writeLock().unlock(); |
1571 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1566 |
< |
lock.writeLock().lock(); |
1567 |
< |
assertFalse(lock.hasWaiters(c)); |
1568 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1569 |
< |
lock.writeLock().unlock(); |
1570 |
< |
t.join(SHORT_DELAY_MS); |
1571 |
< |
assertFalse(t.isAlive()); |
1572 |
< |
} |
1573 |
< |
catch (Exception ex) { |
1574 |
< |
unexpectedException(); |
1575 |
< |
} |
1563 |
> |
await(locked); |
1564 |
> |
lock.writeLock().lock(); |
1565 |
> |
assertHasWaiters(lock, c, t); |
1566 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1567 |
> |
c.signal(); |
1568 |
> |
assertHasNoWaiters(lock, c); |
1569 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1570 |
> |
lock.writeLock().unlock(); |
1571 |
> |
awaitTermination(t); |
1572 |
|
} |
1573 |
|
|
1578 |
– |
|
1574 |
|
/** |
1575 |
|
* getWaitingThreads returns only and all waiting threads |
1576 |
|
*/ |
1577 |
< |
public void testGetWaitingThreads() { |
1578 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1577 |
> |
public void testGetWaitingThreads() { testGetWaitingThreads(false); } |
1578 |
> |
public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); } |
1579 |
> |
public void testGetWaitingThreads(boolean fair) { |
1580 |
> |
final PublicReentrantReadWriteLock lock = |
1581 |
> |
new PublicReentrantReadWriteLock(fair); |
1582 |
|
final Condition c = lock.writeLock().newCondition(); |
1583 |
< |
Thread t1 = new Thread(new Runnable() { |
1584 |
< |
public void run() { |
1585 |
< |
try { |
1586 |
< |
lock.writeLock().lock(); |
1587 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1588 |
< |
c.await(); |
1589 |
< |
lock.writeLock().unlock(); |
1590 |
< |
} |
1591 |
< |
catch(InterruptedException e) { |
1592 |
< |
threadUnexpectedException(); |
1595 |
< |
} |
1596 |
< |
} |
1597 |
< |
}); |
1598 |
< |
|
1599 |
< |
Thread t2 = new Thread(new Runnable() { |
1600 |
< |
public void run() { |
1601 |
< |
try { |
1602 |
< |
lock.writeLock().lock(); |
1603 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1604 |
< |
c.await(); |
1605 |
< |
lock.writeLock().unlock(); |
1606 |
< |
} |
1607 |
< |
catch(InterruptedException e) { |
1608 |
< |
threadUnexpectedException(); |
1609 |
< |
} |
1610 |
< |
} |
1611 |
< |
}); |
1583 |
> |
final CountDownLatch locked1 = new CountDownLatch(1); |
1584 |
> |
final CountDownLatch locked2 = new CountDownLatch(1); |
1585 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
1586 |
> |
public void realRun() throws InterruptedException { |
1587 |
> |
lock.writeLock().lock(); |
1588 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1589 |
> |
locked1.countDown(); |
1590 |
> |
c.await(); |
1591 |
> |
lock.writeLock().unlock(); |
1592 |
> |
}}); |
1593 |
|
|
1594 |
< |
try { |
1595 |
< |
lock.writeLock().lock(); |
1596 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1597 |
< |
lock.writeLock().unlock(); |
1598 |
< |
t1.start(); |
1599 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1600 |
< |
t2.start(); |
1601 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1602 |
< |
lock.writeLock().lock(); |
1603 |
< |
assertTrue(lock.hasWaiters(c)); |
1604 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1605 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1606 |
< |
c.signalAll(); |
1607 |
< |
lock.writeLock().unlock(); |
1608 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1609 |
< |
lock.writeLock().lock(); |
1610 |
< |
assertFalse(lock.hasWaiters(c)); |
1611 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1612 |
< |
lock.writeLock().unlock(); |
1613 |
< |
t1.join(SHORT_DELAY_MS); |
1614 |
< |
t2.join(SHORT_DELAY_MS); |
1615 |
< |
assertFalse(t1.isAlive()); |
1616 |
< |
assertFalse(t2.isAlive()); |
1617 |
< |
} |
1618 |
< |
catch (Exception ex) { |
1619 |
< |
unexpectedException(); |
1620 |
< |
} |
1594 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
1595 |
> |
public void realRun() throws InterruptedException { |
1596 |
> |
lock.writeLock().lock(); |
1597 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
1598 |
> |
locked2.countDown(); |
1599 |
> |
c.await(); |
1600 |
> |
lock.writeLock().unlock(); |
1601 |
> |
}}); |
1602 |
> |
|
1603 |
> |
lock.writeLock().lock(); |
1604 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1605 |
> |
lock.writeLock().unlock(); |
1606 |
> |
|
1607 |
> |
t1.start(); |
1608 |
> |
await(locked1); |
1609 |
> |
t2.start(); |
1610 |
> |
await(locked2); |
1611 |
> |
|
1612 |
> |
lock.writeLock().lock(); |
1613 |
> |
assertTrue(lock.hasWaiters(c)); |
1614 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1615 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1616 |
> |
assertEquals(2, lock.getWaitingThreads(c).size()); |
1617 |
> |
c.signalAll(); |
1618 |
> |
assertHasNoWaiters(lock, c); |
1619 |
> |
lock.writeLock().unlock(); |
1620 |
> |
|
1621 |
> |
awaitTermination(t1); |
1622 |
> |
awaitTermination(t2); |
1623 |
> |
|
1624 |
> |
assertHasNoWaiters(lock, c); |
1625 |
|
} |
1626 |
|
|
1627 |
|
/** |
1628 |
|
* toString indicates current lock state |
1629 |
|
*/ |
1630 |
< |
public void testToString() { |
1631 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1632 |
< |
String us = lock.toString(); |
1633 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1634 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1635 |
< |
lock.writeLock().lock(); |
1636 |
< |
String ws = lock.toString(); |
1637 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1638 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1630 |
> |
public void testToString() { testToString(false); } |
1631 |
> |
public void testToString_fair() { testToString(true); } |
1632 |
> |
public void testToString(boolean fair) { |
1633 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1634 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1635 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1636 |
> |
lock.writeLock().lock(); |
1637 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1638 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1639 |
> |
lock.writeLock().lock(); |
1640 |
> |
assertTrue(lock.toString().contains("Write locks = 2")); |
1641 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1642 |
> |
lock.writeLock().unlock(); |
1643 |
|
lock.writeLock().unlock(); |
1644 |
|
lock.readLock().lock(); |
1645 |
+ |
assertTrue(lock.toString().contains("Write locks = 0")); |
1646 |
+ |
assertTrue(lock.toString().contains("Read locks = 1")); |
1647 |
|
lock.readLock().lock(); |
1648 |
< |
String rs = lock.toString(); |
1649 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1659 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1648 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1649 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1650 |
|
} |
1651 |
|
|
1652 |
|
/** |
1653 |
|
* readLock.toString indicates current lock state |
1654 |
|
*/ |
1655 |
< |
public void testReadLockToString() { |
1656 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1657 |
< |
String us = lock.readLock().toString(); |
1658 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1655 |
> |
public void testReadLockToString() { testReadLockToString(false); } |
1656 |
> |
public void testReadLockToString_fair() { testReadLockToString(true); } |
1657 |
> |
public void testReadLockToString(boolean fair) { |
1658 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1659 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1660 |
|
lock.readLock().lock(); |
1661 |
+ |
assertTrue(lock.readLock().toString().contains("Read locks = 1")); |
1662 |
|
lock.readLock().lock(); |
1663 |
< |
String rs = lock.readLock().toString(); |
1664 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1663 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1664 |
> |
lock.readLock().unlock(); |
1665 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 1")); |
1666 |
> |
lock.readLock().unlock(); |
1667 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1668 |
|
} |
1669 |
|
|
1670 |
|
/** |
1671 |
|
* writeLock.toString indicates current lock state |
1672 |
|
*/ |
1673 |
< |
public void testWriteLockToString() { |
1674 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1675 |
< |
String us = lock.writeLock().toString(); |
1676 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1673 |
> |
public void testWriteLockToString() { testWriteLockToString(false); } |
1674 |
> |
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1675 |
> |
public void testWriteLockToString(boolean fair) { |
1676 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1677 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1678 |
|
lock.writeLock().lock(); |
1679 |
< |
String ls = lock.writeLock().toString(); |
1680 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1679 |
> |
assertTrue(lock.writeLock().toString().contains("Locked by")); |
1680 |
> |
lock.writeLock().unlock(); |
1681 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1682 |
|
} |
1683 |
|
|
1684 |
+ |
/** |
1685 |
+ |
* ThreadMXBean reports the blockers that we expect. |
1686 |
+ |
*/ |
1687 |
+ |
public void testBlockers() { |
1688 |
+ |
if (!testImplementationDetails) return; |
1689 |
+ |
final boolean fair = randomBoolean(); |
1690 |
+ |
final boolean timedAcquire = randomBoolean(); |
1691 |
+ |
final boolean timedAwait = randomBoolean(); |
1692 |
+ |
final String syncClassName = fair |
1693 |
+ |
? "ReentrantReadWriteLock$FairSync" |
1694 |
+ |
: "ReentrantReadWriteLock$NonfairSync"; |
1695 |
+ |
final String conditionClassName |
1696 |
+ |
= "AbstractQueuedSynchronizer$ConditionObject"; |
1697 |
+ |
final Thread.State expectedAcquireState = timedAcquire |
1698 |
+ |
? Thread.State.TIMED_WAITING |
1699 |
+ |
: Thread.State.WAITING; |
1700 |
+ |
final Thread.State expectedAwaitState = timedAwait |
1701 |
+ |
? Thread.State.TIMED_WAITING |
1702 |
+ |
: Thread.State.WAITING; |
1703 |
+ |
final Lock lock = new ReentrantReadWriteLock(fair).writeLock(); |
1704 |
+ |
final Condition condition = lock.newCondition(); |
1705 |
+ |
final AtomicBoolean conditionSatisfied = new AtomicBoolean(false); |
1706 |
+ |
lock.lock(); |
1707 |
+ |
final Thread thread = newStartedThread((Action) () -> { |
1708 |
+ |
if (timedAcquire) |
1709 |
+ |
lock.tryLock(LONGER_DELAY_MS, MILLISECONDS); |
1710 |
+ |
else |
1711 |
+ |
lock.lock(); |
1712 |
+ |
while (!conditionSatisfied.get()) |
1713 |
+ |
if (timedAwait) |
1714 |
+ |
condition.await(LONGER_DELAY_MS, MILLISECONDS); |
1715 |
+ |
else |
1716 |
+ |
condition.await(); |
1717 |
+ |
}); |
1718 |
+ |
Callable<Boolean> waitingForLock = () -> { |
1719 |
+ |
String className; |
1720 |
+ |
return thread.getState() == expectedAcquireState |
1721 |
+ |
&& (className = blockerClassName(thread)) != null |
1722 |
+ |
&& className.endsWith(syncClassName); |
1723 |
+ |
}; |
1724 |
+ |
waitForThreadToEnterWaitState(thread, waitingForLock); |
1725 |
+ |
|
1726 |
+ |
lock.unlock(); |
1727 |
+ |
Callable<Boolean> waitingForCondition = () -> { |
1728 |
+ |
String className; |
1729 |
+ |
return thread.getState() == expectedAwaitState |
1730 |
+ |
&& (className = blockerClassName(thread)) != null |
1731 |
+ |
&& className.endsWith(conditionClassName); |
1732 |
+ |
}; |
1733 |
+ |
waitForThreadToEnterWaitState(thread, waitingForCondition); |
1734 |
+ |
|
1735 |
+ |
// politely release the waiter |
1736 |
+ |
conditionSatisfied.set(true); |
1737 |
+ |
lock.lock(); |
1738 |
+ |
try { |
1739 |
+ |
condition.signal(); |
1740 |
+ |
} finally { lock.unlock(); } |
1741 |
+ |
|
1742 |
+ |
awaitTermination(thread); |
1743 |
+ |
} |
1744 |
|
} |