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