author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea and Martin Buchholz with assistance from |
|
30 |
* members of JCP JSR-166 Expert Group and released to the public |
|
31 |
* domain, as explained at |
|
32 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
33 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
34 |
* Pat Fisher, Mike Judd. |
|
35 |
*/ |
|
36 |
||
37 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
38 |
||
39 |
import java.util.concurrent.CountDownLatch; |
|
40 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
41 |
import java.util.concurrent.locks.LockSupport; |
|
42 |
||
43 |
import junit.framework.Test; |
|
44 |
import junit.framework.TestSuite; |
|
45 |
||
46 |
public class LockSupportTest extends JSR166TestCase { |
|
47 |
public static void main(String[] args) { |
|
48 |
main(suite(), args); |
|
49 |
} |
|
50 |
||
51 |
public static Test suite() { |
|
52 |
return new TestSuite(LockSupportTest.class); |
|
53 |
} |
|
54 |
||
55 |
static { |
|
56 |
// Reduce the risk of rare disastrous classloading in first call to |
|
57 |
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
|
58 |
Class<?> ensureLoaded = LockSupport.class; |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Returns the blocker object used by tests in this file. |
|
63 |
* Any old object will do; we'll return a convenient one. |
|
64 |
*/ |
|
65 |
static Object theBlocker() { |
|
66 |
return LockSupportTest.class; |
|
67 |
} |
|
68 |
||
69 |
enum ParkMethod { |
|
70 |
park() { |
|
71 |
void park() { |
|
72 |
LockSupport.park(); |
|
73 |
} |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
74 |
Thread.State parkedState() { return Thread.State.WAITING; } |
35394 | 75 |
}, |
76 |
parkUntil() { |
|
77 |
void park(long millis) { |
|
78 |
LockSupport.parkUntil(deadline(millis)); |
|
79 |
} |
|
80 |
}, |
|
81 |
parkNanos() { |
|
82 |
void park(long millis) { |
|
83 |
LockSupport.parkNanos(MILLISECONDS.toNanos(millis)); |
|
84 |
} |
|
85 |
}, |
|
86 |
parkBlocker() { |
|
87 |
void park() { |
|
88 |
LockSupport.park(theBlocker()); |
|
89 |
} |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
90 |
Thread.State parkedState() { return Thread.State.WAITING; } |
35394 | 91 |
}, |
92 |
parkUntilBlocker() { |
|
93 |
void park(long millis) { |
|
94 |
LockSupport.parkUntil(theBlocker(), deadline(millis)); |
|
95 |
} |
|
96 |
}, |
|
97 |
parkNanosBlocker() { |
|
98 |
void park(long millis) { |
|
99 |
LockSupport.parkNanos(theBlocker(), |
|
100 |
MILLISECONDS.toNanos(millis)); |
|
101 |
} |
|
102 |
}; |
|
103 |
||
104 |
void park() { park(2 * LONG_DELAY_MS); } |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
105 |
void park(long millis) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
106 |
throw new UnsupportedOperationException(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
107 |
} |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
108 |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
35394 | 109 |
|
110 |
/** Returns a deadline to use with parkUntil. */ |
|
111 |
long deadline(long millis) { |
|
112 |
// beware of rounding |
|
113 |
return System.currentTimeMillis() + millis + 1; |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* park is released by subsequent unpark |
|
119 |
*/ |
|
120 |
public void testParkBeforeUnpark_park() { |
|
121 |
testParkBeforeUnpark(ParkMethod.park); |
|
122 |
} |
|
123 |
public void testParkBeforeUnpark_parkNanos() { |
|
124 |
testParkBeforeUnpark(ParkMethod.parkNanos); |
|
125 |
} |
|
126 |
public void testParkBeforeUnpark_parkUntil() { |
|
127 |
testParkBeforeUnpark(ParkMethod.parkUntil); |
|
128 |
} |
|
129 |
public void testParkBeforeUnpark_parkBlocker() { |
|
130 |
testParkBeforeUnpark(ParkMethod.parkBlocker); |
|
131 |
} |
|
132 |
public void testParkBeforeUnpark_parkNanosBlocker() { |
|
133 |
testParkBeforeUnpark(ParkMethod.parkNanosBlocker); |
|
134 |
} |
|
135 |
public void testParkBeforeUnpark_parkUntilBlocker() { |
|
136 |
testParkBeforeUnpark(ParkMethod.parkUntilBlocker); |
|
137 |
} |
|
138 |
public void testParkBeforeUnpark(final ParkMethod parkMethod) { |
|
139 |
final CountDownLatch pleaseUnpark = new CountDownLatch(1); |
|
140 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
141 |
public void realRun() { |
|
142 |
pleaseUnpark.countDown(); |
|
143 |
parkMethod.park(); |
|
144 |
}}); |
|
145 |
||
146 |
await(pleaseUnpark); |
|
147 |
LockSupport.unpark(t); |
|
148 |
awaitTermination(t); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* park is released by preceding unpark |
|
153 |
*/ |
|
154 |
public void testParkAfterUnpark_park() { |
|
155 |
testParkAfterUnpark(ParkMethod.park); |
|
156 |
} |
|
157 |
public void testParkAfterUnpark_parkNanos() { |
|
158 |
testParkAfterUnpark(ParkMethod.parkNanos); |
|
159 |
} |
|
160 |
public void testParkAfterUnpark_parkUntil() { |
|
161 |
testParkAfterUnpark(ParkMethod.parkUntil); |
|
162 |
} |
|
163 |
public void testParkAfterUnpark_parkBlocker() { |
|
164 |
testParkAfterUnpark(ParkMethod.parkBlocker); |
|
165 |
} |
|
166 |
public void testParkAfterUnpark_parkNanosBlocker() { |
|
167 |
testParkAfterUnpark(ParkMethod.parkNanosBlocker); |
|
168 |
} |
|
169 |
public void testParkAfterUnpark_parkUntilBlocker() { |
|
170 |
testParkAfterUnpark(ParkMethod.parkUntilBlocker); |
|
171 |
} |
|
172 |
public void testParkAfterUnpark(final ParkMethod parkMethod) { |
|
173 |
final CountDownLatch pleaseUnpark = new CountDownLatch(1); |
|
174 |
final AtomicBoolean pleasePark = new AtomicBoolean(false); |
|
175 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
176 |
public void realRun() { |
|
177 |
pleaseUnpark.countDown(); |
|
178 |
while (!pleasePark.get()) |
|
179 |
Thread.yield(); |
|
180 |
parkMethod.park(); |
|
181 |
}}); |
|
182 |
||
183 |
await(pleaseUnpark); |
|
184 |
LockSupport.unpark(t); |
|
185 |
pleasePark.set(true); |
|
186 |
awaitTermination(t); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* park is released by subsequent interrupt |
|
191 |
*/ |
|
192 |
public void testParkBeforeInterrupt_park() { |
|
193 |
testParkBeforeInterrupt(ParkMethod.park); |
|
194 |
} |
|
195 |
public void testParkBeforeInterrupt_parkNanos() { |
|
196 |
testParkBeforeInterrupt(ParkMethod.parkNanos); |
|
197 |
} |
|
198 |
public void testParkBeforeInterrupt_parkUntil() { |
|
199 |
testParkBeforeInterrupt(ParkMethod.parkUntil); |
|
200 |
} |
|
201 |
public void testParkBeforeInterrupt_parkBlocker() { |
|
202 |
testParkBeforeInterrupt(ParkMethod.parkBlocker); |
|
203 |
} |
|
204 |
public void testParkBeforeInterrupt_parkNanosBlocker() { |
|
205 |
testParkBeforeInterrupt(ParkMethod.parkNanosBlocker); |
|
206 |
} |
|
207 |
public void testParkBeforeInterrupt_parkUntilBlocker() { |
|
208 |
testParkBeforeInterrupt(ParkMethod.parkUntilBlocker); |
|
209 |
} |
|
210 |
public void testParkBeforeInterrupt(final ParkMethod parkMethod) { |
|
211 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
|
212 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
213 |
public void realRun() { |
|
214 |
pleaseInterrupt.countDown(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
215 |
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
35394 | 216 |
parkMethod.park(); |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
217 |
if (Thread.interrupted()) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
218 |
return; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
219 |
} |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
220 |
fail("too many consecutive spurious wakeups?"); |
35394 | 221 |
}}); |
222 |
||
223 |
await(pleaseInterrupt); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
224 |
assertThreadBlocks(t, parkMethod.parkedState()); |
35394 | 225 |
t.interrupt(); |
226 |
awaitTermination(t); |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* park is released by preceding interrupt |
|
231 |
*/ |
|
232 |
public void testParkAfterInterrupt_park() { |
|
233 |
testParkAfterInterrupt(ParkMethod.park); |
|
234 |
} |
|
235 |
public void testParkAfterInterrupt_parkNanos() { |
|
236 |
testParkAfterInterrupt(ParkMethod.parkNanos); |
|
237 |
} |
|
238 |
public void testParkAfterInterrupt_parkUntil() { |
|
239 |
testParkAfterInterrupt(ParkMethod.parkUntil); |
|
240 |
} |
|
241 |
public void testParkAfterInterrupt_parkBlocker() { |
|
242 |
testParkAfterInterrupt(ParkMethod.parkBlocker); |
|
243 |
} |
|
244 |
public void testParkAfterInterrupt_parkNanosBlocker() { |
|
245 |
testParkAfterInterrupt(ParkMethod.parkNanosBlocker); |
|
246 |
} |
|
247 |
public void testParkAfterInterrupt_parkUntilBlocker() { |
|
248 |
testParkAfterInterrupt(ParkMethod.parkUntilBlocker); |
|
249 |
} |
|
250 |
public void testParkAfterInterrupt(final ParkMethod parkMethod) { |
|
251 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
|
252 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
253 |
public void realRun() throws Exception { |
|
254 |
pleaseInterrupt.countDown(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
255 |
while (!Thread.currentThread().isInterrupted()) |
35394 | 256 |
Thread.yield(); |
257 |
parkMethod.park(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
258 |
assertTrue(Thread.interrupted()); |
35394 | 259 |
}}); |
260 |
||
261 |
await(pleaseInterrupt); |
|
262 |
t.interrupt(); |
|
263 |
awaitTermination(t); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* timed park times out if not unparked |
|
268 |
*/ |
|
269 |
public void testParkTimesOut_parkNanos() { |
|
270 |
testParkTimesOut(ParkMethod.parkNanos); |
|
271 |
} |
|
272 |
public void testParkTimesOut_parkUntil() { |
|
273 |
testParkTimesOut(ParkMethod.parkUntil); |
|
274 |
} |
|
275 |
public void testParkTimesOut_parkNanosBlocker() { |
|
276 |
testParkTimesOut(ParkMethod.parkNanosBlocker); |
|
277 |
} |
|
278 |
public void testParkTimesOut_parkUntilBlocker() { |
|
279 |
testParkTimesOut(ParkMethod.parkUntilBlocker); |
|
280 |
} |
|
281 |
public void testParkTimesOut(final ParkMethod parkMethod) { |
|
282 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
283 |
public void realRun() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
284 |
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
35394 | 285 |
long startTime = System.nanoTime(); |
286 |
parkMethod.park(timeoutMillis()); |
|
287 |
if (millisElapsedSince(startTime) >= timeoutMillis()) |
|
288 |
return; |
|
289 |
} |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
290 |
fail("too many consecutive spurious wakeups?"); |
35394 | 291 |
}}); |
292 |
||
293 |
awaitTermination(t); |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* getBlocker(null) throws NullPointerException |
|
298 |
*/ |
|
299 |
public void testGetBlockerNull() { |
|
300 |
try { |
|
301 |
LockSupport.getBlocker(null); |
|
302 |
shouldThrow(); |
|
303 |
} catch (NullPointerException success) {} |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* getBlocker returns the blocker object passed to park |
|
308 |
*/ |
|
309 |
public void testGetBlocker_parkBlocker() { |
|
310 |
testGetBlocker(ParkMethod.parkBlocker); |
|
311 |
} |
|
312 |
public void testGetBlocker_parkNanosBlocker() { |
|
313 |
testGetBlocker(ParkMethod.parkNanosBlocker); |
|
314 |
} |
|
315 |
public void testGetBlocker_parkUntilBlocker() { |
|
316 |
testGetBlocker(ParkMethod.parkUntilBlocker); |
|
317 |
} |
|
318 |
public void testGetBlocker(final ParkMethod parkMethod) { |
|
319 |
final CountDownLatch started = new CountDownLatch(1); |
|
320 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
321 |
public void realRun() { |
|
322 |
Thread t = Thread.currentThread(); |
|
323 |
started.countDown(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
324 |
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
35394 | 325 |
assertNull(LockSupport.getBlocker(t)); |
326 |
parkMethod.park(); |
|
327 |
assertNull(LockSupport.getBlocker(t)); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
328 |
if (Thread.interrupted()) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
329 |
return; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
330 |
} |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
331 |
fail("too many consecutive spurious wakeups?"); |
35394 | 332 |
}}); |
333 |
||
334 |
long startTime = System.nanoTime(); |
|
335 |
await(started); |
|
336 |
for (;;) { |
|
337 |
Object x = LockSupport.getBlocker(t); |
|
338 |
if (x == theBlocker()) { // success |
|
339 |
t.interrupt(); |
|
340 |
awaitTermination(t); |
|
341 |
assertNull(LockSupport.getBlocker(t)); |
|
342 |
return; |
|
343 |
} else { |
|
344 |
assertNull(x); // ok |
|
345 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
|
346 |
fail("timed out"); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
347 |
if (t.getState() == Thread.State.TERMINATED) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
348 |
break; |
35394 | 349 |
Thread.yield(); |
350 |
} |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* timed park(0) returns immediately. |
|
356 |
* |
|
357 |
* Requires hotspot fix for: |
|
358 |
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |
|
359 |
* which is in jdk7-b118 and 6u25. |
|
360 |
*/ |
|
361 |
public void testPark0_parkNanos() { |
|
362 |
testPark0(ParkMethod.parkNanos); |
|
363 |
} |
|
364 |
public void testPark0_parkUntil() { |
|
365 |
testPark0(ParkMethod.parkUntil); |
|
366 |
} |
|
367 |
public void testPark0_parkNanosBlocker() { |
|
368 |
testPark0(ParkMethod.parkNanosBlocker); |
|
369 |
} |
|
370 |
public void testPark0_parkUntilBlocker() { |
|
371 |
testPark0(ParkMethod.parkUntilBlocker); |
|
372 |
} |
|
373 |
public void testPark0(final ParkMethod parkMethod) { |
|
374 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
375 |
public void realRun() { |
|
376 |
parkMethod.park(0L); |
|
377 |
}}); |
|
378 |
||
379 |
awaitTermination(t); |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* timed park(Long.MIN_VALUE) returns immediately. |
|
384 |
*/ |
|
385 |
public void testParkNeg_parkNanos() { |
|
386 |
testParkNeg(ParkMethod.parkNanos); |
|
387 |
} |
|
388 |
public void testParkNeg_parkUntil() { |
|
389 |
testParkNeg(ParkMethod.parkUntil); |
|
390 |
} |
|
391 |
public void testParkNeg_parkNanosBlocker() { |
|
392 |
testParkNeg(ParkMethod.parkNanosBlocker); |
|
393 |
} |
|
394 |
public void testParkNeg_parkUntilBlocker() { |
|
395 |
testParkNeg(ParkMethod.parkUntilBlocker); |
|
396 |
} |
|
397 |
public void testParkNeg(final ParkMethod parkMethod) { |
|
398 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
399 |
public void realRun() { |
|
400 |
parkMethod.park(Long.MIN_VALUE); |
|
401 |
}}); |
|
402 |
||
403 |
awaitTermination(t); |
|
404 |
} |
|
405 |
} |