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