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