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 |
}
|
|
74 |
void park(long millis) {
|
|
75 |
throw new UnsupportedOperationException();
|
|
76 |
}
|
|
77 |
},
|
|
78 |
parkUntil() {
|
|
79 |
void park(long millis) {
|
|
80 |
LockSupport.parkUntil(deadline(millis));
|
|
81 |
}
|
|
82 |
},
|
|
83 |
parkNanos() {
|
|
84 |
void park(long millis) {
|
|
85 |
LockSupport.parkNanos(MILLISECONDS.toNanos(millis));
|
|
86 |
}
|
|
87 |
},
|
|
88 |
parkBlocker() {
|
|
89 |
void park() {
|
|
90 |
LockSupport.park(theBlocker());
|
|
91 |
}
|
|
92 |
void park(long millis) {
|
|
93 |
throw new UnsupportedOperationException();
|
|
94 |
}
|
|
95 |
},
|
|
96 |
parkUntilBlocker() {
|
|
97 |
void park(long millis) {
|
|
98 |
LockSupport.parkUntil(theBlocker(), deadline(millis));
|
|
99 |
}
|
|
100 |
},
|
|
101 |
parkNanosBlocker() {
|
|
102 |
void park(long millis) {
|
|
103 |
LockSupport.parkNanos(theBlocker(),
|
|
104 |
MILLISECONDS.toNanos(millis));
|
|
105 |
}
|
|
106 |
};
|
|
107 |
|
|
108 |
void park() { park(2 * LONG_DELAY_MS); }
|
|
109 |
abstract void park(long millis);
|
|
110 |
|
|
111 |
/** Returns a deadline to use with parkUntil. */
|
|
112 |
long deadline(long millis) {
|
|
113 |
// beware of rounding
|
|
114 |
return System.currentTimeMillis() + millis + 1;
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* park is released by subsequent unpark
|
|
120 |
*/
|
|
121 |
public void testParkBeforeUnpark_park() {
|
|
122 |
testParkBeforeUnpark(ParkMethod.park);
|
|
123 |
}
|
|
124 |
public void testParkBeforeUnpark_parkNanos() {
|
|
125 |
testParkBeforeUnpark(ParkMethod.parkNanos);
|
|
126 |
}
|
|
127 |
public void testParkBeforeUnpark_parkUntil() {
|
|
128 |
testParkBeforeUnpark(ParkMethod.parkUntil);
|
|
129 |
}
|
|
130 |
public void testParkBeforeUnpark_parkBlocker() {
|
|
131 |
testParkBeforeUnpark(ParkMethod.parkBlocker);
|
|
132 |
}
|
|
133 |
public void testParkBeforeUnpark_parkNanosBlocker() {
|
|
134 |
testParkBeforeUnpark(ParkMethod.parkNanosBlocker);
|
|
135 |
}
|
|
136 |
public void testParkBeforeUnpark_parkUntilBlocker() {
|
|
137 |
testParkBeforeUnpark(ParkMethod.parkUntilBlocker);
|
|
138 |
}
|
|
139 |
public void testParkBeforeUnpark(final ParkMethod parkMethod) {
|
|
140 |
final CountDownLatch pleaseUnpark = new CountDownLatch(1);
|
|
141 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
142 |
public void realRun() {
|
|
143 |
pleaseUnpark.countDown();
|
|
144 |
parkMethod.park();
|
|
145 |
}});
|
|
146 |
|
|
147 |
await(pleaseUnpark);
|
|
148 |
LockSupport.unpark(t);
|
|
149 |
awaitTermination(t);
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* park is released by preceding unpark
|
|
154 |
*/
|
|
155 |
public void testParkAfterUnpark_park() {
|
|
156 |
testParkAfterUnpark(ParkMethod.park);
|
|
157 |
}
|
|
158 |
public void testParkAfterUnpark_parkNanos() {
|
|
159 |
testParkAfterUnpark(ParkMethod.parkNanos);
|
|
160 |
}
|
|
161 |
public void testParkAfterUnpark_parkUntil() {
|
|
162 |
testParkAfterUnpark(ParkMethod.parkUntil);
|
|
163 |
}
|
|
164 |
public void testParkAfterUnpark_parkBlocker() {
|
|
165 |
testParkAfterUnpark(ParkMethod.parkBlocker);
|
|
166 |
}
|
|
167 |
public void testParkAfterUnpark_parkNanosBlocker() {
|
|
168 |
testParkAfterUnpark(ParkMethod.parkNanosBlocker);
|
|
169 |
}
|
|
170 |
public void testParkAfterUnpark_parkUntilBlocker() {
|
|
171 |
testParkAfterUnpark(ParkMethod.parkUntilBlocker);
|
|
172 |
}
|
|
173 |
public void testParkAfterUnpark(final ParkMethod parkMethod) {
|
|
174 |
final CountDownLatch pleaseUnpark = new CountDownLatch(1);
|
|
175 |
final AtomicBoolean pleasePark = new AtomicBoolean(false);
|
|
176 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
177 |
public void realRun() {
|
|
178 |
pleaseUnpark.countDown();
|
|
179 |
while (!pleasePark.get())
|
|
180 |
Thread.yield();
|
|
181 |
parkMethod.park();
|
|
182 |
}});
|
|
183 |
|
|
184 |
await(pleaseUnpark);
|
|
185 |
LockSupport.unpark(t);
|
|
186 |
pleasePark.set(true);
|
|
187 |
awaitTermination(t);
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* park is released by subsequent interrupt
|
|
192 |
*/
|
|
193 |
public void testParkBeforeInterrupt_park() {
|
|
194 |
testParkBeforeInterrupt(ParkMethod.park);
|
|
195 |
}
|
|
196 |
public void testParkBeforeInterrupt_parkNanos() {
|
|
197 |
testParkBeforeInterrupt(ParkMethod.parkNanos);
|
|
198 |
}
|
|
199 |
public void testParkBeforeInterrupt_parkUntil() {
|
|
200 |
testParkBeforeInterrupt(ParkMethod.parkUntil);
|
|
201 |
}
|
|
202 |
public void testParkBeforeInterrupt_parkBlocker() {
|
|
203 |
testParkBeforeInterrupt(ParkMethod.parkBlocker);
|
|
204 |
}
|
|
205 |
public void testParkBeforeInterrupt_parkNanosBlocker() {
|
|
206 |
testParkBeforeInterrupt(ParkMethod.parkNanosBlocker);
|
|
207 |
}
|
|
208 |
public void testParkBeforeInterrupt_parkUntilBlocker() {
|
|
209 |
testParkBeforeInterrupt(ParkMethod.parkUntilBlocker);
|
|
210 |
}
|
|
211 |
public void testParkBeforeInterrupt(final ParkMethod parkMethod) {
|
|
212 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
|
|
213 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
214 |
public void realRun() {
|
|
215 |
pleaseInterrupt.countDown();
|
|
216 |
do {
|
|
217 |
parkMethod.park();
|
|
218 |
// park may return spuriously
|
|
219 |
} while (! Thread.currentThread().isInterrupted());
|
|
220 |
}});
|
|
221 |
|
|
222 |
await(pleaseInterrupt);
|
|
223 |
assertThreadStaysAlive(t);
|
|
224 |
t.interrupt();
|
|
225 |
awaitTermination(t);
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* park is released by preceding interrupt
|
|
230 |
*/
|
|
231 |
public void testParkAfterInterrupt_park() {
|
|
232 |
testParkAfterInterrupt(ParkMethod.park);
|
|
233 |
}
|
|
234 |
public void testParkAfterInterrupt_parkNanos() {
|
|
235 |
testParkAfterInterrupt(ParkMethod.parkNanos);
|
|
236 |
}
|
|
237 |
public void testParkAfterInterrupt_parkUntil() {
|
|
238 |
testParkAfterInterrupt(ParkMethod.parkUntil);
|
|
239 |
}
|
|
240 |
public void testParkAfterInterrupt_parkBlocker() {
|
|
241 |
testParkAfterInterrupt(ParkMethod.parkBlocker);
|
|
242 |
}
|
|
243 |
public void testParkAfterInterrupt_parkNanosBlocker() {
|
|
244 |
testParkAfterInterrupt(ParkMethod.parkNanosBlocker);
|
|
245 |
}
|
|
246 |
public void testParkAfterInterrupt_parkUntilBlocker() {
|
|
247 |
testParkAfterInterrupt(ParkMethod.parkUntilBlocker);
|
|
248 |
}
|
|
249 |
public void testParkAfterInterrupt(final ParkMethod parkMethod) {
|
|
250 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
|
|
251 |
final AtomicBoolean pleasePark = new AtomicBoolean(false);
|
|
252 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
253 |
public void realRun() throws Exception {
|
|
254 |
pleaseInterrupt.countDown();
|
|
255 |
while (!pleasePark.get())
|
|
256 |
Thread.yield();
|
|
257 |
assertTrue(Thread.currentThread().isInterrupted());
|
|
258 |
parkMethod.park();
|
|
259 |
assertTrue(Thread.currentThread().isInterrupted());
|
|
260 |
}});
|
|
261 |
|
|
262 |
await(pleaseInterrupt);
|
|
263 |
t.interrupt();
|
|
264 |
pleasePark.set(true);
|
|
265 |
awaitTermination(t);
|
|
266 |
}
|
|
267 |
|
|
268 |
/**
|
|
269 |
* timed park times out if not unparked
|
|
270 |
*/
|
|
271 |
public void testParkTimesOut_parkNanos() {
|
|
272 |
testParkTimesOut(ParkMethod.parkNanos);
|
|
273 |
}
|
|
274 |
public void testParkTimesOut_parkUntil() {
|
|
275 |
testParkTimesOut(ParkMethod.parkUntil);
|
|
276 |
}
|
|
277 |
public void testParkTimesOut_parkNanosBlocker() {
|
|
278 |
testParkTimesOut(ParkMethod.parkNanosBlocker);
|
|
279 |
}
|
|
280 |
public void testParkTimesOut_parkUntilBlocker() {
|
|
281 |
testParkTimesOut(ParkMethod.parkUntilBlocker);
|
|
282 |
}
|
|
283 |
public void testParkTimesOut(final ParkMethod parkMethod) {
|
|
284 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
285 |
public void realRun() {
|
|
286 |
for (;;) {
|
|
287 |
long startTime = System.nanoTime();
|
|
288 |
parkMethod.park(timeoutMillis());
|
|
289 |
// park may return spuriously
|
|
290 |
if (millisElapsedSince(startTime) >= timeoutMillis())
|
|
291 |
return;
|
|
292 |
}
|
|
293 |
}});
|
|
294 |
|
|
295 |
awaitTermination(t);
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* getBlocker(null) throws NullPointerException
|
|
300 |
*/
|
|
301 |
public void testGetBlockerNull() {
|
|
302 |
try {
|
|
303 |
LockSupport.getBlocker(null);
|
|
304 |
shouldThrow();
|
|
305 |
} catch (NullPointerException success) {}
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* getBlocker returns the blocker object passed to park
|
|
310 |
*/
|
|
311 |
public void testGetBlocker_parkBlocker() {
|
|
312 |
testGetBlocker(ParkMethod.parkBlocker);
|
|
313 |
}
|
|
314 |
public void testGetBlocker_parkNanosBlocker() {
|
|
315 |
testGetBlocker(ParkMethod.parkNanosBlocker);
|
|
316 |
}
|
|
317 |
public void testGetBlocker_parkUntilBlocker() {
|
|
318 |
testGetBlocker(ParkMethod.parkUntilBlocker);
|
|
319 |
}
|
|
320 |
public void testGetBlocker(final ParkMethod parkMethod) {
|
|
321 |
final CountDownLatch started = new CountDownLatch(1);
|
|
322 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
323 |
public void realRun() {
|
|
324 |
Thread t = Thread.currentThread();
|
|
325 |
started.countDown();
|
|
326 |
do {
|
|
327 |
assertNull(LockSupport.getBlocker(t));
|
|
328 |
parkMethod.park();
|
|
329 |
assertNull(LockSupport.getBlocker(t));
|
|
330 |
// park may return spuriously
|
|
331 |
} while (! Thread.currentThread().isInterrupted());
|
|
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");
|
|
347 |
Thread.yield();
|
|
348 |
}
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* timed park(0) returns immediately.
|
|
354 |
*
|
|
355 |
* Requires hotspot fix for:
|
|
356 |
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
|
|
357 |
* which is in jdk7-b118 and 6u25.
|
|
358 |
*/
|
|
359 |
public void testPark0_parkNanos() {
|
|
360 |
testPark0(ParkMethod.parkNanos);
|
|
361 |
}
|
|
362 |
public void testPark0_parkUntil() {
|
|
363 |
testPark0(ParkMethod.parkUntil);
|
|
364 |
}
|
|
365 |
public void testPark0_parkNanosBlocker() {
|
|
366 |
testPark0(ParkMethod.parkNanosBlocker);
|
|
367 |
}
|
|
368 |
public void testPark0_parkUntilBlocker() {
|
|
369 |
testPark0(ParkMethod.parkUntilBlocker);
|
|
370 |
}
|
|
371 |
public void testPark0(final ParkMethod parkMethod) {
|
|
372 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
373 |
public void realRun() {
|
|
374 |
parkMethod.park(0L);
|
|
375 |
}});
|
|
376 |
|
|
377 |
awaitTermination(t);
|
|
378 |
}
|
|
379 |
|
|
380 |
/**
|
|
381 |
* timed park(Long.MIN_VALUE) returns immediately.
|
|
382 |
*/
|
|
383 |
public void testParkNeg_parkNanos() {
|
|
384 |
testParkNeg(ParkMethod.parkNanos);
|
|
385 |
}
|
|
386 |
public void testParkNeg_parkUntil() {
|
|
387 |
testParkNeg(ParkMethod.parkUntil);
|
|
388 |
}
|
|
389 |
public void testParkNeg_parkNanosBlocker() {
|
|
390 |
testParkNeg(ParkMethod.parkNanosBlocker);
|
|
391 |
}
|
|
392 |
public void testParkNeg_parkUntilBlocker() {
|
|
393 |
testParkNeg(ParkMethod.parkUntilBlocker);
|
|
394 |
}
|
|
395 |
public void testParkNeg(final ParkMethod parkMethod) {
|
|
396 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
397 |
public void realRun() {
|
|
398 |
parkMethod.park(Long.MIN_VALUE);
|
|
399 |
}});
|
|
400 |
|
|
401 |
awaitTermination(t);
|
|
402 |
}
|
|
403 |
}
|