1 |
dl |
1.1 |
/* |
2 |
dl |
1.12 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.44 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.28 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.concurrent.locks.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.2 |
import java.io.*; |
14 |
dl |
1.6 |
import java.util.*; |
15 |
dl |
1.1 |
|
16 |
dl |
1.3 |
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
17 |
dl |
1.1 |
public static void main(String[] args) { |
18 |
jsr166 |
1.41 |
junit.textui.TestRunner.run(suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
public static Test suite() { |
21 |
jsr166 |
1.35 |
return new TestSuite(ReentrantReadWriteLockTest.class); |
22 |
dl |
1.1 |
} |
23 |
|
|
|
24 |
dl |
1.6 |
/** |
25 |
|
|
* A runnable calling lockInterruptibly |
26 |
|
|
*/ |
27 |
jsr166 |
1.32 |
class InterruptibleLockRunnable extends CheckedRunnable { |
28 |
dl |
1.6 |
final ReentrantReadWriteLock lock; |
29 |
|
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
30 |
jsr166 |
1.32 |
public void realRun() throws InterruptedException { |
31 |
|
|
lock.writeLock().lockInterruptibly(); |
32 |
dl |
1.6 |
} |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* A runnable calling lockInterruptibly that expects to be |
38 |
|
|
* interrupted |
39 |
|
|
*/ |
40 |
jsr166 |
1.32 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
41 |
dl |
1.6 |
final ReentrantReadWriteLock lock; |
42 |
|
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
43 |
jsr166 |
1.32 |
public void realRun() throws InterruptedException { |
44 |
|
|
lock.writeLock().lockInterruptibly(); |
45 |
dl |
1.6 |
} |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Subclass to expose protected methods |
50 |
|
|
*/ |
51 |
|
|
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
52 |
|
|
PublicReentrantReadWriteLock() { super(); } |
53 |
jsr166 |
1.28 |
public Collection<Thread> getQueuedThreads() { |
54 |
|
|
return super.getQueuedThreads(); |
55 |
dl |
1.6 |
} |
56 |
jsr166 |
1.28 |
public Collection<Thread> getWaitingThreads(Condition c) { |
57 |
|
|
return super.getWaitingThreads(c); |
58 |
dl |
1.13 |
} |
59 |
dl |
1.6 |
} |
60 |
|
|
|
61 |
|
|
/** |
62 |
jsr166 |
1.46 |
* Releases lock, checking that it had a hold count of 1. |
63 |
|
|
*/ |
64 |
|
|
void releaseLock(ReentrantReadWriteLock.WriteLock lock) { |
65 |
|
|
assertTrue(lock.isHeldByCurrentThread()); |
66 |
|
|
lock.unlock(); |
67 |
|
|
assertFalse(lock.isHeldByCurrentThread()); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
dl |
1.6 |
* Constructor sets given fairness, and is in unlocked state |
72 |
|
|
*/ |
73 |
jsr166 |
1.28 |
public void testConstructor() { |
74 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
75 |
dl |
1.6 |
assertFalse(rl.isFair()); |
76 |
|
|
assertFalse(rl.isWriteLocked()); |
77 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
78 |
jsr166 |
1.35 |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
79 |
dl |
1.6 |
assertTrue(r2.isFair()); |
80 |
|
|
assertFalse(r2.isWriteLocked()); |
81 |
dl |
1.9 |
assertEquals(0, r2.getReadLockCount()); |
82 |
jsr166 |
1.35 |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
83 |
jsr166 |
1.32 |
assertFalse(r3.isFair()); |
84 |
|
|
assertFalse(r3.isWriteLocked()); |
85 |
|
|
assertEquals(0, r3.getReadLockCount()); |
86 |
dl |
1.6 |
} |
87 |
dl |
1.1 |
|
88 |
dl |
1.5 |
/** |
89 |
dl |
1.4 |
* write-locking and read-locking an unlocked lock succeed |
90 |
dl |
1.1 |
*/ |
91 |
jsr166 |
1.28 |
public void testLock() { |
92 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
93 |
dl |
1.4 |
rl.writeLock().lock(); |
94 |
dl |
1.6 |
assertTrue(rl.isWriteLocked()); |
95 |
|
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
96 |
dl |
1.25 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
97 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
98 |
dl |
1.4 |
rl.writeLock().unlock(); |
99 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
100 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
101 |
dl |
1.25 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
102 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
103 |
dl |
1.4 |
rl.readLock().lock(); |
104 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
105 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
106 |
dl |
1.9 |
assertEquals(1, rl.getReadLockCount()); |
107 |
dl |
1.4 |
rl.readLock().unlock(); |
108 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
109 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
110 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
111 |
dl |
1.4 |
} |
112 |
|
|
|
113 |
dl |
1.1 |
|
114 |
dl |
1.5 |
/** |
115 |
dl |
1.4 |
* locking an unlocked fair lock succeeds |
116 |
|
|
*/ |
117 |
jsr166 |
1.28 |
public void testFairLock() { |
118 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
119 |
dl |
1.4 |
rl.writeLock().lock(); |
120 |
dl |
1.6 |
assertTrue(rl.isWriteLocked()); |
121 |
|
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
122 |
dl |
1.25 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
123 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
124 |
dl |
1.4 |
rl.writeLock().unlock(); |
125 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
126 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
127 |
dl |
1.25 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
128 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
129 |
dl |
1.4 |
rl.readLock().lock(); |
130 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
131 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
132 |
dl |
1.9 |
assertEquals(1, rl.getReadLockCount()); |
133 |
dl |
1.4 |
rl.readLock().unlock(); |
134 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
135 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
136 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
137 |
dl |
1.2 |
} |
138 |
dl |
1.1 |
|
139 |
dl |
1.4 |
/** |
140 |
dl |
1.6 |
* getWriteHoldCount returns number of recursive holds |
141 |
|
|
*/ |
142 |
dl |
1.23 |
public void testGetWriteHoldCount() { |
143 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
144 |
|
|
for (int i = 1; i <= SIZE; i++) { |
145 |
|
|
lock.writeLock().lock(); |
146 |
|
|
assertEquals(i,lock.getWriteHoldCount()); |
147 |
|
|
} |
148 |
|
|
for (int i = SIZE; i > 0; i--) { |
149 |
|
|
lock.writeLock().unlock(); |
150 |
|
|
assertEquals(i-1,lock.getWriteHoldCount()); |
151 |
|
|
} |
152 |
dl |
1.6 |
} |
153 |
dl |
1.23 |
|
154 |
|
|
/** |
155 |
dl |
1.25 |
* WriteLock.getHoldCount returns number of recursive holds |
156 |
|
|
*/ |
157 |
|
|
public void testGetHoldCount() { |
158 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
159 |
|
|
for (int i = 1; i <= SIZE; i++) { |
160 |
|
|
lock.writeLock().lock(); |
161 |
|
|
assertEquals(i,lock.writeLock().getHoldCount()); |
162 |
|
|
} |
163 |
|
|
for (int i = SIZE; i > 0; i--) { |
164 |
|
|
lock.writeLock().unlock(); |
165 |
|
|
assertEquals(i-1,lock.writeLock().getHoldCount()); |
166 |
|
|
} |
167 |
dl |
1.25 |
} |
168 |
|
|
|
169 |
|
|
/** |
170 |
dl |
1.23 |
* getReadHoldCount returns number of recursive holds |
171 |
|
|
*/ |
172 |
|
|
public void testGetReadHoldCount() { |
173 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
174 |
|
|
for (int i = 1; i <= SIZE; i++) { |
175 |
|
|
lock.readLock().lock(); |
176 |
|
|
assertEquals(i,lock.getReadHoldCount()); |
177 |
|
|
} |
178 |
|
|
for (int i = SIZE; i > 0; i--) { |
179 |
|
|
lock.readLock().unlock(); |
180 |
|
|
assertEquals(i-1,lock.getReadHoldCount()); |
181 |
|
|
} |
182 |
dl |
1.23 |
} |
183 |
jsr166 |
1.28 |
|
184 |
dl |
1.6 |
|
185 |
|
|
/** |
186 |
dl |
1.4 |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
187 |
|
|
*/ |
188 |
jsr166 |
1.28 |
public void testUnlock_IllegalMonitorStateException() { |
189 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
190 |
|
|
try { |
191 |
|
|
rl.writeLock().unlock(); |
192 |
|
|
shouldThrow(); |
193 |
|
|
} catch (IllegalMonitorStateException success) {} |
194 |
dl |
1.4 |
} |
195 |
dl |
1.1 |
|
196 |
|
|
|
197 |
dl |
1.4 |
/** |
198 |
|
|
* write-lockInterruptibly is interruptible |
199 |
|
|
*/ |
200 |
jsr166 |
1.32 |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
201 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
202 |
jsr166 |
1.45 |
lock.writeLock().lock(); |
203 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
204 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
205 |
|
|
lock.writeLock().lockInterruptibly(); |
206 |
|
|
}}); |
207 |
jsr166 |
1.32 |
|
208 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
|
t.interrupt(); |
210 |
|
|
t.join(); |
211 |
jsr166 |
1.46 |
releaseLock(lock.writeLock()); |
212 |
jsr166 |
1.28 |
} |
213 |
dl |
1.1 |
|
214 |
dl |
1.4 |
/** |
215 |
dl |
1.15 |
* timed write-tryLock is interruptible |
216 |
dl |
1.4 |
*/ |
217 |
jsr166 |
1.32 |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
218 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
219 |
|
|
lock.writeLock().lock(); |
220 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
221 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
222 |
jsr166 |
1.38 |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
223 |
jsr166 |
1.33 |
}}); |
224 |
jsr166 |
1.32 |
|
225 |
jsr166 |
1.38 |
Thread.sleep(SHORT_DELAY_MS); |
226 |
jsr166 |
1.32 |
t.interrupt(); |
227 |
|
|
t.join(); |
228 |
jsr166 |
1.47 |
releaseLock(lock.writeLock()); |
229 |
dl |
1.2 |
} |
230 |
|
|
|
231 |
dl |
1.4 |
/** |
232 |
|
|
* read-lockInterruptibly is interruptible |
233 |
|
|
*/ |
234 |
jsr166 |
1.32 |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
235 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
236 |
|
|
lock.writeLock().lock(); |
237 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
238 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
239 |
|
|
lock.readLock().lockInterruptibly(); |
240 |
|
|
}}); |
241 |
jsr166 |
1.32 |
|
242 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
243 |
|
|
t.interrupt(); |
244 |
|
|
t.join(); |
245 |
jsr166 |
1.48 |
releaseLock(lock.writeLock()); |
246 |
jsr166 |
1.28 |
} |
247 |
dl |
1.2 |
|
248 |
dl |
1.4 |
/** |
249 |
dl |
1.15 |
* timed read-tryLock is interruptible |
250 |
dl |
1.4 |
*/ |
251 |
jsr166 |
1.32 |
public void testReadTryLock_Interrupted() throws InterruptedException { |
252 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
253 |
|
|
lock.writeLock().lock(); |
254 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
255 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
256 |
jsr166 |
1.39 |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
257 |
jsr166 |
1.33 |
}}); |
258 |
jsr166 |
1.32 |
|
259 |
jsr166 |
1.39 |
Thread.sleep(SHORT_DELAY_MS); |
260 |
jsr166 |
1.32 |
t.interrupt(); |
261 |
|
|
t.join(); |
262 |
jsr166 |
1.49 |
releaseLock(lock.writeLock()); |
263 |
dl |
1.1 |
} |
264 |
|
|
|
265 |
jsr166 |
1.28 |
|
266 |
dl |
1.4 |
/** |
267 |
dl |
1.15 |
* write-tryLock fails if locked |
268 |
dl |
1.4 |
*/ |
269 |
jsr166 |
1.32 |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
270 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
271 |
|
|
lock.writeLock().lock(); |
272 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
273 |
jsr166 |
1.37 |
public void realRun() { |
274 |
jsr166 |
1.42 |
assertFalse(lock.writeLock().tryLock()); |
275 |
jsr166 |
1.37 |
}}); |
276 |
jsr166 |
1.32 |
|
277 |
|
|
t.join(); |
278 |
|
|
lock.writeLock().unlock(); |
279 |
jsr166 |
1.28 |
} |
280 |
dl |
1.2 |
|
281 |
dl |
1.4 |
/** |
282 |
dl |
1.15 |
* read-tryLock fails if locked |
283 |
dl |
1.4 |
*/ |
284 |
jsr166 |
1.32 |
public void testReadTryLockWhenLocked() throws InterruptedException { |
285 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
|
|
lock.writeLock().lock(); |
287 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
288 |
jsr166 |
1.37 |
public void realRun() { |
289 |
jsr166 |
1.42 |
assertFalse(lock.readLock().tryLock()); |
290 |
jsr166 |
1.37 |
}}); |
291 |
jsr166 |
1.32 |
|
292 |
|
|
t.join(); |
293 |
|
|
lock.writeLock().unlock(); |
294 |
jsr166 |
1.28 |
} |
295 |
dl |
1.2 |
|
296 |
dl |
1.4 |
/** |
297 |
|
|
* Multiple threads can hold a read lock when not write-locked |
298 |
|
|
*/ |
299 |
jsr166 |
1.32 |
public void testMultipleReadLocks() throws InterruptedException { |
300 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
|
|
lock.readLock().lock(); |
302 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
303 |
jsr166 |
1.37 |
public void realRun() { |
304 |
jsr166 |
1.42 |
assertTrue(lock.readLock().tryLock()); |
305 |
jsr166 |
1.37 |
lock.readLock().unlock(); |
306 |
|
|
}}); |
307 |
jsr166 |
1.32 |
|
308 |
|
|
t.join(); |
309 |
|
|
lock.readLock().unlock(); |
310 |
jsr166 |
1.28 |
} |
311 |
dl |
1.2 |
|
312 |
dl |
1.4 |
/** |
313 |
|
|
* A writelock succeeds after reading threads unlock |
314 |
|
|
*/ |
315 |
jsr166 |
1.32 |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
316 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
317 |
|
|
lock.readLock().lock(); |
318 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
319 |
jsr166 |
1.37 |
public void realRun() { |
320 |
|
|
lock.readLock().lock(); |
321 |
|
|
lock.readLock().unlock(); |
322 |
|
|
}}); |
323 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
324 |
jsr166 |
1.37 |
public void realRun() { |
325 |
|
|
lock.writeLock().lock(); |
326 |
|
|
lock.writeLock().unlock(); |
327 |
|
|
}}); |
328 |
dl |
1.2 |
|
329 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
330 |
|
|
lock.readLock().unlock(); |
331 |
|
|
t1.join(MEDIUM_DELAY_MS); |
332 |
|
|
t2.join(MEDIUM_DELAY_MS); |
333 |
|
|
assertTrue(!t1.isAlive()); |
334 |
|
|
assertTrue(!t2.isAlive()); |
335 |
jsr166 |
1.28 |
} |
336 |
dl |
1.2 |
|
337 |
dl |
1.4 |
/** |
338 |
|
|
* Readlocks succeed after a writing thread unlocks |
339 |
|
|
*/ |
340 |
jsr166 |
1.32 |
public void testReadAfterWriteLock() throws InterruptedException { |
341 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
342 |
|
|
lock.writeLock().lock(); |
343 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
344 |
jsr166 |
1.37 |
public void realRun() { |
345 |
|
|
lock.readLock().lock(); |
346 |
|
|
lock.readLock().unlock(); |
347 |
|
|
}}); |
348 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
349 |
jsr166 |
1.37 |
public void realRun() { |
350 |
|
|
lock.readLock().lock(); |
351 |
|
|
lock.readLock().unlock(); |
352 |
|
|
}}); |
353 |
dl |
1.2 |
|
354 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
355 |
|
|
lock.writeLock().unlock(); |
356 |
|
|
t1.join(MEDIUM_DELAY_MS); |
357 |
|
|
t2.join(MEDIUM_DELAY_MS); |
358 |
|
|
assertTrue(!t1.isAlive()); |
359 |
|
|
assertTrue(!t2.isAlive()); |
360 |
jsr166 |
1.28 |
} |
361 |
dl |
1.2 |
|
362 |
dl |
1.20 |
/** |
363 |
|
|
* Read trylock succeeds if write locked by current thread |
364 |
|
|
*/ |
365 |
jsr166 |
1.28 |
public void testReadHoldingWriteLock() { |
366 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
367 |
|
|
lock.writeLock().lock(); |
368 |
dl |
1.20 |
assertTrue(lock.readLock().tryLock()); |
369 |
|
|
lock.readLock().unlock(); |
370 |
|
|
lock.writeLock().unlock(); |
371 |
jsr166 |
1.28 |
} |
372 |
dl |
1.20 |
|
373 |
|
|
/** |
374 |
|
|
* Read lock succeeds if write locked by current thread even if |
375 |
dl |
1.23 |
* other threads are waiting for readlock |
376 |
dl |
1.20 |
*/ |
377 |
jsr166 |
1.32 |
public void testReadHoldingWriteLock2() throws InterruptedException { |
378 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
379 |
|
|
lock.writeLock().lock(); |
380 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
381 |
jsr166 |
1.37 |
public void realRun() { |
382 |
|
|
lock.readLock().lock(); |
383 |
|
|
lock.readLock().unlock(); |
384 |
|
|
}}); |
385 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
386 |
jsr166 |
1.37 |
public void realRun() { |
387 |
|
|
lock.readLock().lock(); |
388 |
|
|
lock.readLock().unlock(); |
389 |
|
|
}}); |
390 |
dl |
1.20 |
|
391 |
jsr166 |
1.32 |
lock.readLock().lock(); |
392 |
|
|
lock.readLock().unlock(); |
393 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
394 |
|
|
lock.readLock().lock(); |
395 |
|
|
lock.readLock().unlock(); |
396 |
|
|
lock.writeLock().unlock(); |
397 |
|
|
t1.join(MEDIUM_DELAY_MS); |
398 |
|
|
t2.join(MEDIUM_DELAY_MS); |
399 |
|
|
assertTrue(!t1.isAlive()); |
400 |
|
|
assertTrue(!t2.isAlive()); |
401 |
jsr166 |
1.28 |
} |
402 |
dl |
1.20 |
|
403 |
|
|
/** |
404 |
jsr166 |
1.43 |
* Read lock succeeds if write locked by current thread even if |
405 |
dl |
1.23 |
* other threads are waiting for writelock |
406 |
|
|
*/ |
407 |
jsr166 |
1.32 |
public void testReadHoldingWriteLock3() throws InterruptedException { |
408 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
409 |
|
|
lock.writeLock().lock(); |
410 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
411 |
jsr166 |
1.37 |
public void realRun() { |
412 |
|
|
lock.writeLock().lock(); |
413 |
|
|
lock.writeLock().unlock(); |
414 |
|
|
}}); |
415 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
416 |
jsr166 |
1.37 |
public void realRun() { |
417 |
|
|
lock.writeLock().lock(); |
418 |
|
|
lock.writeLock().unlock(); |
419 |
|
|
}}); |
420 |
dl |
1.23 |
|
421 |
jsr166 |
1.32 |
lock.readLock().lock(); |
422 |
|
|
lock.readLock().unlock(); |
423 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
424 |
|
|
lock.readLock().lock(); |
425 |
|
|
lock.readLock().unlock(); |
426 |
|
|
lock.writeLock().unlock(); |
427 |
|
|
t1.join(MEDIUM_DELAY_MS); |
428 |
|
|
t2.join(MEDIUM_DELAY_MS); |
429 |
|
|
assertTrue(!t1.isAlive()); |
430 |
|
|
assertTrue(!t2.isAlive()); |
431 |
jsr166 |
1.28 |
} |
432 |
dl |
1.23 |
|
433 |
|
|
|
434 |
|
|
/** |
435 |
jsr166 |
1.43 |
* Write lock succeeds if write locked by current thread even if |
436 |
dl |
1.23 |
* other threads are waiting for writelock |
437 |
|
|
*/ |
438 |
jsr166 |
1.32 |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
439 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
440 |
|
|
lock.writeLock().lock(); |
441 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
442 |
jsr166 |
1.37 |
public void realRun() { |
443 |
|
|
lock.writeLock().lock(); |
444 |
|
|
lock.writeLock().unlock(); |
445 |
|
|
}}); |
446 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
447 |
jsr166 |
1.37 |
public void realRun() { |
448 |
|
|
lock.writeLock().lock(); |
449 |
|
|
lock.writeLock().unlock(); |
450 |
|
|
}}); |
451 |
dl |
1.23 |
|
452 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
453 |
|
|
lock.writeLock().unlock(); |
454 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
455 |
|
|
lock.writeLock().lock(); |
456 |
|
|
lock.writeLock().unlock(); |
457 |
|
|
lock.writeLock().unlock(); |
458 |
|
|
t1.join(MEDIUM_DELAY_MS); |
459 |
|
|
t2.join(MEDIUM_DELAY_MS); |
460 |
|
|
assertTrue(!t1.isAlive()); |
461 |
|
|
assertTrue(!t2.isAlive()); |
462 |
jsr166 |
1.28 |
} |
463 |
dl |
1.23 |
|
464 |
|
|
|
465 |
|
|
/** |
466 |
dl |
1.20 |
* Fair Read trylock succeeds if write locked by current thread |
467 |
|
|
*/ |
468 |
jsr166 |
1.28 |
public void testReadHoldingWriteLockFair() { |
469 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
470 |
|
|
lock.writeLock().lock(); |
471 |
dl |
1.20 |
assertTrue(lock.readLock().tryLock()); |
472 |
|
|
lock.readLock().unlock(); |
473 |
|
|
lock.writeLock().unlock(); |
474 |
jsr166 |
1.28 |
} |
475 |
dl |
1.20 |
|
476 |
|
|
/** |
477 |
|
|
* Fair Read lock succeeds if write locked by current thread even if |
478 |
dl |
1.23 |
* other threads are waiting for readlock |
479 |
dl |
1.20 |
*/ |
480 |
jsr166 |
1.32 |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
481 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
482 |
|
|
lock.writeLock().lock(); |
483 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
484 |
jsr166 |
1.37 |
public void realRun() { |
485 |
|
|
lock.readLock().lock(); |
486 |
|
|
lock.readLock().unlock(); |
487 |
|
|
}}); |
488 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
489 |
jsr166 |
1.37 |
public void realRun() { |
490 |
|
|
lock.readLock().lock(); |
491 |
|
|
lock.readLock().unlock(); |
492 |
|
|
}}); |
493 |
dl |
1.20 |
|
494 |
jsr166 |
1.32 |
lock.readLock().lock(); |
495 |
|
|
lock.readLock().unlock(); |
496 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
497 |
|
|
lock.readLock().lock(); |
498 |
|
|
lock.readLock().unlock(); |
499 |
|
|
lock.writeLock().unlock(); |
500 |
|
|
t1.join(MEDIUM_DELAY_MS); |
501 |
|
|
t2.join(MEDIUM_DELAY_MS); |
502 |
|
|
assertTrue(!t1.isAlive()); |
503 |
|
|
assertTrue(!t2.isAlive()); |
504 |
jsr166 |
1.28 |
} |
505 |
dl |
1.20 |
|
506 |
dl |
1.2 |
|
507 |
dl |
1.4 |
/** |
508 |
dl |
1.23 |
* Fair Read lock succeeds if write locked by current thread even if |
509 |
|
|
* other threads are waiting for writelock |
510 |
|
|
*/ |
511 |
jsr166 |
1.32 |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
512 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
513 |
|
|
lock.writeLock().lock(); |
514 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
515 |
jsr166 |
1.37 |
public void realRun() { |
516 |
|
|
lock.writeLock().lock(); |
517 |
|
|
lock.writeLock().unlock(); |
518 |
|
|
}}); |
519 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
520 |
jsr166 |
1.37 |
public void realRun() { |
521 |
|
|
lock.writeLock().lock(); |
522 |
|
|
lock.writeLock().unlock(); |
523 |
|
|
}}); |
524 |
dl |
1.23 |
|
525 |
jsr166 |
1.32 |
lock.readLock().lock(); |
526 |
|
|
lock.readLock().unlock(); |
527 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
528 |
|
|
lock.readLock().lock(); |
529 |
|
|
lock.readLock().unlock(); |
530 |
|
|
lock.writeLock().unlock(); |
531 |
|
|
t1.join(MEDIUM_DELAY_MS); |
532 |
|
|
t2.join(MEDIUM_DELAY_MS); |
533 |
|
|
assertTrue(!t1.isAlive()); |
534 |
|
|
assertTrue(!t2.isAlive()); |
535 |
jsr166 |
1.28 |
} |
536 |
dl |
1.23 |
|
537 |
|
|
|
538 |
|
|
/** |
539 |
|
|
* Fair Write lock succeeds if write locked by current thread even if |
540 |
|
|
* other threads are waiting for writelock |
541 |
|
|
*/ |
542 |
jsr166 |
1.32 |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
543 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
544 |
|
|
lock.writeLock().lock(); |
545 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
546 |
jsr166 |
1.37 |
public void realRun() { |
547 |
|
|
lock.writeLock().lock(); |
548 |
|
|
lock.writeLock().unlock(); |
549 |
|
|
}}); |
550 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
551 |
jsr166 |
1.37 |
public void realRun() { |
552 |
|
|
lock.writeLock().lock(); |
553 |
|
|
lock.writeLock().unlock(); |
554 |
|
|
}}); |
555 |
dl |
1.23 |
|
556 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
557 |
|
|
assertTrue(lock.isWriteLockedByCurrentThread()); |
558 |
jsr166 |
1.42 |
assertEquals(1, lock.getWriteHoldCount()); |
559 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
560 |
jsr166 |
1.42 |
assertEquals(2, lock.getWriteHoldCount()); |
561 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
562 |
|
|
lock.writeLock().lock(); |
563 |
|
|
lock.writeLock().unlock(); |
564 |
|
|
lock.writeLock().unlock(); |
565 |
|
|
t1.join(MEDIUM_DELAY_MS); |
566 |
|
|
t2.join(MEDIUM_DELAY_MS); |
567 |
|
|
assertTrue(!t1.isAlive()); |
568 |
|
|
assertTrue(!t2.isAlive()); |
569 |
jsr166 |
1.28 |
} |
570 |
dl |
1.23 |
|
571 |
|
|
|
572 |
|
|
/** |
573 |
dl |
1.15 |
* Read tryLock succeeds if readlocked but not writelocked |
574 |
dl |
1.4 |
*/ |
575 |
jsr166 |
1.32 |
public void testTryLockWhenReadLocked() throws InterruptedException { |
576 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
577 |
|
|
lock.readLock().lock(); |
578 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
579 |
jsr166 |
1.37 |
public void realRun() { |
580 |
jsr166 |
1.42 |
assertTrue(lock.readLock().tryLock()); |
581 |
jsr166 |
1.37 |
lock.readLock().unlock(); |
582 |
|
|
}}); |
583 |
jsr166 |
1.32 |
|
584 |
|
|
t.join(); |
585 |
|
|
lock.readLock().unlock(); |
586 |
jsr166 |
1.28 |
} |
587 |
|
|
|
588 |
dl |
1.4 |
/** |
589 |
dl |
1.15 |
* write tryLock fails when readlocked |
590 |
dl |
1.4 |
*/ |
591 |
jsr166 |
1.32 |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
592 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
593 |
|
|
lock.readLock().lock(); |
594 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
595 |
jsr166 |
1.37 |
public void realRun() { |
596 |
jsr166 |
1.42 |
assertFalse(lock.writeLock().tryLock()); |
597 |
jsr166 |
1.37 |
}}); |
598 |
jsr166 |
1.32 |
|
599 |
|
|
t.join(); |
600 |
|
|
lock.readLock().unlock(); |
601 |
jsr166 |
1.28 |
} |
602 |
dl |
1.2 |
|
603 |
dl |
1.24 |
|
604 |
|
|
/** |
605 |
|
|
* Fair Read tryLock succeeds if readlocked but not writelocked |
606 |
|
|
*/ |
607 |
jsr166 |
1.32 |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
608 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
609 |
|
|
lock.readLock().lock(); |
610 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
611 |
jsr166 |
1.37 |
public void realRun() { |
612 |
jsr166 |
1.42 |
assertTrue(lock.readLock().tryLock()); |
613 |
jsr166 |
1.37 |
lock.readLock().unlock(); |
614 |
|
|
}}); |
615 |
jsr166 |
1.32 |
|
616 |
|
|
t.join(); |
617 |
|
|
lock.readLock().unlock(); |
618 |
jsr166 |
1.28 |
} |
619 |
|
|
|
620 |
dl |
1.24 |
|
621 |
|
|
|
622 |
|
|
/** |
623 |
|
|
* Fair write tryLock fails when readlocked |
624 |
|
|
*/ |
625 |
jsr166 |
1.32 |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
626 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
627 |
|
|
lock.readLock().lock(); |
628 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
629 |
jsr166 |
1.37 |
public void realRun() { |
630 |
jsr166 |
1.42 |
assertFalse(lock.writeLock().tryLock()); |
631 |
jsr166 |
1.37 |
}}); |
632 |
jsr166 |
1.32 |
|
633 |
|
|
t.join(); |
634 |
|
|
lock.readLock().unlock(); |
635 |
jsr166 |
1.28 |
} |
636 |
|
|
|
637 |
dl |
1.24 |
|
638 |
dl |
1.2 |
|
639 |
dl |
1.4 |
/** |
640 |
dl |
1.15 |
* write timed tryLock times out if locked |
641 |
dl |
1.4 |
*/ |
642 |
jsr166 |
1.32 |
public void testWriteTryLock_Timeout() throws InterruptedException { |
643 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
644 |
|
|
lock.writeLock().lock(); |
645 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
646 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
647 |
jsr166 |
1.42 |
assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
648 |
jsr166 |
1.33 |
}}); |
649 |
jsr166 |
1.32 |
|
650 |
|
|
t.join(); |
651 |
jsr166 |
1.34 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
652 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
653 |
jsr166 |
1.28 |
} |
654 |
dl |
1.2 |
|
655 |
dl |
1.4 |
/** |
656 |
dl |
1.15 |
* read timed tryLock times out if write-locked |
657 |
dl |
1.4 |
*/ |
658 |
jsr166 |
1.32 |
public void testReadTryLock_Timeout() throws InterruptedException { |
659 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
660 |
|
|
lock.writeLock().lock(); |
661 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
662 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
663 |
jsr166 |
1.42 |
assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
664 |
jsr166 |
1.33 |
}}); |
665 |
jsr166 |
1.32 |
|
666 |
|
|
t.join(); |
667 |
jsr166 |
1.34 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
668 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
669 |
jsr166 |
1.28 |
} |
670 |
dl |
1.2 |
|
671 |
|
|
|
672 |
dl |
1.4 |
/** |
673 |
|
|
* write lockInterruptibly succeeds if lock free else is interruptible |
674 |
|
|
*/ |
675 |
jsr166 |
1.32 |
public void testWriteLockInterruptibly() throws InterruptedException { |
676 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
677 |
jsr166 |
1.32 |
lock.writeLock().lockInterruptibly(); |
678 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
679 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
680 |
|
|
lock.writeLock().lockInterruptibly(); |
681 |
|
|
}}); |
682 |
jsr166 |
1.32 |
|
683 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
684 |
|
|
t.interrupt(); |
685 |
|
|
t.join(); |
686 |
jsr166 |
1.50 |
releaseLock(lock.writeLock()); |
687 |
dl |
1.2 |
} |
688 |
|
|
|
689 |
dl |
1.4 |
/** |
690 |
jsr166 |
1.43 |
* read lockInterruptibly succeeds if lock free else is interruptible |
691 |
dl |
1.4 |
*/ |
692 |
jsr166 |
1.32 |
public void testReadLockInterruptibly() throws InterruptedException { |
693 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
694 |
jsr166 |
1.32 |
lock.writeLock().lockInterruptibly(); |
695 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
696 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
697 |
|
|
lock.readLock().lockInterruptibly(); |
698 |
|
|
}}); |
699 |
jsr166 |
1.32 |
|
700 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
701 |
|
|
t.interrupt(); |
702 |
|
|
t.join(); |
703 |
jsr166 |
1.51 |
releaseLock(lock.writeLock()); |
704 |
dl |
1.2 |
} |
705 |
|
|
|
706 |
dl |
1.4 |
/** |
707 |
|
|
* Calling await without holding lock throws IllegalMonitorStateException |
708 |
|
|
*/ |
709 |
jsr166 |
1.32 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
710 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
711 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
712 |
|
|
try { |
713 |
|
|
c.await(); |
714 |
dl |
1.4 |
shouldThrow(); |
715 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
716 |
dl |
1.2 |
} |
717 |
|
|
|
718 |
dl |
1.4 |
/** |
719 |
|
|
* Calling signal without holding lock throws IllegalMonitorStateException |
720 |
|
|
*/ |
721 |
dl |
1.2 |
public void testSignal_IllegalMonitor() { |
722 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
723 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
724 |
|
|
try { |
725 |
|
|
c.signal(); |
726 |
dl |
1.4 |
shouldThrow(); |
727 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
728 |
dl |
1.2 |
} |
729 |
|
|
|
730 |
dl |
1.4 |
/** |
731 |
|
|
* awaitNanos without a signal times out |
732 |
|
|
*/ |
733 |
jsr166 |
1.32 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
734 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
735 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
736 |
jsr166 |
1.32 |
|
737 |
|
|
lock.writeLock().lock(); |
738 |
|
|
long t = c.awaitNanos(100); |
739 |
|
|
assertTrue(t <= 0); |
740 |
|
|
lock.writeLock().unlock(); |
741 |
dl |
1.2 |
} |
742 |
|
|
|
743 |
dl |
1.4 |
|
744 |
|
|
/** |
745 |
jsr166 |
1.43 |
* timed await without a signal times out |
746 |
dl |
1.4 |
*/ |
747 |
jsr166 |
1.34 |
public void testAwait_Timeout() throws InterruptedException { |
748 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
749 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
750 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
751 |
jsr166 |
1.36 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
752 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
753 |
dl |
1.2 |
} |
754 |
|
|
|
755 |
dl |
1.4 |
/** |
756 |
|
|
* awaitUntil without a signal times out |
757 |
|
|
*/ |
758 |
jsr166 |
1.34 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
759 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
760 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
761 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
762 |
|
|
java.util.Date d = new java.util.Date(); |
763 |
jsr166 |
1.34 |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
764 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
765 |
dl |
1.2 |
} |
766 |
dl |
1.1 |
|
767 |
dl |
1.4 |
/** |
768 |
|
|
* await returns when signalled |
769 |
|
|
*/ |
770 |
jsr166 |
1.32 |
public void testAwait() throws InterruptedException { |
771 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
772 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
773 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
774 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
775 |
|
|
lock.writeLock().lock(); |
776 |
|
|
c.await(); |
777 |
|
|
lock.writeLock().unlock(); |
778 |
|
|
}}); |
779 |
dl |
1.2 |
|
780 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
781 |
|
|
lock.writeLock().lock(); |
782 |
|
|
c.signal(); |
783 |
|
|
lock.writeLock().unlock(); |
784 |
|
|
t.join(SHORT_DELAY_MS); |
785 |
|
|
assertFalse(t.isAlive()); |
786 |
dl |
1.2 |
} |
787 |
|
|
|
788 |
dl |
1.22 |
/** A helper class for uninterruptible wait tests */ |
789 |
|
|
class UninterruptableThread extends Thread { |
790 |
|
|
private Lock lock; |
791 |
|
|
private Condition c; |
792 |
jsr166 |
1.28 |
|
793 |
dl |
1.22 |
public volatile boolean canAwake = false; |
794 |
|
|
public volatile boolean interrupted = false; |
795 |
|
|
public volatile boolean lockStarted = false; |
796 |
jsr166 |
1.28 |
|
797 |
dl |
1.22 |
public UninterruptableThread(Lock lock, Condition c) { |
798 |
|
|
this.lock = lock; |
799 |
|
|
this.c = c; |
800 |
|
|
} |
801 |
jsr166 |
1.28 |
|
802 |
dl |
1.22 |
public synchronized void run() { |
803 |
|
|
lock.lock(); |
804 |
|
|
lockStarted = true; |
805 |
jsr166 |
1.28 |
|
806 |
dl |
1.22 |
while (!canAwake) { |
807 |
|
|
c.awaitUninterruptibly(); |
808 |
|
|
} |
809 |
jsr166 |
1.28 |
|
810 |
dl |
1.22 |
interrupted = isInterrupted(); |
811 |
|
|
lock.unlock(); |
812 |
|
|
} |
813 |
|
|
} |
814 |
|
|
|
815 |
dl |
1.4 |
/** |
816 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
817 |
|
|
*/ |
818 |
jsr166 |
1.32 |
public void testAwaitUninterruptibly() throws InterruptedException { |
819 |
dl |
1.22 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
820 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
821 |
dl |
1.22 |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
822 |
dl |
1.2 |
|
823 |
jsr166 |
1.32 |
thread.start(); |
824 |
dl |
1.22 |
|
825 |
jsr166 |
1.32 |
while (!thread.lockStarted) { |
826 |
|
|
Thread.sleep(100); |
827 |
|
|
} |
828 |
dl |
1.22 |
|
829 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
830 |
|
|
try { |
831 |
|
|
thread.interrupt(); |
832 |
|
|
thread.canAwake = true; |
833 |
|
|
c.signal(); |
834 |
|
|
} finally { |
835 |
|
|
lock.writeLock().unlock(); |
836 |
|
|
} |
837 |
dl |
1.22 |
|
838 |
jsr166 |
1.32 |
thread.join(); |
839 |
|
|
assertTrue(thread.interrupted); |
840 |
|
|
assertFalse(thread.isAlive()); |
841 |
dl |
1.2 |
} |
842 |
|
|
|
843 |
dl |
1.4 |
/** |
844 |
|
|
* await is interruptible |
845 |
|
|
*/ |
846 |
jsr166 |
1.32 |
public void testAwait_Interrupt() throws InterruptedException { |
847 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
848 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
849 |
jsr166 |
1.52 |
final CountDownLatch locked = new CountDownLatch(1); |
850 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
851 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
852 |
|
|
lock.writeLock().lock(); |
853 |
jsr166 |
1.52 |
assertTrue(lock.isWriteLocked()); |
854 |
|
|
locked.countDown(); |
855 |
|
|
try { c.await(); } |
856 |
|
|
finally { lock.writeLock().unlock(); } |
857 |
jsr166 |
1.33 |
}}); |
858 |
dl |
1.2 |
|
859 |
jsr166 |
1.52 |
locked.await(); |
860 |
|
|
while (lock.isWriteLocked()) |
861 |
|
|
Thread.yield(); |
862 |
jsr166 |
1.32 |
t.interrupt(); |
863 |
jsr166 |
1.52 |
awaitTermination(t, LONG_DELAY_MS); |
864 |
|
|
assertFalse(lock.isWriteLocked()); |
865 |
dl |
1.2 |
} |
866 |
|
|
|
867 |
dl |
1.4 |
/** |
868 |
|
|
* awaitNanos is interruptible |
869 |
|
|
*/ |
870 |
jsr166 |
1.32 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
871 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
872 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
873 |
jsr166 |
1.52 |
final CountDownLatch locked = new CountDownLatch(1); |
874 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
875 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
876 |
|
|
lock.writeLock().lock(); |
877 |
jsr166 |
1.52 |
assertTrue(lock.isWriteLocked()); |
878 |
|
|
locked.countDown(); |
879 |
|
|
try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); } |
880 |
|
|
finally { lock.writeLock().unlock(); } |
881 |
jsr166 |
1.33 |
}}); |
882 |
dl |
1.2 |
|
883 |
jsr166 |
1.52 |
locked.await(); |
884 |
|
|
while (lock.isWriteLocked()) |
885 |
|
|
Thread.yield(); |
886 |
jsr166 |
1.32 |
t.interrupt(); |
887 |
jsr166 |
1.52 |
awaitTermination(t, LONG_DELAY_MS); |
888 |
|
|
assertFalse(lock.isWriteLocked()); |
889 |
dl |
1.2 |
} |
890 |
dl |
1.1 |
|
891 |
dl |
1.4 |
/** |
892 |
|
|
* awaitUntil is interruptible |
893 |
|
|
*/ |
894 |
jsr166 |
1.32 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
895 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
896 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
897 |
jsr166 |
1.52 |
final CountDownLatch locked = new CountDownLatch(1); |
898 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
899 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
900 |
|
|
lock.writeLock().lock(); |
901 |
jsr166 |
1.52 |
assertTrue(lock.isWriteLocked()); |
902 |
|
|
locked.countDown(); |
903 |
jsr166 |
1.33 |
java.util.Date d = new java.util.Date(); |
904 |
jsr166 |
1.52 |
try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); } |
905 |
|
|
finally { lock.writeLock().unlock(); } |
906 |
jsr166 |
1.33 |
}}); |
907 |
dl |
1.2 |
|
908 |
jsr166 |
1.52 |
locked.await(); |
909 |
|
|
while (lock.isWriteLocked()) |
910 |
|
|
Thread.yield(); |
911 |
jsr166 |
1.32 |
t.interrupt(); |
912 |
jsr166 |
1.52 |
awaitTermination(t, LONG_DELAY_MS); |
913 |
|
|
assertFalse(lock.isWriteLocked()); |
914 |
dl |
1.2 |
} |
915 |
|
|
|
916 |
dl |
1.4 |
/** |
917 |
|
|
* signalAll wakes up all threads |
918 |
|
|
*/ |
919 |
jsr166 |
1.32 |
public void testSignalAll() throws InterruptedException { |
920 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
921 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
922 |
jsr166 |
1.45 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
923 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
924 |
|
|
lock.writeLock().lock(); |
925 |
|
|
c.await(); |
926 |
|
|
lock.writeLock().unlock(); |
927 |
|
|
}}); |
928 |
|
|
|
929 |
jsr166 |
1.45 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
930 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
931 |
|
|
lock.writeLock().lock(); |
932 |
|
|
c.await(); |
933 |
|
|
lock.writeLock().unlock(); |
934 |
|
|
}}); |
935 |
dl |
1.2 |
|
936 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
937 |
|
|
lock.writeLock().lock(); |
938 |
|
|
c.signalAll(); |
939 |
|
|
lock.writeLock().unlock(); |
940 |
|
|
t1.join(SHORT_DELAY_MS); |
941 |
|
|
t2.join(SHORT_DELAY_MS); |
942 |
|
|
assertFalse(t1.isAlive()); |
943 |
|
|
assertFalse(t2.isAlive()); |
944 |
dl |
1.2 |
} |
945 |
|
|
|
946 |
dl |
1.4 |
/** |
947 |
|
|
* A serialized lock deserializes as unlocked |
948 |
|
|
*/ |
949 |
jsr166 |
1.32 |
public void testSerialization() throws Exception { |
950 |
dl |
1.2 |
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
951 |
|
|
l.readLock().lock(); |
952 |
|
|
l.readLock().unlock(); |
953 |
|
|
|
954 |
jsr166 |
1.32 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
955 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
956 |
|
|
out.writeObject(l); |
957 |
|
|
out.close(); |
958 |
|
|
|
959 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
960 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
961 |
|
|
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
962 |
|
|
r.readLock().lock(); |
963 |
|
|
r.readLock().unlock(); |
964 |
dl |
1.1 |
} |
965 |
dl |
1.2 |
|
966 |
dl |
1.6 |
/** |
967 |
dl |
1.13 |
* hasQueuedThreads reports whether there are waiting threads |
968 |
|
|
*/ |
969 |
jsr166 |
1.32 |
public void testhasQueuedThreads() throws InterruptedException { |
970 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
971 |
dl |
1.13 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
972 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
973 |
jsr166 |
1.32 |
assertFalse(lock.hasQueuedThreads()); |
974 |
|
|
lock.writeLock().lock(); |
975 |
|
|
t1.start(); |
976 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
977 |
|
|
assertTrue(lock.hasQueuedThreads()); |
978 |
|
|
t2.start(); |
979 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
980 |
|
|
assertTrue(lock.hasQueuedThreads()); |
981 |
|
|
t1.interrupt(); |
982 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
983 |
|
|
assertTrue(lock.hasQueuedThreads()); |
984 |
|
|
lock.writeLock().unlock(); |
985 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
986 |
|
|
assertFalse(lock.hasQueuedThreads()); |
987 |
|
|
t1.join(); |
988 |
|
|
t2.join(); |
989 |
jsr166 |
1.28 |
} |
990 |
dl |
1.13 |
|
991 |
|
|
/** |
992 |
dl |
1.19 |
* hasQueuedThread(null) throws NPE |
993 |
|
|
*/ |
994 |
jsr166 |
1.28 |
public void testHasQueuedThreadNPE() { |
995 |
jsr166 |
1.35 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
996 |
dl |
1.19 |
try { |
997 |
|
|
sync.hasQueuedThread(null); |
998 |
|
|
shouldThrow(); |
999 |
jsr166 |
1.33 |
} catch (NullPointerException success) {} |
1000 |
dl |
1.19 |
} |
1001 |
|
|
|
1002 |
|
|
/** |
1003 |
|
|
* hasQueuedThread reports whether a thread is queued. |
1004 |
|
|
*/ |
1005 |
jsr166 |
1.32 |
public void testHasQueuedThread() throws InterruptedException { |
1006 |
jsr166 |
1.35 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1007 |
dl |
1.19 |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1008 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1009 |
jsr166 |
1.32 |
assertFalse(sync.hasQueuedThread(t1)); |
1010 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
1011 |
|
|
sync.writeLock().lock(); |
1012 |
|
|
t1.start(); |
1013 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1014 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
1015 |
|
|
t2.start(); |
1016 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1017 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
1018 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
1019 |
|
|
t1.interrupt(); |
1020 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1021 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
1022 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
1023 |
|
|
sync.writeLock().unlock(); |
1024 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1025 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
1026 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1027 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
1028 |
|
|
t1.join(); |
1029 |
|
|
t2.join(); |
1030 |
jsr166 |
1.28 |
} |
1031 |
dl |
1.19 |
|
1032 |
|
|
|
1033 |
|
|
/** |
1034 |
dl |
1.6 |
* getQueueLength reports number of waiting threads |
1035 |
|
|
*/ |
1036 |
jsr166 |
1.32 |
public void testGetQueueLength() throws InterruptedException { |
1037 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1038 |
dl |
1.6 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1039 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1040 |
jsr166 |
1.32 |
assertEquals(0, lock.getQueueLength()); |
1041 |
|
|
lock.writeLock().lock(); |
1042 |
|
|
t1.start(); |
1043 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1044 |
|
|
assertEquals(1, lock.getQueueLength()); |
1045 |
|
|
t2.start(); |
1046 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1047 |
|
|
assertEquals(2, lock.getQueueLength()); |
1048 |
|
|
t1.interrupt(); |
1049 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1050 |
|
|
assertEquals(1, lock.getQueueLength()); |
1051 |
|
|
lock.writeLock().unlock(); |
1052 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1053 |
|
|
assertEquals(0, lock.getQueueLength()); |
1054 |
|
|
t1.join(); |
1055 |
|
|
t2.join(); |
1056 |
jsr166 |
1.28 |
} |
1057 |
dl |
1.6 |
|
1058 |
|
|
/** |
1059 |
|
|
* getQueuedThreads includes waiting threads |
1060 |
|
|
*/ |
1061 |
jsr166 |
1.32 |
public void testGetQueuedThreads() throws InterruptedException { |
1062 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1063 |
dl |
1.6 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1064 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1065 |
jsr166 |
1.32 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1066 |
|
|
lock.writeLock().lock(); |
1067 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1068 |
|
|
t1.start(); |
1069 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1070 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
1071 |
|
|
t2.start(); |
1072 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1073 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
1074 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
1075 |
|
|
t1.interrupt(); |
1076 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1077 |
|
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
1078 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
1079 |
|
|
lock.writeLock().unlock(); |
1080 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1081 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1082 |
|
|
t1.join(); |
1083 |
|
|
t2.join(); |
1084 |
jsr166 |
1.28 |
} |
1085 |
dl |
1.6 |
|
1086 |
|
|
/** |
1087 |
dl |
1.14 |
* hasWaiters throws NPE if null |
1088 |
|
|
*/ |
1089 |
|
|
public void testHasWaitersNPE() { |
1090 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1091 |
dl |
1.14 |
try { |
1092 |
|
|
lock.hasWaiters(null); |
1093 |
|
|
shouldThrow(); |
1094 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1095 |
dl |
1.14 |
} |
1096 |
|
|
|
1097 |
|
|
/** |
1098 |
|
|
* getWaitQueueLength throws NPE if null |
1099 |
|
|
*/ |
1100 |
|
|
public void testGetWaitQueueLengthNPE() { |
1101 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1102 |
dl |
1.14 |
try { |
1103 |
|
|
lock.getWaitQueueLength(null); |
1104 |
|
|
shouldThrow(); |
1105 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1106 |
dl |
1.14 |
} |
1107 |
|
|
|
1108 |
|
|
|
1109 |
|
|
/** |
1110 |
|
|
* getWaitingThreads throws NPE if null |
1111 |
|
|
*/ |
1112 |
|
|
public void testGetWaitingThreadsNPE() { |
1113 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1114 |
dl |
1.14 |
try { |
1115 |
|
|
lock.getWaitingThreads(null); |
1116 |
|
|
shouldThrow(); |
1117 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1118 |
dl |
1.14 |
} |
1119 |
|
|
|
1120 |
|
|
/** |
1121 |
dl |
1.13 |
* hasWaiters throws IAE if not owned |
1122 |
|
|
*/ |
1123 |
|
|
public void testHasWaitersIAE() { |
1124 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1125 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1126 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1127 |
dl |
1.13 |
try { |
1128 |
|
|
lock2.hasWaiters(c); |
1129 |
|
|
shouldThrow(); |
1130 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1131 |
dl |
1.13 |
} |
1132 |
|
|
|
1133 |
|
|
/** |
1134 |
|
|
* hasWaiters throws IMSE if not locked |
1135 |
|
|
*/ |
1136 |
|
|
public void testHasWaitersIMSE() { |
1137 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1138 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1139 |
dl |
1.13 |
try { |
1140 |
|
|
lock.hasWaiters(c); |
1141 |
|
|
shouldThrow(); |
1142 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1143 |
dl |
1.13 |
} |
1144 |
|
|
|
1145 |
|
|
|
1146 |
|
|
/** |
1147 |
|
|
* getWaitQueueLength throws IAE if not owned |
1148 |
|
|
*/ |
1149 |
|
|
public void testGetWaitQueueLengthIAE() { |
1150 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1151 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1152 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1153 |
dl |
1.13 |
try { |
1154 |
|
|
lock2.getWaitQueueLength(c); |
1155 |
|
|
shouldThrow(); |
1156 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1157 |
dl |
1.13 |
} |
1158 |
|
|
|
1159 |
|
|
/** |
1160 |
|
|
* getWaitQueueLength throws IMSE if not locked |
1161 |
|
|
*/ |
1162 |
|
|
public void testGetWaitQueueLengthIMSE() { |
1163 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1164 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1165 |
dl |
1.13 |
try { |
1166 |
|
|
lock.getWaitQueueLength(c); |
1167 |
|
|
shouldThrow(); |
1168 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1169 |
dl |
1.13 |
} |
1170 |
|
|
|
1171 |
|
|
|
1172 |
|
|
/** |
1173 |
|
|
* getWaitingThreads throws IAE if not owned |
1174 |
|
|
*/ |
1175 |
|
|
public void testGetWaitingThreadsIAE() { |
1176 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1177 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1178 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1179 |
dl |
1.13 |
try { |
1180 |
|
|
lock2.getWaitingThreads(c); |
1181 |
|
|
shouldThrow(); |
1182 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1183 |
dl |
1.13 |
} |
1184 |
|
|
|
1185 |
|
|
/** |
1186 |
|
|
* getWaitingThreads throws IMSE if not locked |
1187 |
|
|
*/ |
1188 |
|
|
public void testGetWaitingThreadsIMSE() { |
1189 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1190 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1191 |
dl |
1.13 |
try { |
1192 |
|
|
lock.getWaitingThreads(c); |
1193 |
|
|
shouldThrow(); |
1194 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1195 |
dl |
1.13 |
} |
1196 |
|
|
|
1197 |
|
|
|
1198 |
|
|
/** |
1199 |
dl |
1.6 |
* hasWaiters returns true when a thread is waiting, else false |
1200 |
|
|
*/ |
1201 |
jsr166 |
1.32 |
public void testHasWaiters() throws InterruptedException { |
1202 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1203 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1204 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
1205 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1206 |
|
|
lock.writeLock().lock(); |
1207 |
jsr166 |
1.42 |
assertFalse(lock.hasWaiters(c)); |
1208 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1209 |
jsr166 |
1.33 |
c.await(); |
1210 |
|
|
lock.writeLock().unlock(); |
1211 |
|
|
}}); |
1212 |
dl |
1.6 |
|
1213 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
1214 |
|
|
lock.writeLock().lock(); |
1215 |
|
|
assertTrue(lock.hasWaiters(c)); |
1216 |
|
|
assertEquals(1, lock.getWaitQueueLength(c)); |
1217 |
|
|
c.signal(); |
1218 |
|
|
lock.writeLock().unlock(); |
1219 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1220 |
|
|
lock.writeLock().lock(); |
1221 |
|
|
assertFalse(lock.hasWaiters(c)); |
1222 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1223 |
|
|
lock.writeLock().unlock(); |
1224 |
|
|
t.join(SHORT_DELAY_MS); |
1225 |
|
|
assertFalse(t.isAlive()); |
1226 |
dl |
1.6 |
} |
1227 |
|
|
|
1228 |
|
|
/** |
1229 |
|
|
* getWaitQueueLength returns number of waiting threads |
1230 |
|
|
*/ |
1231 |
jsr166 |
1.32 |
public void testGetWaitQueueLength() throws InterruptedException { |
1232 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1233 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1234 |
jsr166 |
1.45 |
Thread t = newStartedThread(new CheckedRunnable() { |
1235 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1236 |
|
|
lock.writeLock().lock(); |
1237 |
jsr166 |
1.42 |
assertFalse(lock.hasWaiters(c)); |
1238 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1239 |
jsr166 |
1.33 |
c.await(); |
1240 |
|
|
lock.writeLock().unlock(); |
1241 |
|
|
}}); |
1242 |
dl |
1.13 |
|
1243 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
1244 |
|
|
lock.writeLock().lock(); |
1245 |
|
|
assertTrue(lock.hasWaiters(c)); |
1246 |
|
|
assertEquals(1, lock.getWaitQueueLength(c)); |
1247 |
|
|
c.signal(); |
1248 |
|
|
lock.writeLock().unlock(); |
1249 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1250 |
|
|
lock.writeLock().lock(); |
1251 |
|
|
assertFalse(lock.hasWaiters(c)); |
1252 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1253 |
|
|
lock.writeLock().unlock(); |
1254 |
|
|
t.join(SHORT_DELAY_MS); |
1255 |
|
|
assertFalse(t.isAlive()); |
1256 |
dl |
1.13 |
} |
1257 |
|
|
|
1258 |
|
|
|
1259 |
|
|
/** |
1260 |
|
|
* getWaitingThreads returns only and all waiting threads |
1261 |
|
|
*/ |
1262 |
jsr166 |
1.32 |
public void testGetWaitingThreads() throws InterruptedException { |
1263 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1264 |
dl |
1.13 |
final Condition c = lock.writeLock().newCondition(); |
1265 |
jsr166 |
1.35 |
Thread t1 = new Thread(new CheckedRunnable() { |
1266 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1267 |
|
|
lock.writeLock().lock(); |
1268 |
jsr166 |
1.42 |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1269 |
jsr166 |
1.33 |
c.await(); |
1270 |
|
|
lock.writeLock().unlock(); |
1271 |
|
|
}}); |
1272 |
|
|
|
1273 |
jsr166 |
1.35 |
Thread t2 = new Thread(new CheckedRunnable() { |
1274 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1275 |
|
|
lock.writeLock().lock(); |
1276 |
jsr166 |
1.42 |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
1277 |
jsr166 |
1.33 |
c.await(); |
1278 |
|
|
lock.writeLock().unlock(); |
1279 |
|
|
}}); |
1280 |
dl |
1.6 |
|
1281 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
1282 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1283 |
|
|
lock.writeLock().unlock(); |
1284 |
|
|
t1.start(); |
1285 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1286 |
|
|
t2.start(); |
1287 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1288 |
|
|
lock.writeLock().lock(); |
1289 |
|
|
assertTrue(lock.hasWaiters(c)); |
1290 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1291 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1292 |
|
|
c.signalAll(); |
1293 |
|
|
lock.writeLock().unlock(); |
1294 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1295 |
|
|
lock.writeLock().lock(); |
1296 |
|
|
assertFalse(lock.hasWaiters(c)); |
1297 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1298 |
|
|
lock.writeLock().unlock(); |
1299 |
|
|
t1.join(SHORT_DELAY_MS); |
1300 |
|
|
t2.join(SHORT_DELAY_MS); |
1301 |
|
|
assertFalse(t1.isAlive()); |
1302 |
|
|
assertFalse(t2.isAlive()); |
1303 |
dl |
1.6 |
} |
1304 |
dl |
1.13 |
|
1305 |
dl |
1.18 |
/** |
1306 |
|
|
* toString indicates current lock state |
1307 |
|
|
*/ |
1308 |
|
|
public void testToString() { |
1309 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1310 |
|
|
String us = lock.toString(); |
1311 |
|
|
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1312 |
|
|
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1313 |
|
|
lock.writeLock().lock(); |
1314 |
|
|
String ws = lock.toString(); |
1315 |
|
|
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1316 |
|
|
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1317 |
|
|
lock.writeLock().unlock(); |
1318 |
|
|
lock.readLock().lock(); |
1319 |
|
|
lock.readLock().lock(); |
1320 |
|
|
String rs = lock.toString(); |
1321 |
|
|
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1322 |
|
|
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1323 |
|
|
} |
1324 |
|
|
|
1325 |
|
|
/** |
1326 |
|
|
* readLock.toString indicates current lock state |
1327 |
|
|
*/ |
1328 |
|
|
public void testReadLockToString() { |
1329 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1330 |
|
|
String us = lock.readLock().toString(); |
1331 |
|
|
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1332 |
|
|
lock.readLock().lock(); |
1333 |
|
|
lock.readLock().lock(); |
1334 |
|
|
String rs = lock.readLock().toString(); |
1335 |
|
|
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1336 |
|
|
} |
1337 |
|
|
|
1338 |
|
|
/** |
1339 |
|
|
* writeLock.toString indicates current lock state |
1340 |
|
|
*/ |
1341 |
|
|
public void testWriteLockToString() { |
1342 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1343 |
|
|
String us = lock.writeLock().toString(); |
1344 |
|
|
assertTrue(us.indexOf("Unlocked") >= 0); |
1345 |
|
|
lock.writeLock().lock(); |
1346 |
|
|
String ls = lock.writeLock().toString(); |
1347 |
|
|
assertTrue(ls.indexOf("Locked") >= 0); |
1348 |
|
|
} |
1349 |
|
|
|
1350 |
dl |
1.1 |
} |