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