1 |
|
/* |
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/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
|
12 |
< |
import junit.framework.*; |
13 |
< |
import java.util.*; |
14 |
< |
import java.util.concurrent.*; |
15 |
< |
import java.util.concurrent.locks.*; |
16 |
< |
import java.io.*; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.HashSet; |
15 |
> |
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
16 |
> |
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
17 |
> |
|
18 |
> |
import junit.framework.AssertionFailedError; |
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run (suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* A simple mutex class, adapted from the |
32 |
< |
* AbstractQueuedLongSynchronizer javadoc. Exclusive acquire tests |
27 |
< |
* exercise this as a sample user extension. Other |
28 |
< |
* methods/features of AbstractQueuedLongSynchronizerTest are tested |
29 |
< |
* via other test classes, including those for ReentrantLock, |
30 |
< |
* ReentrantReadWriteLock, and Semaphore |
31 |
> |
* A simple mutex class, adapted from the class javadoc. Exclusive |
32 |
> |
* acquire tests exercise this as a sample user extension. |
33 |
|
*/ |
34 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
35 |
< |
// Use value > 32 bits for locked state |
36 |
< |
static final long LOCKED = 1 << 48; |
37 |
< |
public boolean isHeldExclusively() { |
38 |
< |
return getState() == LOCKED; |
35 |
> |
/** An eccentric value > 32 bits for locked synchronizer state. */ |
36 |
> |
static final long LOCKED = (1L << 63) | (1L << 15); |
37 |
> |
|
38 |
> |
static final long UNLOCKED = 0; |
39 |
> |
|
40 |
> |
public boolean isHeldExclusively() { |
41 |
> |
long state = getState(); |
42 |
> |
assertTrue(state == UNLOCKED || state == LOCKED); |
43 |
> |
return state == LOCKED; |
44 |
|
} |
45 |
< |
|
45 |
> |
|
46 |
|
public boolean tryAcquire(long acquires) { |
47 |
< |
return compareAndSetState(0, LOCKED); |
47 |
> |
assertEquals(LOCKED, acquires); |
48 |
> |
return compareAndSetState(UNLOCKED, LOCKED); |
49 |
|
} |
50 |
< |
|
50 |
> |
|
51 |
|
public boolean tryRelease(long releases) { |
52 |
< |
if (getState() == 0) throw new IllegalMonitorStateException(); |
53 |
< |
setState(0); |
52 |
> |
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
53 |
> |
setState(UNLOCKED); |
54 |
|
return true; |
55 |
|
} |
48 |
– |
|
49 |
– |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
56 |
|
|
57 |
+ |
public boolean tryAcquireNanos(long nanos) throws InterruptedException { |
58 |
+ |
return tryAcquireNanos(LOCKED, nanos); |
59 |
+ |
} |
60 |
+ |
|
61 |
+ |
public boolean tryAcquire() { |
62 |
+ |
return tryAcquire(LOCKED); |
63 |
+ |
} |
64 |
+ |
|
65 |
+ |
public boolean tryRelease() { |
66 |
+ |
return tryRelease(LOCKED); |
67 |
+ |
} |
68 |
+ |
|
69 |
+ |
public void acquire() { |
70 |
+ |
acquire(LOCKED); |
71 |
+ |
} |
72 |
+ |
|
73 |
+ |
public void acquireInterruptibly() throws InterruptedException { |
74 |
+ |
acquireInterruptibly(LOCKED); |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
public void release() { |
78 |
+ |
release(LOCKED); |
79 |
+ |
} |
80 |
+ |
|
81 |
+ |
public ConditionObject newCondition() { |
82 |
+ |
return new ConditionObject(); |
83 |
+ |
} |
84 |
|
} |
85 |
|
|
53 |
– |
|
86 |
|
/** |
87 |
|
* A simple latch class, to test shared mode. |
88 |
|
*/ |
89 |
< |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
89 |
> |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
90 |
|
public boolean isSignalled() { return getState() != 0; } |
91 |
|
|
92 |
|
public long tryAcquireShared(long ignore) { |
93 |
< |
return isSignalled()? 1 : -1; |
93 |
> |
return isSignalled() ? 1 : -1; |
94 |
|
} |
95 |
< |
|
95 |
> |
|
96 |
|
public boolean tryReleaseShared(long ignore) { |
97 |
< |
setState(1 << 62); |
97 |
> |
setState(1L << 62); |
98 |
|
return true; |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* A runnable calling acquireInterruptibly |
103 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
104 |
> |
* be interrupted. |
105 |
|
*/ |
106 |
< |
class InterruptibleSyncRunnable implements Runnable { |
106 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
107 |
|
final Mutex sync; |
108 |
< |
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
109 |
< |
public void run() { |
110 |
< |
try { |
78 |
< |
sync.acquireInterruptibly(1); |
79 |
< |
} catch(InterruptedException success){} |
108 |
> |
InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; } |
109 |
> |
public void realRun() throws InterruptedException { |
110 |
> |
sync.acquireInterruptibly(); |
111 |
|
} |
112 |
|
} |
113 |
|
|
83 |
– |
|
114 |
|
/** |
115 |
|
* A runnable calling acquireInterruptibly that expects to be |
116 |
< |
* interrupted |
116 |
> |
* interrupted. |
117 |
|
*/ |
118 |
< |
class InterruptedSyncRunnable implements Runnable { |
118 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
119 |
|
final Mutex sync; |
120 |
< |
InterruptedSyncRunnable(Mutex l) { sync = l; } |
121 |
< |
public void run() { |
122 |
< |
try { |
123 |
< |
sync.acquireInterruptibly(1); |
124 |
< |
threadShouldThrow(); |
125 |
< |
} catch(InterruptedException success){} |
120 |
> |
InterruptedSyncRunnable(Mutex sync) { this.sync = sync; } |
121 |
> |
public void realRun() throws InterruptedException { |
122 |
> |
sync.acquireInterruptibly(); |
123 |
> |
} |
124 |
> |
} |
125 |
> |
|
126 |
> |
/** A constant to clarify calls to checking methods below. */ |
127 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
128 |
> |
|
129 |
> |
/** |
130 |
> |
* Spin-waits until sync.isQueued(t) becomes true. |
131 |
> |
*/ |
132 |
> |
void waitForQueuedThread(AbstractQueuedLongSynchronizer sync, |
133 |
> |
Thread t) { |
134 |
> |
long startTime = System.nanoTime(); |
135 |
> |
while (!sync.isQueued(t)) { |
136 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
137 |
> |
throw new AssertionFailedError("timed out"); |
138 |
> |
Thread.yield(); |
139 |
> |
} |
140 |
> |
assertTrue(t.isAlive()); |
141 |
> |
} |
142 |
> |
|
143 |
> |
/** |
144 |
> |
* Checks that sync has exactly the given queued threads. |
145 |
> |
*/ |
146 |
> |
void assertHasQueuedThreads(AbstractQueuedLongSynchronizer sync, |
147 |
> |
Thread... expected) { |
148 |
> |
Collection<Thread> actual = sync.getQueuedThreads(); |
149 |
> |
assertEquals(expected.length > 0, sync.hasQueuedThreads()); |
150 |
> |
assertEquals(expected.length, sync.getQueueLength()); |
151 |
> |
assertEquals(expected.length, actual.size()); |
152 |
> |
assertEquals(expected.length == 0, actual.isEmpty()); |
153 |
> |
assertEquals(new HashSet<Thread>(actual), |
154 |
> |
new HashSet<Thread>(Arrays.asList(expected))); |
155 |
> |
} |
156 |
> |
|
157 |
> |
/** |
158 |
> |
* Checks that sync has exactly the given (exclusive) queued threads. |
159 |
> |
*/ |
160 |
> |
void assertHasExclusiveQueuedThreads(AbstractQueuedLongSynchronizer sync, |
161 |
> |
Thread... expected) { |
162 |
> |
assertHasQueuedThreads(sync, expected); |
163 |
> |
assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()), |
164 |
> |
new HashSet<Thread>(sync.getQueuedThreads())); |
165 |
> |
assertEquals(0, sync.getSharedQueuedThreads().size()); |
166 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
167 |
> |
} |
168 |
> |
|
169 |
> |
/** |
170 |
> |
* Checks that sync has exactly the given (shared) queued threads. |
171 |
> |
*/ |
172 |
> |
void assertHasSharedQueuedThreads(AbstractQueuedLongSynchronizer sync, |
173 |
> |
Thread... expected) { |
174 |
> |
assertHasQueuedThreads(sync, expected); |
175 |
> |
assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()), |
176 |
> |
new HashSet<Thread>(sync.getQueuedThreads())); |
177 |
> |
assertEquals(0, sync.getExclusiveQueuedThreads().size()); |
178 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
179 |
> |
} |
180 |
> |
|
181 |
> |
/** |
182 |
> |
* Checks that condition c has exactly the given waiter threads, |
183 |
> |
* after acquiring mutex. |
184 |
> |
*/ |
185 |
> |
void assertHasWaitersUnlocked(Mutex sync, ConditionObject c, |
186 |
> |
Thread... threads) { |
187 |
> |
sync.acquire(); |
188 |
> |
assertHasWaitersLocked(sync, c, threads); |
189 |
> |
sync.release(); |
190 |
> |
} |
191 |
> |
|
192 |
> |
/** |
193 |
> |
* Checks that condition c has exactly the given waiter threads. |
194 |
> |
*/ |
195 |
> |
void assertHasWaitersLocked(Mutex sync, ConditionObject c, |
196 |
> |
Thread... threads) { |
197 |
> |
assertEquals(threads.length > 0, sync.hasWaiters(c)); |
198 |
> |
assertEquals(threads.length, sync.getWaitQueueLength(c)); |
199 |
> |
assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty()); |
200 |
> |
assertEquals(threads.length, sync.getWaitingThreads(c).size()); |
201 |
> |
assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)), |
202 |
> |
new HashSet<Thread>(Arrays.asList(threads))); |
203 |
> |
} |
204 |
> |
|
205 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
206 |
> |
|
207 |
> |
/** |
208 |
> |
* Awaits condition using the specified AwaitMethod. |
209 |
> |
*/ |
210 |
> |
void await(ConditionObject c, AwaitMethod awaitMethod) |
211 |
> |
throws InterruptedException { |
212 |
> |
long timeoutMillis = 2 * LONG_DELAY_MS; |
213 |
> |
switch (awaitMethod) { |
214 |
> |
case await: |
215 |
> |
c.await(); |
216 |
> |
break; |
217 |
> |
case awaitTimed: |
218 |
> |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
219 |
> |
break; |
220 |
> |
case awaitNanos: |
221 |
> |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
222 |
> |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
223 |
> |
assertTrue(nanosRemaining > 0); |
224 |
> |
break; |
225 |
> |
case awaitUntil: |
226 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
227 |
> |
break; |
228 |
> |
default: |
229 |
> |
throw new AssertionError(); |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
+ |
* Checks that awaiting the given condition times out (using the |
235 |
+ |
* default timeout duration). |
236 |
+ |
*/ |
237 |
+ |
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) { |
238 |
+ |
long timeoutMillis = timeoutMillis(); |
239 |
+ |
long startTime = System.nanoTime(); |
240 |
+ |
try { |
241 |
+ |
switch (awaitMethod) { |
242 |
+ |
case awaitTimed: |
243 |
+ |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
244 |
+ |
break; |
245 |
+ |
case awaitNanos: |
246 |
+ |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
247 |
+ |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
248 |
+ |
assertTrue(nanosRemaining <= 0); |
249 |
+ |
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
250 |
+ |
break; |
251 |
+ |
case awaitUntil: |
252 |
+ |
assertFalse(c.awaitUntil(delayedDate(timeoutMillis))); |
253 |
+ |
break; |
254 |
+ |
default: |
255 |
+ |
throw new UnsupportedOperationException(); |
256 |
+ |
} |
257 |
+ |
} catch (InterruptedException ie) { threadUnexpectedException(ie); } |
258 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
259 |
+ |
} |
260 |
+ |
|
261 |
+ |
/** |
262 |
|
* isHeldExclusively is false upon construction |
263 |
|
*/ |
264 |
< |
public void testIsHeldExclusively() { |
265 |
< |
Mutex rl = new Mutex(); |
266 |
< |
assertFalse(rl.isHeldExclusively()); |
264 |
> |
public void testIsHeldExclusively() { |
265 |
> |
Mutex sync = new Mutex(); |
266 |
> |
assertFalse(sync.isHeldExclusively()); |
267 |
|
} |
268 |
< |
|
268 |
> |
|
269 |
|
/** |
270 |
|
* acquiring released sync succeeds |
271 |
|
*/ |
272 |
< |
public void testAcquire() { |
273 |
< |
Mutex rl = new Mutex(); |
274 |
< |
rl.acquire(1); |
275 |
< |
assertTrue(rl.isHeldExclusively()); |
276 |
< |
rl.release(1); |
277 |
< |
assertFalse(rl.isHeldExclusively()); |
272 |
> |
public void testAcquire() { |
273 |
> |
Mutex sync = new Mutex(); |
274 |
> |
sync.acquire(); |
275 |
> |
assertTrue(sync.isHeldExclusively()); |
276 |
> |
sync.release(); |
277 |
> |
assertFalse(sync.isHeldExclusively()); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* tryAcquire on an released sync succeeds |
282 |
< |
*/ |
283 |
< |
public void testTryAcquire() { |
284 |
< |
Mutex rl = new Mutex(); |
285 |
< |
assertTrue(rl.tryAcquire(1)); |
286 |
< |
assertTrue(rl.isHeldExclusively()); |
287 |
< |
rl.release(1); |
281 |
> |
* tryAcquire on a released sync succeeds |
282 |
> |
*/ |
283 |
> |
public void testTryAcquire() { |
284 |
> |
Mutex sync = new Mutex(); |
285 |
> |
assertTrue(sync.tryAcquire()); |
286 |
> |
assertTrue(sync.isHeldExclusively()); |
287 |
> |
sync.release(); |
288 |
> |
assertFalse(sync.isHeldExclusively()); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
292 |
|
* hasQueuedThreads reports whether there are waiting threads |
293 |
|
*/ |
294 |
< |
public void testhasQueuedThreads() { |
295 |
< |
final Mutex sync = new Mutex(); |
296 |
< |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
297 |
< |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
298 |
< |
try { |
299 |
< |
assertFalse(sync.hasQueuedThreads()); |
300 |
< |
sync.acquire(1); |
301 |
< |
t1.start(); |
302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
303 |
< |
assertTrue(sync.hasQueuedThreads()); |
304 |
< |
t2.start(); |
305 |
< |
Thread.sleep(SHORT_DELAY_MS); |
306 |
< |
assertTrue(sync.hasQueuedThreads()); |
307 |
< |
t1.interrupt(); |
308 |
< |
Thread.sleep(SHORT_DELAY_MS); |
309 |
< |
assertTrue(sync.hasQueuedThreads()); |
310 |
< |
sync.release(1); |
148 |
< |
Thread.sleep(SHORT_DELAY_MS); |
149 |
< |
assertFalse(sync.hasQueuedThreads()); |
150 |
< |
t1.join(); |
151 |
< |
t2.join(); |
152 |
< |
} catch(Exception e){ |
153 |
< |
unexpectedException(); |
154 |
< |
} |
155 |
< |
} |
294 |
> |
public void testHasQueuedThreads() { |
295 |
> |
final Mutex sync = new Mutex(); |
296 |
> |
assertFalse(sync.hasQueuedThreads()); |
297 |
> |
sync.acquire(); |
298 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
299 |
> |
waitForQueuedThread(sync, t1); |
300 |
> |
assertTrue(sync.hasQueuedThreads()); |
301 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
302 |
> |
waitForQueuedThread(sync, t2); |
303 |
> |
assertTrue(sync.hasQueuedThreads()); |
304 |
> |
t1.interrupt(); |
305 |
> |
awaitTermination(t1); |
306 |
> |
assertTrue(sync.hasQueuedThreads()); |
307 |
> |
sync.release(); |
308 |
> |
awaitTermination(t2); |
309 |
> |
assertFalse(sync.hasQueuedThreads()); |
310 |
> |
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* isQueued(null) throws NPE |
313 |
> |
* isQueued(null) throws NullPointerException |
314 |
|
*/ |
315 |
< |
public void testIsQueuedNPE() { |
316 |
< |
final Mutex sync = new Mutex(); |
315 |
> |
public void testIsQueuedNPE() { |
316 |
> |
final Mutex sync = new Mutex(); |
317 |
|
try { |
318 |
|
sync.isQueued(null); |
319 |
|
shouldThrow(); |
320 |
< |
} catch (NullPointerException success) { |
166 |
< |
} |
320 |
> |
} catch (NullPointerException success) {} |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* isQueued reports whether a thread is queued. |
324 |
> |
* isQueued reports whether a thread is queued |
325 |
|
*/ |
326 |
< |
public void testIsQueued() { |
327 |
< |
final Mutex sync = new Mutex(); |
326 |
> |
public void testIsQueued() { |
327 |
> |
final Mutex sync = new Mutex(); |
328 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
329 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
330 |
< |
try { |
331 |
< |
assertFalse(sync.isQueued(t1)); |
332 |
< |
assertFalse(sync.isQueued(t2)); |
333 |
< |
sync.acquire(1); |
334 |
< |
t1.start(); |
335 |
< |
Thread.sleep(SHORT_DELAY_MS); |
336 |
< |
assertTrue(sync.isQueued(t1)); |
337 |
< |
t2.start(); |
338 |
< |
Thread.sleep(SHORT_DELAY_MS); |
339 |
< |
assertTrue(sync.isQueued(t1)); |
340 |
< |
assertTrue(sync.isQueued(t2)); |
341 |
< |
t1.interrupt(); |
342 |
< |
Thread.sleep(SHORT_DELAY_MS); |
343 |
< |
assertFalse(sync.isQueued(t1)); |
344 |
< |
assertTrue(sync.isQueued(t2)); |
345 |
< |
sync.release(1); |
346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
347 |
< |
assertFalse(sync.isQueued(t1)); |
348 |
< |
Thread.sleep(SHORT_DELAY_MS); |
349 |
< |
assertFalse(sync.isQueued(t2)); |
196 |
< |
t1.join(); |
197 |
< |
t2.join(); |
198 |
< |
} catch(Exception e){ |
199 |
< |
unexpectedException(); |
200 |
< |
} |
201 |
< |
} |
330 |
> |
assertFalse(sync.isQueued(t1)); |
331 |
> |
assertFalse(sync.isQueued(t2)); |
332 |
> |
sync.acquire(); |
333 |
> |
t1.start(); |
334 |
> |
waitForQueuedThread(sync, t1); |
335 |
> |
assertTrue(sync.isQueued(t1)); |
336 |
> |
assertFalse(sync.isQueued(t2)); |
337 |
> |
t2.start(); |
338 |
> |
waitForQueuedThread(sync, t2); |
339 |
> |
assertTrue(sync.isQueued(t1)); |
340 |
> |
assertTrue(sync.isQueued(t2)); |
341 |
> |
t1.interrupt(); |
342 |
> |
awaitTermination(t1); |
343 |
> |
assertFalse(sync.isQueued(t1)); |
344 |
> |
assertTrue(sync.isQueued(t2)); |
345 |
> |
sync.release(); |
346 |
> |
awaitTermination(t2); |
347 |
> |
assertFalse(sync.isQueued(t1)); |
348 |
> |
assertFalse(sync.isQueued(t2)); |
349 |
> |
} |
350 |
|
|
351 |
|
/** |
352 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
353 |
|
*/ |
354 |
< |
public void testGetFirstQueuedThread() { |
355 |
< |
final Mutex sync = new Mutex(); |
356 |
< |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
357 |
< |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
358 |
< |
try { |
359 |
< |
assertNull(sync.getFirstQueuedThread()); |
360 |
< |
sync.acquire(1); |
361 |
< |
t1.start(); |
362 |
< |
Thread.sleep(SHORT_DELAY_MS); |
363 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
364 |
< |
t2.start(); |
365 |
< |
Thread.sleep(SHORT_DELAY_MS); |
366 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
367 |
< |
t1.interrupt(); |
368 |
< |
Thread.sleep(SHORT_DELAY_MS); |
369 |
< |
Thread.sleep(SHORT_DELAY_MS); |
370 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
223 |
< |
sync.release(1); |
224 |
< |
Thread.sleep(SHORT_DELAY_MS); |
225 |
< |
assertNull(sync.getFirstQueuedThread()); |
226 |
< |
t1.join(); |
227 |
< |
t2.join(); |
228 |
< |
} catch(Exception e){ |
229 |
< |
unexpectedException(); |
230 |
< |
} |
231 |
< |
} |
232 |
< |
|
354 |
> |
public void testGetFirstQueuedThread() { |
355 |
> |
final Mutex sync = new Mutex(); |
356 |
> |
assertNull(sync.getFirstQueuedThread()); |
357 |
> |
sync.acquire(); |
358 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
359 |
> |
waitForQueuedThread(sync, t1); |
360 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
361 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
362 |
> |
waitForQueuedThread(sync, t2); |
363 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
364 |
> |
t1.interrupt(); |
365 |
> |
awaitTermination(t1); |
366 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
367 |
> |
sync.release(); |
368 |
> |
awaitTermination(t2); |
369 |
> |
assertNull(sync.getFirstQueuedThread()); |
370 |
> |
} |
371 |
|
|
372 |
|
/** |
373 |
|
* hasContended reports false if no thread has ever blocked, else true |
374 |
|
*/ |
375 |
< |
public void testHasContended() { |
376 |
< |
final Mutex sync = new Mutex(); |
377 |
< |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
378 |
< |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
379 |
< |
try { |
380 |
< |
assertFalse(sync.hasContended()); |
381 |
< |
sync.acquire(1); |
382 |
< |
t1.start(); |
383 |
< |
Thread.sleep(SHORT_DELAY_MS); |
384 |
< |
assertTrue(sync.hasContended()); |
385 |
< |
t2.start(); |
386 |
< |
Thread.sleep(SHORT_DELAY_MS); |
387 |
< |
assertTrue(sync.hasContended()); |
388 |
< |
t1.interrupt(); |
389 |
< |
Thread.sleep(SHORT_DELAY_MS); |
390 |
< |
assertTrue(sync.hasContended()); |
391 |
< |
sync.release(1); |
392 |
< |
Thread.sleep(SHORT_DELAY_MS); |
255 |
< |
assertTrue(sync.hasContended()); |
256 |
< |
t1.join(); |
257 |
< |
t2.join(); |
258 |
< |
} catch(Exception e){ |
259 |
< |
unexpectedException(); |
260 |
< |
} |
261 |
< |
} |
375 |
> |
public void testHasContended() { |
376 |
> |
final Mutex sync = new Mutex(); |
377 |
> |
assertFalse(sync.hasContended()); |
378 |
> |
sync.acquire(); |
379 |
> |
assertFalse(sync.hasContended()); |
380 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
381 |
> |
waitForQueuedThread(sync, t1); |
382 |
> |
assertTrue(sync.hasContended()); |
383 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
384 |
> |
waitForQueuedThread(sync, t2); |
385 |
> |
assertTrue(sync.hasContended()); |
386 |
> |
t1.interrupt(); |
387 |
> |
awaitTermination(t1); |
388 |
> |
assertTrue(sync.hasContended()); |
389 |
> |
sync.release(); |
390 |
> |
awaitTermination(t2); |
391 |
> |
assertTrue(sync.hasContended()); |
392 |
> |
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* getQueuedThreads includes waiting threads |
395 |
> |
* getQueuedThreads returns all waiting threads |
396 |
|
*/ |
397 |
< |
public void testGetQueuedThreads() { |
398 |
< |
final Mutex sync = new Mutex(); |
397 |
> |
public void testGetQueuedThreads() { |
398 |
> |
final Mutex sync = new Mutex(); |
399 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
400 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
401 |
< |
try { |
402 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
403 |
< |
sync.acquire(1); |
404 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
405 |
< |
t1.start(); |
406 |
< |
Thread.sleep(SHORT_DELAY_MS); |
407 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
408 |
< |
t2.start(); |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
410 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
411 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
412 |
< |
t1.interrupt(); |
413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
414 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
415 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
416 |
< |
sync.release(1); |
417 |
< |
Thread.sleep(SHORT_DELAY_MS); |
418 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
419 |
< |
t1.join(); |
420 |
< |
t2.join(); |
290 |
< |
} catch(Exception e){ |
291 |
< |
unexpectedException(); |
292 |
< |
} |
293 |
< |
} |
401 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
402 |
> |
sync.acquire(); |
403 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
404 |
> |
t1.start(); |
405 |
> |
waitForQueuedThread(sync, t1); |
406 |
> |
assertHasExclusiveQueuedThreads(sync, t1); |
407 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
408 |
> |
assertFalse(sync.getQueuedThreads().contains(t2)); |
409 |
> |
t2.start(); |
410 |
> |
waitForQueuedThread(sync, t2); |
411 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
412 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
413 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
414 |
> |
t1.interrupt(); |
415 |
> |
awaitTermination(t1); |
416 |
> |
assertHasExclusiveQueuedThreads(sync, t2); |
417 |
> |
sync.release(); |
418 |
> |
awaitTermination(t2); |
419 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
420 |
> |
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* getExclusiveQueuedThreads includes waiting threads |
423 |
> |
* getExclusiveQueuedThreads returns all exclusive waiting threads |
424 |
|
*/ |
425 |
< |
public void testGetExclusiveQueuedThreads() { |
426 |
< |
final Mutex sync = new Mutex(); |
425 |
> |
public void testGetExclusiveQueuedThreads() { |
426 |
> |
final Mutex sync = new Mutex(); |
427 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
428 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
429 |
< |
try { |
430 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
431 |
< |
sync.acquire(1); |
432 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
433 |
< |
t1.start(); |
434 |
< |
Thread.sleep(SHORT_DELAY_MS); |
435 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
436 |
< |
t2.start(); |
437 |
< |
Thread.sleep(SHORT_DELAY_MS); |
438 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
439 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
440 |
< |
t1.interrupt(); |
441 |
< |
Thread.sleep(SHORT_DELAY_MS); |
442 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
443 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
444 |
< |
sync.release(1); |
445 |
< |
Thread.sleep(SHORT_DELAY_MS); |
446 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
447 |
< |
t1.join(); |
448 |
< |
t2.join(); |
322 |
< |
} catch(Exception e){ |
323 |
< |
unexpectedException(); |
324 |
< |
} |
325 |
< |
} |
429 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
430 |
> |
sync.acquire(); |
431 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
432 |
> |
t1.start(); |
433 |
> |
waitForQueuedThread(sync, t1); |
434 |
> |
assertHasExclusiveQueuedThreads(sync, t1); |
435 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
436 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t2)); |
437 |
> |
t2.start(); |
438 |
> |
waitForQueuedThread(sync, t2); |
439 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
440 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
441 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
442 |
> |
t1.interrupt(); |
443 |
> |
awaitTermination(t1); |
444 |
> |
assertHasExclusiveQueuedThreads(sync, t2); |
445 |
> |
sync.release(); |
446 |
> |
awaitTermination(t2); |
447 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
448 |
> |
} |
449 |
|
|
450 |
|
/** |
451 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
452 |
|
*/ |
453 |
< |
public void testGetSharedQueuedThreads() { |
454 |
< |
final Mutex sync = new Mutex(); |
455 |
< |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
456 |
< |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
457 |
< |
try { |
458 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
459 |
< |
sync.acquire(1); |
460 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
461 |
< |
t1.start(); |
462 |
< |
Thread.sleep(SHORT_DELAY_MS); |
463 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
464 |
< |
t2.start(); |
465 |
< |
Thread.sleep(SHORT_DELAY_MS); |
466 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
467 |
< |
t1.interrupt(); |
468 |
< |
Thread.sleep(SHORT_DELAY_MS); |
469 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
470 |
< |
sync.release(1); |
348 |
< |
Thread.sleep(SHORT_DELAY_MS); |
349 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
350 |
< |
t1.join(); |
351 |
< |
t2.join(); |
352 |
< |
} catch(Exception e){ |
353 |
< |
unexpectedException(); |
354 |
< |
} |
355 |
< |
} |
453 |
> |
public void testGetSharedQueuedThreads_Exclusive() { |
454 |
> |
final Mutex sync = new Mutex(); |
455 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
456 |
> |
sync.acquire(); |
457 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
458 |
> |
Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync)); |
459 |
> |
waitForQueuedThread(sync, t1); |
460 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
461 |
> |
Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync)); |
462 |
> |
waitForQueuedThread(sync, t2); |
463 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
464 |
> |
t1.interrupt(); |
465 |
> |
awaitTermination(t1); |
466 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
467 |
> |
sync.release(); |
468 |
> |
awaitTermination(t2); |
469 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
470 |
> |
} |
471 |
|
|
472 |
|
/** |
473 |
< |
* tryAcquireNanos is interruptible. |
473 |
> |
* getSharedQueuedThreads returns all shared waiting threads |
474 |
|
*/ |
475 |
< |
public void testInterruptedException2() { |
476 |
< |
final Mutex sync = new Mutex(); |
477 |
< |
sync.acquire(1); |
478 |
< |
Thread t = new Thread(new Runnable() { |
479 |
< |
public void run() { |
480 |
< |
try { |
481 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
482 |
< |
threadShouldThrow(); |
483 |
< |
} catch(InterruptedException success){} |
484 |
< |
} |
485 |
< |
}); |
486 |
< |
try { |
487 |
< |
t.start(); |
488 |
< |
t.interrupt(); |
489 |
< |
} catch(Exception e){ |
490 |
< |
unexpectedException(); |
491 |
< |
} |
475 |
> |
public void testGetSharedQueuedThreads_Shared() { |
476 |
> |
final BooleanLatch l = new BooleanLatch(); |
477 |
> |
assertHasSharedQueuedThreads(l, NO_THREADS); |
478 |
> |
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
479 |
> |
public void realRun() throws InterruptedException { |
480 |
> |
l.acquireSharedInterruptibly(0); |
481 |
> |
}}); |
482 |
> |
waitForQueuedThread(l, t1); |
483 |
> |
assertHasSharedQueuedThreads(l, t1); |
484 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
485 |
> |
public void realRun() throws InterruptedException { |
486 |
> |
l.acquireSharedInterruptibly(0); |
487 |
> |
}}); |
488 |
> |
waitForQueuedThread(l, t2); |
489 |
> |
assertHasSharedQueuedThreads(l, t1, t2); |
490 |
> |
t1.interrupt(); |
491 |
> |
awaitTermination(t1); |
492 |
> |
assertHasSharedQueuedThreads(l, t2); |
493 |
> |
assertTrue(l.releaseShared(0)); |
494 |
> |
awaitTermination(t2); |
495 |
> |
assertHasSharedQueuedThreads(l, NO_THREADS); |
496 |
|
} |
497 |
|
|
498 |
+ |
/** |
499 |
+ |
* tryAcquireNanos is interruptible |
500 |
+ |
*/ |
501 |
+ |
public void testTryAcquireNanos_Interruptible() { |
502 |
+ |
final Mutex sync = new Mutex(); |
503 |
+ |
sync.acquire(); |
504 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
505 |
+ |
public void realRun() throws InterruptedException { |
506 |
+ |
sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
507 |
+ |
}}); |
508 |
+ |
|
509 |
+ |
waitForQueuedThread(sync, t); |
510 |
+ |
t.interrupt(); |
511 |
+ |
awaitTermination(t); |
512 |
+ |
} |
513 |
|
|
514 |
|
/** |
515 |
< |
* TryAcquire on exclusively held sync fails |
516 |
< |
*/ |
517 |
< |
public void testTryAcquireWhenSynced() { |
518 |
< |
final Mutex sync = new Mutex(); |
519 |
< |
sync.acquire(1); |
520 |
< |
Thread t = new Thread(new Runnable() { |
521 |
< |
public void run() { |
522 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
523 |
< |
} |
524 |
< |
}); |
525 |
< |
try { |
526 |
< |
t.start(); |
527 |
< |
t.join(); |
394 |
< |
sync.release(1); |
395 |
< |
} catch(Exception e){ |
396 |
< |
unexpectedException(); |
397 |
< |
} |
398 |
< |
} |
515 |
> |
* tryAcquire on exclusively held sync fails |
516 |
> |
*/ |
517 |
> |
public void testTryAcquireWhenSynced() { |
518 |
> |
final Mutex sync = new Mutex(); |
519 |
> |
sync.acquire(); |
520 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
521 |
> |
public void realRun() { |
522 |
> |
assertFalse(sync.tryAcquire()); |
523 |
> |
}}); |
524 |
> |
|
525 |
> |
awaitTermination(t); |
526 |
> |
sync.release(); |
527 |
> |
} |
528 |
|
|
529 |
|
/** |
530 |
|
* tryAcquireNanos on an exclusively held sync times out |
531 |
|
*/ |
532 |
< |
public void testAcquireNanos_Timeout() { |
533 |
< |
final Mutex sync = new Mutex(); |
534 |
< |
sync.acquire(1); |
535 |
< |
Thread t = new Thread(new Runnable() { |
536 |
< |
public void run() { |
537 |
< |
try { |
538 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
539 |
< |
} catch (Exception ex) { |
540 |
< |
threadUnexpectedException(); |
541 |
< |
} |
542 |
< |
} |
543 |
< |
}); |
544 |
< |
try { |
545 |
< |
t.start(); |
546 |
< |
t.join(); |
418 |
< |
sync.release(1); |
419 |
< |
} catch(Exception e){ |
420 |
< |
unexpectedException(); |
421 |
< |
} |
422 |
< |
} |
423 |
< |
|
424 |
< |
|
532 |
> |
public void testAcquireNanos_Timeout() { |
533 |
> |
final Mutex sync = new Mutex(); |
534 |
> |
sync.acquire(); |
535 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
536 |
> |
public void realRun() throws InterruptedException { |
537 |
> |
long startTime = System.nanoTime(); |
538 |
> |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
539 |
> |
assertFalse(sync.tryAcquireNanos(nanos)); |
540 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
541 |
> |
}}); |
542 |
> |
|
543 |
> |
awaitTermination(t); |
544 |
> |
sync.release(); |
545 |
> |
} |
546 |
> |
|
547 |
|
/** |
548 |
|
* getState is true when acquired and false when not |
549 |
|
*/ |
550 |
|
public void testGetState() { |
551 |
< |
final Mutex sync = new Mutex(); |
552 |
< |
sync.acquire(1); |
553 |
< |
assertTrue(sync.isHeldExclusively()); |
554 |
< |
sync.release(1); |
555 |
< |
assertFalse(sync.isHeldExclusively()); |
556 |
< |
Thread t = new Thread(new Runnable() { |
557 |
< |
public void run() { |
558 |
< |
sync.acquire(1); |
559 |
< |
try { |
560 |
< |
Thread.sleep(SMALL_DELAY_MS); |
561 |
< |
} |
562 |
< |
catch(Exception e) { |
563 |
< |
threadUnexpectedException(); |
564 |
< |
} |
565 |
< |
sync.release(1); |
566 |
< |
} |
567 |
< |
}); |
568 |
< |
try { |
569 |
< |
t.start(); |
570 |
< |
Thread.sleep(SHORT_DELAY_MS); |
571 |
< |
assertTrue(sync.isHeldExclusively()); |
450 |
< |
t.join(); |
451 |
< |
assertFalse(sync.isHeldExclusively()); |
452 |
< |
} catch(Exception e){ |
453 |
< |
unexpectedException(); |
454 |
< |
} |
551 |
> |
final Mutex sync = new Mutex(); |
552 |
> |
sync.acquire(); |
553 |
> |
assertTrue(sync.isHeldExclusively()); |
554 |
> |
sync.release(); |
555 |
> |
assertFalse(sync.isHeldExclusively()); |
556 |
> |
|
557 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
558 |
> |
final BooleanLatch done = new BooleanLatch(); |
559 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
560 |
> |
public void realRun() throws InterruptedException { |
561 |
> |
sync.acquire(); |
562 |
> |
assertTrue(acquired.releaseShared(0)); |
563 |
> |
done.acquireShared(0); |
564 |
> |
sync.release(); |
565 |
> |
}}); |
566 |
> |
|
567 |
> |
acquired.acquireShared(0); |
568 |
> |
assertTrue(sync.isHeldExclusively()); |
569 |
> |
assertTrue(done.releaseShared(0)); |
570 |
> |
awaitTermination(t); |
571 |
> |
assertFalse(sync.isHeldExclusively()); |
572 |
|
} |
573 |
|
|
457 |
– |
|
458 |
– |
/** |
459 |
– |
* acquireInterruptibly is interruptible. |
460 |
– |
*/ |
461 |
– |
public void testAcquireInterruptibly1() { |
462 |
– |
final Mutex sync = new Mutex(); |
463 |
– |
sync.acquire(1); |
464 |
– |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
465 |
– |
try { |
466 |
– |
t.start(); |
467 |
– |
t.interrupt(); |
468 |
– |
sync.release(1); |
469 |
– |
t.join(); |
470 |
– |
} catch(Exception e){ |
471 |
– |
unexpectedException(); |
472 |
– |
} |
473 |
– |
} |
474 |
– |
|
574 |
|
/** |
575 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
576 |
|
*/ |
577 |
< |
public void testAcquireInterruptibly2() { |
578 |
< |
final Mutex sync = new Mutex(); |
579 |
< |
try { |
580 |
< |
sync.acquireInterruptibly(1); |
581 |
< |
} catch(Exception e) { |
582 |
< |
unexpectedException(); |
583 |
< |
} |
584 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
585 |
< |
try { |
586 |
< |
t.start(); |
587 |
< |
t.interrupt(); |
588 |
< |
assertTrue(sync.isHeldExclusively()); |
589 |
< |
t.join(); |
590 |
< |
} catch(Exception e){ |
591 |
< |
unexpectedException(); |
493 |
< |
} |
577 |
> |
public void testAcquireInterruptibly() throws InterruptedException { |
578 |
> |
final Mutex sync = new Mutex(); |
579 |
> |
final BooleanLatch threadStarted = new BooleanLatch(); |
580 |
> |
sync.acquireInterruptibly(); |
581 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
582 |
> |
public void realRun() throws InterruptedException { |
583 |
> |
assertTrue(threadStarted.releaseShared(0)); |
584 |
> |
sync.acquireInterruptibly(); |
585 |
> |
}}); |
586 |
> |
|
587 |
> |
threadStarted.acquireShared(0); |
588 |
> |
waitForQueuedThread(sync, t); |
589 |
> |
t.interrupt(); |
590 |
> |
awaitTermination(t); |
591 |
> |
assertTrue(sync.isHeldExclusively()); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
|
* owns is true for a condition created by sync else false |
596 |
|
*/ |
597 |
|
public void testOwns() { |
598 |
< |
final Mutex sync = new Mutex(); |
599 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
598 |
> |
final Mutex sync = new Mutex(); |
599 |
> |
final ConditionObject c = sync.newCondition(); |
600 |
|
final Mutex sync2 = new Mutex(); |
601 |
|
assertTrue(sync.owns(c)); |
602 |
|
assertFalse(sync2.owns(c)); |
605 |
|
/** |
606 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
607 |
|
*/ |
608 |
< |
public void testAwait_IllegalMonitor() { |
609 |
< |
final Mutex sync = new Mutex(); |
610 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
611 |
< |
try { |
612 |
< |
c.await(); |
613 |
< |
shouldThrow(); |
614 |
< |
} |
615 |
< |
catch (IllegalMonitorStateException success) { |
616 |
< |
} |
617 |
< |
catch (Exception ex) { |
618 |
< |
unexpectedException(); |
608 |
> |
public void testAwait_IMSE() { |
609 |
> |
final Mutex sync = new Mutex(); |
610 |
> |
final ConditionObject c = sync.newCondition(); |
611 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
612 |
> |
long startTime = System.nanoTime(); |
613 |
> |
try { |
614 |
> |
await(c, awaitMethod); |
615 |
> |
shouldThrow(); |
616 |
> |
} catch (IllegalMonitorStateException success) { |
617 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
618 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
619 |
|
} |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
624 |
|
*/ |
625 |
< |
public void testSignal_IllegalMonitor() { |
626 |
< |
final Mutex sync = new Mutex(); |
627 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
625 |
> |
public void testSignal_IMSE() { |
626 |
> |
final Mutex sync = new Mutex(); |
627 |
> |
final ConditionObject c = sync.newCondition(); |
628 |
|
try { |
629 |
|
c.signal(); |
630 |
|
shouldThrow(); |
631 |
< |
} |
632 |
< |
catch (IllegalMonitorStateException success) { |
535 |
< |
} |
536 |
< |
catch (Exception ex) { |
537 |
< |
unexpectedException(); |
538 |
< |
} |
631 |
> |
} catch (IllegalMonitorStateException success) {} |
632 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* awaitNanos without a signal times out |
636 |
> |
* Calling signalAll without holding sync throws IllegalMonitorStateException |
637 |
|
*/ |
638 |
< |
public void testAwaitNanos_Timeout() { |
639 |
< |
final Mutex sync = new Mutex(); |
640 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
638 |
> |
public void testSignalAll_IMSE() { |
639 |
> |
final Mutex sync = new Mutex(); |
640 |
> |
final ConditionObject c = sync.newCondition(); |
641 |
|
try { |
642 |
< |
sync.acquire(1); |
643 |
< |
long t = c.awaitNanos(100); |
644 |
< |
assertTrue(t <= 0); |
551 |
< |
sync.release(1); |
552 |
< |
} |
553 |
< |
catch (Exception ex) { |
554 |
< |
unexpectedException(); |
555 |
< |
} |
642 |
> |
c.signalAll(); |
643 |
> |
shouldThrow(); |
644 |
> |
} catch (IllegalMonitorStateException success) {} |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* Timed await without a signal times out |
648 |
> |
* await/awaitNanos/awaitUntil without a signal times out |
649 |
|
*/ |
650 |
< |
public void testAwait_Timeout() { |
651 |
< |
final Mutex sync = new Mutex(); |
652 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
653 |
< |
try { |
654 |
< |
sync.acquire(1); |
655 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
656 |
< |
sync.release(1); |
657 |
< |
} |
658 |
< |
catch (Exception ex) { |
570 |
< |
unexpectedException(); |
571 |
< |
} |
650 |
> |
public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); } |
651 |
> |
public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); } |
652 |
> |
public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); } |
653 |
> |
public void testAwait_Timeout(AwaitMethod awaitMethod) { |
654 |
> |
final Mutex sync = new Mutex(); |
655 |
> |
final ConditionObject c = sync.newCondition(); |
656 |
> |
sync.acquire(); |
657 |
> |
assertAwaitTimesOut(c, awaitMethod); |
658 |
> |
sync.release(); |
659 |
|
} |
660 |
|
|
661 |
|
/** |
662 |
< |
* awaitUntil without a signal times out |
663 |
< |
*/ |
664 |
< |
public void testAwaitUntil_Timeout() { |
665 |
< |
final Mutex sync = new Mutex(); |
666 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
667 |
< |
try { |
668 |
< |
sync.acquire(1); |
669 |
< |
java.util.Date d = new java.util.Date(); |
670 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
671 |
< |
sync.release(1); |
672 |
< |
} |
673 |
< |
catch (Exception ex) { |
674 |
< |
unexpectedException(); |
675 |
< |
} |
662 |
> |
* await/awaitNanos/awaitUntil returns when signalled |
663 |
> |
*/ |
664 |
> |
public void testSignal_await() { testSignal(AwaitMethod.await); } |
665 |
> |
public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); } |
666 |
> |
public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); } |
667 |
> |
public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); } |
668 |
> |
public void testSignal(final AwaitMethod awaitMethod) { |
669 |
> |
final Mutex sync = new Mutex(); |
670 |
> |
final ConditionObject c = sync.newCondition(); |
671 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
672 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
673 |
> |
public void realRun() throws InterruptedException { |
674 |
> |
sync.acquire(); |
675 |
> |
assertTrue(acquired.releaseShared(0)); |
676 |
> |
await(c, awaitMethod); |
677 |
> |
sync.release(); |
678 |
> |
}}); |
679 |
> |
|
680 |
> |
acquired.acquireShared(0); |
681 |
> |
sync.acquire(); |
682 |
> |
assertHasWaitersLocked(sync, c, t); |
683 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
684 |
> |
c.signal(); |
685 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
686 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
687 |
> |
sync.release(); |
688 |
> |
awaitTermination(t); |
689 |
|
} |
690 |
|
|
691 |
|
/** |
692 |
< |
* await returns when signalled |
593 |
< |
*/ |
594 |
< |
public void testAwait() { |
595 |
< |
final Mutex sync = new Mutex(); |
596 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
597 |
< |
Thread t = new Thread(new Runnable() { |
598 |
< |
public void run() { |
599 |
< |
try { |
600 |
< |
sync.acquire(1); |
601 |
< |
c.await(); |
602 |
< |
sync.release(1); |
603 |
< |
} |
604 |
< |
catch(InterruptedException e) { |
605 |
< |
threadUnexpectedException(); |
606 |
< |
} |
607 |
< |
} |
608 |
< |
}); |
609 |
< |
|
610 |
< |
try { |
611 |
< |
t.start(); |
612 |
< |
Thread.sleep(SHORT_DELAY_MS); |
613 |
< |
sync.acquire(1); |
614 |
< |
c.signal(); |
615 |
< |
sync.release(1); |
616 |
< |
t.join(SHORT_DELAY_MS); |
617 |
< |
assertFalse(t.isAlive()); |
618 |
< |
} |
619 |
< |
catch (Exception ex) { |
620 |
< |
unexpectedException(); |
621 |
< |
} |
622 |
< |
} |
623 |
< |
|
624 |
< |
|
625 |
< |
|
626 |
< |
/** |
627 |
< |
* hasWaiters throws NPE if null |
692 |
> |
* hasWaiters(null) throws NullPointerException |
693 |
|
*/ |
694 |
|
public void testHasWaitersNPE() { |
695 |
< |
final Mutex sync = new Mutex(); |
695 |
> |
final Mutex sync = new Mutex(); |
696 |
|
try { |
697 |
|
sync.hasWaiters(null); |
698 |
|
shouldThrow(); |
699 |
< |
} catch (NullPointerException success) { |
635 |
< |
} catch (Exception ex) { |
636 |
< |
unexpectedException(); |
637 |
< |
} |
699 |
> |
} catch (NullPointerException success) {} |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
< |
* getWaitQueueLength throws NPE if null |
703 |
> |
* getWaitQueueLength(null) throws NullPointerException |
704 |
|
*/ |
705 |
|
public void testGetWaitQueueLengthNPE() { |
706 |
< |
final Mutex sync = new Mutex(); |
706 |
> |
final Mutex sync = new Mutex(); |
707 |
|
try { |
708 |
|
sync.getWaitQueueLength(null); |
709 |
|
shouldThrow(); |
710 |
< |
} catch (NullPointerException success) { |
649 |
< |
} catch (Exception ex) { |
650 |
< |
unexpectedException(); |
651 |
< |
} |
710 |
> |
} catch (NullPointerException success) {} |
711 |
|
} |
712 |
|
|
654 |
– |
|
713 |
|
/** |
714 |
|
* getWaitingThreads throws NPE if null |
715 |
|
*/ |
716 |
|
public void testGetWaitingThreadsNPE() { |
717 |
< |
final Mutex sync = new Mutex(); |
717 |
> |
final Mutex sync = new Mutex(); |
718 |
|
try { |
719 |
|
sync.getWaitingThreads(null); |
720 |
|
shouldThrow(); |
721 |
< |
} catch (NullPointerException success) { |
664 |
< |
} catch (Exception ex) { |
665 |
< |
unexpectedException(); |
666 |
< |
} |
721 |
> |
} catch (NullPointerException success) {} |
722 |
|
} |
723 |
|
|
669 |
– |
|
724 |
|
/** |
725 |
< |
* hasWaiters throws IAE if not owned |
725 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
726 |
|
*/ |
727 |
|
public void testHasWaitersIAE() { |
728 |
< |
final Mutex sync = new Mutex(); |
729 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
730 |
< |
final Mutex sync2 = new Mutex(); |
728 |
> |
final Mutex sync = new Mutex(); |
729 |
> |
final ConditionObject c = sync.newCondition(); |
730 |
> |
final Mutex sync2 = new Mutex(); |
731 |
|
try { |
732 |
|
sync2.hasWaiters(c); |
733 |
|
shouldThrow(); |
734 |
< |
} catch (IllegalArgumentException success) { |
735 |
< |
} catch (Exception ex) { |
682 |
< |
unexpectedException(); |
683 |
< |
} |
734 |
> |
} catch (IllegalArgumentException success) {} |
735 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
736 |
|
} |
737 |
|
|
738 |
|
/** |
739 |
< |
* hasWaiters throws IMSE if not synced |
739 |
> |
* hasWaiters throws IllegalMonitorStateException if not synced |
740 |
|
*/ |
741 |
|
public void testHasWaitersIMSE() { |
742 |
< |
final Mutex sync = new Mutex(); |
743 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
742 |
> |
final Mutex sync = new Mutex(); |
743 |
> |
final ConditionObject c = sync.newCondition(); |
744 |
|
try { |
745 |
|
sync.hasWaiters(c); |
746 |
|
shouldThrow(); |
747 |
< |
} catch (IllegalMonitorStateException success) { |
748 |
< |
} catch (Exception ex) { |
697 |
< |
unexpectedException(); |
698 |
< |
} |
747 |
> |
} catch (IllegalMonitorStateException success) {} |
748 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
749 |
|
} |
750 |
|
|
701 |
– |
|
751 |
|
/** |
752 |
< |
* getWaitQueueLength throws IAE if not owned |
752 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
753 |
|
*/ |
754 |
|
public void testGetWaitQueueLengthIAE() { |
755 |
< |
final Mutex sync = new Mutex(); |
756 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
757 |
< |
final Mutex sync2 = new Mutex(); |
755 |
> |
final Mutex sync = new Mutex(); |
756 |
> |
final ConditionObject c = sync.newCondition(); |
757 |
> |
final Mutex sync2 = new Mutex(); |
758 |
|
try { |
759 |
|
sync2.getWaitQueueLength(c); |
760 |
|
shouldThrow(); |
761 |
< |
} catch (IllegalArgumentException success) { |
762 |
< |
} catch (Exception ex) { |
714 |
< |
unexpectedException(); |
715 |
< |
} |
761 |
> |
} catch (IllegalArgumentException success) {} |
762 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
763 |
|
} |
764 |
|
|
765 |
|
/** |
766 |
< |
* getWaitQueueLength throws IMSE if not synced |
766 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not synced |
767 |
|
*/ |
768 |
|
public void testGetWaitQueueLengthIMSE() { |
769 |
< |
final Mutex sync = new Mutex(); |
770 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
769 |
> |
final Mutex sync = new Mutex(); |
770 |
> |
final ConditionObject c = sync.newCondition(); |
771 |
|
try { |
772 |
|
sync.getWaitQueueLength(c); |
773 |
|
shouldThrow(); |
774 |
< |
} catch (IllegalMonitorStateException success) { |
775 |
< |
} catch (Exception ex) { |
729 |
< |
unexpectedException(); |
730 |
< |
} |
774 |
> |
} catch (IllegalMonitorStateException success) {} |
775 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
776 |
|
} |
777 |
|
|
733 |
– |
|
778 |
|
/** |
779 |
< |
* getWaitingThreads throws IAE if not owned |
779 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
780 |
|
*/ |
781 |
|
public void testGetWaitingThreadsIAE() { |
782 |
< |
final Mutex sync = new Mutex(); |
783 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
784 |
< |
final Mutex sync2 = new Mutex(); |
782 |
> |
final Mutex sync = new Mutex(); |
783 |
> |
final ConditionObject c = sync.newCondition(); |
784 |
> |
final Mutex sync2 = new Mutex(); |
785 |
|
try { |
786 |
|
sync2.getWaitingThreads(c); |
787 |
|
shouldThrow(); |
788 |
< |
} catch (IllegalArgumentException success) { |
789 |
< |
} catch (Exception ex) { |
746 |
< |
unexpectedException(); |
747 |
< |
} |
788 |
> |
} catch (IllegalArgumentException success) {} |
789 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
790 |
|
} |
791 |
|
|
792 |
|
/** |
793 |
< |
* getWaitingThreads throws IMSE if not synced |
793 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not synced |
794 |
|
*/ |
795 |
|
public void testGetWaitingThreadsIMSE() { |
796 |
< |
final Mutex sync = new Mutex(); |
797 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
796 |
> |
final Mutex sync = new Mutex(); |
797 |
> |
final ConditionObject c = sync.newCondition(); |
798 |
|
try { |
799 |
|
sync.getWaitingThreads(c); |
800 |
|
shouldThrow(); |
801 |
< |
} catch (IllegalMonitorStateException success) { |
802 |
< |
} catch (Exception ex) { |
761 |
< |
unexpectedException(); |
762 |
< |
} |
801 |
> |
} catch (IllegalMonitorStateException success) {} |
802 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
803 |
|
} |
804 |
|
|
765 |
– |
|
766 |
– |
|
805 |
|
/** |
806 |
|
* hasWaiters returns true when a thread is waiting, else false |
807 |
|
*/ |
808 |
|
public void testHasWaiters() { |
809 |
< |
final Mutex sync = new Mutex(); |
810 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
< |
Thread t = new Thread(new Runnable() { |
812 |
< |
public void run() { |
813 |
< |
try { |
814 |
< |
sync.acquire(1); |
815 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
816 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
817 |
< |
c.await(); |
818 |
< |
sync.release(1); |
819 |
< |
} |
820 |
< |
catch(InterruptedException e) { |
821 |
< |
threadUnexpectedException(); |
822 |
< |
} |
823 |
< |
} |
824 |
< |
}); |
809 |
> |
final Mutex sync = new Mutex(); |
810 |
> |
final ConditionObject c = sync.newCondition(); |
811 |
> |
final BooleanLatch acquired = new BooleanLatch(); |
812 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
813 |
> |
public void realRun() throws InterruptedException { |
814 |
> |
sync.acquire(); |
815 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
816 |
> |
assertFalse(sync.hasWaiters(c)); |
817 |
> |
assertTrue(acquired.releaseShared(0)); |
818 |
> |
c.await(); |
819 |
> |
sync.release(); |
820 |
> |
}}); |
821 |
> |
|
822 |
> |
acquired.acquireShared(0); |
823 |
> |
sync.acquire(); |
824 |
> |
assertHasWaitersLocked(sync, c, t); |
825 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
826 |
> |
assertTrue(sync.hasWaiters(c)); |
827 |
> |
c.signal(); |
828 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
829 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
830 |
> |
assertFalse(sync.hasWaiters(c)); |
831 |
> |
sync.release(); |
832 |
|
|
833 |
< |
try { |
834 |
< |
t.start(); |
790 |
< |
Thread.sleep(SHORT_DELAY_MS); |
791 |
< |
sync.acquire(1); |
792 |
< |
assertTrue(sync.hasWaiters(c)); |
793 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
794 |
< |
c.signal(); |
795 |
< |
sync.release(1); |
796 |
< |
Thread.sleep(SHORT_DELAY_MS); |
797 |
< |
sync.acquire(1); |
798 |
< |
assertFalse(sync.hasWaiters(c)); |
799 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
800 |
< |
sync.release(1); |
801 |
< |
t.join(SHORT_DELAY_MS); |
802 |
< |
assertFalse(t.isAlive()); |
803 |
< |
} |
804 |
< |
catch (Exception ex) { |
805 |
< |
unexpectedException(); |
806 |
< |
} |
833 |
> |
awaitTermination(t); |
834 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
|
* getWaitQueueLength returns number of waiting threads |
839 |
|
*/ |
840 |
|
public void testGetWaitQueueLength() { |
841 |
< |
final Mutex sync = new Mutex(); |
842 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
843 |
< |
Thread t1 = new Thread(new Runnable() { |
844 |
< |
public void run() { |
845 |
< |
try { |
846 |
< |
sync.acquire(1); |
847 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
848 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
849 |
< |
c.await(); |
850 |
< |
sync.release(1); |
851 |
< |
} |
852 |
< |
catch(InterruptedException e) { |
853 |
< |
threadUnexpectedException(); |
854 |
< |
} |
855 |
< |
} |
856 |
< |
}); |
857 |
< |
|
858 |
< |
Thread t2 = new Thread(new Runnable() { |
859 |
< |
public void run() { |
860 |
< |
try { |
861 |
< |
sync.acquire(1); |
862 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
863 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
864 |
< |
c.await(); |
865 |
< |
sync.release(1); |
866 |
< |
} |
867 |
< |
catch(InterruptedException e) { |
868 |
< |
threadUnexpectedException(); |
869 |
< |
} |
870 |
< |
} |
871 |
< |
}); |
872 |
< |
|
873 |
< |
try { |
874 |
< |
t1.start(); |
875 |
< |
Thread.sleep(SHORT_DELAY_MS); |
876 |
< |
t2.start(); |
877 |
< |
Thread.sleep(SHORT_DELAY_MS); |
878 |
< |
sync.acquire(1); |
879 |
< |
assertTrue(sync.hasWaiters(c)); |
880 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
881 |
< |
c.signalAll(); |
882 |
< |
sync.release(1); |
855 |
< |
Thread.sleep(SHORT_DELAY_MS); |
856 |
< |
sync.acquire(1); |
857 |
< |
assertFalse(sync.hasWaiters(c)); |
858 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
859 |
< |
sync.release(1); |
860 |
< |
t1.join(SHORT_DELAY_MS); |
861 |
< |
t2.join(SHORT_DELAY_MS); |
862 |
< |
assertFalse(t1.isAlive()); |
863 |
< |
assertFalse(t2.isAlive()); |
864 |
< |
} |
865 |
< |
catch (Exception ex) { |
866 |
< |
unexpectedException(); |
867 |
< |
} |
841 |
> |
final Mutex sync = new Mutex(); |
842 |
> |
final ConditionObject c = sync.newCondition(); |
843 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
844 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
845 |
> |
final Thread t1 = newStartedThread(new CheckedRunnable() { |
846 |
> |
public void realRun() throws InterruptedException { |
847 |
> |
sync.acquire(); |
848 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
849 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
850 |
> |
assertTrue(acquired1.releaseShared(0)); |
851 |
> |
c.await(); |
852 |
> |
sync.release(); |
853 |
> |
}}); |
854 |
> |
acquired1.acquireShared(0); |
855 |
> |
sync.acquire(); |
856 |
> |
assertHasWaitersLocked(sync, c, t1); |
857 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
858 |
> |
sync.release(); |
859 |
> |
|
860 |
> |
final Thread t2 = newStartedThread(new CheckedRunnable() { |
861 |
> |
public void realRun() throws InterruptedException { |
862 |
> |
sync.acquire(); |
863 |
> |
assertHasWaitersLocked(sync, c, t1); |
864 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
865 |
> |
assertTrue(acquired2.releaseShared(0)); |
866 |
> |
c.await(); |
867 |
> |
sync.release(); |
868 |
> |
}}); |
869 |
> |
acquired2.acquireShared(0); |
870 |
> |
sync.acquire(); |
871 |
> |
assertHasWaitersLocked(sync, c, t1, t2); |
872 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
873 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
874 |
> |
c.signalAll(); |
875 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
876 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
877 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
878 |
> |
sync.release(); |
879 |
> |
|
880 |
> |
awaitTermination(t1); |
881 |
> |
awaitTermination(t2); |
882 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
886 |
|
* getWaitingThreads returns only and all waiting threads |
887 |
|
*/ |
888 |
|
public void testGetWaitingThreads() { |
889 |
< |
final Mutex sync = new Mutex(); |
890 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
891 |
< |
Thread t1 = new Thread(new Runnable() { |
892 |
< |
public void run() { |
893 |
< |
try { |
894 |
< |
sync.acquire(1); |
895 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
896 |
< |
c.await(); |
897 |
< |
sync.release(1); |
898 |
< |
} |
899 |
< |
catch(InterruptedException e) { |
900 |
< |
threadUnexpectedException(); |
901 |
< |
} |
902 |
< |
} |
903 |
< |
}); |
904 |
< |
|
905 |
< |
Thread t2 = new Thread(new Runnable() { |
906 |
< |
public void run() { |
907 |
< |
try { |
908 |
< |
sync.acquire(1); |
909 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
910 |
< |
c.await(); |
911 |
< |
sync.release(1); |
912 |
< |
} |
913 |
< |
catch(InterruptedException e) { |
914 |
< |
threadUnexpectedException(); |
915 |
< |
} |
916 |
< |
} |
917 |
< |
}); |
918 |
< |
|
919 |
< |
try { |
920 |
< |
sync.acquire(1); |
921 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
922 |
< |
sync.release(1); |
923 |
< |
t1.start(); |
924 |
< |
Thread.sleep(SHORT_DELAY_MS); |
925 |
< |
t2.start(); |
926 |
< |
Thread.sleep(SHORT_DELAY_MS); |
927 |
< |
sync.acquire(1); |
928 |
< |
assertTrue(sync.hasWaiters(c)); |
929 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
930 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
931 |
< |
c.signalAll(); |
932 |
< |
sync.release(1); |
933 |
< |
Thread.sleep(SHORT_DELAY_MS); |
934 |
< |
sync.acquire(1); |
935 |
< |
assertFalse(sync.hasWaiters(c)); |
936 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
937 |
< |
sync.release(1); |
938 |
< |
t1.join(SHORT_DELAY_MS); |
939 |
< |
t2.join(SHORT_DELAY_MS); |
940 |
< |
assertFalse(t1.isAlive()); |
941 |
< |
assertFalse(t2.isAlive()); |
942 |
< |
} |
943 |
< |
catch (Exception ex) { |
944 |
< |
unexpectedException(); |
945 |
< |
} |
889 |
> |
final Mutex sync = new Mutex(); |
890 |
> |
final ConditionObject c = sync.newCondition(); |
891 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
892 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
893 |
> |
final Thread t1 = new Thread(new CheckedRunnable() { |
894 |
> |
public void realRun() throws InterruptedException { |
895 |
> |
sync.acquire(); |
896 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
897 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
898 |
> |
assertTrue(acquired1.releaseShared(0)); |
899 |
> |
c.await(); |
900 |
> |
sync.release(); |
901 |
> |
}}); |
902 |
> |
|
903 |
> |
final Thread t2 = new Thread(new CheckedRunnable() { |
904 |
> |
public void realRun() throws InterruptedException { |
905 |
> |
sync.acquire(); |
906 |
> |
assertHasWaitersLocked(sync, c, t1); |
907 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
908 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
909 |
> |
assertEquals(1, sync.getWaitingThreads(c).size()); |
910 |
> |
assertTrue(acquired2.releaseShared(0)); |
911 |
> |
c.await(); |
912 |
> |
sync.release(); |
913 |
> |
}}); |
914 |
> |
|
915 |
> |
sync.acquire(); |
916 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
917 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
918 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
919 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
920 |
> |
assertEquals(0, sync.getWaitingThreads(c).size()); |
921 |
> |
sync.release(); |
922 |
> |
|
923 |
> |
t1.start(); |
924 |
> |
acquired1.acquireShared(0); |
925 |
> |
sync.acquire(); |
926 |
> |
assertHasWaitersLocked(sync, c, t1); |
927 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
928 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
929 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
930 |
> |
assertEquals(1, sync.getWaitingThreads(c).size()); |
931 |
> |
sync.release(); |
932 |
> |
|
933 |
> |
t2.start(); |
934 |
> |
acquired2.acquireShared(0); |
935 |
> |
sync.acquire(); |
936 |
> |
assertHasWaitersLocked(sync, c, t1, t2); |
937 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
938 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
939 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
940 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
941 |
> |
assertEquals(2, sync.getWaitingThreads(c).size()); |
942 |
> |
c.signalAll(); |
943 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
944 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
945 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t1)); |
946 |
> |
assertFalse(sync.getWaitingThreads(c).contains(t2)); |
947 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
948 |
> |
assertEquals(0, sync.getWaitingThreads(c).size()); |
949 |
> |
sync.release(); |
950 |
> |
|
951 |
> |
awaitTermination(t1); |
952 |
> |
awaitTermination(t2); |
953 |
> |
assertHasWaitersUnlocked(sync, c, NO_THREADS); |
954 |
|
} |
955 |
|
|
933 |
– |
|
934 |
– |
|
956 |
|
/** |
957 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
957 |
> |
* awaitUninterruptibly is uninterruptible |
958 |
|
*/ |
959 |
|
public void testAwaitUninterruptibly() { |
960 |
< |
final Mutex sync = new Mutex(); |
961 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
962 |
< |
Thread t = new Thread(new Runnable() { |
963 |
< |
public void run() { |
964 |
< |
sync.acquire(1); |
965 |
< |
c.awaitUninterruptibly(); |
966 |
< |
sync.release(1); |
967 |
< |
} |
968 |
< |
}); |
969 |
< |
|
970 |
< |
try { |
971 |
< |
t.start(); |
972 |
< |
Thread.sleep(SHORT_DELAY_MS); |
973 |
< |
t.interrupt(); |
974 |
< |
sync.acquire(1); |
975 |
< |
c.signal(); |
976 |
< |
sync.release(1); |
977 |
< |
assert(t.isInterrupted()); |
978 |
< |
t.join(SHORT_DELAY_MS); |
979 |
< |
assertFalse(t.isAlive()); |
980 |
< |
} |
981 |
< |
catch (Exception ex) { |
982 |
< |
unexpectedException(); |
983 |
< |
} |
960 |
> |
final Mutex sync = new Mutex(); |
961 |
> |
final ConditionObject c = sync.newCondition(); |
962 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
963 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
964 |
> |
public void realRun() { |
965 |
> |
sync.acquire(); |
966 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
967 |
> |
c.awaitUninterruptibly(); |
968 |
> |
assertTrue(Thread.interrupted()); |
969 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
970 |
> |
sync.release(); |
971 |
> |
}}); |
972 |
> |
|
973 |
> |
pleaseInterrupt.acquireShared(0); |
974 |
> |
sync.acquire(); |
975 |
> |
assertHasWaitersLocked(sync, c, t); |
976 |
> |
sync.release(); |
977 |
> |
t.interrupt(); |
978 |
> |
assertHasWaitersUnlocked(sync, c, t); |
979 |
> |
assertThreadStaysAlive(t); |
980 |
> |
sync.acquire(); |
981 |
> |
assertHasWaitersLocked(sync, c, t); |
982 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
983 |
> |
c.signal(); |
984 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
985 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
986 |
> |
sync.release(); |
987 |
> |
awaitTermination(t); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
991 |
< |
* await is interruptible |
992 |
< |
*/ |
993 |
< |
public void testAwait_Interrupt() { |
994 |
< |
final Mutex sync = new Mutex(); |
995 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
996 |
< |
Thread t = new Thread(new Runnable() { |
997 |
< |
public void run() { |
998 |
< |
try { |
999 |
< |
sync.acquire(1); |
1000 |
< |
c.await(); |
1001 |
< |
sync.release(1); |
1002 |
< |
threadShouldThrow(); |
1003 |
< |
} |
1004 |
< |
catch(InterruptedException success) { |
1005 |
< |
} |
1006 |
< |
} |
1007 |
< |
}); |
1008 |
< |
|
1009 |
< |
try { |
1010 |
< |
t.start(); |
986 |
< |
Thread.sleep(SHORT_DELAY_MS); |
987 |
< |
t.interrupt(); |
988 |
< |
t.join(SHORT_DELAY_MS); |
989 |
< |
assertFalse(t.isAlive()); |
990 |
< |
} |
991 |
< |
catch (Exception ex) { |
992 |
< |
unexpectedException(); |
993 |
< |
} |
994 |
< |
} |
995 |
< |
|
996 |
< |
/** |
997 |
< |
* awaitNanos is interruptible |
998 |
< |
*/ |
999 |
< |
public void testAwaitNanos_Interrupt() { |
1000 |
< |
final Mutex sync = new Mutex(); |
1001 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1002 |
< |
Thread t = new Thread(new Runnable() { |
1003 |
< |
public void run() { |
1004 |
< |
try { |
1005 |
< |
sync.acquire(1); |
1006 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1007 |
< |
sync.release(1); |
1008 |
< |
threadShouldThrow(); |
1009 |
< |
} |
1010 |
< |
catch(InterruptedException success) { |
1011 |
< |
} |
1012 |
< |
} |
1013 |
< |
}); |
1014 |
< |
|
1015 |
< |
try { |
1016 |
< |
t.start(); |
1017 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1018 |
< |
t.interrupt(); |
1019 |
< |
t.join(SHORT_DELAY_MS); |
1020 |
< |
assertFalse(t.isAlive()); |
1021 |
< |
} |
1022 |
< |
catch (Exception ex) { |
1023 |
< |
unexpectedException(); |
1024 |
< |
} |
1025 |
< |
} |
1026 |
< |
|
1027 |
< |
/** |
1028 |
< |
* awaitUntil is interruptible |
1029 |
< |
*/ |
1030 |
< |
public void testAwaitUntil_Interrupt() { |
1031 |
< |
final Mutex sync = new Mutex(); |
1032 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1033 |
< |
Thread t = new Thread(new Runnable() { |
1034 |
< |
public void run() { |
1035 |
< |
try { |
1036 |
< |
sync.acquire(1); |
1037 |
< |
java.util.Date d = new java.util.Date(); |
1038 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1039 |
< |
sync.release(1); |
1040 |
< |
threadShouldThrow(); |
1041 |
< |
} |
1042 |
< |
catch(InterruptedException success) { |
1043 |
< |
} |
1044 |
< |
} |
1045 |
< |
}); |
1046 |
< |
|
1047 |
< |
try { |
1048 |
< |
t.start(); |
1049 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1050 |
< |
t.interrupt(); |
1051 |
< |
t.join(SHORT_DELAY_MS); |
1052 |
< |
assertFalse(t.isAlive()); |
1053 |
< |
} |
1054 |
< |
catch (Exception ex) { |
1055 |
< |
unexpectedException(); |
1056 |
< |
} |
991 |
> |
* await/awaitNanos/awaitUntil is interruptible |
992 |
> |
*/ |
993 |
> |
public void testInterruptible_await() { testInterruptible(AwaitMethod.await); } |
994 |
> |
public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); } |
995 |
> |
public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); } |
996 |
> |
public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); } |
997 |
> |
public void testInterruptible(final AwaitMethod awaitMethod) { |
998 |
> |
final Mutex sync = new Mutex(); |
999 |
> |
final ConditionObject c = sync.newCondition(); |
1000 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
1001 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1002 |
> |
public void realRun() throws InterruptedException { |
1003 |
> |
sync.acquire(); |
1004 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
1005 |
> |
await(c, awaitMethod); |
1006 |
> |
}}); |
1007 |
> |
|
1008 |
> |
pleaseInterrupt.acquireShared(0); |
1009 |
> |
t.interrupt(); |
1010 |
> |
awaitTermination(t); |
1011 |
|
} |
1012 |
|
|
1013 |
|
/** |
1014 |
|
* signalAll wakes up all threads |
1015 |
|
*/ |
1016 |
< |
public void testSignalAll() { |
1017 |
< |
final Mutex sync = new Mutex(); |
1018 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1019 |
< |
Thread t1 = new Thread(new Runnable() { |
1020 |
< |
public void run() { |
1021 |
< |
try { |
1022 |
< |
sync.acquire(1); |
1023 |
< |
c.await(); |
1024 |
< |
sync.release(1); |
1025 |
< |
} |
1026 |
< |
catch(InterruptedException e) { |
1027 |
< |
threadUnexpectedException(); |
1028 |
< |
} |
1029 |
< |
} |
1030 |
< |
}); |
1031 |
< |
|
1032 |
< |
Thread t2 = new Thread(new Runnable() { |
1033 |
< |
public void run() { |
1034 |
< |
try { |
1035 |
< |
sync.acquire(1); |
1036 |
< |
c.await(); |
1037 |
< |
sync.release(1); |
1038 |
< |
} |
1039 |
< |
catch(InterruptedException e) { |
1040 |
< |
threadUnexpectedException(); |
1041 |
< |
} |
1042 |
< |
} |
1043 |
< |
}); |
1044 |
< |
|
1045 |
< |
try { |
1046 |
< |
t1.start(); |
1047 |
< |
t2.start(); |
1048 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1049 |
< |
sync.acquire(1); |
1050 |
< |
c.signalAll(); |
1051 |
< |
sync.release(1); |
1098 |
< |
t1.join(SHORT_DELAY_MS); |
1099 |
< |
t2.join(SHORT_DELAY_MS); |
1100 |
< |
assertFalse(t1.isAlive()); |
1101 |
< |
assertFalse(t2.isAlive()); |
1102 |
< |
} |
1103 |
< |
catch (Exception ex) { |
1104 |
< |
unexpectedException(); |
1105 |
< |
} |
1016 |
> |
public void testSignalAll_await() { testSignalAll(AwaitMethod.await); } |
1017 |
> |
public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); } |
1018 |
> |
public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); } |
1019 |
> |
public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); } |
1020 |
> |
public void testSignalAll(final AwaitMethod awaitMethod) { |
1021 |
> |
final Mutex sync = new Mutex(); |
1022 |
> |
final ConditionObject c = sync.newCondition(); |
1023 |
> |
final BooleanLatch acquired1 = new BooleanLatch(); |
1024 |
> |
final BooleanLatch acquired2 = new BooleanLatch(); |
1025 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1026 |
> |
public void realRun() throws InterruptedException { |
1027 |
> |
sync.acquire(); |
1028 |
> |
acquired1.releaseShared(0); |
1029 |
> |
await(c, awaitMethod); |
1030 |
> |
sync.release(); |
1031 |
> |
}}); |
1032 |
> |
|
1033 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1034 |
> |
public void realRun() throws InterruptedException { |
1035 |
> |
sync.acquire(); |
1036 |
> |
acquired2.releaseShared(0); |
1037 |
> |
await(c, awaitMethod); |
1038 |
> |
sync.release(); |
1039 |
> |
}}); |
1040 |
> |
|
1041 |
> |
acquired1.acquireShared(0); |
1042 |
> |
acquired2.acquireShared(0); |
1043 |
> |
sync.acquire(); |
1044 |
> |
assertHasWaitersLocked(sync, c, t1, t2); |
1045 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1046 |
> |
c.signalAll(); |
1047 |
> |
assertHasWaitersLocked(sync, c, NO_THREADS); |
1048 |
> |
assertHasExclusiveQueuedThreads(sync, t1, t2); |
1049 |
> |
sync.release(); |
1050 |
> |
awaitTermination(t1); |
1051 |
> |
awaitTermination(t2); |
1052 |
|
} |
1053 |
|
|
1108 |
– |
|
1054 |
|
/** |
1055 |
|
* toString indicates current state |
1056 |
|
*/ |
1057 |
|
public void testToString() { |
1058 |
|
Mutex sync = new Mutex(); |
1059 |
< |
String us = sync.toString(); |
1060 |
< |
assertTrue(us.indexOf("State = 0") >= 0); |
1061 |
< |
sync.acquire(1); |
1117 |
< |
String ls = sync.toString(); |
1118 |
< |
assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0); |
1059 |
> |
assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED)); |
1060 |
> |
sync.acquire(); |
1061 |
> |
assertTrue(sync.toString().contains("State = " + Mutex.LOCKED)); |
1062 |
|
} |
1063 |
|
|
1064 |
|
/** |
1065 |
< |
* A serialized AQS deserializes with current state |
1065 |
> |
* A serialized AQS deserializes with current state, but no queued threads |
1066 |
|
*/ |
1067 |
|
public void testSerialization() { |
1068 |
< |
Mutex l = new Mutex(); |
1069 |
< |
l.acquire(1); |
1070 |
< |
assertTrue(l.isHeldExclusively()); |
1071 |
< |
|
1072 |
< |
try { |
1073 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1074 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1075 |
< |
out.writeObject(l); |
1076 |
< |
out.close(); |
1077 |
< |
|
1078 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1079 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1080 |
< |
Mutex r = (Mutex) in.readObject(); |
1081 |
< |
assertTrue(r.isHeldExclusively()); |
1082 |
< |
} catch(Exception e){ |
1083 |
< |
e.printStackTrace(); |
1084 |
< |
unexpectedException(); |
1085 |
< |
} |
1068 |
> |
Mutex sync = new Mutex(); |
1069 |
> |
assertFalse(serialClone(sync).isHeldExclusively()); |
1070 |
> |
sync.acquire(); |
1071 |
> |
Thread t = newStartedThread(new InterruptedSyncRunnable(sync)); |
1072 |
> |
waitForQueuedThread(sync, t); |
1073 |
> |
assertTrue(sync.isHeldExclusively()); |
1074 |
> |
|
1075 |
> |
Mutex clone = serialClone(sync); |
1076 |
> |
assertTrue(clone.isHeldExclusively()); |
1077 |
> |
assertHasExclusiveQueuedThreads(sync, t); |
1078 |
> |
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1079 |
> |
t.interrupt(); |
1080 |
> |
awaitTermination(t); |
1081 |
> |
sync.release(); |
1082 |
> |
assertFalse(sync.isHeldExclusively()); |
1083 |
> |
assertTrue(clone.isHeldExclusively()); |
1084 |
> |
assertHasExclusiveQueuedThreads(sync, NO_THREADS); |
1085 |
> |
assertHasExclusiveQueuedThreads(clone, NO_THREADS); |
1086 |
|
} |
1087 |
|
|
1145 |
– |
|
1088 |
|
/** |
1089 |
|
* tryReleaseShared setting state changes getState |
1090 |
|
*/ |
1091 |
|
public void testGetStateWithReleaseShared() { |
1092 |
< |
final BooleanLatch l = new BooleanLatch(); |
1093 |
< |
assertFalse(l.isSignalled()); |
1094 |
< |
l.releaseShared(0); |
1095 |
< |
assertTrue(l.isSignalled()); |
1092 |
> |
final BooleanLatch l = new BooleanLatch(); |
1093 |
> |
assertFalse(l.isSignalled()); |
1094 |
> |
assertTrue(l.releaseShared(0)); |
1095 |
> |
assertTrue(l.isSignalled()); |
1096 |
|
} |
1097 |
|
|
1098 |
|
/** |
1099 |
|
* releaseShared has no effect when already signalled |
1100 |
|
*/ |
1101 |
|
public void testReleaseShared() { |
1102 |
< |
final BooleanLatch l = new BooleanLatch(); |
1103 |
< |
assertFalse(l.isSignalled()); |
1104 |
< |
l.releaseShared(0); |
1105 |
< |
assertTrue(l.isSignalled()); |
1106 |
< |
l.releaseShared(0); |
1107 |
< |
assertTrue(l.isSignalled()); |
1102 |
> |
final BooleanLatch l = new BooleanLatch(); |
1103 |
> |
assertFalse(l.isSignalled()); |
1104 |
> |
assertTrue(l.releaseShared(0)); |
1105 |
> |
assertTrue(l.isSignalled()); |
1106 |
> |
assertTrue(l.releaseShared(0)); |
1107 |
> |
assertTrue(l.isSignalled()); |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1111 |
|
* acquireSharedInterruptibly returns after release, but not before |
1112 |
|
*/ |
1113 |
|
public void testAcquireSharedInterruptibly() { |
1114 |
< |
final BooleanLatch l = new BooleanLatch(); |
1114 |
> |
final BooleanLatch l = new BooleanLatch(); |
1115 |
|
|
1116 |
< |
Thread t = new Thread(new Runnable() { |
1117 |
< |
public void run() { |
1118 |
< |
try { |
1119 |
< |
threadAssertFalse(l.isSignalled()); |
1120 |
< |
l.acquireSharedInterruptibly(0); |
1121 |
< |
threadAssertTrue(l.isSignalled()); |
1122 |
< |
} catch(InterruptedException e){ |
1123 |
< |
threadUnexpectedException(); |
1124 |
< |
} |
1125 |
< |
} |
1126 |
< |
}); |
1127 |
< |
try { |
1128 |
< |
t.start(); |
1129 |
< |
assertFalse(l.isSignalled()); |
1130 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1131 |
< |
l.releaseShared(0); |
1190 |
< |
assertTrue(l.isSignalled()); |
1191 |
< |
t.join(); |
1192 |
< |
} catch (InterruptedException e){ |
1193 |
< |
unexpectedException(); |
1194 |
< |
} |
1116 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1117 |
> |
public void realRun() throws InterruptedException { |
1118 |
> |
assertFalse(l.isSignalled()); |
1119 |
> |
l.acquireSharedInterruptibly(0); |
1120 |
> |
assertTrue(l.isSignalled()); |
1121 |
> |
l.acquireSharedInterruptibly(0); |
1122 |
> |
assertTrue(l.isSignalled()); |
1123 |
> |
}}); |
1124 |
> |
|
1125 |
> |
waitForQueuedThread(l, t); |
1126 |
> |
assertFalse(l.isSignalled()); |
1127 |
> |
assertThreadStaysAlive(t); |
1128 |
> |
assertHasSharedQueuedThreads(l, t); |
1129 |
> |
assertTrue(l.releaseShared(0)); |
1130 |
> |
assertTrue(l.isSignalled()); |
1131 |
> |
awaitTermination(t); |
1132 |
|
} |
1196 |
– |
|
1133 |
|
|
1134 |
|
/** |
1135 |
< |
* acquireSharedTimed returns after release |
1135 |
> |
* tryAcquireSharedNanos returns after release, but not before |
1136 |
|
*/ |
1137 |
< |
public void testAsquireSharedTimed() { |
1138 |
< |
final BooleanLatch l = new BooleanLatch(); |
1137 |
> |
public void testTryAcquireSharedNanos() { |
1138 |
> |
final BooleanLatch l = new BooleanLatch(); |
1139 |
|
|
1140 |
< |
Thread t = new Thread(new Runnable() { |
1141 |
< |
public void run() { |
1142 |
< |
try { |
1143 |
< |
threadAssertFalse(l.isSignalled()); |
1144 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1145 |
< |
threadAssertTrue(l.isSignalled()); |
1146 |
< |
|
1147 |
< |
} catch(InterruptedException e){ |
1148 |
< |
threadUnexpectedException(); |
1149 |
< |
} |
1150 |
< |
} |
1151 |
< |
}); |
1152 |
< |
try { |
1153 |
< |
t.start(); |
1154 |
< |
assertFalse(l.isSignalled()); |
1155 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1220 |
< |
l.releaseShared(0); |
1221 |
< |
assertTrue(l.isSignalled()); |
1222 |
< |
t.join(); |
1223 |
< |
} catch (InterruptedException e){ |
1224 |
< |
unexpectedException(); |
1225 |
< |
} |
1140 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1141 |
> |
public void realRun() throws InterruptedException { |
1142 |
> |
assertFalse(l.isSignalled()); |
1143 |
> |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1144 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1145 |
> |
assertTrue(l.isSignalled()); |
1146 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
1147 |
> |
assertTrue(l.isSignalled()); |
1148 |
> |
}}); |
1149 |
> |
|
1150 |
> |
waitForQueuedThread(l, t); |
1151 |
> |
assertFalse(l.isSignalled()); |
1152 |
> |
assertThreadStaysAlive(t); |
1153 |
> |
assertTrue(l.releaseShared(0)); |
1154 |
> |
assertTrue(l.isSignalled()); |
1155 |
> |
awaitTermination(t); |
1156 |
|
} |
1157 |
< |
|
1157 |
> |
|
1158 |
|
/** |
1159 |
< |
* acquireSharedInterruptibly throws IE if interrupted before released |
1159 |
> |
* acquireSharedInterruptibly is interruptible |
1160 |
|
*/ |
1161 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
1161 |
> |
public void testAcquireSharedInterruptibly_Interruptible() { |
1162 |
|
final BooleanLatch l = new BooleanLatch(); |
1163 |
< |
Thread t = new Thread(new Runnable() { |
1164 |
< |
public void run() { |
1165 |
< |
try { |
1166 |
< |
threadAssertFalse(l.isSignalled()); |
1167 |
< |
l.acquireSharedInterruptibly(0); |
1168 |
< |
threadShouldThrow(); |
1169 |
< |
} catch(InterruptedException success){} |
1170 |
< |
} |
1171 |
< |
}); |
1172 |
< |
t.start(); |
1173 |
< |
try { |
1244 |
< |
assertFalse(l.isSignalled()); |
1245 |
< |
t.interrupt(); |
1246 |
< |
t.join(); |
1247 |
< |
} catch (InterruptedException e){ |
1248 |
< |
unexpectedException(); |
1249 |
< |
} |
1163 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1164 |
> |
public void realRun() throws InterruptedException { |
1165 |
> |
assertFalse(l.isSignalled()); |
1166 |
> |
l.acquireSharedInterruptibly(0); |
1167 |
> |
}}); |
1168 |
> |
|
1169 |
> |
waitForQueuedThread(l, t); |
1170 |
> |
assertFalse(l.isSignalled()); |
1171 |
> |
t.interrupt(); |
1172 |
> |
awaitTermination(t); |
1173 |
> |
assertFalse(l.isSignalled()); |
1174 |
|
} |
1175 |
|
|
1176 |
|
/** |
1177 |
< |
* acquireSharedTimed throws IE if interrupted before released |
1177 |
> |
* tryAcquireSharedNanos is interruptible |
1178 |
|
*/ |
1179 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
1179 |
> |
public void testTryAcquireSharedNanos_Interruptible() { |
1180 |
|
final BooleanLatch l = new BooleanLatch(); |
1181 |
< |
Thread t = new Thread(new Runnable() { |
1182 |
< |
public void run() { |
1183 |
< |
try { |
1184 |
< |
threadAssertFalse(l.isSignalled()); |
1185 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1186 |
< |
threadShouldThrow(); |
1187 |
< |
} catch(InterruptedException success){} |
1188 |
< |
} |
1189 |
< |
}); |
1190 |
< |
t.start(); |
1191 |
< |
try { |
1192 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1269 |
< |
assertFalse(l.isSignalled()); |
1270 |
< |
t.interrupt(); |
1271 |
< |
t.join(); |
1272 |
< |
} catch (InterruptedException e){ |
1273 |
< |
unexpectedException(); |
1274 |
< |
} |
1181 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1182 |
> |
public void realRun() throws InterruptedException { |
1183 |
> |
assertFalse(l.isSignalled()); |
1184 |
> |
long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); |
1185 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1186 |
> |
}}); |
1187 |
> |
|
1188 |
> |
waitForQueuedThread(l, t); |
1189 |
> |
assertFalse(l.isSignalled()); |
1190 |
> |
t.interrupt(); |
1191 |
> |
awaitTermination(t); |
1192 |
> |
assertFalse(l.isSignalled()); |
1193 |
|
} |
1194 |
|
|
1195 |
|
/** |
1196 |
< |
* acquireSharedTimed times out if not released before timeout |
1196 |
> |
* tryAcquireSharedNanos times out if not released before timeout |
1197 |
|
*/ |
1198 |
< |
public void testAcquireSharedNanos_Timeout() { |
1198 |
> |
public void testTryAcquireSharedNanos_Timeout() { |
1199 |
|
final BooleanLatch l = new BooleanLatch(); |
1200 |
< |
Thread t = new Thread(new Runnable() { |
1201 |
< |
public void run() { |
1202 |
< |
try { |
1203 |
< |
threadAssertFalse(l.isSignalled()); |
1204 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1205 |
< |
} catch(InterruptedException ie){ |
1206 |
< |
threadUnexpectedException(); |
1207 |
< |
} |
1200 |
> |
final BooleanLatch observedQueued = new BooleanLatch(); |
1201 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1202 |
> |
public void realRun() throws InterruptedException { |
1203 |
> |
assertFalse(l.isSignalled()); |
1204 |
> |
for (long millis = timeoutMillis(); |
1205 |
> |
!observedQueued.isSignalled(); |
1206 |
> |
millis *= 2) { |
1207 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1208 |
> |
long startTime = System.nanoTime(); |
1209 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1210 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1211 |
|
} |
1212 |
< |
}); |
1213 |
< |
t.start(); |
1214 |
< |
try { |
1215 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1216 |
< |
assertFalse(l.isSignalled()); |
1217 |
< |
t.join(); |
1218 |
< |
} catch (InterruptedException e){ |
1219 |
< |
unexpectedException(); |
1220 |
< |
} |
1212 |
> |
assertFalse(l.isSignalled()); |
1213 |
> |
}}); |
1214 |
> |
|
1215 |
> |
waitForQueuedThread(l, t); |
1216 |
> |
observedQueued.releaseShared(0); |
1217 |
> |
assertFalse(l.isSignalled()); |
1218 |
> |
awaitTermination(t); |
1219 |
> |
assertFalse(l.isSignalled()); |
1220 |
> |
} |
1221 |
> |
|
1222 |
> |
/** |
1223 |
> |
* awaitNanos/timed await with 0 wait times out immediately |
1224 |
> |
*/ |
1225 |
> |
public void testAwait_Zero() throws InterruptedException { |
1226 |
> |
final Mutex sync = new Mutex(); |
1227 |
> |
final ConditionObject c = sync.newCondition(); |
1228 |
> |
sync.acquire(); |
1229 |
> |
assertTrue(c.awaitNanos(0L) <= 0); |
1230 |
> |
assertFalse(c.await(0L, NANOSECONDS)); |
1231 |
> |
sync.release(); |
1232 |
> |
} |
1233 |
> |
|
1234 |
> |
/** |
1235 |
> |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1236 |
> |
*/ |
1237 |
> |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1238 |
> |
final Mutex sync = new Mutex(); |
1239 |
> |
final ConditionObject c = sync.newCondition(); |
1240 |
> |
sync.acquire(); |
1241 |
> |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1242 |
> |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1243 |
> |
sync.release(); |
1244 |
|
} |
1245 |
|
|
1302 |
– |
|
1246 |
|
} |