238 |
|
* only by pool. |
239 |
|
*/ |
240 |
|
final void internalAwaitDone(long millis, int nanos) { |
241 |
< |
if (status >= 0) { |
241 |
> |
int s = status; |
242 |
> |
if ((s == 0 && |
243 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL)) || |
244 |
> |
s > 0) { |
245 |
|
try { // the odd construction reduces lock bias effects |
246 |
|
synchronized (this) { |
247 |
< |
if (status > 0 || |
245 |
< |
UNSAFE.compareAndSwapInt(this, statusOffset, |
246 |
< |
0, SIGNAL)) |
247 |
> |
if (status > 0) |
248 |
|
wait(millis, nanos); |
249 |
+ |
else |
250 |
+ |
notifyAll(); |
251 |
|
} |
252 |
|
} catch (InterruptedException ie) { |
253 |
|
cancelIfTerminating(); |
261 |
|
private void externalAwaitDone() { |
262 |
|
if (status >= 0) { |
263 |
|
boolean interrupted = false; |
264 |
< |
synchronized(this) { |
265 |
< |
int s; |
266 |
< |
while ((s = status) >= 0) { |
267 |
< |
if (s == 0 && |
268 |
< |
!UNSAFE.compareAndSwapInt(this, statusOffset, |
269 |
< |
0, SIGNAL)) |
270 |
< |
continue; |
271 |
< |
try { |
272 |
< |
wait(); |
273 |
< |
} catch (InterruptedException ie) { |
274 |
< |
interrupted = true; |
264 |
> |
synchronized (this) { |
265 |
> |
for (;;) { |
266 |
> |
int s = status; |
267 |
> |
if (s == 0) |
268 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, |
269 |
> |
0, SIGNAL); |
270 |
> |
else if (s < 0) { |
271 |
> |
notifyAll(); |
272 |
> |
break; |
273 |
> |
} |
274 |
> |
else { |
275 |
> |
try { |
276 |
> |
wait(); |
277 |
> |
} catch (InterruptedException ie) { |
278 |
> |
interrupted = true; |
279 |
> |
} |
280 |
|
} |
281 |
|
} |
282 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* Blocks a non-worker-thread until completion or interruption or timeout |
289 |
> |
* Blocks a non-worker-thread until completion or interruption or timeout. |
290 |
|
*/ |
291 |
|
private void externalInterruptibleAwaitDone(boolean timed, long nanos) |
292 |
|
throws InterruptedException { |
294 |
|
throw new InterruptedException(); |
295 |
|
if (status >= 0) { |
296 |
|
long startTime = timed ? System.nanoTime() : 0L; |
297 |
< |
synchronized(this) { |
298 |
< |
int s; |
291 |
< |
while ((s = status) >= 0) { |
297 |
> |
synchronized (this) { |
298 |
> |
for (;;) { |
299 |
|
long nt; |
300 |
< |
if (s == 0 && |
301 |
< |
!UNSAFE.compareAndSwapInt(this, statusOffset, |
302 |
< |
0, SIGNAL)) |
303 |
< |
continue; |
300 |
> |
int s = status; |
301 |
> |
if (s == 0) |
302 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, |
303 |
> |
0, SIGNAL); |
304 |
> |
else if (s < 0) { |
305 |
> |
notifyAll(); |
306 |
> |
break; |
307 |
> |
} |
308 |
|
else if (!timed) |
309 |
|
wait(); |
310 |
|
else if ((nt = nanos - (System.nanoTime()-startTime)) > 0L) |
853 |
|
} |
854 |
|
|
855 |
|
/** |
856 |
< |
* Returns {@code true} if the current thread is executing as a |
857 |
< |
* ForkJoinPool computation. |
856 |
> |
* Returns {@code true} if the current thread is a {@link |
857 |
> |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation. |
858 |
|
* |
859 |
< |
* @return {@code true} if the current thread is executing as a |
860 |
< |
* ForkJoinPool computation, or false otherwise |
859 |
> |
* @return {@code true} if the current thread is a {@link |
860 |
> |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation, |
861 |
> |
* or {@code false} otherwise |
862 |
|
*/ |
863 |
|
public static boolean inForkJoinPool() { |
864 |
|
return Thread.currentThread() instanceof ForkJoinWorkerThread; |