50 |
|
*/ |
51 |
|
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
52 |
|
PublicReentrantReadWriteLock() { super(); } |
53 |
+ |
PublicReentrantReadWriteLock(boolean fair) { super(fair); } |
54 |
+ |
public Thread getOwner() { |
55 |
+ |
return super.getOwner(); |
56 |
+ |
} |
57 |
|
public Collection<Thread> getQueuedThreads() { |
58 |
|
return super.getQueuedThreads(); |
59 |
|
} |
65 |
|
/** |
66 |
|
* Releases write lock, checking that it had a hold count of 1. |
67 |
|
*/ |
68 |
< |
void releaseWriteLock(ReentrantReadWriteLock lock) { |
68 |
> |
void releaseWriteLock(PublicReentrantReadWriteLock lock) { |
69 |
|
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); |
70 |
< |
assertTrue(writeLock.isHeldByCurrentThread()); |
70 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
71 |
> |
assertEquals(1, lock.getWriteHoldCount()); |
72 |
|
writeLock.unlock(); |
73 |
< |
assertFalse(writeLock.isHeldByCurrentThread()); |
73 |
> |
assertNotWriteLocked(lock); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
|
* Spin-waits until lock.hasQueuedThread(t) becomes true. |
78 |
|
*/ |
79 |
< |
void waitForQueuedThread(ReentrantReadWriteLock lock, Thread t) { |
79 |
> |
void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) { |
80 |
|
long startTime = System.nanoTime(); |
81 |
|
while (!lock.hasQueuedThread(t)) { |
82 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
83 |
|
throw new AssertionError("timed out"); |
84 |
|
Thread.yield(); |
85 |
|
} |
86 |
+ |
assertTrue(t.isAlive()); |
87 |
+ |
assertTrue(lock.getOwner() != t); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* Checks that lock is not write-locked. |
92 |
|
*/ |
93 |
< |
void assertNotWriteLocked(ReentrantReadWriteLock lock) { |
93 |
> |
void assertNotWriteLocked(PublicReentrantReadWriteLock lock) { |
94 |
|
assertFalse(lock.isWriteLocked()); |
95 |
|
assertFalse(lock.isWriteLockedByCurrentThread()); |
96 |
+ |
assertFalse(lock.writeLock().isHeldByCurrentThread()); |
97 |
+ |
assertNull(lock.getOwner()); |
98 |
|
assertEquals(0, lock.getWriteHoldCount()); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
+ |
* Checks that lock is write-locked by the given thread. |
103 |
+ |
*/ |
104 |
+ |
void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) { |
105 |
+ |
assertTrue(lock.isWriteLocked()); |
106 |
+ |
assertSame(t, lock.getOwner()); |
107 |
+ |
assertEquals(t == Thread.currentThread(), |
108 |
+ |
lock.isWriteLockedByCurrentThread()); |
109 |
+ |
assertEquals(t == Thread.currentThread(), |
110 |
+ |
lock.writeLock().isHeldByCurrentThread()); |
111 |
+ |
assertEquals(t == Thread.currentThread(), |
112 |
+ |
lock.getWriteHoldCount() > 0); |
113 |
+ |
assertEquals(0, lock.getReadLockCount()); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
/** |
117 |
|
* Checks that condition c has no waiters. |
118 |
|
*/ |
119 |
|
void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { |
139 |
|
* Constructor sets given fairness, and is in unlocked state |
140 |
|
*/ |
141 |
|
public void testConstructor() { |
142 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
143 |
< |
assertFalse(rl.isFair()); |
144 |
< |
assertFalse(rl.isWriteLocked()); |
145 |
< |
assertEquals(0, rl.getReadLockCount()); |
146 |
< |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
147 |
< |
assertTrue(r2.isFair()); |
148 |
< |
assertFalse(r2.isWriteLocked()); |
149 |
< |
assertEquals(0, r2.getReadLockCount()); |
150 |
< |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
151 |
< |
assertFalse(r3.isFair()); |
152 |
< |
assertFalse(r3.isWriteLocked()); |
153 |
< |
assertEquals(0, r3.getReadLockCount()); |
142 |
> |
PublicReentrantReadWriteLock lock; |
143 |
> |
|
144 |
> |
lock = new PublicReentrantReadWriteLock(); |
145 |
> |
assertFalse(lock.isFair()); |
146 |
> |
assertNotWriteLocked(lock); |
147 |
> |
assertEquals(0, lock.getReadLockCount()); |
148 |
> |
|
149 |
> |
lock = new PublicReentrantReadWriteLock(true); |
150 |
> |
assertTrue(lock.isFair()); |
151 |
> |
assertNotWriteLocked(lock); |
152 |
> |
assertEquals(0, lock.getReadLockCount()); |
153 |
> |
|
154 |
> |
lock = new PublicReentrantReadWriteLock(false); |
155 |
> |
assertFalse(lock.isFair()); |
156 |
> |
assertNotWriteLocked(lock); |
157 |
> |
assertEquals(0, lock.getReadLockCount()); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
|
* write-locking and read-locking an unlocked lock succeed |
162 |
|
*/ |
163 |
|
public void testLock() { |
164 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
165 |
< |
rl.writeLock().lock(); |
166 |
< |
assertTrue(rl.isWriteLocked()); |
167 |
< |
assertTrue(rl.isWriteLockedByCurrentThread()); |
168 |
< |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
169 |
< |
assertEquals(0, rl.getReadLockCount()); |
170 |
< |
rl.writeLock().unlock(); |
171 |
< |
assertFalse(rl.isWriteLocked()); |
172 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
173 |
< |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
174 |
< |
assertEquals(0, rl.getReadLockCount()); |
175 |
< |
rl.readLock().lock(); |
176 |
< |
assertFalse(rl.isWriteLocked()); |
149 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
150 |
< |
assertEquals(1, rl.getReadLockCount()); |
151 |
< |
rl.readLock().unlock(); |
152 |
< |
assertFalse(rl.isWriteLocked()); |
153 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
154 |
< |
assertEquals(0, rl.getReadLockCount()); |
164 |
> |
PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
165 |
> |
assertNotWriteLocked(lock); |
166 |
> |
lock.writeLock().lock(); |
167 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
168 |
> |
lock.writeLock().unlock(); |
169 |
> |
assertNotWriteLocked(lock); |
170 |
> |
assertEquals(0, lock.getReadLockCount()); |
171 |
> |
lock.readLock().lock(); |
172 |
> |
assertNotWriteLocked(lock); |
173 |
> |
assertEquals(1, lock.getReadLockCount()); |
174 |
> |
lock.readLock().unlock(); |
175 |
> |
assertNotWriteLocked(lock); |
176 |
> |
assertEquals(0, lock.getReadLockCount()); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
|
* locking an unlocked fair lock succeeds |
181 |
|
*/ |
182 |
|
public void testFairLock() { |
183 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
184 |
< |
rl.writeLock().lock(); |
185 |
< |
assertTrue(rl.isWriteLocked()); |
186 |
< |
assertTrue(rl.isWriteLockedByCurrentThread()); |
187 |
< |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
188 |
< |
assertEquals(0, rl.getReadLockCount()); |
189 |
< |
rl.writeLock().unlock(); |
190 |
< |
assertFalse(rl.isWriteLocked()); |
191 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
192 |
< |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
193 |
< |
assertEquals(0, rl.getReadLockCount()); |
194 |
< |
rl.readLock().lock(); |
195 |
< |
assertFalse(rl.isWriteLocked()); |
174 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
175 |
< |
assertEquals(1, rl.getReadLockCount()); |
176 |
< |
rl.readLock().unlock(); |
177 |
< |
assertFalse(rl.isWriteLocked()); |
178 |
< |
assertFalse(rl.isWriteLockedByCurrentThread()); |
179 |
< |
assertEquals(0, rl.getReadLockCount()); |
183 |
> |
PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); |
184 |
> |
assertNotWriteLocked(lock); |
185 |
> |
lock.writeLock().lock(); |
186 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
187 |
> |
lock.writeLock().unlock(); |
188 |
> |
assertNotWriteLocked(lock); |
189 |
> |
assertEquals(0, lock.getReadLockCount()); |
190 |
> |
lock.readLock().lock(); |
191 |
> |
assertNotWriteLocked(lock); |
192 |
> |
assertEquals(1, lock.getReadLockCount()); |
193 |
> |
lock.readLock().unlock(); |
194 |
> |
assertNotWriteLocked(lock); |
195 |
> |
assertEquals(0, lock.getReadLockCount()); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
244 |
|
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
245 |
|
*/ |
246 |
|
public void testWriteUnlock_IllegalMonitorStateException() { |
247 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
247 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
248 |
|
try { |
249 |
< |
rl.writeLock().unlock(); |
249 |
> |
lock.writeLock().unlock(); |
250 |
|
shouldThrow(); |
251 |
|
} catch (IllegalMonitorStateException success) {} |
252 |
|
} |
255 |
|
* read-unlocking an unlocked lock throws IllegalMonitorStateException |
256 |
|
*/ |
257 |
|
public void testReadUnlock_IllegalMonitorStateException() { |
258 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
258 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
259 |
|
try { |
260 |
< |
rl.readLock().unlock(); |
260 |
> |
lock.readLock().unlock(); |
261 |
|
shouldThrow(); |
262 |
|
} catch (IllegalMonitorStateException success) {} |
263 |
|
} |
266 |
|
* write-lockInterruptibly is interruptible |
267 |
|
*/ |
268 |
|
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
269 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
269 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
270 |
|
lock.writeLock().lock(); |
271 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
272 |
|
public void realRun() throws InterruptedException { |
283 |
|
* timed write-tryLock is interruptible |
284 |
|
*/ |
285 |
|
public void testWriteTryLock_Interrupted() throws InterruptedException { |
286 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
287 |
|
lock.writeLock().lock(); |
288 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
289 |
|
public void realRun() throws InterruptedException { |
300 |
|
* read-lockInterruptibly is interruptible |
301 |
|
*/ |
302 |
|
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
303 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
303 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
304 |
|
lock.writeLock().lock(); |
305 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
306 |
|
public void realRun() throws InterruptedException { |
317 |
|
* timed read-tryLock is interruptible |
318 |
|
*/ |
319 |
|
public void testReadTryLock_Interrupted() throws InterruptedException { |
320 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
320 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
321 |
|
lock.writeLock().lock(); |
322 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
323 |
|
public void realRun() throws InterruptedException { |
334 |
|
* write-tryLock fails if locked |
335 |
|
*/ |
336 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
337 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
337 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
338 |
|
lock.writeLock().lock(); |
339 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
340 |
|
public void realRun() { |
349 |
|
* read-tryLock fails if locked |
350 |
|
*/ |
351 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
352 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
352 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
353 |
|
lock.writeLock().lock(); |
354 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
355 |
|
public void realRun() { |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
+ |
* A writelock succeeds only after a reading thread unlocks |
381 |
+ |
*/ |
382 |
+ |
public void testWriteAfterReadLock() throws InterruptedException { |
383 |
+ |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
384 |
+ |
lock.readLock().lock(); |
385 |
+ |
|
386 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
387 |
+ |
public void realRun() { |
388 |
+ |
assertEquals(1, lock.getReadLockCount()); |
389 |
+ |
lock.writeLock().lock(); |
390 |
+ |
assertEquals(0, lock.getReadLockCount()); |
391 |
+ |
lock.writeLock().unlock(); |
392 |
+ |
}}); |
393 |
+ |
waitForQueuedThread(lock, t); |
394 |
+ |
assertNotWriteLocked(lock); |
395 |
+ |
assertEquals(1, lock.getReadLockCount()); |
396 |
+ |
lock.readLock().unlock(); |
397 |
+ |
assertEquals(0, lock.getReadLockCount()); |
398 |
+ |
awaitTermination(t); |
399 |
+ |
assertNotWriteLocked(lock); |
400 |
+ |
} |
401 |
+ |
|
402 |
+ |
/** |
403 |
|
* A writelock succeeds only after reading threads unlock |
404 |
|
*/ |
405 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
406 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
406 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
407 |
> |
lock.readLock().lock(); |
408 |
|
lock.readLock().lock(); |
409 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
410 |
|
public void realRun() { |
411 |
|
lock.readLock().lock(); |
412 |
+ |
assertEquals(3, lock.getReadLockCount()); |
413 |
|
lock.readLock().unlock(); |
414 |
|
}}); |
415 |
+ |
awaitTermination(t1); |
416 |
+ |
|
417 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
418 |
|
public void realRun() { |
419 |
+ |
assertEquals(2, lock.getReadLockCount()); |
420 |
+ |
lock.writeLock().lock(); |
421 |
+ |
assertEquals(0, lock.getReadLockCount()); |
422 |
+ |
lock.writeLock().unlock(); |
423 |
+ |
}}); |
424 |
+ |
waitForQueuedThread(lock, t2); |
425 |
+ |
assertNotWriteLocked(lock); |
426 |
+ |
assertEquals(2, lock.getReadLockCount()); |
427 |
+ |
lock.readLock().unlock(); |
428 |
+ |
lock.readLock().unlock(); |
429 |
+ |
assertEquals(0, lock.getReadLockCount()); |
430 |
+ |
awaitTermination(t2); |
431 |
+ |
assertNotWriteLocked(lock); |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
/** |
435 |
+ |
* A thread that tries to acquire a fair read lock (non-reentrantly) |
436 |
+ |
* will block if there is a waiting writer thread. |
437 |
+ |
*/ |
438 |
+ |
public void testReaderWriterReaderFairFifo() throws InterruptedException { |
439 |
+ |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); |
440 |
+ |
final AtomicBoolean t1GotLock = new AtomicBoolean(false); |
441 |
+ |
|
442 |
+ |
lock.readLock().lock(); |
443 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
444 |
+ |
public void realRun() { |
445 |
+ |
assertEquals(1, lock.getReadLockCount()); |
446 |
|
lock.writeLock().lock(); |
447 |
+ |
assertEquals(0, lock.getReadLockCount()); |
448 |
+ |
t1GotLock.set(true); |
449 |
|
lock.writeLock().unlock(); |
450 |
|
}}); |
451 |
< |
delay(SHORT_DELAY_MS); |
451 |
> |
waitForQueuedThread(lock, t1); |
452 |
> |
|
453 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
454 |
> |
public void realRun() { |
455 |
> |
assertEquals(1, lock.getReadLockCount()); |
456 |
> |
lock.readLock().lock(); |
457 |
> |
assertEquals(1, lock.getReadLockCount()); |
458 |
> |
assertTrue(t1GotLock.get()); |
459 |
> |
lock.readLock().unlock(); |
460 |
> |
}}); |
461 |
> |
waitForQueuedThread(lock, t2); |
462 |
> |
assertTrue(t1.isAlive()); |
463 |
> |
assertNotWriteLocked(lock); |
464 |
> |
assertEquals(1, lock.getReadLockCount()); |
465 |
|
lock.readLock().unlock(); |
466 |
|
awaitTermination(t1); |
467 |
|
awaitTermination(t2); |
472 |
|
* Readlocks succeed only after a writing thread unlocks |
473 |
|
*/ |
474 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
475 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
475 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
476 |
|
lock.writeLock().lock(); |
477 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
478 |
|
public void realRun() { |
508 |
|
* other threads are waiting for readlock |
509 |
|
*/ |
510 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
511 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
511 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
512 |
|
lock.writeLock().lock(); |
513 |
|
lock.readLock().lock(); |
514 |
|
lock.readLock().unlock(); |
526 |
|
|
527 |
|
waitForQueuedThread(lock, t1); |
528 |
|
waitForQueuedThread(lock, t2); |
529 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
529 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
530 |
|
lock.readLock().lock(); |
531 |
|
lock.readLock().unlock(); |
532 |
|
releaseWriteLock(lock); |
539 |
|
* other threads are waiting for writelock |
540 |
|
*/ |
541 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
542 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
542 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
543 |
|
lock.writeLock().lock(); |
544 |
|
lock.readLock().lock(); |
545 |
|
lock.readLock().unlock(); |
557 |
|
|
558 |
|
waitForQueuedThread(lock, t1); |
559 |
|
waitForQueuedThread(lock, t2); |
560 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
560 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
561 |
|
lock.readLock().lock(); |
562 |
|
lock.readLock().unlock(); |
563 |
|
releaseWriteLock(lock); |
570 |
|
* other threads are waiting for writelock |
571 |
|
*/ |
572 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
573 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
573 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
574 |
|
lock.writeLock().lock(); |
575 |
|
lock.writeLock().lock(); |
576 |
|
lock.writeLock().unlock(); |
588 |
|
|
589 |
|
waitForQueuedThread(lock, t1); |
590 |
|
waitForQueuedThread(lock, t2); |
591 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
591 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
592 |
|
assertEquals(1, lock.getWriteHoldCount()); |
593 |
|
lock.writeLock().lock(); |
594 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
594 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
595 |
|
assertEquals(2, lock.getWriteHoldCount()); |
596 |
|
lock.writeLock().unlock(); |
597 |
|
releaseWriteLock(lock); |
615 |
|
* other threads are waiting for readlock |
616 |
|
*/ |
617 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
618 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
618 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); |
619 |
|
lock.writeLock().lock(); |
620 |
|
lock.readLock().lock(); |
621 |
|
lock.readLock().unlock(); |
633 |
|
|
634 |
|
waitForQueuedThread(lock, t1); |
635 |
|
waitForQueuedThread(lock, t2); |
636 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
636 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
637 |
|
lock.readLock().lock(); |
638 |
|
lock.readLock().unlock(); |
639 |
|
releaseWriteLock(lock); |
646 |
|
* other threads are waiting for writelock |
647 |
|
*/ |
648 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
649 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
649 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); |
650 |
|
lock.writeLock().lock(); |
651 |
|
lock.readLock().lock(); |
652 |
|
lock.readLock().unlock(); |
664 |
|
|
665 |
|
waitForQueuedThread(lock, t1); |
666 |
|
waitForQueuedThread(lock, t2); |
667 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
667 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
668 |
|
lock.readLock().lock(); |
669 |
|
lock.readLock().unlock(); |
670 |
|
releaseWriteLock(lock); |
677 |
|
* other threads are waiting for writelock |
678 |
|
*/ |
679 |
|
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
680 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
680 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true); |
681 |
|
lock.writeLock().lock(); |
682 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
683 |
|
public void realRun() { |
692 |
|
|
693 |
|
waitForQueuedThread(lock, t1); |
694 |
|
waitForQueuedThread(lock, t2); |
695 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
695 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
696 |
|
assertEquals(1, lock.getWriteHoldCount()); |
697 |
|
lock.writeLock().lock(); |
698 |
|
assertEquals(2, lock.getWriteHoldCount()); |
802 |
|
* write lockInterruptibly succeeds if lock free else is interruptible |
803 |
|
*/ |
804 |
|
public void testWriteLockInterruptibly() throws InterruptedException { |
805 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
805 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
806 |
|
lock.writeLock().lockInterruptibly(); |
807 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
808 |
|
public void realRun() throws InterruptedException { |
819 |
|
* read lockInterruptibly succeeds if lock free else is interruptible |
820 |
|
*/ |
821 |
|
public void testReadLockInterruptibly() throws InterruptedException { |
822 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
822 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
823 |
|
lock.writeLock().lockInterruptibly(); |
824 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
825 |
|
public void realRun() throws InterruptedException { |
984 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
985 |
|
public void realRun() throws InterruptedException { |
986 |
|
lock.writeLock().lock(); |
987 |
< |
assertTrue(lock.isWriteLocked()); |
903 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
987 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
988 |
|
assertHasNoWaiters(lock, c); |
989 |
|
locked.countDown(); |
990 |
|
try { |
991 |
|
c.await(); |
992 |
|
} finally { |
993 |
< |
assertTrue(lock.isWriteLocked()); |
910 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
993 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
994 |
|
assertHasNoWaiters(lock, c); |
995 |
|
lock.writeLock().unlock(); |
996 |
|
assertFalse(Thread.interrupted()); |
1001 |
|
assertHasWaiters(lock, c, t); |
1002 |
|
t.interrupt(); |
1003 |
|
awaitTermination(t); |
1004 |
< |
assertFalse(lock.isWriteLocked()); |
1004 |
> |
assertNotWriteLocked(lock); |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1014 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1015 |
|
public void realRun() throws InterruptedException { |
1016 |
|
lock.writeLock().lock(); |
1017 |
< |
assertTrue(lock.isWriteLocked()); |
935 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
1017 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
1018 |
|
assertHasNoWaiters(lock, c); |
1019 |
|
locked.countDown(); |
1020 |
|
try { |
1021 |
|
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
1022 |
|
} finally { |
1023 |
< |
assertTrue(lock.isWriteLocked()); |
942 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
1023 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
1024 |
|
assertHasNoWaiters(lock, c); |
1025 |
|
lock.writeLock().unlock(); |
1026 |
|
assertFalse(Thread.interrupted()); |
1031 |
|
assertHasWaiters(lock, c, t); |
1032 |
|
t.interrupt(); |
1033 |
|
awaitTermination(t); |
1034 |
< |
assertFalse(lock.isWriteLocked()); |
1034 |
> |
assertNotWriteLocked(lock); |
1035 |
|
} |
1036 |
|
|
1037 |
|
/** |
1044 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1045 |
|
public void realRun() throws InterruptedException { |
1046 |
|
lock.writeLock().lock(); |
1047 |
< |
assertTrue(lock.isWriteLocked()); |
967 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
1047 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
1048 |
|
assertHasNoWaiters(lock, c); |
1049 |
|
locked.countDown(); |
1050 |
|
java.util.Date d = new java.util.Date(); |
1051 |
|
try { |
1052 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1053 |
|
} finally { |
1054 |
< |
assertTrue(lock.isWriteLocked()); |
975 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
1054 |
> |
assertWriteLockedBy(lock, Thread.currentThread()); |
1055 |
|
assertHasNoWaiters(lock, c); |
1056 |
|
lock.writeLock().unlock(); |
1057 |
|
assertFalse(Thread.interrupted()); |
1062 |
|
assertHasWaiters(lock, c, t); |
1063 |
|
t.interrupt(); |
1064 |
|
awaitTermination(t); |
1065 |
< |
assertFalse(lock.isWriteLocked()); |
1065 |
> |
assertNotWriteLocked(lock); |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1168 |
|
* hasQueuedThreads reports whether there are waiting threads |
1169 |
|
*/ |
1170 |
|
public void testhasQueuedThreads() throws InterruptedException { |
1171 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1171 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1172 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1173 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1174 |
|
assertFalse(lock.hasQueuedThreads()); |
1203 |
|
* hasQueuedThread reports whether a thread is queued. |
1204 |
|
*/ |
1205 |
|
public void testHasQueuedThread() throws InterruptedException { |
1206 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1206 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1207 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1208 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1209 |
|
assertFalse(lock.hasQueuedThread(t1)); |
1232 |
|
* getQueueLength reports number of waiting threads |
1233 |
|
*/ |
1234 |
|
public void testGetQueueLength() throws InterruptedException { |
1235 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1235 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1236 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1237 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1238 |
|
assertEquals(0, lock.getQueueLength()); |