7 |
|
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
9 |
|
|
10 |
< |
import junit.framework.*; |
11 |
< |
import java.util.*; |
12 |
< |
import java.util.concurrent.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
> |
|
12 |
> |
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
14 |
< |
import java.util.concurrent.locks.*; |
14 |
> |
import java.util.concurrent.locks.LockSupport; |
15 |
> |
|
16 |
> |
import junit.framework.Test; |
17 |
> |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class LockSupportTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run(suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
26 |
|
} |
27 |
|
|
28 |
|
/** |
29 |
< |
* park is released by subsequent unpark |
29 |
> |
* Returns the blocker object used by tests in this file. |
30 |
> |
* Any old object will do; we'll return a convenient one. |
31 |
|
*/ |
32 |
< |
public void testParkBeforeUnpark() throws InterruptedException { |
33 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
30 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
31 |
< |
public void realRun() { |
32 |
< |
threadStarted.countDown(); |
33 |
< |
LockSupport.park(); |
34 |
< |
}}); |
35 |
< |
|
36 |
< |
threadStarted.await(); |
37 |
< |
delay(SHORT_DELAY_MS); |
38 |
< |
LockSupport.unpark(t); |
39 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
32 |
> |
static Object theBlocker() { |
33 |
> |
return LockSupportTest.class; |
34 |
|
} |
35 |
|
|
36 |
< |
/** |
37 |
< |
* parkUntil is released by subsequent unpark |
38 |
< |
*/ |
39 |
< |
public void testParkUntilBeforeUnpark() throws InterruptedException { |
40 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
41 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
42 |
< |
public void realRun() { |
43 |
< |
long d = new Date().getTime() + LONG_DELAY_MS; |
44 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
45 |
< |
long t0 = System.nanoTime(); |
46 |
< |
threadStarted.countDown(); |
47 |
< |
LockSupport.parkUntil(d); |
48 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
49 |
< |
}}); |
50 |
< |
|
51 |
< |
threadStarted.await(); |
52 |
< |
delay(SHORT_DELAY_MS); |
53 |
< |
LockSupport.unpark(t); |
54 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
36 |
> |
enum ParkMethod { |
37 |
> |
park() { |
38 |
> |
void park() { |
39 |
> |
LockSupport.park(); |
40 |
> |
} |
41 |
> |
void park(long millis) { |
42 |
> |
throw new UnsupportedOperationException(); |
43 |
> |
} |
44 |
> |
}, |
45 |
> |
parkUntil() { |
46 |
> |
void park(long millis) { |
47 |
> |
LockSupport.parkUntil(deadline(millis)); |
48 |
> |
} |
49 |
> |
}, |
50 |
> |
parkNanos() { |
51 |
> |
void park(long millis) { |
52 |
> |
LockSupport.parkNanos(MILLISECONDS.toNanos(millis)); |
53 |
> |
} |
54 |
> |
}, |
55 |
> |
parkBlocker() { |
56 |
> |
void park() { |
57 |
> |
LockSupport.park(theBlocker()); |
58 |
> |
} |
59 |
> |
void park(long millis) { |
60 |
> |
throw new UnsupportedOperationException(); |
61 |
> |
} |
62 |
> |
}, |
63 |
> |
parkUntilBlocker() { |
64 |
> |
void park(long millis) { |
65 |
> |
LockSupport.parkUntil(theBlocker(), deadline(millis)); |
66 |
> |
} |
67 |
> |
}, |
68 |
> |
parkNanosBlocker() { |
69 |
> |
void park(long millis) { |
70 |
> |
LockSupport.parkNanos(theBlocker(), |
71 |
> |
MILLISECONDS.toNanos(millis)); |
72 |
> |
} |
73 |
> |
}; |
74 |
> |
|
75 |
> |
void park() { park(2 * LONG_DELAY_MS); } |
76 |
> |
abstract void park(long millis); |
77 |
> |
|
78 |
> |
/** Returns a deadline to use with parkUntil. */ |
79 |
> |
long deadline(long millis) { |
80 |
> |
// beware of rounding |
81 |
> |
return System.currentTimeMillis() + millis + 1; |
82 |
> |
} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* parkNanos is released by subsequent unpark |
86 |
> |
* park is released by subsequent unpark |
87 |
|
*/ |
88 |
< |
public void testParkNanosBeforeUnpark() throws InterruptedException { |
89 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
88 |
> |
public void testParkBeforeUnpark_park() { |
89 |
> |
testParkBeforeUnpark(ParkMethod.park); |
90 |
> |
} |
91 |
> |
public void testParkBeforeUnpark_parkNanos() { |
92 |
> |
testParkBeforeUnpark(ParkMethod.parkNanos); |
93 |
> |
} |
94 |
> |
public void testParkBeforeUnpark_parkUntil() { |
95 |
> |
testParkBeforeUnpark(ParkMethod.parkUntil); |
96 |
> |
} |
97 |
> |
public void testParkBeforeUnpark_parkBlocker() { |
98 |
> |
testParkBeforeUnpark(ParkMethod.parkBlocker); |
99 |
> |
} |
100 |
> |
public void testParkBeforeUnpark_parkNanosBlocker() { |
101 |
> |
testParkBeforeUnpark(ParkMethod.parkNanosBlocker); |
102 |
> |
} |
103 |
> |
public void testParkBeforeUnpark_parkUntilBlocker() { |
104 |
> |
testParkBeforeUnpark(ParkMethod.parkUntilBlocker); |
105 |
> |
} |
106 |
> |
public void testParkBeforeUnpark(final ParkMethod parkMethod) { |
107 |
> |
final CountDownLatch pleaseUnpark = new CountDownLatch(1); |
108 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
109 |
|
public void realRun() { |
110 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
111 |
< |
long t0 = System.nanoTime(); |
72 |
< |
threadStarted.countDown(); |
73 |
< |
LockSupport.parkNanos(nanos); |
74 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
110 |
> |
pleaseUnpark.countDown(); |
111 |
> |
parkMethod.park(); |
112 |
|
}}); |
113 |
|
|
114 |
< |
threadStarted.await(); |
78 |
< |
delay(SHORT_DELAY_MS); |
114 |
> |
await(pleaseUnpark); |
115 |
|
LockSupport.unpark(t); |
116 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
116 |
> |
awaitTermination(t); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* park is released by preceding unpark |
121 |
|
*/ |
122 |
< |
public void testParkAfterUnpark() throws Exception { |
123 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
88 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
89 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
< |
public void realRun() throws Exception { |
91 |
< |
threadStarted.countDown(); |
92 |
< |
while (!unparked.get()) |
93 |
< |
Thread.yield(); |
94 |
< |
LockSupport.park(); |
95 |
< |
}}); |
96 |
< |
|
97 |
< |
threadStarted.await(); |
98 |
< |
LockSupport.unpark(t); |
99 |
< |
unparked.set(true); |
100 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
122 |
> |
public void testParkAfterUnpark_park() { |
123 |
> |
testParkAfterUnpark(ParkMethod.park); |
124 |
|
} |
125 |
< |
|
126 |
< |
/** |
104 |
< |
* parkUntil is released by preceding unpark |
105 |
< |
*/ |
106 |
< |
public void testParkUntilAfterUnpark() throws Exception { |
107 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
108 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
109 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
110 |
< |
public void realRun() throws Exception { |
111 |
< |
threadStarted.countDown(); |
112 |
< |
while (!unparked.get()) |
113 |
< |
Thread.yield(); |
114 |
< |
long d = new Date().getTime() + LONG_DELAY_MS; |
115 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
116 |
< |
long t0 = System.nanoTime(); |
117 |
< |
LockSupport.parkUntil(d); |
118 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
119 |
< |
}}); |
120 |
< |
|
121 |
< |
threadStarted.await(); |
122 |
< |
LockSupport.unpark(t); |
123 |
< |
unparked.set(true); |
124 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
125 |
> |
public void testParkAfterUnpark_parkNanos() { |
126 |
> |
testParkAfterUnpark(ParkMethod.parkNanos); |
127 |
|
} |
128 |
< |
|
129 |
< |
/** |
130 |
< |
* parkNanos is released by preceding unpark |
131 |
< |
*/ |
132 |
< |
public void testParkNanosAfterUnpark() throws Exception { |
133 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
134 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
128 |
> |
public void testParkAfterUnpark_parkUntil() { |
129 |
> |
testParkAfterUnpark(ParkMethod.parkUntil); |
130 |
> |
} |
131 |
> |
public void testParkAfterUnpark_parkBlocker() { |
132 |
> |
testParkAfterUnpark(ParkMethod.parkBlocker); |
133 |
> |
} |
134 |
> |
public void testParkAfterUnpark_parkNanosBlocker() { |
135 |
> |
testParkAfterUnpark(ParkMethod.parkNanosBlocker); |
136 |
> |
} |
137 |
> |
public void testParkAfterUnpark_parkUntilBlocker() { |
138 |
> |
testParkAfterUnpark(ParkMethod.parkUntilBlocker); |
139 |
> |
} |
140 |
> |
public void testParkAfterUnpark(final ParkMethod parkMethod) { |
141 |
> |
final CountDownLatch pleaseUnpark = new CountDownLatch(1); |
142 |
> |
final AtomicBoolean pleasePark = new AtomicBoolean(false); |
143 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
144 |
< |
public void realRun() throws Exception { |
145 |
< |
threadStarted.countDown(); |
146 |
< |
while (!unparked.get()) |
144 |
> |
public void realRun() { |
145 |
> |
pleaseUnpark.countDown(); |
146 |
> |
while (!pleasePark.get()) |
147 |
|
Thread.yield(); |
148 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
139 |
< |
long t0 = System.nanoTime(); |
140 |
< |
LockSupport.parkNanos(nanos); |
141 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
148 |
> |
parkMethod.park(); |
149 |
|
}}); |
150 |
|
|
151 |
< |
threadStarted.await(); |
151 |
> |
await(pleaseUnpark); |
152 |
|
LockSupport.unpark(t); |
153 |
< |
unparked.set(true); |
154 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
153 |
> |
pleasePark.set(true); |
154 |
> |
awaitTermination(t); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* park is released by subsequent interrupt |
159 |
|
*/ |
160 |
< |
public void testParkBeforeInterrupt() throws InterruptedException { |
161 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
155 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
156 |
< |
public void realRun() { |
157 |
< |
assertFalse(Thread.currentThread().isInterrupted()); |
158 |
< |
threadStarted.countDown(); |
159 |
< |
do { |
160 |
< |
LockSupport.park(); |
161 |
< |
// park may return spuriously |
162 |
< |
} while (! Thread.currentThread().isInterrupted()); |
163 |
< |
}}); |
164 |
< |
|
165 |
< |
threadStarted.await(); |
166 |
< |
delay(SHORT_DELAY_MS); |
167 |
< |
t.interrupt(); |
168 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
160 |
> |
public void testParkBeforeInterrupt_park() { |
161 |
> |
testParkBeforeInterrupt(ParkMethod.park); |
162 |
|
} |
163 |
< |
|
164 |
< |
/** |
172 |
< |
* parkUntil is released by subsequent interrupt |
173 |
< |
*/ |
174 |
< |
public void testParkUntilBeforeInterrupt() throws InterruptedException { |
175 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
176 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
177 |
< |
public void realRun() { |
178 |
< |
long d = new Date().getTime() + LONG_DELAY_MS; |
179 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
180 |
< |
long t0 = System.nanoTime(); |
181 |
< |
assertFalse(Thread.currentThread().isInterrupted()); |
182 |
< |
threadStarted.countDown(); |
183 |
< |
do { |
184 |
< |
LockSupport.parkUntil(d); |
185 |
< |
// parkUntil may return spuriously |
186 |
< |
} while (! Thread.currentThread().isInterrupted()); |
187 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
188 |
< |
}}); |
189 |
< |
|
190 |
< |
threadStarted.await(); |
191 |
< |
delay(SHORT_DELAY_MS); |
192 |
< |
t.interrupt(); |
193 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
163 |
> |
public void testParkBeforeInterrupt_parkNanos() { |
164 |
> |
testParkBeforeInterrupt(ParkMethod.parkNanos); |
165 |
|
} |
166 |
< |
|
167 |
< |
/** |
168 |
< |
* parkNanos is released by subsequent interrupt |
169 |
< |
*/ |
170 |
< |
public void testParkNanosBeforeInterrupt() throws InterruptedException { |
171 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
166 |
> |
public void testParkBeforeInterrupt_parkUntil() { |
167 |
> |
testParkBeforeInterrupt(ParkMethod.parkUntil); |
168 |
> |
} |
169 |
> |
public void testParkBeforeInterrupt_parkBlocker() { |
170 |
> |
testParkBeforeInterrupt(ParkMethod.parkBlocker); |
171 |
> |
} |
172 |
> |
public void testParkBeforeInterrupt_parkNanosBlocker() { |
173 |
> |
testParkBeforeInterrupt(ParkMethod.parkNanosBlocker); |
174 |
> |
} |
175 |
> |
public void testParkBeforeInterrupt_parkUntilBlocker() { |
176 |
> |
testParkBeforeInterrupt(ParkMethod.parkUntilBlocker); |
177 |
> |
} |
178 |
> |
public void testParkBeforeInterrupt(final ParkMethod parkMethod) { |
179 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
180 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
181 |
|
public void realRun() { |
182 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
204 |
< |
long t0 = System.nanoTime(); |
205 |
< |
assertFalse(Thread.currentThread().isInterrupted()); |
206 |
< |
threadStarted.countDown(); |
182 |
> |
pleaseInterrupt.countDown(); |
183 |
|
do { |
184 |
< |
LockSupport.parkNanos(nanos); |
185 |
< |
// parkNanos may return spuriously |
184 |
> |
parkMethod.park(); |
185 |
> |
// park may return spuriously |
186 |
|
} while (! Thread.currentThread().isInterrupted()); |
211 |
– |
assertTrue(System.nanoTime() - t0 < nanos); |
187 |
|
}}); |
188 |
|
|
189 |
< |
threadStarted.await(); |
190 |
< |
delay(SHORT_DELAY_MS); |
189 |
> |
await(pleaseInterrupt); |
190 |
> |
assertThreadStaysAlive(t); |
191 |
|
t.interrupt(); |
192 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
192 |
> |
awaitTermination(t); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
|
* park is released by preceding interrupt |
197 |
|
*/ |
198 |
< |
public void testParkAfterInterrupt() throws Exception { |
199 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
200 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
198 |
> |
public void testParkAfterInterrupt_park() { |
199 |
> |
testParkAfterInterrupt(ParkMethod.park); |
200 |
> |
} |
201 |
> |
public void testParkAfterInterrupt_parkNanos() { |
202 |
> |
testParkAfterInterrupt(ParkMethod.parkNanos); |
203 |
> |
} |
204 |
> |
public void testParkAfterInterrupt_parkUntil() { |
205 |
> |
testParkAfterInterrupt(ParkMethod.parkUntil); |
206 |
> |
} |
207 |
> |
public void testParkAfterInterrupt_parkBlocker() { |
208 |
> |
testParkAfterInterrupt(ParkMethod.parkBlocker); |
209 |
> |
} |
210 |
> |
public void testParkAfterInterrupt_parkNanosBlocker() { |
211 |
> |
testParkAfterInterrupt(ParkMethod.parkNanosBlocker); |
212 |
> |
} |
213 |
> |
public void testParkAfterInterrupt_parkUntilBlocker() { |
214 |
> |
testParkAfterInterrupt(ParkMethod.parkUntilBlocker); |
215 |
> |
} |
216 |
> |
public void testParkAfterInterrupt(final ParkMethod parkMethod) { |
217 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
218 |
> |
final AtomicBoolean pleasePark = new AtomicBoolean(false); |
219 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
220 |
|
public void realRun() throws Exception { |
221 |
< |
threadStarted.countDown(); |
222 |
< |
while (!unparked.get()) |
221 |
> |
pleaseInterrupt.countDown(); |
222 |
> |
while (!pleasePark.get()) |
223 |
|
Thread.yield(); |
224 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
225 |
< |
LockSupport.park(); |
225 |
> |
parkMethod.park(); |
226 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
227 |
|
}}); |
228 |
|
|
229 |
< |
threadStarted.await(); |
229 |
> |
await(pleaseInterrupt); |
230 |
|
t.interrupt(); |
231 |
< |
unparked.set(true); |
232 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
231 |
> |
pleasePark.set(true); |
232 |
> |
awaitTermination(t); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* parkUntil is released by preceding interrupt |
236 |
> |
* timed park times out if not unparked |
237 |
|
*/ |
238 |
< |
public void testParkUntilAfterInterrupt() throws Exception { |
239 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
240 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
238 |
> |
public void testParkTimesOut_parkNanos() { |
239 |
> |
testParkTimesOut(ParkMethod.parkNanos); |
240 |
> |
} |
241 |
> |
public void testParkTimesOut_parkUntil() { |
242 |
> |
testParkTimesOut(ParkMethod.parkUntil); |
243 |
> |
} |
244 |
> |
public void testParkTimesOut_parkNanosBlocker() { |
245 |
> |
testParkTimesOut(ParkMethod.parkNanosBlocker); |
246 |
> |
} |
247 |
> |
public void testParkTimesOut_parkUntilBlocker() { |
248 |
> |
testParkTimesOut(ParkMethod.parkUntilBlocker); |
249 |
> |
} |
250 |
> |
public void testParkTimesOut(final ParkMethod parkMethod) { |
251 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
252 |
< |
public void realRun() throws Exception { |
253 |
< |
threadStarted.countDown(); |
254 |
< |
while (!unparked.get()) |
255 |
< |
Thread.yield(); |
256 |
< |
long d = new Date().getTime() + LONG_DELAY_MS; |
257 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
258 |
< |
long t0 = System.nanoTime(); |
259 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
257 |
< |
LockSupport.parkUntil(d); |
258 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
259 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
252 |
> |
public void realRun() { |
253 |
> |
for (;;) { |
254 |
> |
long startTime = System.nanoTime(); |
255 |
> |
parkMethod.park(timeoutMillis()); |
256 |
> |
// park may return spuriously |
257 |
> |
if (millisElapsedSince(startTime) >= timeoutMillis()) |
258 |
> |
return; |
259 |
> |
} |
260 |
|
}}); |
261 |
|
|
262 |
< |
threadStarted.await(); |
263 |
< |
t.interrupt(); |
264 |
< |
unparked.set(true); |
265 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
262 |
> |
awaitTermination(t); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* parkNanos is released by preceding interrupt |
266 |
> |
* getBlocker(null) throws NullPointerException |
267 |
|
*/ |
268 |
< |
public void testParkNanosAfterInterrupt() throws Exception { |
269 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
270 |
< |
final AtomicBoolean unparked = new AtomicBoolean(false); |
271 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
272 |
< |
public void realRun() throws Exception { |
276 |
< |
threadStarted.countDown(); |
277 |
< |
while (!unparked.get()) |
278 |
< |
Thread.yield(); |
279 |
< |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
280 |
< |
long t0 = System.nanoTime(); |
281 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
282 |
< |
LockSupport.parkNanos(nanos); |
283 |
< |
assertTrue(System.nanoTime() - t0 < nanos); |
284 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
285 |
< |
}}); |
286 |
< |
|
287 |
< |
threadStarted.await(); |
288 |
< |
t.interrupt(); |
289 |
< |
unparked.set(true); |
290 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
268 |
> |
public void testGetBlockerNull() { |
269 |
> |
try { |
270 |
> |
LockSupport.getBlocker(null); |
271 |
> |
shouldThrow(); |
272 |
> |
} catch (NullPointerException success) {} |
273 |
|
} |
274 |
|
|
275 |
|
/** |
276 |
< |
* parkNanos times out if not unparked |
276 |
> |
* getBlocker returns the blocker object passed to park |
277 |
|
*/ |
278 |
< |
public void testParkNanosTimesOut() throws InterruptedException { |
278 |
> |
public void testGetBlocker_parkBlocker() { |
279 |
> |
testGetBlocker(ParkMethod.parkBlocker); |
280 |
> |
} |
281 |
> |
public void testGetBlocker_parkNanosBlocker() { |
282 |
> |
testGetBlocker(ParkMethod.parkNanosBlocker); |
283 |
> |
} |
284 |
> |
public void testGetBlocker_parkUntilBlocker() { |
285 |
> |
testGetBlocker(ParkMethod.parkUntilBlocker); |
286 |
> |
} |
287 |
> |
public void testGetBlocker(final ParkMethod parkMethod) { |
288 |
> |
final CountDownLatch started = new CountDownLatch(1); |
289 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
290 |
|
public void realRun() { |
291 |
< |
for (;;) { |
292 |
< |
long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
293 |
< |
long t0 = System.nanoTime(); |
294 |
< |
LockSupport.parkNanos(timeoutNanos); |
295 |
< |
// parkNanos may return spuriously |
296 |
< |
if (System.nanoTime() - t0 >= timeoutNanos) |
297 |
< |
return; |
298 |
< |
} |
291 |
> |
Thread t = Thread.currentThread(); |
292 |
> |
started.countDown(); |
293 |
> |
do { |
294 |
> |
assertNull(LockSupport.getBlocker(t)); |
295 |
> |
parkMethod.park(); |
296 |
> |
assertNull(LockSupport.getBlocker(t)); |
297 |
> |
// park may return spuriously |
298 |
> |
} while (! Thread.currentThread().isInterrupted()); |
299 |
|
}}); |
300 |
|
|
301 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
301 |
> |
long startTime = System.nanoTime(); |
302 |
> |
await(started); |
303 |
> |
for (;;) { |
304 |
> |
Object x = LockSupport.getBlocker(t); |
305 |
> |
if (x == theBlocker()) { // success |
306 |
> |
t.interrupt(); |
307 |
> |
awaitTermination(t); |
308 |
> |
assertNull(LockSupport.getBlocker(t)); |
309 |
> |
return; |
310 |
> |
} else { |
311 |
> |
assertNull(x); // ok |
312 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
313 |
> |
fail("timed out"); |
314 |
> |
Thread.yield(); |
315 |
> |
} |
316 |
> |
} |
317 |
|
} |
318 |
|
|
312 |
– |
|
319 |
|
/** |
320 |
< |
* parkUntil times out if not unparked |
320 |
> |
* timed park(0) returns immediately. |
321 |
> |
* |
322 |
> |
* Requires hotspot fix for: |
323 |
> |
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |
324 |
> |
* which is in jdk7-b118 and 6u25. |
325 |
|
*/ |
326 |
< |
public void testParkUntilTimesOut() throws InterruptedException { |
326 |
> |
public void testPark0_parkNanos() { |
327 |
> |
testPark0(ParkMethod.parkNanos); |
328 |
> |
} |
329 |
> |
public void testPark0_parkUntil() { |
330 |
> |
testPark0(ParkMethod.parkUntil); |
331 |
> |
} |
332 |
> |
public void testPark0_parkNanosBlocker() { |
333 |
> |
testPark0(ParkMethod.parkNanosBlocker); |
334 |
> |
} |
335 |
> |
public void testPark0_parkUntilBlocker() { |
336 |
> |
testPark0(ParkMethod.parkUntilBlocker); |
337 |
> |
} |
338 |
> |
public void testPark0(final ParkMethod parkMethod) { |
339 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
340 |
|
public void realRun() { |
341 |
< |
for (;;) { |
320 |
< |
long d = new Date().getTime() + SHORT_DELAY_MS; |
321 |
< |
// beware of rounding |
322 |
< |
long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L; |
323 |
< |
long t0 = System.nanoTime(); |
324 |
< |
LockSupport.parkUntil(d); |
325 |
< |
// parkUntil may return spuriously |
326 |
< |
if (System.nanoTime() - t0 >= timeoutNanos) |
327 |
< |
return; |
328 |
< |
} |
341 |
> |
parkMethod.park(0L); |
342 |
|
}}); |
343 |
|
|
344 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
344 |
> |
awaitTermination(t); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
< |
* getBlocker(null) throws NullPointerException |
348 |
> |
* timed park(Long.MIN_VALUE) returns immediately. |
349 |
|
*/ |
350 |
< |
public void testGetBlockerNull() { |
351 |
< |
try { |
339 |
< |
LockSupport.getBlocker(null); |
340 |
< |
shouldThrow(); |
341 |
< |
} catch (NullPointerException success) {} |
350 |
> |
public void testParkNeg_parkNanos() { |
351 |
> |
testParkNeg(ParkMethod.parkNanos); |
352 |
|
} |
353 |
< |
|
354 |
< |
/** |
355 |
< |
* parkUntil(0) returns immediately |
356 |
< |
* Requires hotspot fix for: |
357 |
< |
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |
358 |
< |
*/ |
359 |
< |
public void XXXXtestParkUntil0Returns() throws InterruptedException { |
353 |
> |
public void testParkNeg_parkUntil() { |
354 |
> |
testParkNeg(ParkMethod.parkUntil); |
355 |
> |
} |
356 |
> |
public void testParkNeg_parkNanosBlocker() { |
357 |
> |
testParkNeg(ParkMethod.parkNanosBlocker); |
358 |
> |
} |
359 |
> |
public void testParkNeg_parkUntilBlocker() { |
360 |
> |
testParkNeg(ParkMethod.parkUntilBlocker); |
361 |
> |
} |
362 |
> |
public void testParkNeg(final ParkMethod parkMethod) { |
363 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
364 |
|
public void realRun() { |
365 |
< |
LockSupport.parkUntil(0L); |
365 |
> |
parkMethod.park(Long.MIN_VALUE); |
366 |
|
}}); |
367 |
|
|
368 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
368 |
> |
awaitTermination(t); |
369 |
|
} |
370 |
|
} |