254 |
|
*/ |
255 |
|
Object waitForHole(boolean timed, long nanos) { |
256 |
|
long lastTime = (timed)? System.nanoTime() : 0; |
257 |
< |
Object h = get(); |
258 |
< |
while (h == null) { |
259 |
< |
if (!timed) |
257 |
> |
Object h; |
258 |
> |
while ((h = get()) == null) { |
259 |
> |
// If interrupted or timed out, try to cancel by |
260 |
> |
// CASing FAIL as hole value. |
261 |
> |
if (Thread.currentThread().isInterrupted() || |
262 |
> |
(timed && nanos <= 0)) |
263 |
> |
compareAndSet(null, FAIL); |
264 |
> |
else if (!timed) |
265 |
|
LockSupport.park(); |
266 |
|
else { |
267 |
|
LockSupport.parkNanos(nanos); |
269 |
|
nanos -= now - lastTime; |
270 |
|
lastTime = now; |
271 |
|
} |
267 |
– |
// If interrupted or timed out, try to cancel by |
268 |
– |
// CASing FAIL as hole value. |
269 |
– |
if ((h = get()) == null && |
270 |
– |
(Thread.currentThread().isInterrupted() || |
271 |
– |
(timed && nanos <= 0))) { |
272 |
– |
compareAndSet(null, FAIL); |
273 |
– |
h = get(); |
274 |
– |
} |
272 |
|
} |
273 |
|
return h; |
274 |
|
} |