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