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.DAYS; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.Future; |
16 |
> |
import java.util.concurrent.TimeUnit; |
17 |
|
import java.util.concurrent.locks.Lock; |
14 |
– |
import java.util.concurrent.locks.ReadWriteLock; |
18 |
|
import java.util.concurrent.locks.StampedLock; |
19 |
< |
import java.util.concurrent.CountDownLatch; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
< |
import java.util.*; |
19 |
> |
import java.util.function.BiConsumer; |
20 |
> |
import java.util.function.Function; |
21 |
> |
|
22 |
> |
import junit.framework.Test; |
23 |
> |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
public class StampedLockTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(StampedLockTest.class); |
31 |
|
} |
32 |
|
|
33 |
< |
// XXXX Just a skeleton implementation for now. |
34 |
< |
public void testTODO() { |
35 |
< |
fail("Please add some real tests!"); |
36 |
< |
} |
37 |
< |
|
38 |
< |
public void testConstructor() { new StampedLock(); } |
39 |
< |
|
40 |
< |
// /** |
41 |
< |
// * A runnable calling lockInterruptibly |
42 |
< |
// */ |
43 |
< |
// class InterruptibleLockRunnable extends CheckedRunnable { |
44 |
< |
// final ReentrantReadWriteLock lock; |
45 |
< |
// InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
46 |
< |
// public void realRun() throws InterruptedException { |
47 |
< |
// lock.writeLock().lockInterruptibly(); |
48 |
< |
// } |
49 |
< |
// } |
50 |
< |
|
51 |
< |
// /** |
52 |
< |
// * A runnable calling lockInterruptibly that expects to be |
53 |
< |
// * interrupted |
54 |
< |
// */ |
55 |
< |
// class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
56 |
< |
// final ReentrantReadWriteLock lock; |
57 |
< |
// InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
58 |
< |
// public void realRun() throws InterruptedException { |
59 |
< |
// lock.writeLock().lockInterruptibly(); |
60 |
< |
// } |
61 |
< |
// } |
62 |
< |
|
63 |
< |
// /** |
64 |
< |
// * Subclass to expose protected methods |
65 |
< |
// */ |
66 |
< |
// static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
67 |
< |
// PublicReentrantReadWriteLock() { super(); } |
68 |
< |
// PublicReentrantReadWriteLock(boolean fair) { super(fair); } |
69 |
< |
// public Thread getOwner() { |
70 |
< |
// return super.getOwner(); |
71 |
< |
// } |
72 |
< |
// public Collection<Thread> getQueuedThreads() { |
73 |
< |
// return super.getQueuedThreads(); |
74 |
< |
// } |
75 |
< |
// public Collection<Thread> getWaitingThreads(Condition c) { |
76 |
< |
// return super.getWaitingThreads(c); |
77 |
< |
// } |
78 |
< |
// } |
79 |
< |
|
80 |
< |
// /** |
81 |
< |
// * Releases write lock, checking that it had a hold count of 1. |
82 |
< |
// */ |
83 |
< |
// void releaseWriteLock(PublicReentrantReadWriteLock lock) { |
84 |
< |
// ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); |
85 |
< |
// assertWriteLockedByMoi(lock); |
86 |
< |
// assertEquals(1, lock.getWriteHoldCount()); |
87 |
< |
// writeLock.unlock(); |
88 |
< |
// assertNotWriteLocked(lock); |
89 |
< |
// } |
90 |
< |
|
91 |
< |
// /** |
92 |
< |
// * Spin-waits until lock.hasQueuedThread(t) becomes true. |
93 |
< |
// */ |
94 |
< |
// void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) { |
95 |
< |
// long startTime = System.nanoTime(); |
96 |
< |
// while (!lock.hasQueuedThread(t)) { |
97 |
< |
// if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
98 |
< |
// throw new AssertionFailedError("timed out"); |
99 |
< |
// Thread.yield(); |
100 |
< |
// } |
101 |
< |
// assertTrue(t.isAlive()); |
102 |
< |
// assertTrue(lock.getOwner() != t); |
103 |
< |
// } |
104 |
< |
|
105 |
< |
// /** |
106 |
< |
// * Checks that lock is not write-locked. |
107 |
< |
// */ |
108 |
< |
// void assertNotWriteLocked(PublicReentrantReadWriteLock lock) { |
109 |
< |
// assertFalse(lock.isWriteLocked()); |
110 |
< |
// assertFalse(lock.isWriteLockedByCurrentThread()); |
111 |
< |
// assertFalse(lock.writeLock().isHeldByCurrentThread()); |
112 |
< |
// assertEquals(0, lock.getWriteHoldCount()); |
113 |
< |
// assertEquals(0, lock.writeLock().getHoldCount()); |
114 |
< |
// assertNull(lock.getOwner()); |
115 |
< |
// } |
116 |
< |
|
117 |
< |
// /** |
118 |
< |
// * Checks that lock is write-locked by the given thread. |
119 |
< |
// */ |
120 |
< |
// void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) { |
121 |
< |
// assertTrue(lock.isWriteLocked()); |
122 |
< |
// assertSame(t, lock.getOwner()); |
123 |
< |
// assertEquals(t == Thread.currentThread(), |
124 |
< |
// lock.isWriteLockedByCurrentThread()); |
125 |
< |
// assertEquals(t == Thread.currentThread(), |
126 |
< |
// lock.writeLock().isHeldByCurrentThread()); |
127 |
< |
// assertEquals(t == Thread.currentThread(), |
128 |
< |
// lock.getWriteHoldCount() > 0); |
129 |
< |
// assertEquals(t == Thread.currentThread(), |
130 |
< |
// lock.writeLock().getHoldCount() > 0); |
131 |
< |
// assertEquals(0, lock.getReadLockCount()); |
132 |
< |
// } |
133 |
< |
|
134 |
< |
// /** |
135 |
< |
// * Checks that lock is write-locked by the current thread. |
136 |
< |
// */ |
137 |
< |
// void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) { |
138 |
< |
// assertWriteLockedBy(lock, Thread.currentThread()); |
139 |
< |
// } |
140 |
< |
|
141 |
< |
// /** |
142 |
< |
// * Checks that condition c has no waiters. |
143 |
< |
// */ |
144 |
< |
// void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) { |
145 |
< |
// assertHasWaiters(lock, c, new Thread[] {}); |
146 |
< |
// } |
147 |
< |
|
148 |
< |
// /** |
149 |
< |
// * Checks that condition c has exactly the given waiter threads. |
150 |
< |
// */ |
151 |
< |
// void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c, |
152 |
< |
// Thread... threads) { |
153 |
< |
// lock.writeLock().lock(); |
154 |
< |
// assertEquals(threads.length > 0, lock.hasWaiters(c)); |
155 |
< |
// assertEquals(threads.length, lock.getWaitQueueLength(c)); |
156 |
< |
// assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); |
157 |
< |
// assertEquals(threads.length, lock.getWaitingThreads(c).size()); |
158 |
< |
// assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)), |
159 |
< |
// new HashSet<Thread>(Arrays.asList(threads))); |
160 |
< |
// lock.writeLock().unlock(); |
161 |
< |
// } |
162 |
< |
|
163 |
< |
// enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
164 |
< |
|
165 |
< |
// /** |
166 |
< |
// * Awaits condition using the specified AwaitMethod. |
167 |
< |
// */ |
168 |
< |
// void await(Condition c, AwaitMethod awaitMethod) |
169 |
< |
// throws InterruptedException { |
170 |
< |
// switch (awaitMethod) { |
171 |
< |
// case await: |
172 |
< |
// c.await(); |
173 |
< |
// break; |
174 |
< |
// case awaitTimed: |
175 |
< |
// assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
176 |
< |
// break; |
177 |
< |
// case awaitNanos: |
178 |
< |
// long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
179 |
< |
// assertTrue(nanosRemaining > 0); |
180 |
< |
// break; |
181 |
< |
// case awaitUntil: |
182 |
< |
// java.util.Date d = new java.util.Date(); |
183 |
< |
// assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
184 |
< |
// break; |
185 |
< |
// } |
186 |
< |
// } |
187 |
< |
|
188 |
< |
// /** |
189 |
< |
// * Constructor sets given fairness, and is in unlocked state |
190 |
< |
// */ |
191 |
< |
// public void testConstructor() { |
192 |
< |
// PublicReentrantReadWriteLock lock; |
193 |
< |
|
194 |
< |
// lock = new PublicReentrantReadWriteLock(); |
195 |
< |
// assertFalse(lock.isFair()); |
196 |
< |
// assertNotWriteLocked(lock); |
197 |
< |
// assertEquals(0, lock.getReadLockCount()); |
198 |
< |
|
199 |
< |
// lock = new PublicReentrantReadWriteLock(true); |
200 |
< |
// assertTrue(lock.isFair()); |
201 |
< |
// assertNotWriteLocked(lock); |
202 |
< |
// assertEquals(0, lock.getReadLockCount()); |
203 |
< |
|
204 |
< |
// lock = new PublicReentrantReadWriteLock(false); |
205 |
< |
// assertFalse(lock.isFair()); |
206 |
< |
// assertNotWriteLocked(lock); |
207 |
< |
// assertEquals(0, lock.getReadLockCount()); |
208 |
< |
// } |
209 |
< |
|
210 |
< |
// /** |
211 |
< |
// * write-locking and read-locking an unlocked lock succeed |
212 |
< |
// */ |
213 |
< |
// public void testLock() { testLock(false); } |
214 |
< |
// public void testLock_fair() { testLock(true); } |
215 |
< |
// public void testLock(boolean fair) { |
216 |
< |
// PublicReentrantReadWriteLock lock = |
217 |
< |
// new PublicReentrantReadWriteLock(fair); |
218 |
< |
// assertNotWriteLocked(lock); |
219 |
< |
// lock.writeLock().lock(); |
220 |
< |
// assertWriteLockedByMoi(lock); |
221 |
< |
// lock.writeLock().unlock(); |
222 |
< |
// assertNotWriteLocked(lock); |
223 |
< |
// assertEquals(0, lock.getReadLockCount()); |
224 |
< |
// lock.readLock().lock(); |
225 |
< |
// assertNotWriteLocked(lock); |
226 |
< |
// assertEquals(1, lock.getReadLockCount()); |
227 |
< |
// lock.readLock().unlock(); |
228 |
< |
// assertNotWriteLocked(lock); |
229 |
< |
// assertEquals(0, lock.getReadLockCount()); |
230 |
< |
// } |
231 |
< |
|
232 |
< |
// /** |
233 |
< |
// * getWriteHoldCount returns number of recursive holds |
234 |
< |
// */ |
235 |
< |
// public void testGetWriteHoldCount() { testGetWriteHoldCount(false); } |
236 |
< |
// public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); } |
237 |
< |
// public void testGetWriteHoldCount(boolean fair) { |
238 |
< |
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
239 |
< |
// for (int i = 1; i <= SIZE; i++) { |
240 |
< |
// lock.writeLock().lock(); |
241 |
< |
// assertEquals(i,lock.getWriteHoldCount()); |
242 |
< |
// } |
243 |
< |
// for (int i = SIZE; i > 0; i--) { |
244 |
< |
// lock.writeLock().unlock(); |
245 |
< |
// assertEquals(i-1,lock.getWriteHoldCount()); |
246 |
< |
// } |
247 |
< |
// } |
248 |
< |
|
249 |
< |
// /** |
250 |
< |
// * writelock.getHoldCount returns number of recursive holds |
251 |
< |
// */ |
252 |
< |
// public void testGetHoldCount() { testGetHoldCount(false); } |
253 |
< |
// public void testGetHoldCount_fair() { testGetHoldCount(true); } |
254 |
< |
// public void testGetHoldCount(boolean fair) { |
255 |
< |
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
256 |
< |
// for (int i = 1; i <= SIZE; i++) { |
257 |
< |
// lock.writeLock().lock(); |
258 |
< |
// assertEquals(i,lock.writeLock().getHoldCount()); |
259 |
< |
// } |
260 |
< |
// for (int i = SIZE; i > 0; i--) { |
261 |
< |
// lock.writeLock().unlock(); |
262 |
< |
// assertEquals(i-1,lock.writeLock().getHoldCount()); |
263 |
< |
// } |
264 |
< |
// } |
265 |
< |
|
266 |
< |
// /** |
267 |
< |
// * getReadHoldCount returns number of recursive holds |
268 |
< |
// */ |
269 |
< |
// public void testGetReadHoldCount() { testGetReadHoldCount(false); } |
270 |
< |
// public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); } |
271 |
< |
// public void testGetReadHoldCount(boolean fair) { |
272 |
< |
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
273 |
< |
// for (int i = 1; i <= SIZE; i++) { |
274 |
< |
// lock.readLock().lock(); |
275 |
< |
// assertEquals(i,lock.getReadHoldCount()); |
276 |
< |
// } |
277 |
< |
// for (int i = SIZE; i > 0; i--) { |
278 |
< |
// lock.readLock().unlock(); |
279 |
< |
// assertEquals(i-1,lock.getReadHoldCount()); |
280 |
< |
// } |
281 |
< |
// } |
282 |
< |
|
283 |
< |
// /** |
284 |
< |
// * write-unlocking an unlocked lock throws IllegalMonitorStateException |
285 |
< |
// */ |
286 |
< |
// public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); } |
287 |
< |
// public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); } |
288 |
< |
// public void testWriteUnlock_IMSE(boolean fair) { |
289 |
< |
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
290 |
< |
// try { |
291 |
< |
// lock.writeLock().unlock(); |
292 |
< |
// shouldThrow(); |
293 |
< |
// } catch (IllegalMonitorStateException success) {} |
294 |
< |
// } |
295 |
< |
|
296 |
< |
// /** |
297 |
< |
// * read-unlocking an unlocked lock throws IllegalMonitorStateException |
298 |
< |
// */ |
299 |
< |
// public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); } |
300 |
< |
// public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); } |
301 |
< |
// public void testReadUnlock_IMSE(boolean fair) { |
302 |
< |
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
303 |
< |
// try { |
304 |
< |
// lock.readLock().unlock(); |
305 |
< |
// shouldThrow(); |
306 |
< |
// } catch (IllegalMonitorStateException success) {} |
307 |
< |
// } |
308 |
< |
|
309 |
< |
// /** |
310 |
< |
// * write-lockInterruptibly is interruptible |
311 |
< |
// */ |
312 |
< |
// public void testWriteLockInterruptibly_Interruptible() { testWriteLockInterruptibly_Interruptible(false); } |
313 |
< |
// public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); } |
314 |
< |
// public void testWriteLockInterruptibly_Interruptible(boolean fair) { |
315 |
< |
// final PublicReentrantReadWriteLock lock = |
316 |
< |
// new PublicReentrantReadWriteLock(fair); |
317 |
< |
// lock.writeLock().lock(); |
318 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
319 |
< |
// public void realRun() throws InterruptedException { |
320 |
< |
// lock.writeLock().lockInterruptibly(); |
321 |
< |
// }}); |
322 |
< |
|
323 |
< |
// waitForQueuedThread(lock, t); |
324 |
< |
// t.interrupt(); |
325 |
< |
// awaitTermination(t); |
326 |
< |
// releaseWriteLock(lock); |
327 |
< |
// } |
328 |
< |
|
329 |
< |
// /** |
330 |
< |
// * timed write-tryLock is interruptible |
331 |
< |
// */ |
332 |
< |
// public void testWriteTryLock_Interruptible() { testWriteTryLock_Interruptible(false); } |
333 |
< |
// public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); } |
334 |
< |
// public void testWriteTryLock_Interruptible(boolean fair) { |
335 |
< |
// final PublicReentrantReadWriteLock lock = |
336 |
< |
// new PublicReentrantReadWriteLock(fair); |
337 |
< |
// lock.writeLock().lock(); |
338 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
339 |
< |
// public void realRun() throws InterruptedException { |
340 |
< |
// lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
341 |
< |
// }}); |
342 |
< |
|
343 |
< |
// waitForQueuedThread(lock, t); |
344 |
< |
// t.interrupt(); |
345 |
< |
// awaitTermination(t); |
346 |
< |
// releaseWriteLock(lock); |
347 |
< |
// } |
348 |
< |
|
349 |
< |
// /** |
350 |
< |
// * read-lockInterruptibly is interruptible |
351 |
< |
// */ |
352 |
< |
// public void testReadLockInterruptibly_Interruptible() { testReadLockInterruptibly_Interruptible(false); } |
353 |
< |
// public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); } |
354 |
< |
// public void testReadLockInterruptibly_Interruptible(boolean fair) { |
355 |
< |
// final PublicReentrantReadWriteLock lock = |
356 |
< |
// new PublicReentrantReadWriteLock(fair); |
357 |
< |
// lock.writeLock().lock(); |
358 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
359 |
< |
// public void realRun() throws InterruptedException { |
360 |
< |
// lock.readLock().lockInterruptibly(); |
361 |
< |
// }}); |
362 |
< |
|
363 |
< |
// waitForQueuedThread(lock, t); |
364 |
< |
// t.interrupt(); |
365 |
< |
// awaitTermination(t); |
366 |
< |
// releaseWriteLock(lock); |
367 |
< |
// } |
368 |
< |
|
369 |
< |
// /** |
370 |
< |
// * timed read-tryLock is interruptible |
371 |
< |
// */ |
372 |
< |
// public void testReadTryLock_Interruptible() { testReadTryLock_Interruptible(false); } |
373 |
< |
// public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); } |
374 |
< |
// public void testReadTryLock_Interruptible(boolean fair) { |
375 |
< |
// final PublicReentrantReadWriteLock lock = |
376 |
< |
// new PublicReentrantReadWriteLock(fair); |
377 |
< |
// lock.writeLock().lock(); |
378 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
379 |
< |
// public void realRun() throws InterruptedException { |
380 |
< |
// lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
381 |
< |
// }}); |
382 |
< |
|
383 |
< |
// waitForQueuedThread(lock, t); |
384 |
< |
// t.interrupt(); |
385 |
< |
// awaitTermination(t); |
386 |
< |
// releaseWriteLock(lock); |
387 |
< |
// } |
388 |
< |
|
389 |
< |
// /** |
390 |
< |
// * write-tryLock on an unlocked lock succeeds |
391 |
< |
// */ |
392 |
< |
// public void testWriteTryLock() { testWriteTryLock(false); } |
393 |
< |
// public void testWriteTryLock_fair() { testWriteTryLock(true); } |
394 |
< |
// public void testWriteTryLock(boolean fair) { |
395 |
< |
// final PublicReentrantReadWriteLock lock = |
396 |
< |
// new PublicReentrantReadWriteLock(fair); |
397 |
< |
// assertTrue(lock.writeLock().tryLock()); |
398 |
< |
// assertWriteLockedByMoi(lock); |
399 |
< |
// assertTrue(lock.writeLock().tryLock()); |
400 |
< |
// assertWriteLockedByMoi(lock); |
401 |
< |
// lock.writeLock().unlock(); |
402 |
< |
// releaseWriteLock(lock); |
403 |
< |
// } |
404 |
< |
|
405 |
< |
// /** |
406 |
< |
// * write-tryLock fails if locked |
407 |
< |
// */ |
408 |
< |
// public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); } |
409 |
< |
// public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); } |
410 |
< |
// public void testWriteTryLockWhenLocked(boolean fair) { |
411 |
< |
// final PublicReentrantReadWriteLock lock = |
412 |
< |
// new PublicReentrantReadWriteLock(fair); |
413 |
< |
// lock.writeLock().lock(); |
414 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
415 |
< |
// public void realRun() { |
416 |
< |
// assertFalse(lock.writeLock().tryLock()); |
417 |
< |
// }}); |
418 |
< |
|
419 |
< |
// awaitTermination(t); |
420 |
< |
// releaseWriteLock(lock); |
421 |
< |
// } |
422 |
< |
|
423 |
< |
// /** |
424 |
< |
// * read-tryLock fails if locked |
425 |
< |
// */ |
426 |
< |
// public void testReadTryLockWhenLocked() { testReadTryLockWhenLocked(false); } |
427 |
< |
// public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); } |
428 |
< |
// public void testReadTryLockWhenLocked(boolean fair) { |
429 |
< |
// final PublicReentrantReadWriteLock lock = |
430 |
< |
// new PublicReentrantReadWriteLock(fair); |
431 |
< |
// lock.writeLock().lock(); |
432 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
433 |
< |
// public void realRun() { |
434 |
< |
// assertFalse(lock.readLock().tryLock()); |
435 |
< |
// }}); |
436 |
< |
|
437 |
< |
// awaitTermination(t); |
438 |
< |
// releaseWriteLock(lock); |
439 |
< |
// } |
440 |
< |
|
441 |
< |
// /** |
442 |
< |
// * Multiple threads can hold a read lock when not write-locked |
443 |
< |
// */ |
444 |
< |
// public void testMultipleReadLocks() { testMultipleReadLocks(false); } |
445 |
< |
// public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); } |
446 |
< |
// public void testMultipleReadLocks(boolean fair) { |
447 |
< |
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
448 |
< |
// lock.readLock().lock(); |
449 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
450 |
< |
// public void realRun() throws InterruptedException { |
451 |
< |
// assertTrue(lock.readLock().tryLock()); |
452 |
< |
// lock.readLock().unlock(); |
453 |
< |
// assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS)); |
454 |
< |
// lock.readLock().unlock(); |
455 |
< |
// lock.readLock().lock(); |
456 |
< |
// lock.readLock().unlock(); |
457 |
< |
// }}); |
458 |
< |
|
459 |
< |
// awaitTermination(t); |
460 |
< |
// lock.readLock().unlock(); |
461 |
< |
// } |
462 |
< |
|
463 |
< |
// /** |
464 |
< |
// * A writelock succeeds only after a reading thread unlocks |
465 |
< |
// */ |
466 |
< |
// public void testWriteAfterReadLock() { testWriteAfterReadLock(false); } |
467 |
< |
// public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); } |
468 |
< |
// public void testWriteAfterReadLock(boolean fair) { |
469 |
< |
// final PublicReentrantReadWriteLock lock = |
470 |
< |
// new PublicReentrantReadWriteLock(fair); |
471 |
< |
// lock.readLock().lock(); |
472 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
468 |
< |
// public void realRun() { |
469 |
< |
// assertEquals(1, lock.getReadLockCount()); |
470 |
< |
// lock.writeLock().lock(); |
471 |
< |
// assertEquals(0, lock.getReadLockCount()); |
472 |
< |
// lock.writeLock().unlock(); |
473 |
< |
// }}); |
474 |
< |
// waitForQueuedThread(lock, t); |
475 |
< |
// assertNotWriteLocked(lock); |
476 |
< |
// assertEquals(1, lock.getReadLockCount()); |
477 |
< |
// lock.readLock().unlock(); |
478 |
< |
// assertEquals(0, lock.getReadLockCount()); |
479 |
< |
// awaitTermination(t); |
480 |
< |
// assertNotWriteLocked(lock); |
481 |
< |
// } |
482 |
< |
|
483 |
< |
// /** |
484 |
< |
// * A writelock succeeds only after reading threads unlock |
485 |
< |
// */ |
486 |
< |
// public void testWriteAfterMultipleReadLocks() { testWriteAfterMultipleReadLocks(false); } |
487 |
< |
// public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); } |
488 |
< |
// public void testWriteAfterMultipleReadLocks(boolean fair) { |
489 |
< |
// final PublicReentrantReadWriteLock lock = |
490 |
< |
// new PublicReentrantReadWriteLock(fair); |
491 |
< |
// lock.readLock().lock(); |
492 |
< |
// lock.readLock().lock(); |
493 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
494 |
< |
// public void realRun() { |
495 |
< |
// lock.readLock().lock(); |
496 |
< |
// assertEquals(3, lock.getReadLockCount()); |
497 |
< |
// lock.readLock().unlock(); |
498 |
< |
// }}); |
499 |
< |
// awaitTermination(t1); |
500 |
< |
|
501 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
502 |
< |
// public void realRun() { |
503 |
< |
// assertEquals(2, lock.getReadLockCount()); |
504 |
< |
// lock.writeLock().lock(); |
505 |
< |
// assertEquals(0, lock.getReadLockCount()); |
506 |
< |
// lock.writeLock().unlock(); |
507 |
< |
// }}); |
508 |
< |
// waitForQueuedThread(lock, t2); |
509 |
< |
// assertNotWriteLocked(lock); |
510 |
< |
// assertEquals(2, lock.getReadLockCount()); |
511 |
< |
// lock.readLock().unlock(); |
512 |
< |
// lock.readLock().unlock(); |
513 |
< |
// assertEquals(0, lock.getReadLockCount()); |
514 |
< |
// awaitTermination(t2); |
515 |
< |
// assertNotWriteLocked(lock); |
516 |
< |
// } |
517 |
< |
|
518 |
< |
// /** |
519 |
< |
// * A thread that tries to acquire a fair read lock (non-reentrantly) |
520 |
< |
// * will block if there is a waiting writer thread |
521 |
< |
// */ |
522 |
< |
// public void testReaderWriterReaderFairFifo() { |
523 |
< |
// final PublicReentrantReadWriteLock lock = |
524 |
< |
// new PublicReentrantReadWriteLock(true); |
525 |
< |
// final AtomicBoolean t1GotLock = new AtomicBoolean(false); |
526 |
< |
|
527 |
< |
// lock.readLock().lock(); |
528 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
529 |
< |
// public void realRun() { |
530 |
< |
// assertEquals(1, lock.getReadLockCount()); |
531 |
< |
// lock.writeLock().lock(); |
532 |
< |
// assertEquals(0, lock.getReadLockCount()); |
533 |
< |
// t1GotLock.set(true); |
534 |
< |
// lock.writeLock().unlock(); |
535 |
< |
// }}); |
536 |
< |
// waitForQueuedThread(lock, t1); |
537 |
< |
|
538 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
539 |
< |
// public void realRun() { |
540 |
< |
// assertEquals(1, lock.getReadLockCount()); |
541 |
< |
// lock.readLock().lock(); |
542 |
< |
// assertEquals(1, lock.getReadLockCount()); |
543 |
< |
// assertTrue(t1GotLock.get()); |
544 |
< |
// lock.readLock().unlock(); |
545 |
< |
// }}); |
546 |
< |
// waitForQueuedThread(lock, t2); |
547 |
< |
// assertTrue(t1.isAlive()); |
548 |
< |
// assertNotWriteLocked(lock); |
549 |
< |
// assertEquals(1, lock.getReadLockCount()); |
550 |
< |
// lock.readLock().unlock(); |
551 |
< |
// awaitTermination(t1); |
552 |
< |
// awaitTermination(t2); |
553 |
< |
// assertNotWriteLocked(lock); |
554 |
< |
// } |
555 |
< |
|
556 |
< |
// /** |
557 |
< |
// * Readlocks succeed only after a writing thread unlocks |
558 |
< |
// */ |
559 |
< |
// public void testReadAfterWriteLock() { testReadAfterWriteLock(false); } |
560 |
< |
// public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); } |
561 |
< |
// public void testReadAfterWriteLock(boolean fair) { |
562 |
< |
// final PublicReentrantReadWriteLock lock = |
563 |
< |
// new PublicReentrantReadWriteLock(fair); |
564 |
< |
// lock.writeLock().lock(); |
565 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
566 |
< |
// public void realRun() { |
567 |
< |
// lock.readLock().lock(); |
568 |
< |
// lock.readLock().unlock(); |
569 |
< |
// }}); |
570 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
571 |
< |
// public void realRun() { |
572 |
< |
// lock.readLock().lock(); |
573 |
< |
// lock.readLock().unlock(); |
574 |
< |
// }}); |
575 |
< |
|
576 |
< |
// waitForQueuedThread(lock, t1); |
577 |
< |
// waitForQueuedThread(lock, t2); |
578 |
< |
// releaseWriteLock(lock); |
579 |
< |
// awaitTermination(t1); |
580 |
< |
// awaitTermination(t2); |
581 |
< |
// } |
582 |
< |
|
583 |
< |
// /** |
584 |
< |
// * Read trylock succeeds if write locked by current thread |
585 |
< |
// */ |
586 |
< |
// public void testReadHoldingWriteLock() { testReadHoldingWriteLock(false); } |
587 |
< |
// public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); } |
588 |
< |
// public void testReadHoldingWriteLock(boolean fair) { |
589 |
< |
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
590 |
< |
// lock.writeLock().lock(); |
591 |
< |
// assertTrue(lock.readLock().tryLock()); |
592 |
< |
// lock.readLock().unlock(); |
593 |
< |
// lock.writeLock().unlock(); |
594 |
< |
// } |
595 |
< |
|
596 |
< |
// /** |
597 |
< |
// * Read trylock succeeds (barging) even in the presence of waiting |
598 |
< |
// * readers and/or writers |
599 |
< |
// */ |
600 |
< |
// public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
601 |
< |
// public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
602 |
< |
// public void testReadTryLockBarging(boolean fair) { |
603 |
< |
// final PublicReentrantReadWriteLock lock = |
604 |
< |
// new PublicReentrantReadWriteLock(fair); |
605 |
< |
// lock.readLock().lock(); |
606 |
< |
|
607 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
608 |
< |
// public void realRun() { |
609 |
< |
// lock.writeLock().lock(); |
610 |
< |
// lock.writeLock().unlock(); |
611 |
< |
// }}); |
612 |
< |
|
613 |
< |
// waitForQueuedThread(lock, t1); |
614 |
< |
|
615 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
616 |
< |
// public void realRun() { |
617 |
< |
// lock.readLock().lock(); |
618 |
< |
// lock.readLock().unlock(); |
619 |
< |
// }}); |
620 |
< |
|
621 |
< |
// if (fair) |
622 |
< |
// waitForQueuedThread(lock, t2); |
623 |
< |
|
624 |
< |
// Thread t3 = newStartedThread(new CheckedRunnable() { |
625 |
< |
// public void realRun() { |
626 |
< |
// lock.readLock().tryLock(); |
627 |
< |
// lock.readLock().unlock(); |
628 |
< |
// }}); |
629 |
< |
|
630 |
< |
// assertTrue(lock.getReadLockCount() > 0); |
631 |
< |
// awaitTermination(t3); |
632 |
< |
// assertTrue(t1.isAlive()); |
633 |
< |
// if (fair) assertTrue(t2.isAlive()); |
634 |
< |
// lock.readLock().unlock(); |
635 |
< |
// awaitTermination(t1); |
636 |
< |
// awaitTermination(t2); |
637 |
< |
// } |
638 |
< |
|
639 |
< |
// /** |
640 |
< |
// * Read lock succeeds if write locked by current thread even if |
641 |
< |
// * other threads are waiting for readlock |
642 |
< |
// */ |
643 |
< |
// public void testReadHoldingWriteLock2() { testReadHoldingWriteLock2(false); } |
644 |
< |
// public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); } |
645 |
< |
// public void testReadHoldingWriteLock2(boolean fair) { |
646 |
< |
// final PublicReentrantReadWriteLock lock = |
647 |
< |
// new PublicReentrantReadWriteLock(fair); |
648 |
< |
// lock.writeLock().lock(); |
649 |
< |
// lock.readLock().lock(); |
650 |
< |
// lock.readLock().unlock(); |
651 |
< |
|
652 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
653 |
< |
// public void realRun() { |
654 |
< |
// lock.readLock().lock(); |
655 |
< |
// lock.readLock().unlock(); |
656 |
< |
// }}); |
657 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
658 |
< |
// public void realRun() { |
659 |
< |
// lock.readLock().lock(); |
660 |
< |
// lock.readLock().unlock(); |
661 |
< |
// }}); |
662 |
< |
|
663 |
< |
// waitForQueuedThread(lock, t1); |
664 |
< |
// waitForQueuedThread(lock, t2); |
665 |
< |
// assertWriteLockedByMoi(lock); |
666 |
< |
// lock.readLock().lock(); |
667 |
< |
// lock.readLock().unlock(); |
668 |
< |
// releaseWriteLock(lock); |
669 |
< |
// awaitTermination(t1); |
670 |
< |
// awaitTermination(t2); |
671 |
< |
// } |
672 |
< |
|
673 |
< |
// /** |
674 |
< |
// * Read lock succeeds if write locked by current thread even if |
675 |
< |
// * other threads are waiting for writelock |
676 |
< |
// */ |
677 |
< |
// public void testReadHoldingWriteLock3() { testReadHoldingWriteLock3(false); } |
678 |
< |
// public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); } |
679 |
< |
// public void testReadHoldingWriteLock3(boolean fair) { |
680 |
< |
// final PublicReentrantReadWriteLock lock = |
681 |
< |
// new PublicReentrantReadWriteLock(fair); |
682 |
< |
// lock.writeLock().lock(); |
683 |
< |
// lock.readLock().lock(); |
684 |
< |
// lock.readLock().unlock(); |
685 |
< |
|
686 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
687 |
< |
// public void realRun() { |
688 |
< |
// lock.writeLock().lock(); |
689 |
< |
// lock.writeLock().unlock(); |
690 |
< |
// }}); |
691 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
692 |
< |
// public void realRun() { |
693 |
< |
// lock.writeLock().lock(); |
694 |
< |
// lock.writeLock().unlock(); |
695 |
< |
// }}); |
696 |
< |
|
697 |
< |
// waitForQueuedThread(lock, t1); |
698 |
< |
// waitForQueuedThread(lock, t2); |
699 |
< |
// assertWriteLockedByMoi(lock); |
700 |
< |
// lock.readLock().lock(); |
701 |
< |
// lock.readLock().unlock(); |
702 |
< |
// assertWriteLockedByMoi(lock); |
703 |
< |
// lock.writeLock().unlock(); |
704 |
< |
// awaitTermination(t1); |
705 |
< |
// awaitTermination(t2); |
706 |
< |
// } |
707 |
< |
|
708 |
< |
// /** |
709 |
< |
// * Write lock succeeds if write locked by current thread even if |
710 |
< |
// * other threads are waiting for writelock |
711 |
< |
// */ |
712 |
< |
// public void testWriteHoldingWriteLock4() { testWriteHoldingWriteLock4(false); } |
713 |
< |
// public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); } |
714 |
< |
// public void testWriteHoldingWriteLock4(boolean fair) { |
715 |
< |
// final PublicReentrantReadWriteLock lock = |
716 |
< |
// new PublicReentrantReadWriteLock(fair); |
717 |
< |
// lock.writeLock().lock(); |
718 |
< |
// lock.writeLock().lock(); |
719 |
< |
// lock.writeLock().unlock(); |
720 |
< |
|
721 |
< |
// Thread t1 = newStartedThread(new CheckedRunnable() { |
722 |
< |
// public void realRun() { |
723 |
< |
// lock.writeLock().lock(); |
724 |
< |
// lock.writeLock().unlock(); |
725 |
< |
// }}); |
726 |
< |
// Thread t2 = newStartedThread(new CheckedRunnable() { |
727 |
< |
// public void realRun() { |
728 |
< |
// lock.writeLock().lock(); |
729 |
< |
// lock.writeLock().unlock(); |
730 |
< |
// }}); |
731 |
< |
|
732 |
< |
// waitForQueuedThread(lock, t1); |
733 |
< |
// waitForQueuedThread(lock, t2); |
734 |
< |
// assertWriteLockedByMoi(lock); |
735 |
< |
// assertEquals(1, lock.getWriteHoldCount()); |
736 |
< |
// lock.writeLock().lock(); |
737 |
< |
// assertWriteLockedByMoi(lock); |
738 |
< |
// assertEquals(2, lock.getWriteHoldCount()); |
739 |
< |
// lock.writeLock().unlock(); |
740 |
< |
// assertWriteLockedByMoi(lock); |
741 |
< |
// assertEquals(1, lock.getWriteHoldCount()); |
742 |
< |
// lock.writeLock().unlock(); |
743 |
< |
// awaitTermination(t1); |
744 |
< |
// awaitTermination(t2); |
745 |
< |
// } |
746 |
< |
|
747 |
< |
// /** |
748 |
< |
// * Read tryLock succeeds if readlocked but not writelocked |
749 |
< |
// */ |
750 |
< |
// public void testTryLockWhenReadLocked() { testTryLockWhenReadLocked(false); } |
751 |
< |
// public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); } |
752 |
< |
// public void testTryLockWhenReadLocked(boolean fair) { |
753 |
< |
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
754 |
< |
// lock.readLock().lock(); |
755 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
756 |
< |
// public void realRun() { |
757 |
< |
// assertTrue(lock.readLock().tryLock()); |
758 |
< |
// lock.readLock().unlock(); |
759 |
< |
// }}); |
760 |
< |
|
761 |
< |
// awaitTermination(t); |
762 |
< |
// lock.readLock().unlock(); |
763 |
< |
// } |
764 |
< |
|
765 |
< |
// /** |
766 |
< |
// * write tryLock fails when readlocked |
767 |
< |
// */ |
768 |
< |
// public void testWriteTryLockWhenReadLocked() { testWriteTryLockWhenReadLocked(false); } |
769 |
< |
// public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); } |
770 |
< |
// public void testWriteTryLockWhenReadLocked(boolean fair) { |
771 |
< |
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
772 |
< |
// lock.readLock().lock(); |
773 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
774 |
< |
// public void realRun() { |
775 |
< |
// assertFalse(lock.writeLock().tryLock()); |
776 |
< |
// }}); |
777 |
< |
|
778 |
< |
// awaitTermination(t); |
779 |
< |
// lock.readLock().unlock(); |
780 |
< |
// } |
781 |
< |
|
782 |
< |
// /** |
783 |
< |
// * write timed tryLock times out if locked |
784 |
< |
// */ |
785 |
< |
// public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); } |
786 |
< |
// public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); } |
787 |
< |
// public void testWriteTryLock_Timeout(boolean fair) { |
788 |
< |
// final PublicReentrantReadWriteLock lock = |
789 |
< |
// new PublicReentrantReadWriteLock(fair); |
790 |
< |
// lock.writeLock().lock(); |
791 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
792 |
< |
// public void realRun() throws InterruptedException { |
793 |
< |
// long startTime = System.nanoTime(); |
794 |
< |
// long timeoutMillis = 10; |
795 |
< |
// assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS)); |
796 |
< |
// assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
797 |
< |
// }}); |
798 |
< |
|
799 |
< |
// awaitTermination(t); |
800 |
< |
// releaseWriteLock(lock); |
801 |
< |
// } |
802 |
< |
|
803 |
< |
// /** |
804 |
< |
// * read timed tryLock times out if write-locked |
805 |
< |
// */ |
806 |
< |
// public void testReadTryLock_Timeout() { testReadTryLock_Timeout(false); } |
807 |
< |
// public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); } |
808 |
< |
// public void testReadTryLock_Timeout(boolean fair) { |
809 |
< |
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
810 |
< |
// lock.writeLock().lock(); |
811 |
< |
// Thread t = newStartedThread(new CheckedRunnable() { |
812 |
< |
// public void realRun() throws InterruptedException { |
813 |
< |
// long startTime = System.nanoTime(); |
814 |
< |
// long timeoutMillis = 10; |
815 |
< |
// assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS)); |
816 |
< |
// assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
817 |
< |
// }}); |
818 |
< |
|
819 |
< |
// awaitTermination(t); |
820 |
< |
// assertTrue(lock.writeLock().isHeldByCurrentThread()); |
821 |
< |
// lock.writeLock().unlock(); |
822 |
< |
// } |
823 |
< |
|
824 |
< |
// /** |
825 |
< |
// * write lockInterruptibly succeeds if unlocked, else is interruptible |
826 |
< |
// */ |
827 |
< |
// public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); } |
828 |
< |
// public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); } |
829 |
< |
// public void testWriteLockInterruptibly(boolean fair) { |
830 |
< |
// final PublicReentrantReadWriteLock lock = |
831 |
< |
// new PublicReentrantReadWriteLock(fair); |
832 |
< |
// try { |
833 |
< |
// lock.writeLock().lockInterruptibly(); |
834 |
< |
// } catch (InterruptedException ie) { |
835 |
< |
// threadUnexpectedException(ie); |
836 |
< |
// } |
837 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
838 |
< |
// public void realRun() throws InterruptedException { |
839 |
< |
// lock.writeLock().lockInterruptibly(); |
840 |
< |
// }}); |
841 |
< |
|
842 |
< |
// waitForQueuedThread(lock, t); |
843 |
< |
// t.interrupt(); |
844 |
< |
// assertTrue(lock.writeLock().isHeldByCurrentThread()); |
845 |
< |
// awaitTermination(t); |
846 |
< |
// releaseWriteLock(lock); |
847 |
< |
// } |
848 |
< |
|
849 |
< |
// /** |
850 |
< |
// * read lockInterruptibly succeeds if lock free else is interruptible |
851 |
< |
// */ |
852 |
< |
// public void testReadLockInterruptibly() { testReadLockInterruptibly(false); } |
853 |
< |
// public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); } |
854 |
< |
// public void testReadLockInterruptibly(boolean fair) { |
855 |
< |
// final PublicReentrantReadWriteLock lock = |
856 |
< |
// new PublicReentrantReadWriteLock(fair); |
857 |
< |
// try { |
858 |
< |
// lock.readLock().lockInterruptibly(); |
859 |
< |
// lock.readLock().unlock(); |
860 |
< |
// lock.writeLock().lockInterruptibly(); |
861 |
< |
// } catch (InterruptedException ie) { |
862 |
< |
// threadUnexpectedException(ie); |
863 |
< |
// } |
864 |
< |
// Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
865 |
< |
// public void realRun() throws InterruptedException { |
866 |
< |
// lock.readLock().lockInterruptibly(); |
867 |
< |
// }}); |
868 |
< |
|
869 |
< |
// waitForQueuedThread(lock, t); |
870 |
< |
// t.interrupt(); |
871 |
< |
// awaitTermination(t); |
872 |
< |
// releaseWriteLock(lock); |
873 |
< |
// } |
874 |
< |
|
875 |
< |
// /** |
876 |
< |
// * Calling await without holding lock throws IllegalMonitorStateException |
877 |
< |
// */ |
878 |
< |
// public void testAwait_IMSE() { testAwait_IMSE(false); } |
879 |
< |
// public void testAwait_IMSE_fair() { testAwait_IMSE(true); } |
880 |
< |
// 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 |
< |
// } |
33 |
> |
/** |
34 |
> |
* Releases write lock, checking isWriteLocked before and after |
35 |
> |
*/ |
36 |
> |
void releaseWriteLock(StampedLock lock, long stamp) { |
37 |
> |
assertTrue(lock.isWriteLocked()); |
38 |
> |
assertValid(lock, stamp); |
39 |
> |
lock.unlockWrite(stamp); |
40 |
> |
assertFalse(lock.isWriteLocked()); |
41 |
> |
assertFalse(lock.validate(stamp)); |
42 |
> |
} |
43 |
> |
|
44 |
> |
/** |
45 |
> |
* Releases read lock, checking isReadLocked before and after |
46 |
> |
*/ |
47 |
> |
void releaseReadLock(StampedLock lock, long stamp) { |
48 |
> |
assertTrue(lock.isReadLocked()); |
49 |
> |
assertValid(lock, stamp); |
50 |
> |
lock.unlockRead(stamp); |
51 |
> |
assertFalse(lock.isReadLocked()); |
52 |
> |
assertTrue(lock.validate(stamp)); |
53 |
> |
} |
54 |
> |
|
55 |
> |
long assertNonZero(long v) { |
56 |
> |
assertTrue(v != 0L); |
57 |
> |
return v; |
58 |
> |
} |
59 |
> |
|
60 |
> |
long assertValid(StampedLock lock, long stamp) { |
61 |
> |
assertTrue(stamp != 0L); |
62 |
> |
assertTrue(lock.validate(stamp)); |
63 |
> |
return stamp; |
64 |
> |
} |
65 |
> |
|
66 |
> |
void assertUnlocked(StampedLock lock) { |
67 |
> |
assertFalse(lock.isReadLocked()); |
68 |
> |
assertFalse(lock.isWriteLocked()); |
69 |
> |
assertEquals(0, lock.getReadLockCount()); |
70 |
> |
assertValid(lock, lock.tryOptimisticRead()); |
71 |
> |
} |
72 |
> |
|
73 |
> |
List<Action> lockLockers(Lock lock) { |
74 |
> |
List<Action> lockers = new ArrayList<>(); |
75 |
> |
lockers.add(() -> lock.lock()); |
76 |
> |
lockers.add(() -> lock.lockInterruptibly()); |
77 |
> |
lockers.add(() -> lock.tryLock()); |
78 |
> |
lockers.add(() -> lock.tryLock(Long.MIN_VALUE, DAYS)); |
79 |
> |
lockers.add(() -> lock.tryLock(0L, DAYS)); |
80 |
> |
lockers.add(() -> lock.tryLock(Long.MAX_VALUE, DAYS)); |
81 |
> |
return lockers; |
82 |
> |
} |
83 |
> |
|
84 |
> |
List<Function<StampedLock, Long>> readLockers() { |
85 |
> |
List<Function<StampedLock, Long>> readLockers = new ArrayList<>(); |
86 |
> |
readLockers.add(sl -> sl.readLock()); |
87 |
> |
readLockers.add(sl -> sl.tryReadLock()); |
88 |
> |
readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl)); |
89 |
> |
readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
90 |
> |
readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS)); |
91 |
> |
readLockers.add(sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead())); |
92 |
> |
return readLockers; |
93 |
> |
} |
94 |
> |
|
95 |
> |
List<BiConsumer<StampedLock, Long>> readUnlockers() { |
96 |
> |
List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>(); |
97 |
> |
readUnlockers.add((sl, stamp) -> sl.unlockRead(stamp)); |
98 |
> |
readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead())); |
99 |
> |
readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock()); |
100 |
> |
readUnlockers.add((sl, stamp) -> sl.unlock(stamp)); |
101 |
> |
readUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp))); |
102 |
> |
return readUnlockers; |
103 |
> |
} |
104 |
> |
|
105 |
> |
List<Function<StampedLock, Long>> writeLockers() { |
106 |
> |
List<Function<StampedLock, Long>> writeLockers = new ArrayList<>(); |
107 |
> |
writeLockers.add(sl -> sl.writeLock()); |
108 |
> |
writeLockers.add(sl -> sl.tryWriteLock()); |
109 |
> |
writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl)); |
110 |
> |
writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS)); |
111 |
> |
writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS)); |
112 |
> |
writeLockers.add(sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead())); |
113 |
> |
return writeLockers; |
114 |
> |
} |
115 |
> |
|
116 |
> |
List<BiConsumer<StampedLock, Long>> writeUnlockers() { |
117 |
> |
List<BiConsumer<StampedLock, Long>> writeUnlockers = new ArrayList<>(); |
118 |
> |
writeUnlockers.add((sl, stamp) -> sl.unlockWrite(stamp)); |
119 |
> |
writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite())); |
120 |
> |
writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock()); |
121 |
> |
writeUnlockers.add((sl, stamp) -> sl.unlock(stamp)); |
122 |
> |
writeUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp))); |
123 |
> |
return writeUnlockers; |
124 |
> |
} |
125 |
> |
|
126 |
> |
/** |
127 |
> |
* Constructed StampedLock is in unlocked state |
128 |
> |
*/ |
129 |
> |
public void testConstructor() { |
130 |
> |
assertUnlocked(new StampedLock()); |
131 |
> |
} |
132 |
> |
|
133 |
> |
/** |
134 |
> |
* write-locking, then unlocking, an unlocked lock succeed |
135 |
> |
*/ |
136 |
> |
public void testWriteLock_lockUnlock() { |
137 |
> |
StampedLock lock = new StampedLock(); |
138 |
> |
|
139 |
> |
for (Function<StampedLock, Long> writeLocker : writeLockers()) |
140 |
> |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
141 |
> |
assertFalse(lock.isWriteLocked()); |
142 |
> |
assertFalse(lock.isReadLocked()); |
143 |
> |
assertEquals(0, lock.getReadLockCount()); |
144 |
> |
|
145 |
> |
long s = writeLocker.apply(lock); |
146 |
> |
assertValid(lock, s); |
147 |
> |
assertTrue(lock.isWriteLocked()); |
148 |
> |
assertFalse(lock.isReadLocked()); |
149 |
> |
assertEquals(0, lock.getReadLockCount()); |
150 |
> |
writeUnlocker.accept(lock, s); |
151 |
> |
assertUnlocked(lock); |
152 |
> |
} |
153 |
> |
} |
154 |
> |
|
155 |
> |
/** |
156 |
> |
* read-locking, then unlocking, an unlocked lock succeed |
157 |
> |
*/ |
158 |
> |
public void testReadLock_lockUnlock() { |
159 |
> |
StampedLock lock = new StampedLock(); |
160 |
> |
|
161 |
> |
for (Function<StampedLock, Long> readLocker : readLockers()) |
162 |
> |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
163 |
> |
long s = 42; |
164 |
> |
for (int i = 0; i < 2; i++) { |
165 |
> |
s = assertValid(lock, readLocker.apply(lock)); |
166 |
> |
assertFalse(lock.isWriteLocked()); |
167 |
> |
assertTrue(lock.isReadLocked()); |
168 |
> |
assertEquals(i + 1, lock.getReadLockCount()); |
169 |
> |
} |
170 |
> |
for (int i = 0; i < 2; i++) { |
171 |
> |
assertFalse(lock.isWriteLocked()); |
172 |
> |
assertTrue(lock.isReadLocked()); |
173 |
> |
assertEquals(2 - i, lock.getReadLockCount()); |
174 |
> |
readUnlocker.accept(lock, s); |
175 |
> |
} |
176 |
> |
assertUnlocked(lock); |
177 |
> |
} |
178 |
> |
} |
179 |
> |
|
180 |
> |
/** |
181 |
> |
* tryUnlockWrite fails if not write locked |
182 |
> |
*/ |
183 |
> |
public void testTryUnlockWrite_failure() { |
184 |
> |
StampedLock lock = new StampedLock(); |
185 |
> |
assertFalse(lock.tryUnlockWrite()); |
186 |
> |
|
187 |
> |
for (Function<StampedLock, Long> readLocker : readLockers()) |
188 |
> |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
189 |
> |
long s = assertValid(lock, readLocker.apply(lock)); |
190 |
> |
assertFalse(lock.tryUnlockWrite()); |
191 |
> |
assertTrue(lock.isReadLocked()); |
192 |
> |
readUnlocker.accept(lock, s); |
193 |
> |
assertUnlocked(lock); |
194 |
> |
} |
195 |
> |
} |
196 |
> |
|
197 |
> |
/** |
198 |
> |
* tryUnlockRead fails if not read locked |
199 |
> |
*/ |
200 |
> |
public void testTryUnlockRead_failure() { |
201 |
> |
StampedLock lock = new StampedLock(); |
202 |
> |
assertFalse(lock.tryUnlockRead()); |
203 |
> |
|
204 |
> |
for (Function<StampedLock, Long> writeLocker : writeLockers()) |
205 |
> |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
206 |
> |
long s = writeLocker.apply(lock); |
207 |
> |
assertFalse(lock.tryUnlockRead()); |
208 |
> |
assertTrue(lock.isWriteLocked()); |
209 |
> |
writeUnlocker.accept(lock, s); |
210 |
> |
assertUnlocked(lock); |
211 |
> |
} |
212 |
> |
} |
213 |
> |
|
214 |
> |
/** |
215 |
> |
* validate(0L) fails |
216 |
> |
*/ |
217 |
> |
public void testValidate0() { |
218 |
> |
StampedLock lock = new StampedLock(); |
219 |
> |
assertFalse(lock.validate(0L)); |
220 |
> |
} |
221 |
> |
|
222 |
> |
/** |
223 |
> |
* A stamp obtained from a successful lock operation validates while the lock is held |
224 |
> |
*/ |
225 |
> |
public void testValidate() throws InterruptedException { |
226 |
> |
StampedLock lock = new StampedLock(); |
227 |
> |
|
228 |
> |
for (Function<StampedLock, Long> readLocker : readLockers()) |
229 |
> |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
230 |
> |
long s = assertNonZero(readLocker.apply(lock)); |
231 |
> |
assertTrue(lock.validate(s)); |
232 |
> |
readUnlocker.accept(lock, s); |
233 |
> |
} |
234 |
> |
|
235 |
> |
for (Function<StampedLock, Long> writeLocker : writeLockers()) |
236 |
> |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
237 |
> |
long s = assertNonZero(writeLocker.apply(lock)); |
238 |
> |
assertTrue(lock.validate(s)); |
239 |
> |
writeUnlocker.accept(lock, s); |
240 |
> |
} |
241 |
> |
} |
242 |
> |
|
243 |
> |
/** |
244 |
> |
* A stamp obtained from an unsuccessful lock operation does not validate |
245 |
> |
*/ |
246 |
> |
public void testValidate2() throws InterruptedException { |
247 |
> |
StampedLock lock = new StampedLock(); |
248 |
> |
long s = assertNonZero(lock.writeLock()); |
249 |
> |
assertTrue(lock.validate(s)); |
250 |
> |
assertFalse(lock.validate(lock.tryWriteLock())); |
251 |
> |
assertFalse(lock.validate(lock.tryWriteLock(0L, SECONDS))); |
252 |
> |
assertFalse(lock.validate(lock.tryReadLock())); |
253 |
> |
assertFalse(lock.validate(lock.tryReadLock(0L, SECONDS))); |
254 |
> |
assertFalse(lock.validate(lock.tryOptimisticRead())); |
255 |
> |
lock.unlockWrite(s); |
256 |
> |
} |
257 |
> |
|
258 |
> |
void assertThrowInterruptedExceptionWhenPreInterrupted(Action[] actions) { |
259 |
> |
for (Action action : actions) { |
260 |
> |
Thread.currentThread().interrupt(); |
261 |
> |
try { |
262 |
> |
action.run(); |
263 |
> |
shouldThrow(); |
264 |
> |
} |
265 |
> |
catch (InterruptedException success) {} |
266 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
267 |
> |
assertFalse(Thread.interrupted()); |
268 |
> |
} |
269 |
> |
} |
270 |
> |
|
271 |
> |
/** |
272 |
> |
* interruptible operations throw InterruptedException when pre-interrupted |
273 |
> |
*/ |
274 |
> |
public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() { |
275 |
> |
final StampedLock lock = new StampedLock(); |
276 |
> |
|
277 |
> |
Action[] interruptibleLockActions = { |
278 |
> |
() -> lock.writeLockInterruptibly(), |
279 |
> |
() -> lock.tryWriteLock(Long.MIN_VALUE, DAYS), |
280 |
> |
() -> lock.tryWriteLock(Long.MAX_VALUE, DAYS), |
281 |
> |
() -> lock.readLockInterruptibly(), |
282 |
> |
() -> lock.tryReadLock(Long.MIN_VALUE, DAYS), |
283 |
> |
() -> lock.tryReadLock(Long.MAX_VALUE, DAYS), |
284 |
> |
() -> lock.asWriteLock().lockInterruptibly(), |
285 |
> |
() -> lock.asWriteLock().tryLock(0L, DAYS), |
286 |
> |
() -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS), |
287 |
> |
() -> lock.asReadLock().lockInterruptibly(), |
288 |
> |
() -> lock.asReadLock().tryLock(0L, DAYS), |
289 |
> |
() -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS), |
290 |
> |
}; |
291 |
> |
shuffle(interruptibleLockActions); |
292 |
> |
|
293 |
> |
assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions); |
294 |
> |
{ |
295 |
> |
long s = lock.writeLock(); |
296 |
> |
assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions); |
297 |
> |
lock.unlockWrite(s); |
298 |
> |
} |
299 |
> |
{ |
300 |
> |
long s = lock.readLock(); |
301 |
> |
assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions); |
302 |
> |
lock.unlockRead(s); |
303 |
> |
} |
304 |
> |
} |
305 |
> |
|
306 |
> |
void assertThrowInterruptedExceptionWhenInterrupted(Action[] actions) { |
307 |
> |
int n = actions.length; |
308 |
> |
Future<?>[] futures = new Future<?>[n]; |
309 |
> |
CountDownLatch threadsStarted = new CountDownLatch(n); |
310 |
> |
CountDownLatch done = new CountDownLatch(n); |
311 |
> |
|
312 |
> |
for (int i = 0; i < n; i++) { |
313 |
> |
Action action = actions[i]; |
314 |
> |
futures[i] = cachedThreadPool.submit(new CheckedRunnable() { |
315 |
> |
public void realRun() throws Throwable { |
316 |
> |
threadsStarted.countDown(); |
317 |
> |
try { |
318 |
> |
action.run(); |
319 |
> |
shouldThrow(); |
320 |
> |
} |
321 |
> |
catch (InterruptedException success) {} |
322 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
323 |
> |
assertFalse(Thread.interrupted()); |
324 |
> |
done.countDown(); |
325 |
> |
}}); |
326 |
> |
} |
327 |
> |
|
328 |
> |
await(threadsStarted); |
329 |
> |
assertEquals(n, done.getCount()); |
330 |
> |
for (Future<?> future : futures) // Interrupt all the tasks |
331 |
> |
future.cancel(true); |
332 |
> |
await(done); |
333 |
> |
} |
334 |
> |
|
335 |
> |
/** |
336 |
> |
* interruptible operations throw InterruptedException when write locked and interrupted |
337 |
> |
*/ |
338 |
> |
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() { |
339 |
> |
final StampedLock lock = new StampedLock(); |
340 |
> |
long s = lock.writeLock(); |
341 |
> |
|
342 |
> |
Action[] interruptibleLockBlockingActions = { |
343 |
> |
() -> lock.writeLockInterruptibly(), |
344 |
> |
() -> lock.tryWriteLock(Long.MAX_VALUE, DAYS), |
345 |
> |
() -> lock.readLockInterruptibly(), |
346 |
> |
() -> lock.tryReadLock(Long.MAX_VALUE, DAYS), |
347 |
> |
() -> lock.asWriteLock().lockInterruptibly(), |
348 |
> |
() -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS), |
349 |
> |
() -> lock.asReadLock().lockInterruptibly(), |
350 |
> |
() -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS), |
351 |
> |
}; |
352 |
> |
shuffle(interruptibleLockBlockingActions); |
353 |
> |
|
354 |
> |
assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions); |
355 |
> |
} |
356 |
> |
|
357 |
> |
/** |
358 |
> |
* interruptible operations throw InterruptedException when read locked and interrupted |
359 |
> |
*/ |
360 |
> |
public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() { |
361 |
> |
final StampedLock lock = new StampedLock(); |
362 |
> |
long s = lock.readLock(); |
363 |
> |
|
364 |
> |
Action[] interruptibleLockBlockingActions = { |
365 |
> |
() -> lock.writeLockInterruptibly(), |
366 |
> |
() -> lock.tryWriteLock(Long.MAX_VALUE, DAYS), |
367 |
> |
() -> lock.asWriteLock().lockInterruptibly(), |
368 |
> |
() -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS), |
369 |
> |
}; |
370 |
> |
shuffle(interruptibleLockBlockingActions); |
371 |
> |
|
372 |
> |
assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions); |
373 |
> |
} |
374 |
> |
|
375 |
> |
/** |
376 |
> |
* Non-interruptible operations ignore and preserve interrupt status |
377 |
> |
*/ |
378 |
> |
public void testNonInterruptibleOperationsIgnoreInterrupts() { |
379 |
> |
final StampedLock lock = new StampedLock(); |
380 |
> |
Thread.currentThread().interrupt(); |
381 |
> |
|
382 |
> |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
383 |
> |
long s = assertValid(lock, lock.readLock()); |
384 |
> |
readUnlocker.accept(lock, s); |
385 |
> |
s = assertValid(lock, lock.tryReadLock()); |
386 |
> |
readUnlocker.accept(lock, s); |
387 |
> |
} |
388 |
> |
|
389 |
> |
lock.asReadLock().lock(); |
390 |
> |
lock.asReadLock().unlock(); |
391 |
> |
|
392 |
> |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
393 |
> |
long s = assertValid(lock, lock.writeLock()); |
394 |
> |
writeUnlocker.accept(lock, s); |
395 |
> |
s = assertValid(lock, lock.tryWriteLock()); |
396 |
> |
writeUnlocker.accept(lock, s); |
397 |
> |
} |
398 |
> |
|
399 |
> |
lock.asWriteLock().lock(); |
400 |
> |
lock.asWriteLock().unlock(); |
401 |
> |
|
402 |
> |
assertTrue(Thread.interrupted()); |
403 |
> |
} |
404 |
> |
|
405 |
> |
/** |
406 |
> |
* tryWriteLock on an unlocked lock succeeds |
407 |
> |
*/ |
408 |
> |
public void testTryWriteLock() { |
409 |
> |
final StampedLock lock = new StampedLock(); |
410 |
> |
long s = lock.tryWriteLock(); |
411 |
> |
assertTrue(s != 0L); |
412 |
> |
assertTrue(lock.isWriteLocked()); |
413 |
> |
assertEquals(0L, lock.tryWriteLock()); |
414 |
> |
releaseWriteLock(lock, s); |
415 |
> |
} |
416 |
> |
|
417 |
> |
/** |
418 |
> |
* tryWriteLock fails if locked |
419 |
> |
*/ |
420 |
> |
public void testTryWriteLockWhenLocked() { |
421 |
> |
final StampedLock lock = new StampedLock(); |
422 |
> |
long s = lock.writeLock(); |
423 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
424 |
> |
public void realRun() { |
425 |
> |
assertEquals(0L, lock.tryWriteLock()); |
426 |
> |
}}); |
427 |
> |
|
428 |
> |
assertEquals(0L, lock.tryWriteLock()); |
429 |
> |
awaitTermination(t); |
430 |
> |
releaseWriteLock(lock, s); |
431 |
> |
} |
432 |
> |
|
433 |
> |
/** |
434 |
> |
* tryReadLock fails if write-locked |
435 |
> |
*/ |
436 |
> |
public void testTryReadLockWhenLocked() { |
437 |
> |
final StampedLock lock = new StampedLock(); |
438 |
> |
long s = lock.writeLock(); |
439 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
440 |
> |
public void realRun() { |
441 |
> |
assertEquals(0L, lock.tryReadLock()); |
442 |
> |
}}); |
443 |
> |
|
444 |
> |
assertEquals(0L, lock.tryReadLock()); |
445 |
> |
awaitTermination(t); |
446 |
> |
releaseWriteLock(lock, s); |
447 |
> |
} |
448 |
> |
|
449 |
> |
/** |
450 |
> |
* Multiple threads can hold a read lock when not write-locked |
451 |
> |
*/ |
452 |
> |
public void testMultipleReadLocks() { |
453 |
> |
final StampedLock lock = new StampedLock(); |
454 |
> |
final long s = lock.readLock(); |
455 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
456 |
> |
public void realRun() throws InterruptedException { |
457 |
> |
long s2 = lock.tryReadLock(); |
458 |
> |
assertValid(lock, s2); |
459 |
> |
lock.unlockRead(s2); |
460 |
> |
long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS); |
461 |
> |
assertValid(lock, s3); |
462 |
> |
lock.unlockRead(s3); |
463 |
> |
long s4 = lock.readLock(); |
464 |
> |
assertValid(lock, s4); |
465 |
> |
lock.unlockRead(s4); |
466 |
> |
lock.asReadLock().lock(); |
467 |
> |
lock.asReadLock().unlock(); |
468 |
> |
lock.asReadLock().lockInterruptibly(); |
469 |
> |
lock.asReadLock().unlock(); |
470 |
> |
lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS); |
471 |
> |
lock.asReadLock().unlock(); |
472 |
> |
}}); |
473 |
|
|
474 |
+ |
awaitTermination(t); |
475 |
+ |
lock.unlockRead(s); |
476 |
+ |
} |
477 |
+ |
|
478 |
+ |
/** |
479 |
+ |
* A writelock succeeds only after a reading thread unlocks |
480 |
+ |
*/ |
481 |
+ |
public void testWriteAfterReadLock() throws InterruptedException { |
482 |
+ |
final CountDownLatch running = new CountDownLatch(1); |
483 |
+ |
final StampedLock lock = new StampedLock(); |
484 |
+ |
long rs = lock.readLock(); |
485 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
486 |
+ |
public void realRun() { |
487 |
+ |
running.countDown(); |
488 |
+ |
long s = lock.writeLock(); |
489 |
+ |
lock.unlockWrite(s); |
490 |
+ |
}}); |
491 |
+ |
|
492 |
+ |
running.await(); |
493 |
+ |
waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS); |
494 |
+ |
assertFalse(lock.isWriteLocked()); |
495 |
+ |
lock.unlockRead(rs); |
496 |
+ |
awaitTermination(t); |
497 |
+ |
assertFalse(lock.isWriteLocked()); |
498 |
+ |
} |
499 |
+ |
|
500 |
+ |
/** |
501 |
+ |
* A writelock succeeds only after reading threads unlock |
502 |
+ |
*/ |
503 |
+ |
public void testWriteAfterMultipleReadLocks() { |
504 |
+ |
final StampedLock lock = new StampedLock(); |
505 |
+ |
long s = lock.readLock(); |
506 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
507 |
+ |
public void realRun() { |
508 |
+ |
long rs = lock.readLock(); |
509 |
+ |
lock.unlockRead(rs); |
510 |
+ |
}}); |
511 |
+ |
|
512 |
+ |
awaitTermination(t1); |
513 |
+ |
|
514 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
515 |
+ |
public void realRun() { |
516 |
+ |
long ws = lock.writeLock(); |
517 |
+ |
lock.unlockWrite(ws); |
518 |
+ |
}}); |
519 |
+ |
|
520 |
+ |
assertTrue(lock.isReadLocked()); |
521 |
+ |
assertFalse(lock.isWriteLocked()); |
522 |
+ |
lock.unlockRead(s); |
523 |
+ |
awaitTermination(t2); |
524 |
+ |
assertFalse(lock.isWriteLocked()); |
525 |
+ |
} |
526 |
+ |
|
527 |
+ |
/** |
528 |
+ |
* Readlocks succeed only after a writing thread unlocks |
529 |
+ |
*/ |
530 |
+ |
public void testReadAfterWriteLock() { |
531 |
+ |
final StampedLock lock = new StampedLock(); |
532 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
533 |
+ |
final long s = lock.writeLock(); |
534 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
535 |
+ |
public void realRun() { |
536 |
+ |
threadsStarted.countDown(); |
537 |
+ |
long rs = lock.readLock(); |
538 |
+ |
lock.unlockRead(rs); |
539 |
+ |
}}); |
540 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
541 |
+ |
public void realRun() { |
542 |
+ |
threadsStarted.countDown(); |
543 |
+ |
long rs = lock.readLock(); |
544 |
+ |
lock.unlockRead(rs); |
545 |
+ |
}}); |
546 |
+ |
|
547 |
+ |
await(threadsStarted); |
548 |
+ |
waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS); |
549 |
+ |
waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS); |
550 |
+ |
releaseWriteLock(lock, s); |
551 |
+ |
awaitTermination(t1); |
552 |
+ |
awaitTermination(t2); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
+ |
* tryReadLock succeeds if read locked but not write locked |
557 |
+ |
*/ |
558 |
+ |
public void testTryLockWhenReadLocked() { |
559 |
+ |
final StampedLock lock = new StampedLock(); |
560 |
+ |
long s = lock.readLock(); |
561 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
562 |
+ |
public void realRun() { |
563 |
+ |
long rs = lock.tryReadLock(); |
564 |
+ |
assertValid(lock, rs); |
565 |
+ |
lock.unlockRead(rs); |
566 |
+ |
}}); |
567 |
+ |
|
568 |
+ |
awaitTermination(t); |
569 |
+ |
lock.unlockRead(s); |
570 |
+ |
} |
571 |
+ |
|
572 |
+ |
/** |
573 |
+ |
* tryWriteLock fails when read locked |
574 |
+ |
*/ |
575 |
+ |
public void testTryWriteLockWhenReadLocked() { |
576 |
+ |
final StampedLock lock = new StampedLock(); |
577 |
+ |
long s = lock.readLock(); |
578 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
579 |
+ |
public void realRun() { |
580 |
+ |
threadAssertEquals(0L, lock.tryWriteLock()); |
581 |
+ |
}}); |
582 |
+ |
|
583 |
+ |
awaitTermination(t); |
584 |
+ |
lock.unlockRead(s); |
585 |
+ |
} |
586 |
+ |
|
587 |
+ |
/** |
588 |
+ |
* timed lock operations time out if lock not available |
589 |
+ |
*/ |
590 |
+ |
public void testTimedLock_Timeout() throws Exception { |
591 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(); |
592 |
+ |
|
593 |
+ |
// Write locked |
594 |
+ |
final StampedLock lock = new StampedLock(); |
595 |
+ |
long stamp = lock.writeLock(); |
596 |
+ |
assertEquals(0L, lock.tryReadLock(0L, DAYS)); |
597 |
+ |
assertEquals(0L, lock.tryReadLock(Long.MIN_VALUE, DAYS)); |
598 |
+ |
assertFalse(lock.asReadLock().tryLock(0L, DAYS)); |
599 |
+ |
assertFalse(lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS)); |
600 |
+ |
assertEquals(0L, lock.tryWriteLock(0L, DAYS)); |
601 |
+ |
assertEquals(0L, lock.tryWriteLock(Long.MIN_VALUE, DAYS)); |
602 |
+ |
assertFalse(lock.asWriteLock().tryLock(0L, DAYS)); |
603 |
+ |
assertFalse(lock.asWriteLock().tryLock(Long.MIN_VALUE, DAYS)); |
604 |
+ |
|
605 |
+ |
futures.add(cachedThreadPool.submit(new CheckedRunnable() { |
606 |
+ |
public void realRun() throws InterruptedException { |
607 |
+ |
long startTime = System.nanoTime(); |
608 |
+ |
assertEquals(0L, lock.tryWriteLock(timeoutMillis(), MILLISECONDS)); |
609 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
610 |
+ |
}})); |
611 |
+ |
|
612 |
+ |
futures.add(cachedThreadPool.submit(new CheckedRunnable() { |
613 |
+ |
public void realRun() throws InterruptedException { |
614 |
+ |
long startTime = System.nanoTime(); |
615 |
+ |
assertEquals(0L, lock.tryReadLock(timeoutMillis(), MILLISECONDS)); |
616 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
617 |
+ |
}})); |
618 |
+ |
|
619 |
+ |
// Read locked |
620 |
+ |
final StampedLock lock2 = new StampedLock(); |
621 |
+ |
long stamp2 = lock2.readLock(); |
622 |
+ |
assertEquals(0L, lock2.tryWriteLock(0L, DAYS)); |
623 |
+ |
assertEquals(0L, lock2.tryWriteLock(Long.MIN_VALUE, DAYS)); |
624 |
+ |
assertFalse(lock2.asWriteLock().tryLock(0L, DAYS)); |
625 |
+ |
assertFalse(lock2.asWriteLock().tryLock(Long.MIN_VALUE, DAYS)); |
626 |
+ |
|
627 |
+ |
futures.add(cachedThreadPool.submit(new CheckedRunnable() { |
628 |
+ |
public void realRun() throws InterruptedException { |
629 |
+ |
long startTime = System.nanoTime(); |
630 |
+ |
assertEquals(0L, lock2.tryWriteLock(timeoutMillis(), MILLISECONDS)); |
631 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
632 |
+ |
}})); |
633 |
+ |
|
634 |
+ |
for (Future<?> future : futures) |
635 |
+ |
assertNull(future.get()); |
636 |
+ |
|
637 |
+ |
releaseWriteLock(lock, stamp); |
638 |
+ |
releaseReadLock(lock2, stamp2); |
639 |
+ |
} |
640 |
+ |
|
641 |
+ |
/** |
642 |
+ |
* writeLockInterruptibly succeeds if unlocked |
643 |
+ |
*/ |
644 |
+ |
public void testWriteLockInterruptibly() throws InterruptedException { |
645 |
+ |
final StampedLock lock = new StampedLock(); |
646 |
+ |
long s = lock.writeLockInterruptibly(); |
647 |
+ |
assertTrue(lock.isWriteLocked()); |
648 |
+ |
releaseWriteLock(lock, s); |
649 |
+ |
} |
650 |
+ |
|
651 |
+ |
/** |
652 |
+ |
* readLockInterruptibly succeeds if lock free |
653 |
+ |
*/ |
654 |
+ |
public void testReadLockInterruptibly() throws InterruptedException { |
655 |
+ |
final StampedLock lock = new StampedLock(); |
656 |
+ |
|
657 |
+ |
long s = assertValid(lock, lock.readLockInterruptibly()); |
658 |
+ |
assertTrue(lock.isReadLocked()); |
659 |
+ |
lock.unlockRead(s); |
660 |
+ |
|
661 |
+ |
lock.asReadLock().lockInterruptibly(); |
662 |
+ |
assertTrue(lock.isReadLocked()); |
663 |
+ |
lock.asReadLock().unlock(); |
664 |
+ |
} |
665 |
+ |
|
666 |
+ |
/** |
667 |
+ |
* A serialized lock deserializes as unlocked |
668 |
+ |
*/ |
669 |
+ |
public void testSerialization() { |
670 |
+ |
StampedLock lock = new StampedLock(); |
671 |
+ |
lock.writeLock(); |
672 |
+ |
StampedLock clone = serialClone(lock); |
673 |
+ |
assertTrue(lock.isWriteLocked()); |
674 |
+ |
assertFalse(clone.isWriteLocked()); |
675 |
+ |
long s = clone.writeLock(); |
676 |
+ |
assertTrue(clone.isWriteLocked()); |
677 |
+ |
clone.unlockWrite(s); |
678 |
+ |
assertFalse(clone.isWriteLocked()); |
679 |
+ |
} |
680 |
+ |
|
681 |
+ |
/** |
682 |
+ |
* toString indicates current lock state |
683 |
+ |
*/ |
684 |
+ |
public void testToString() { |
685 |
+ |
StampedLock lock = new StampedLock(); |
686 |
+ |
assertTrue(lock.toString().contains("Unlocked")); |
687 |
+ |
long s = lock.writeLock(); |
688 |
+ |
assertTrue(lock.toString().contains("Write-locked")); |
689 |
+ |
lock.unlockWrite(s); |
690 |
+ |
s = lock.readLock(); |
691 |
+ |
assertTrue(lock.toString().contains("Read-locks")); |
692 |
+ |
} |
693 |
+ |
|
694 |
+ |
/** |
695 |
+ |
* tryOptimisticRead succeeds and validates if unlocked, fails if |
696 |
+ |
* exclusively locked |
697 |
+ |
*/ |
698 |
+ |
public void testValidateOptimistic() throws InterruptedException { |
699 |
+ |
StampedLock lock = new StampedLock(); |
700 |
+ |
|
701 |
+ |
assertValid(lock, lock.tryOptimisticRead()); |
702 |
+ |
|
703 |
+ |
for (Function<StampedLock, Long> writeLocker : writeLockers()) { |
704 |
+ |
long s = assertValid(lock, writeLocker.apply(lock)); |
705 |
+ |
assertEquals(0L, lock.tryOptimisticRead()); |
706 |
+ |
releaseWriteLock(lock, s); |
707 |
+ |
} |
708 |
+ |
|
709 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) { |
710 |
+ |
long s = assertValid(lock, readLocker.apply(lock)); |
711 |
+ |
long p = assertValid(lock, lock.tryOptimisticRead()); |
712 |
+ |
releaseReadLock(lock, s); |
713 |
+ |
assertTrue(lock.validate(p)); |
714 |
+ |
} |
715 |
+ |
|
716 |
+ |
assertValid(lock, lock.tryOptimisticRead()); |
717 |
+ |
} |
718 |
+ |
|
719 |
+ |
/** |
720 |
+ |
* tryOptimisticRead stamp does not validate if a write lock intervenes |
721 |
+ |
*/ |
722 |
+ |
public void testValidateOptimisticWriteLocked() { |
723 |
+ |
final StampedLock lock = new StampedLock(); |
724 |
+ |
final long p = assertValid(lock, lock.tryOptimisticRead()); |
725 |
+ |
final long s = assertValid(lock, lock.writeLock()); |
726 |
+ |
assertFalse(lock.validate(p)); |
727 |
+ |
assertEquals(0L, lock.tryOptimisticRead()); |
728 |
+ |
assertTrue(lock.validate(s)); |
729 |
+ |
lock.unlockWrite(s); |
730 |
+ |
} |
731 |
+ |
|
732 |
+ |
/** |
733 |
+ |
* tryOptimisticRead stamp does not validate if a write lock |
734 |
+ |
* intervenes in another thread |
735 |
+ |
*/ |
736 |
+ |
public void testValidateOptimisticWriteLocked2() |
737 |
+ |
throws InterruptedException { |
738 |
+ |
final CountDownLatch running = new CountDownLatch(1); |
739 |
+ |
final StampedLock lock = new StampedLock(); |
740 |
+ |
final long p = assertValid(lock, lock.tryOptimisticRead()); |
741 |
+ |
|
742 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
743 |
+ |
public void realRun() throws InterruptedException { |
744 |
+ |
lock.writeLockInterruptibly(); |
745 |
+ |
running.countDown(); |
746 |
+ |
lock.writeLockInterruptibly(); |
747 |
+ |
}}); |
748 |
+ |
|
749 |
+ |
running.await(); |
750 |
+ |
assertFalse(lock.validate(p)); |
751 |
+ |
assertEquals(0L, lock.tryOptimisticRead()); |
752 |
+ |
t.interrupt(); |
753 |
+ |
awaitTermination(t); |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
/** |
757 |
+ |
* tryConvertToOptimisticRead succeeds and validates if successfully locked |
758 |
+ |
*/ |
759 |
+ |
public void testTryConvertToOptimisticRead() throws InterruptedException { |
760 |
+ |
StampedLock lock = new StampedLock(); |
761 |
+ |
long s, p, q; |
762 |
+ |
assertEquals(0L, lock.tryConvertToOptimisticRead(0L)); |
763 |
+ |
|
764 |
+ |
s = assertValid(lock, lock.tryOptimisticRead()); |
765 |
+ |
assertEquals(s, lock.tryConvertToOptimisticRead(s)); |
766 |
+ |
assertTrue(lock.validate(s)); |
767 |
+ |
|
768 |
+ |
for (Function<StampedLock, Long> writeLocker : writeLockers()) { |
769 |
+ |
s = assertValid(lock, writeLocker.apply(lock)); |
770 |
+ |
p = assertValid(lock, lock.tryConvertToOptimisticRead(s)); |
771 |
+ |
assertFalse(lock.validate(s)); |
772 |
+ |
assertTrue(lock.validate(p)); |
773 |
+ |
assertUnlocked(lock); |
774 |
+ |
} |
775 |
+ |
|
776 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) { |
777 |
+ |
s = assertValid(lock, readLocker.apply(lock)); |
778 |
+ |
q = assertValid(lock, lock.tryOptimisticRead()); |
779 |
+ |
assertEquals(q, lock.tryConvertToOptimisticRead(q)); |
780 |
+ |
assertTrue(lock.validate(q)); |
781 |
+ |
assertTrue(lock.isReadLocked()); |
782 |
+ |
p = assertValid(lock, lock.tryConvertToOptimisticRead(s)); |
783 |
+ |
assertTrue(lock.validate(p)); |
784 |
+ |
assertTrue(lock.validate(s)); |
785 |
+ |
assertUnlocked(lock); |
786 |
+ |
assertEquals(q, lock.tryConvertToOptimisticRead(q)); |
787 |
+ |
assertTrue(lock.validate(q)); |
788 |
+ |
} |
789 |
+ |
} |
790 |
+ |
|
791 |
+ |
/** |
792 |
+ |
* tryConvertToReadLock succeeds for valid stamps |
793 |
+ |
*/ |
794 |
+ |
public void testTryConvertToReadLock() throws InterruptedException { |
795 |
+ |
StampedLock lock = new StampedLock(); |
796 |
+ |
long s, p; |
797 |
+ |
|
798 |
+ |
assertEquals(0L, lock.tryConvertToReadLock(0L)); |
799 |
+ |
|
800 |
+ |
s = assertValid(lock, lock.tryOptimisticRead()); |
801 |
+ |
p = assertValid(lock, lock.tryConvertToReadLock(s)); |
802 |
+ |
assertTrue(lock.isReadLocked()); |
803 |
+ |
assertEquals(1, lock.getReadLockCount()); |
804 |
+ |
assertTrue(lock.validate(s)); |
805 |
+ |
lock.unlockRead(p); |
806 |
+ |
|
807 |
+ |
s = assertValid(lock, lock.tryOptimisticRead()); |
808 |
+ |
lock.readLock(); |
809 |
+ |
p = assertValid(lock, lock.tryConvertToReadLock(s)); |
810 |
+ |
assertTrue(lock.isReadLocked()); |
811 |
+ |
assertEquals(2, lock.getReadLockCount()); |
812 |
+ |
lock.unlockRead(p); |
813 |
+ |
lock.unlockRead(p); |
814 |
+ |
assertUnlocked(lock); |
815 |
+ |
|
816 |
+ |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
817 |
+ |
for (Function<StampedLock, Long> writeLocker : writeLockers()) { |
818 |
+ |
s = assertValid(lock, writeLocker.apply(lock)); |
819 |
+ |
p = assertValid(lock, lock.tryConvertToReadLock(s)); |
820 |
+ |
assertFalse(lock.validate(s)); |
821 |
+ |
assertTrue(lock.isReadLocked()); |
822 |
+ |
assertEquals(1, lock.getReadLockCount()); |
823 |
+ |
readUnlocker.accept(lock, p); |
824 |
+ |
} |
825 |
+ |
|
826 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) { |
827 |
+ |
s = assertValid(lock, readLocker.apply(lock)); |
828 |
+ |
assertEquals(s, lock.tryConvertToReadLock(s)); |
829 |
+ |
assertTrue(lock.validate(s)); |
830 |
+ |
assertTrue(lock.isReadLocked()); |
831 |
+ |
assertEquals(1, lock.getReadLockCount()); |
832 |
+ |
readUnlocker.accept(lock, s); |
833 |
+ |
} |
834 |
+ |
} |
835 |
+ |
} |
836 |
+ |
|
837 |
+ |
/** |
838 |
+ |
* tryConvertToWriteLock succeeds if lock available; fails if multiply read locked |
839 |
+ |
*/ |
840 |
+ |
public void testTryConvertToWriteLock() throws InterruptedException { |
841 |
+ |
StampedLock lock = new StampedLock(); |
842 |
+ |
long s, p; |
843 |
+ |
|
844 |
+ |
assertEquals(0L, lock.tryConvertToWriteLock(0L)); |
845 |
+ |
|
846 |
+ |
assertTrue((s = lock.tryOptimisticRead()) != 0L); |
847 |
+ |
assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L); |
848 |
+ |
assertTrue(lock.isWriteLocked()); |
849 |
+ |
lock.unlockWrite(p); |
850 |
+ |
|
851 |
+ |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
852 |
+ |
for (Function<StampedLock, Long> writeLocker : writeLockers()) { |
853 |
+ |
s = assertValid(lock, writeLocker.apply(lock)); |
854 |
+ |
assertEquals(s, lock.tryConvertToWriteLock(s)); |
855 |
+ |
assertTrue(lock.validate(s)); |
856 |
+ |
assertTrue(lock.isWriteLocked()); |
857 |
+ |
writeUnlocker.accept(lock, s); |
858 |
+ |
} |
859 |
+ |
|
860 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) { |
861 |
+ |
s = assertValid(lock, readLocker.apply(lock)); |
862 |
+ |
p = assertValid(lock, lock.tryConvertToWriteLock(s)); |
863 |
+ |
assertFalse(lock.validate(s)); |
864 |
+ |
assertTrue(lock.validate(p)); |
865 |
+ |
assertTrue(lock.isWriteLocked()); |
866 |
+ |
writeUnlocker.accept(lock, p); |
867 |
+ |
} |
868 |
+ |
} |
869 |
+ |
|
870 |
+ |
// failure if multiply read locked |
871 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) { |
872 |
+ |
s = assertValid(lock, readLocker.apply(lock)); |
873 |
+ |
p = assertValid(lock, readLocker.apply(lock)); |
874 |
+ |
assertEquals(0L, lock.tryConvertToWriteLock(s)); |
875 |
+ |
assertTrue(lock.validate(s)); |
876 |
+ |
assertTrue(lock.validate(p)); |
877 |
+ |
assertEquals(2, lock.getReadLockCount()); |
878 |
+ |
lock.unlock(p); |
879 |
+ |
lock.unlock(s); |
880 |
+ |
assertUnlocked(lock); |
881 |
+ |
} |
882 |
+ |
} |
883 |
+ |
|
884 |
+ |
/** |
885 |
+ |
* asWriteLock can be locked and unlocked |
886 |
+ |
*/ |
887 |
+ |
public void testAsWriteLock() throws Throwable { |
888 |
+ |
StampedLock sl = new StampedLock(); |
889 |
+ |
Lock lock = sl.asWriteLock(); |
890 |
+ |
for (Action locker : lockLockers(lock)) { |
891 |
+ |
locker.run(); |
892 |
+ |
assertTrue(sl.isWriteLocked()); |
893 |
+ |
assertFalse(sl.isReadLocked()); |
894 |
+ |
assertFalse(lock.tryLock()); |
895 |
+ |
lock.unlock(); |
896 |
+ |
assertUnlocked(sl); |
897 |
+ |
} |
898 |
+ |
} |
899 |
+ |
|
900 |
+ |
/** |
901 |
+ |
* asReadLock can be locked and unlocked |
902 |
+ |
*/ |
903 |
+ |
public void testAsReadLock() throws Throwable { |
904 |
+ |
StampedLock sl = new StampedLock(); |
905 |
+ |
Lock lock = sl.asReadLock(); |
906 |
+ |
for (Action locker : lockLockers(lock)) { |
907 |
+ |
locker.run(); |
908 |
+ |
assertTrue(sl.isReadLocked()); |
909 |
+ |
assertFalse(sl.isWriteLocked()); |
910 |
+ |
assertEquals(1, sl.getReadLockCount()); |
911 |
+ |
locker.run(); |
912 |
+ |
assertTrue(sl.isReadLocked()); |
913 |
+ |
assertEquals(2, sl.getReadLockCount()); |
914 |
+ |
lock.unlock(); |
915 |
+ |
lock.unlock(); |
916 |
+ |
assertUnlocked(sl); |
917 |
+ |
} |
918 |
+ |
} |
919 |
+ |
|
920 |
+ |
/** |
921 |
+ |
* asReadWriteLock.writeLock can be locked and unlocked |
922 |
+ |
*/ |
923 |
+ |
public void testAsReadWriteLockWriteLock() throws Throwable { |
924 |
+ |
StampedLock sl = new StampedLock(); |
925 |
+ |
Lock lock = sl.asReadWriteLock().writeLock(); |
926 |
+ |
for (Action locker : lockLockers(lock)) { |
927 |
+ |
locker.run(); |
928 |
+ |
assertTrue(sl.isWriteLocked()); |
929 |
+ |
assertFalse(sl.isReadLocked()); |
930 |
+ |
assertFalse(lock.tryLock()); |
931 |
+ |
lock.unlock(); |
932 |
+ |
assertUnlocked(sl); |
933 |
+ |
} |
934 |
+ |
} |
935 |
+ |
|
936 |
+ |
/** |
937 |
+ |
* asReadWriteLock.readLock can be locked and unlocked |
938 |
+ |
*/ |
939 |
+ |
public void testAsReadWriteLockReadLock() throws Throwable { |
940 |
+ |
StampedLock sl = new StampedLock(); |
941 |
+ |
Lock lock = sl.asReadWriteLock().readLock(); |
942 |
+ |
for (Action locker : lockLockers(lock)) { |
943 |
+ |
locker.run(); |
944 |
+ |
assertTrue(sl.isReadLocked()); |
945 |
+ |
assertFalse(sl.isWriteLocked()); |
946 |
+ |
assertEquals(1, sl.getReadLockCount()); |
947 |
+ |
locker.run(); |
948 |
+ |
assertTrue(sl.isReadLocked()); |
949 |
+ |
assertEquals(2, sl.getReadLockCount()); |
950 |
+ |
lock.unlock(); |
951 |
+ |
lock.unlock(); |
952 |
+ |
assertUnlocked(sl); |
953 |
+ |
} |
954 |
+ |
} |
955 |
+ |
|
956 |
+ |
/** |
957 |
+ |
* Lock.newCondition throws UnsupportedOperationException |
958 |
+ |
*/ |
959 |
+ |
public void testLockViewsDoNotSupportConditions() { |
960 |
+ |
StampedLock sl = new StampedLock(); |
961 |
+ |
assertThrows(UnsupportedOperationException.class, |
962 |
+ |
() -> sl.asWriteLock().newCondition(), |
963 |
+ |
() -> sl.asReadLock().newCondition(), |
964 |
+ |
() -> sl.asReadWriteLock().writeLock().newCondition(), |
965 |
+ |
() -> sl.asReadWriteLock().readLock().newCondition()); |
966 |
+ |
} |
967 |
+ |
|
968 |
+ |
/** |
969 |
+ |
* Passing optimistic read stamps to unlock operations result in |
970 |
+ |
* IllegalMonitorStateException |
971 |
+ |
*/ |
972 |
+ |
public void testCannotUnlockOptimisticReadStamps() { |
973 |
+ |
Runnable[] actions = { |
974 |
+ |
() -> { |
975 |
+ |
StampedLock sl = new StampedLock(); |
976 |
+ |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
977 |
+ |
sl.unlockRead(stamp); |
978 |
+ |
}, |
979 |
+ |
() -> { |
980 |
+ |
StampedLock sl = new StampedLock(); |
981 |
+ |
long stamp = sl.tryOptimisticRead(); |
982 |
+ |
sl.unlock(stamp); |
983 |
+ |
}, |
984 |
+ |
|
985 |
+ |
() -> { |
986 |
+ |
StampedLock sl = new StampedLock(); |
987 |
+ |
long stamp = sl.tryOptimisticRead(); |
988 |
+ |
sl.writeLock(); |
989 |
+ |
sl.unlock(stamp); |
990 |
+ |
}, |
991 |
+ |
() -> { |
992 |
+ |
StampedLock sl = new StampedLock(); |
993 |
+ |
sl.readLock(); |
994 |
+ |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
995 |
+ |
sl.unlockRead(stamp); |
996 |
+ |
}, |
997 |
+ |
() -> { |
998 |
+ |
StampedLock sl = new StampedLock(); |
999 |
+ |
sl.readLock(); |
1000 |
+ |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
1001 |
+ |
sl.unlock(stamp); |
1002 |
+ |
}, |
1003 |
+ |
|
1004 |
+ |
() -> { |
1005 |
+ |
StampedLock sl = new StampedLock(); |
1006 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1007 |
+ |
assertValid(sl, stamp); |
1008 |
+ |
sl.writeLock(); |
1009 |
+ |
sl.unlockWrite(stamp); |
1010 |
+ |
}, |
1011 |
+ |
() -> { |
1012 |
+ |
StampedLock sl = new StampedLock(); |
1013 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1014 |
+ |
sl.writeLock(); |
1015 |
+ |
sl.unlock(stamp); |
1016 |
+ |
}, |
1017 |
+ |
() -> { |
1018 |
+ |
StampedLock sl = new StampedLock(); |
1019 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1020 |
+ |
sl.readLock(); |
1021 |
+ |
sl.unlockRead(stamp); |
1022 |
+ |
}, |
1023 |
+ |
() -> { |
1024 |
+ |
StampedLock sl = new StampedLock(); |
1025 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1026 |
+ |
sl.readLock(); |
1027 |
+ |
sl.unlock(stamp); |
1028 |
+ |
}, |
1029 |
+ |
|
1030 |
+ |
() -> { |
1031 |
+ |
StampedLock sl = new StampedLock(); |
1032 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1033 |
+ |
assertValid(sl, stamp); |
1034 |
+ |
sl.writeLock(); |
1035 |
+ |
sl.unlockWrite(stamp); |
1036 |
+ |
}, |
1037 |
+ |
() -> { |
1038 |
+ |
StampedLock sl = new StampedLock(); |
1039 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1040 |
+ |
sl.writeLock(); |
1041 |
+ |
sl.unlock(stamp); |
1042 |
+ |
}, |
1043 |
+ |
() -> { |
1044 |
+ |
StampedLock sl = new StampedLock(); |
1045 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1046 |
+ |
sl.readLock(); |
1047 |
+ |
sl.unlockRead(stamp); |
1048 |
+ |
}, |
1049 |
+ |
() -> { |
1050 |
+ |
StampedLock sl = new StampedLock(); |
1051 |
+ |
sl.readLock(); |
1052 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1053 |
+ |
assertValid(sl, stamp); |
1054 |
+ |
sl.readLock(); |
1055 |
+ |
sl.unlockRead(stamp); |
1056 |
+ |
}, |
1057 |
+ |
() -> { |
1058 |
+ |
StampedLock sl = new StampedLock(); |
1059 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1060 |
+ |
sl.readLock(); |
1061 |
+ |
sl.unlock(stamp); |
1062 |
+ |
}, |
1063 |
+ |
() -> { |
1064 |
+ |
StampedLock sl = new StampedLock(); |
1065 |
+ |
sl.readLock(); |
1066 |
+ |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1067 |
+ |
sl.readLock(); |
1068 |
+ |
sl.unlock(stamp); |
1069 |
+ |
}, |
1070 |
+ |
}; |
1071 |
+ |
|
1072 |
+ |
assertThrows(IllegalMonitorStateException.class, actions); |
1073 |
+ |
} |
1074 |
+ |
|
1075 |
+ |
static long writeLockInterruptiblyUninterrupted(StampedLock sl) { |
1076 |
+ |
try { return sl.writeLockInterruptibly(); } |
1077 |
+ |
catch (InterruptedException ex) { throw new AssertionError(ex); } |
1078 |
+ |
} |
1079 |
+ |
|
1080 |
+ |
static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) { |
1081 |
+ |
try { return sl.tryWriteLock(time, unit); } |
1082 |
+ |
catch (InterruptedException ex) { throw new AssertionError(ex); } |
1083 |
+ |
} |
1084 |
+ |
|
1085 |
+ |
static long readLockInterruptiblyUninterrupted(StampedLock sl) { |
1086 |
+ |
try { return sl.readLockInterruptibly(); } |
1087 |
+ |
catch (InterruptedException ex) { throw new AssertionError(ex); } |
1088 |
+ |
} |
1089 |
+ |
|
1090 |
+ |
static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) { |
1091 |
+ |
try { return sl.tryReadLock(time, unit); } |
1092 |
+ |
catch (InterruptedException ex) { throw new AssertionError(ex); } |
1093 |
+ |
} |
1094 |
+ |
|
1095 |
+ |
/** |
1096 |
+ |
* Invalid stamps result in IllegalMonitorStateException |
1097 |
+ |
*/ |
1098 |
+ |
public void testInvalidStampsThrowIllegalMonitorStateException() { |
1099 |
+ |
final StampedLock sl = new StampedLock(); |
1100 |
+ |
|
1101 |
+ |
assertThrows(IllegalMonitorStateException.class, |
1102 |
+ |
() -> sl.unlockWrite(0L), |
1103 |
+ |
() -> sl.unlockRead(0L), |
1104 |
+ |
() -> sl.unlock(0L)); |
1105 |
+ |
|
1106 |
+ |
final long optimisticStamp = sl.tryOptimisticRead(); |
1107 |
+ |
final long readStamp = sl.readLock(); |
1108 |
+ |
sl.unlockRead(readStamp); |
1109 |
+ |
final long writeStamp = sl.writeLock(); |
1110 |
+ |
sl.unlockWrite(writeStamp); |
1111 |
+ |
assertTrue(optimisticStamp != 0L && readStamp != 0L && writeStamp != 0L); |
1112 |
+ |
final long[] noLongerValidStamps = { optimisticStamp, readStamp, writeStamp }; |
1113 |
+ |
final Runnable assertNoLongerValidStampsThrow = () -> { |
1114 |
+ |
for (long noLongerValidStamp : noLongerValidStamps) |
1115 |
+ |
assertThrows(IllegalMonitorStateException.class, |
1116 |
+ |
() -> sl.unlockWrite(noLongerValidStamp), |
1117 |
+ |
() -> sl.unlockRead(noLongerValidStamp), |
1118 |
+ |
() -> sl.unlock(noLongerValidStamp)); |
1119 |
+ |
}; |
1120 |
+ |
assertNoLongerValidStampsThrow.run(); |
1121 |
+ |
|
1122 |
+ |
for (Function<StampedLock, Long> readLocker : readLockers()) |
1123 |
+ |
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) { |
1124 |
+ |
final long stamp = readLocker.apply(sl); |
1125 |
+ |
assertValid(sl, stamp); |
1126 |
+ |
assertNoLongerValidStampsThrow.run(); |
1127 |
+ |
assertThrows(IllegalMonitorStateException.class, |
1128 |
+ |
() -> sl.unlockWrite(stamp), |
1129 |
+ |
() -> sl.unlockRead(sl.tryOptimisticRead()), |
1130 |
+ |
() -> sl.unlockRead(0L)); |
1131 |
+ |
readUnlocker.accept(sl, stamp); |
1132 |
+ |
assertUnlocked(sl); |
1133 |
+ |
assertNoLongerValidStampsThrow.run(); |
1134 |
+ |
} |
1135 |
+ |
|
1136 |
+ |
for (Function<StampedLock, Long> writeLocker : writeLockers()) |
1137 |
+ |
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) { |
1138 |
+ |
final long stamp = writeLocker.apply(sl); |
1139 |
+ |
assertValid(sl, stamp); |
1140 |
+ |
assertNoLongerValidStampsThrow.run(); |
1141 |
+ |
assertThrows(IllegalMonitorStateException.class, |
1142 |
+ |
() -> sl.unlockRead(stamp), |
1143 |
+ |
() -> sl.unlockWrite(0L)); |
1144 |
+ |
writeUnlocker.accept(sl, stamp); |
1145 |
+ |
assertUnlocked(sl); |
1146 |
+ |
assertNoLongerValidStampsThrow.run(); |
1147 |
+ |
} |
1148 |
+ |
} |
1149 |
+ |
|
1150 |
+ |
/** |
1151 |
+ |
* Read locks can be very deeply nested |
1152 |
+ |
*/ |
1153 |
+ |
public void testDeeplyNestedReadLocks() { |
1154 |
+ |
final StampedLock lock = new StampedLock(); |
1155 |
+ |
final int depth = 300; |
1156 |
+ |
final long[] stamps = new long[depth]; |
1157 |
+ |
final List<Function<StampedLock, Long>> readLockers = readLockers(); |
1158 |
+ |
final List<BiConsumer<StampedLock, Long>> readUnlockers = readUnlockers(); |
1159 |
+ |
for (int i = 0; i < depth; i++) { |
1160 |
+ |
Function<StampedLock, Long> readLocker |
1161 |
+ |
= readLockers.get(i % readLockers.size()); |
1162 |
+ |
long stamp = readLocker.apply(lock); |
1163 |
+ |
assertEquals(i + 1, lock.getReadLockCount()); |
1164 |
+ |
assertTrue(lock.isReadLocked()); |
1165 |
+ |
stamps[i] = stamp; |
1166 |
+ |
} |
1167 |
+ |
for (int i = 0; i < depth; i++) { |
1168 |
+ |
BiConsumer<StampedLock, Long> readUnlocker |
1169 |
+ |
= readUnlockers.get(i % readUnlockers.size()); |
1170 |
+ |
assertEquals(depth - i, lock.getReadLockCount()); |
1171 |
+ |
assertTrue(lock.isReadLocked()); |
1172 |
+ |
readUnlocker.accept(lock, stamps[depth - 1 - i]); |
1173 |
+ |
} |
1174 |
+ |
assertUnlocked(lock); |
1175 |
+ |
} |
1176 |
|
} |