64 |
|
* results of a task is {@link #join}, but there are several variants: |
65 |
|
* The {@link Future#get} methods support interruptible and/or timed |
66 |
|
* waits for completion and report results using {@code Future} |
67 |
< |
* conventions. Method {@link #helpJoin} enables callers to actively |
68 |
< |
* execute other tasks while awaiting joins, which is sometimes more |
69 |
< |
* efficient but only applies when all subtasks are known to be |
70 |
< |
* strictly tree-structured. Method {@link #invoke} is semantically |
67 |
> |
* conventions. Method {@link #invoke} is semantically |
68 |
|
* equivalent to {@code fork(); join()} but always attempts to begin |
69 |
|
* execution in the current thread. The "<em>quiet</em>" forms of |
70 |
|
* these methods do not extract results or report exceptions. These |
122 |
|
* |
123 |
|
* <p>This class provides {@code adapt} methods for {@link Runnable} |
124 |
|
* and {@link Callable}, that may be of use when mixing execution of |
125 |
< |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks |
126 |
< |
* are of this form, consider using a pool in |
130 |
< |
* {@linkplain ForkJoinPool#setAsyncMode async mode}. |
125 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks are |
126 |
> |
* of this form, consider using a pool constructed in <em>asyncMode</em>. |
127 |
|
* |
128 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
129 |
|
* used in extensions such as remote execution frameworks. It is |
144 |
|
* status maintenance (2) execution and awaiting completion (3) |
145 |
|
* user-level methods that additionally report results. This is |
146 |
|
* sometimes hard to see because this file orders exported methods |
147 |
< |
* in a way that flows well in javadocs. |
147 |
> |
* in a way that flows well in javadocs. In particular, most |
148 |
> |
* join mechanics are in method quietlyJoin, below. |
149 |
|
*/ |
150 |
|
|
151 |
< |
/** |
152 |
< |
* Run control status bits packed into a single int to minimize |
153 |
< |
* footprint and to ensure atomicity (via CAS). Status is |
154 |
< |
* initially zero, and takes on nonnegative values until |
155 |
< |
* completed, upon which status holds COMPLETED. CANCELLED, or |
156 |
< |
* EXCEPTIONAL, which use the top 3 bits. Tasks undergoing |
157 |
< |
* blocking waits by other threads have SIGNAL_MASK bits set -- |
158 |
< |
* bit 15 for external (nonFJ) waits, and the rest a count of |
159 |
< |
* waiting FJ threads. (This representation relies on |
160 |
< |
* ForkJoinPool max thread limits). Signal counts are not directly |
161 |
< |
* incremented by ForkJoinTask methods, but instead via a call to |
162 |
< |
* requestSignal within ForkJoinPool.preJoin, once their need is |
163 |
< |
* established. |
164 |
< |
* |
165 |
< |
* Completion of a stolen task with SIGNAL_MASK bits set awakens |
169 |
< |
* any waiters via notifyAll. Even though suboptimal for some |
170 |
< |
* purposes, we use basic builtin wait/notify to take advantage of |
171 |
< |
* "monitor inflation" in JVMs that we would otherwise need to |
172 |
< |
* emulate to avoid adding further per-task bookkeeping overhead. |
173 |
< |
* We want these monitors to be "fat", i.e., not use biasing or |
174 |
< |
* thin-lock techniques, so use some odd coding idioms that tend |
175 |
< |
* to avoid them. |
176 |
< |
* |
177 |
< |
* Note that bits 16-28 are currently unused. Also value |
178 |
< |
* 0x80000000 is available as spare completion value. |
151 |
> |
/* |
152 |
> |
* The status field holds run control status bits packed into a |
153 |
> |
* single int to minimize footprint and to ensure atomicity (via |
154 |
> |
* CAS). Status is initially zero, and takes on nonnegative |
155 |
> |
* values until completed, upon which status holds value |
156 |
> |
* COMPLETED. CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
157 |
> |
* waits by other threads have the SIGNAL bit set. Completion of |
158 |
> |
* a stolen task with SIGNAL set awakens any waiters via |
159 |
> |
* notifyAll. Even though suboptimal for some purposes, we use |
160 |
> |
* basic builtin wait/notify to take advantage of "monitor |
161 |
> |
* inflation" in JVMs that we would otherwise need to emulate to |
162 |
> |
* avoid adding further per-task bookkeeping overhead. We want |
163 |
> |
* these monitors to be "fat", i.e., not use biasing or thin-lock |
164 |
> |
* techniques, so use some odd coding idioms that tend to avoid |
165 |
> |
* them. |
166 |
|
*/ |
167 |
+ |
|
168 |
+ |
/** The run status of this task */ |
169 |
|
volatile int status; // accessed directly by pool and workers |
170 |
|
|
171 |
< |
private static final int COMPLETION_MASK = 0xe0000000; |
172 |
< |
private static final int NORMAL = 0xe0000000; // == mask |
173 |
< |
private static final int CANCELLED = 0xc0000000; |
174 |
< |
private static final int EXCEPTIONAL = 0xa0000000; |
186 |
< |
private static final int SIGNAL_MASK = 0x0000ffff; |
187 |
< |
private static final int INTERNAL_SIGNAL_MASK = 0x00007fff; |
188 |
< |
private static final int EXTERNAL_SIGNAL = 0x00008000; |
171 |
> |
private static final int NORMAL = -1; |
172 |
> |
private static final int CANCELLED = -2; |
173 |
> |
private static final int EXCEPTIONAL = -3; |
174 |
> |
private static final int SIGNAL = 1; |
175 |
|
|
176 |
|
/** |
177 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
197 |
|
int s; |
198 |
|
while ((s = status) >= 0) { |
199 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
200 |
< |
if ((s & SIGNAL_MASK) != 0) { |
215 |
< |
Thread t = Thread.currentThread(); |
216 |
< |
if (t instanceof ForkJoinWorkerThread) |
217 |
< |
((ForkJoinWorkerThread) t).pool.updateRunningCount |
218 |
< |
(s & INTERNAL_SIGNAL_MASK); |
200 |
> |
if (s != 0) |
201 |
|
synchronized (this) { notifyAll(); } |
202 |
< |
} |
221 |
< |
return; |
202 |
> |
break; |
203 |
|
} |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* Record exception and set exceptional completion |
209 |
+ |
* @return status on exit |
210 |
|
*/ |
211 |
< |
private void setDoneExceptionally(Throwable rex) { |
211 |
> |
private void setExceptionalCompletion(Throwable rex) { |
212 |
|
exceptionMap.put(this, rex); |
213 |
|
setCompletion(EXCEPTIONAL); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* Main internal execution method: Unless done, calls exec and |
218 |
< |
* records completion. |
219 |
< |
* |
238 |
< |
* @return true if ran and completed normally |
217 |
> |
* Blocks a worker thread until completion. Called only by |
218 |
> |
* pool. Currently unused -- pool-based waits use timeout |
219 |
> |
* version below. |
220 |
|
*/ |
221 |
< |
final boolean tryExec() { |
222 |
< |
try { |
223 |
< |
if (status < 0 || !exec()) |
224 |
< |
return false; |
225 |
< |
} catch (Throwable rex) { |
226 |
< |
setDoneExceptionally(rex); |
227 |
< |
return false; |
221 |
> |
final void internalAwaitDone() { |
222 |
> |
int s; // the odd construction reduces lock bias effects |
223 |
> |
while ((s = status) >= 0) { |
224 |
> |
try { |
225 |
> |
synchronized(this) { |
226 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
227 |
> |
wait(); |
228 |
> |
} |
229 |
> |
} catch (InterruptedException ie) { |
230 |
> |
cancelIfTerminating(); |
231 |
> |
} |
232 |
|
} |
248 |
– |
setCompletion(NORMAL); // must be outside try block |
249 |
– |
return true; |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* Increments internal signal count (thus requesting signal upon |
237 |
< |
* completion) unless already done. Call only once per join. |
255 |
< |
* Used by ForkJoinPool.preJoin. |
236 |
> |
* Blocks a worker thread until completed or timed out. Called |
237 |
> |
* only by pool. |
238 |
|
* |
239 |
< |
* @return status |
239 |
> |
* @return status on exit |
240 |
|
*/ |
241 |
< |
final int requestSignal() { |
241 |
> |
final int internalAwaitDone(long millis) { |
242 |
|
int s; |
243 |
< |
do {} while ((s = status) >= 0 && |
244 |
< |
!UNSAFE.compareAndSwapInt(this, statusOffset, s, s + 1)); |
245 |
< |
return s; |
246 |
< |
} |
247 |
< |
|
266 |
< |
/** |
267 |
< |
* Sets external signal request unless already done. |
268 |
< |
* |
269 |
< |
* @return status |
270 |
< |
*/ |
271 |
< |
private int requestExternalSignal() { |
272 |
< |
int s; |
273 |
< |
do {} while ((s = status) >= 0 && |
274 |
< |
!UNSAFE.compareAndSwapInt(this, statusOffset, |
275 |
< |
s, s | EXTERNAL_SIGNAL)); |
276 |
< |
return s; |
277 |
< |
} |
278 |
< |
|
279 |
< |
/* |
280 |
< |
* Awaiting completion. The four versions, internal vs external X |
281 |
< |
* untimed vs timed, have the same overall structure but differ |
282 |
< |
* from each other enough to defy simple integration. |
283 |
< |
*/ |
284 |
< |
|
285 |
< |
/** |
286 |
< |
* Blocks a worker until this task is done, also maintaining pool |
287 |
< |
* and signal counts |
288 |
< |
*/ |
289 |
< |
private void awaitDone(ForkJoinWorkerThread w) { |
290 |
< |
if (status >= 0) { |
291 |
< |
w.pool.preJoin(this); |
292 |
< |
while (status >= 0) { |
293 |
< |
try { // minimize lock scope |
294 |
< |
synchronized(this) { |
295 |
< |
if (status >= 0) |
296 |
< |
wait(); |
297 |
< |
else { // help release; also helps avoid lock-biasing |
298 |
< |
notifyAll(); |
299 |
< |
break; |
300 |
< |
} |
301 |
< |
} |
302 |
< |
} catch (InterruptedException ie) { |
303 |
< |
cancelIfTerminating(); |
243 |
> |
if ((s = status) >= 0) { |
244 |
> |
try { |
245 |
> |
synchronized(this) { |
246 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
247 |
> |
wait(millis, 0); |
248 |
|
} |
249 |
+ |
} catch (InterruptedException ie) { |
250 |
+ |
cancelIfTerminating(); |
251 |
|
} |
252 |
+ |
s = status; |
253 |
|
} |
254 |
+ |
return s; |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* Blocks a non-ForkJoin thread until this task is done. |
258 |
> |
* Blocks a non-worker-thread until completion. |
259 |
|
*/ |
260 |
|
private void externalAwaitDone() { |
261 |
< |
if (requestExternalSignal() >= 0) { |
262 |
< |
boolean interrupted = false; |
263 |
< |
while (status >= 0) { |
264 |
< |
try { |
265 |
< |
synchronized(this) { |
266 |
< |
if (status >= 0) |
267 |
< |
wait(); |
268 |
< |
else { |
269 |
< |
notifyAll(); |
270 |
< |
break; |
323 |
< |
} |
324 |
< |
} |
325 |
< |
} catch (InterruptedException ie) { |
326 |
< |
interrupted = true; |
327 |
< |
} |
328 |
< |
} |
329 |
< |
if (interrupted) |
330 |
< |
Thread.currentThread().interrupt(); |
331 |
< |
} |
332 |
< |
} |
333 |
< |
|
334 |
< |
/** |
335 |
< |
* Blocks a worker until this task is done or timeout elapses |
336 |
< |
*/ |
337 |
< |
private void timedAwaitDone(ForkJoinWorkerThread w, long nanos) { |
338 |
< |
if (status >= 0) { |
339 |
< |
long startTime = System.nanoTime(); |
340 |
< |
ForkJoinPool pool = w.pool; |
341 |
< |
pool.preJoin(this); |
342 |
< |
while (status >= 0) { |
343 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
344 |
< |
if (nt > 0) { |
345 |
< |
long ms = nt / 1000000; |
346 |
< |
int ns = (int) (nt % 1000000); |
347 |
< |
try { |
348 |
< |
synchronized(this) { if (status >= 0) wait(ms, ns); } |
349 |
< |
} catch (InterruptedException ie) { |
350 |
< |
cancelIfTerminating(); |
351 |
< |
} |
352 |
< |
} |
353 |
< |
else { |
354 |
< |
int s; // adjust running count on timeout |
355 |
< |
while ((s = status) >= 0 && |
356 |
< |
(s & INTERNAL_SIGNAL_MASK) != 0) { |
357 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
358 |
< |
s, s - 1)) { |
359 |
< |
pool.updateRunningCount(1); |
360 |
< |
break; |
261 |
> |
int s; |
262 |
> |
while ((s = status) >= 0) { |
263 |
> |
synchronized(this) { |
264 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){ |
265 |
> |
boolean interrupted = false; |
266 |
> |
while (status >= 0) { |
267 |
> |
try { |
268 |
> |
wait(); |
269 |
> |
} catch (InterruptedException ie) { |
270 |
> |
interrupted = true; |
271 |
|
} |
272 |
|
} |
273 |
+ |
if (interrupted) |
274 |
+ |
Thread.currentThread().interrupt(); |
275 |
|
break; |
276 |
|
} |
277 |
|
} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* Blocks a non-ForkJoin thread until this task is done or timeout elapses |
283 |
< |
*/ |
284 |
< |
private void externalTimedAwaitDone(long nanos) { |
373 |
< |
if (requestExternalSignal() >= 0) { |
374 |
< |
long startTime = System.nanoTime(); |
375 |
< |
boolean interrupted = false; |
376 |
< |
while (status >= 0) { |
377 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
378 |
< |
if (nt <= 0) |
379 |
< |
break; |
380 |
< |
long ms = nt / 1000000; |
381 |
< |
int ns = (int) (nt % 1000000); |
382 |
< |
try { |
383 |
< |
synchronized(this) { if (status >= 0) wait(ms, ns); } |
384 |
< |
} catch (InterruptedException ie) { |
385 |
< |
interrupted = true; |
386 |
< |
} |
387 |
< |
} |
388 |
< |
if (interrupted) |
389 |
< |
Thread.currentThread().interrupt(); |
390 |
< |
} |
391 |
< |
} |
392 |
< |
|
393 |
< |
// reporting results |
394 |
< |
|
395 |
< |
/** |
396 |
< |
* Returns result or throws the exception associated with status. |
397 |
< |
* Uses Unsafe as a workaround for javac not allowing rethrow of |
398 |
< |
* unchecked exceptions. |
282 |
> |
* Unless done, calls exec and records status if completed, but |
283 |
> |
* doesn't wait for completion otherwise. Primary execution method |
284 |
> |
* for ForkJoinWorkerThread. |
285 |
|
*/ |
286 |
< |
private V reportResult() { |
287 |
< |
if ((status & COMPLETION_MASK) < NORMAL) { |
288 |
< |
Throwable ex = getException(); |
289 |
< |
if (ex != null) |
290 |
< |
UNSAFE.throwException(ex); |
291 |
< |
} |
292 |
< |
return getRawResult(); |
407 |
< |
} |
408 |
< |
|
409 |
< |
/** |
410 |
< |
* Returns result or throws exception using j.u.c.Future conventions. |
411 |
< |
* Only call when {@code isDone} known to be true or thread known |
412 |
< |
* to be interrupted. |
413 |
< |
*/ |
414 |
< |
private V reportFutureResult() |
415 |
< |
throws InterruptedException, ExecutionException { |
416 |
< |
if (Thread.interrupted()) |
417 |
< |
throw new InterruptedException(); |
418 |
< |
int s = status & COMPLETION_MASK; |
419 |
< |
if (s < NORMAL) { |
420 |
< |
Throwable ex; |
421 |
< |
if (s == CANCELLED) |
422 |
< |
throw new CancellationException(); |
423 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
424 |
< |
throw new ExecutionException(ex); |
286 |
> |
final void quietlyExec() { |
287 |
> |
try { |
288 |
> |
if (status < 0 || !exec()) |
289 |
> |
return; |
290 |
> |
} catch (Throwable rex) { |
291 |
> |
setExceptionalCompletion(rex); |
292 |
> |
return; |
293 |
|
} |
294 |
< |
return getRawResult(); |
427 |
< |
} |
428 |
< |
|
429 |
< |
/** |
430 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
431 |
< |
* with timeouts. |
432 |
< |
*/ |
433 |
< |
private V reportTimedFutureResult() |
434 |
< |
throws InterruptedException, ExecutionException, TimeoutException { |
435 |
< |
if (Thread.interrupted()) |
436 |
< |
throw new InterruptedException(); |
437 |
< |
Throwable ex; |
438 |
< |
int s = status & COMPLETION_MASK; |
439 |
< |
if (s == NORMAL) |
440 |
< |
return getRawResult(); |
441 |
< |
else if (s == CANCELLED) |
442 |
< |
throw new CancellationException(); |
443 |
< |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
444 |
< |
throw new ExecutionException(ex); |
445 |
< |
else |
446 |
< |
throw new TimeoutException(); |
294 |
> |
setCompletion(NORMAL); // must be outside try block |
295 |
|
} |
296 |
|
|
297 |
|
// public methods |
330 |
|
*/ |
331 |
|
public final V join() { |
332 |
|
quietlyJoin(); |
333 |
< |
return reportResult(); |
333 |
> |
Throwable ex; |
334 |
> |
if (status < NORMAL && (ex = getException()) != null) |
335 |
> |
UNSAFE.throwException(ex); |
336 |
> |
return getRawResult(); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
344 |
|
* @return the computed result |
345 |
|
*/ |
346 |
|
public final V invoke() { |
347 |
< |
if (!tryExec()) |
348 |
< |
quietlyJoin(); |
349 |
< |
return reportResult(); |
347 |
> |
quietlyInvoke(); |
348 |
> |
Throwable ex; |
349 |
> |
if (status < NORMAL && (ex = getException()) != null) |
350 |
> |
UNSAFE.throwException(ex); |
351 |
> |
return getRawResult(); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
408 |
|
t.fork(); |
409 |
|
else { |
410 |
|
t.quietlyInvoke(); |
411 |
< |
if (ex == null) |
411 |
> |
if (ex == null && t.status < NORMAL) |
412 |
|
ex = t.getException(); |
413 |
|
} |
414 |
|
} |
419 |
|
t.cancel(false); |
420 |
|
else { |
421 |
|
t.quietlyJoin(); |
422 |
< |
if (ex == null) |
422 |
> |
if (ex == null && t.status < NORMAL) |
423 |
|
ex = t.getException(); |
424 |
|
} |
425 |
|
} |
470 |
|
t.fork(); |
471 |
|
else { |
472 |
|
t.quietlyInvoke(); |
473 |
< |
if (ex == null) |
473 |
> |
if (ex == null && t.status < NORMAL) |
474 |
|
ex = t.getException(); |
475 |
|
} |
476 |
|
} |
481 |
|
t.cancel(false); |
482 |
|
else { |
483 |
|
t.quietlyJoin(); |
484 |
< |
if (ex == null) |
484 |
> |
if (ex == null && t.status < NORMAL) |
485 |
|
ex = t.getException(); |
486 |
|
} |
487 |
|
} |
517 |
|
*/ |
518 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
519 |
|
setCompletion(CANCELLED); |
520 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
520 |
> |
return status == CANCELLED; |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
< |
* Cancels, ignoring any exceptions it throws. Used during worker |
525 |
< |
* and pool shutdown. |
524 |
> |
* Cancels, ignoring any exceptions thrown by cancel. Used during |
525 |
> |
* worker and pool shutdown. Cancel is spec'ed not to throw any |
526 |
> |
* exceptions, but if it does anyway, we have no recourse during |
527 |
> |
* shutdown, so guard against this case. |
528 |
|
*/ |
529 |
|
final void cancelIgnoringExceptions() { |
530 |
|
try { |
536 |
|
/** |
537 |
|
* Cancels ignoring exceptions if worker is terminating |
538 |
|
*/ |
539 |
< |
private void cancelIfTerminating() { |
539 |
> |
final void cancelIfTerminating() { |
540 |
|
Thread t = Thread.currentThread(); |
541 |
|
if ((t instanceof ForkJoinWorkerThread) && |
542 |
< |
((ForkJoinWorkerThread) t).isTerminating()) { |
542 |
> |
((ForkJoinWorkerThread) t).isTerminating()) { |
543 |
|
try { |
544 |
|
cancel(false); |
545 |
|
} catch (Throwable ignore) { |
552 |
|
} |
553 |
|
|
554 |
|
public final boolean isCancelled() { |
555 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
555 |
> |
return status == CANCELLED; |
556 |
|
} |
557 |
|
|
558 |
|
/** |
561 |
|
* @return {@code true} if this task threw an exception or was cancelled |
562 |
|
*/ |
563 |
|
public final boolean isCompletedAbnormally() { |
564 |
< |
return (status & COMPLETION_MASK) < NORMAL; |
564 |
> |
return status < NORMAL; |
565 |
|
} |
566 |
|
|
567 |
|
/** |
572 |
|
* exception and was not cancelled |
573 |
|
*/ |
574 |
|
public final boolean isCompletedNormally() { |
575 |
< |
return (status & COMPLETION_MASK) == NORMAL; |
575 |
> |
return status == NORMAL; |
576 |
|
} |
577 |
|
|
578 |
|
/** |
583 |
|
* @return the exception, or {@code null} if none |
584 |
|
*/ |
585 |
|
public final Throwable getException() { |
586 |
< |
int s = status & COMPLETION_MASK; |
586 |
> |
int s = status; |
587 |
|
return ((s >= NORMAL) ? null : |
588 |
|
(s == CANCELLED) ? new CancellationException() : |
589 |
|
exceptionMap.get(this)); |
604 |
|
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
605 |
|
*/ |
606 |
|
public void completeExceptionally(Throwable ex) { |
607 |
< |
setDoneExceptionally((ex instanceof RuntimeException) || |
608 |
< |
(ex instanceof Error) ? ex : |
609 |
< |
new RuntimeException(ex)); |
607 |
> |
setExceptionalCompletion((ex instanceof RuntimeException) || |
608 |
> |
(ex instanceof Error) ? ex : |
609 |
> |
new RuntimeException(ex)); |
610 |
|
} |
611 |
|
|
612 |
|
/** |
625 |
|
try { |
626 |
|
setRawResult(value); |
627 |
|
} catch (Throwable rex) { |
628 |
< |
setDoneExceptionally(rex); |
628 |
> |
setExceptionalCompletion(rex); |
629 |
|
return; |
630 |
|
} |
631 |
|
setCompletion(NORMAL); |
633 |
|
|
634 |
|
public final V get() throws InterruptedException, ExecutionException { |
635 |
|
quietlyJoin(); |
636 |
< |
return reportFutureResult(); |
636 |
> |
if (Thread.interrupted()) |
637 |
> |
throw new InterruptedException(); |
638 |
> |
int s = status; |
639 |
> |
if (s < NORMAL) { |
640 |
> |
Throwable ex; |
641 |
> |
if (s == CANCELLED) |
642 |
> |
throw new CancellationException(); |
643 |
> |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
644 |
> |
throw new ExecutionException(ex); |
645 |
> |
} |
646 |
> |
return getRawResult(); |
647 |
|
} |
648 |
< |
|
648 |
> |
|
649 |
|
public final V get(long timeout, TimeUnit unit) |
650 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
786 |
– |
long nanos = unit.toNanos(timeout); |
651 |
|
Thread t = Thread.currentThread(); |
652 |
+ |
ForkJoinPool pool; |
653 |
|
if (t instanceof ForkJoinWorkerThread) { |
654 |
|
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
655 |
< |
if (!w.unpushTask(this) || !tryExec()) |
656 |
< |
timedAwaitDone(w, nanos); |
655 |
> |
if (status >= 0 && w.unpushTask(this)) |
656 |
> |
quietlyExec(); |
657 |
> |
pool = w.pool; |
658 |
|
} |
659 |
|
else |
660 |
< |
externalTimedAwaitDone(nanos); |
661 |
< |
return reportTimedFutureResult(); |
662 |
< |
} |
663 |
< |
|
664 |
< |
/** |
665 |
< |
* Possibly executes other tasks until this task {@link #isDone is |
666 |
< |
* done}, then returns the result of the computation. This method |
667 |
< |
* may be more efficient than {@code join}, but is only applicable |
668 |
< |
* when there are no potential dependencies between continuation |
669 |
< |
* of the current task and that of any other task that might be |
670 |
< |
* executed while helping. (This usually holds for pure |
671 |
< |
* divide-and-conquer tasks). |
672 |
< |
* |
673 |
< |
* <p>This method may be invoked only from within {@code |
674 |
< |
* ForkJoinTask} computations (as may be determined using method |
675 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
676 |
< |
* result in exceptions or errors, possibly including {@code |
677 |
< |
* ClassCastException}. |
678 |
< |
* |
679 |
< |
* @return the computed result |
680 |
< |
*/ |
681 |
< |
public final V helpJoin() { |
682 |
< |
quietlyHelpJoin(); |
683 |
< |
return reportResult(); |
684 |
< |
} |
685 |
< |
|
686 |
< |
/** |
687 |
< |
* Possibly executes other tasks until this task {@link #isDone is |
688 |
< |
* done}. This method may be useful when processing collections |
689 |
< |
* of tasks when some have been cancelled or otherwise known to |
690 |
< |
* have aborted. |
691 |
< |
* |
692 |
< |
* <p>This method may be invoked only from within {@code |
693 |
< |
* ForkJoinTask} computations (as may be determined using method |
694 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
695 |
< |
* result in exceptions or errors, possibly including {@code |
696 |
< |
* ClassCastException}. |
697 |
< |
*/ |
698 |
< |
public final void quietlyHelpJoin() { |
699 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
700 |
< |
if (!w.unpushTask(this) || !tryExec()) { |
701 |
< |
while (status >= 0) { |
702 |
< |
ForkJoinTask<?> t = w.scanWhileJoining(this); |
837 |
< |
if (t == null) { |
838 |
< |
if (status >= 0) |
839 |
< |
awaitDone(w); |
840 |
< |
break; |
660 |
> |
pool = null; |
661 |
> |
/* |
662 |
> |
* Timed wait loop intermixes cases for FJ (pool != null) and |
663 |
> |
* non FJ threads. For FJ, decrement pool count but don't try |
664 |
> |
* for replacement; increment count on completion. For non-FJ, |
665 |
> |
* deal with interrupts. This is messy, but a little less so |
666 |
> |
* than is splitting the FJ and nonFJ cases. |
667 |
> |
*/ |
668 |
> |
boolean interrupted = false; |
669 |
> |
boolean dec = false; // true if pool count decremented |
670 |
> |
long nanos = unit.toNanos(timeout); |
671 |
> |
for (;;) { |
672 |
> |
if (Thread.interrupted() && pool == null) { |
673 |
> |
interrupted = true; |
674 |
> |
break; |
675 |
> |
} |
676 |
> |
int s = status; |
677 |
> |
if (s < 0) |
678 |
> |
break; |
679 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
680 |
> |
long startTime = System.nanoTime(); |
681 |
> |
long nt; // wait time |
682 |
> |
while (status >= 0 && |
683 |
> |
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
684 |
> |
if (pool != null && !dec) |
685 |
> |
dec = pool.tryDecrementRunningCount(); |
686 |
> |
else { |
687 |
> |
long ms = nt / 1000000; |
688 |
> |
int ns = (int) (nt % 1000000); |
689 |
> |
try { |
690 |
> |
synchronized(this) { |
691 |
> |
if (status >= 0) |
692 |
> |
wait(ms, ns); |
693 |
> |
} |
694 |
> |
} catch (InterruptedException ie) { |
695 |
> |
if (pool != null) |
696 |
> |
cancelIfTerminating(); |
697 |
> |
else { |
698 |
> |
interrupted = true; |
699 |
> |
break; |
700 |
> |
} |
701 |
> |
} |
702 |
> |
} |
703 |
|
} |
704 |
< |
t.tryExec(); |
704 |
> |
break; |
705 |
|
} |
706 |
|
} |
707 |
+ |
if (pool != null && dec) |
708 |
+ |
pool.incrementRunningCount(); |
709 |
+ |
if (interrupted) |
710 |
+ |
throw new InterruptedException(); |
711 |
+ |
int es = status; |
712 |
+ |
if (es != NORMAL) { |
713 |
+ |
Throwable ex; |
714 |
+ |
if (es == CANCELLED) |
715 |
+ |
throw new CancellationException(); |
716 |
+ |
if (es == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
717 |
+ |
throw new ExecutionException(ex); |
718 |
+ |
throw new TimeoutException(); |
719 |
+ |
} |
720 |
+ |
return getRawResult(); |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* Joins this task, without returning its result or throwing an |
724 |
> |
* Joins this task, without returning its result or throwing its |
725 |
|
* exception. This method may be useful when processing |
726 |
|
* collections of tasks when some have been cancelled or otherwise |
727 |
|
* known to have aborted. |
728 |
|
*/ |
729 |
|
public final void quietlyJoin() { |
730 |
< |
Thread t = Thread.currentThread(); |
731 |
< |
if (t instanceof ForkJoinWorkerThread) { |
730 |
> |
Thread t; |
731 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
732 |
|
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
733 |
< |
if (!w.unpushTask(this) || !tryExec()) |
734 |
< |
awaitDone(w); |
733 |
> |
if (status >= 0) { |
734 |
> |
if (w.unpushTask(this)) { |
735 |
> |
boolean completed; |
736 |
> |
try { |
737 |
> |
completed = exec(); |
738 |
> |
} catch (Throwable rex) { |
739 |
> |
setExceptionalCompletion(rex); |
740 |
> |
return; |
741 |
> |
} |
742 |
> |
if (completed) { |
743 |
> |
setCompletion(NORMAL); |
744 |
> |
return; |
745 |
> |
} |
746 |
> |
} |
747 |
> |
w.joinTask(this); |
748 |
> |
} |
749 |
|
} |
750 |
|
else |
751 |
|
externalAwaitDone(); |
753 |
|
|
754 |
|
/** |
755 |
|
* Commences performing this task and awaits its completion if |
756 |
< |
* necessary, without returning its result or throwing an |
756 |
> |
* necessary, without returning its result or throwing its |
757 |
|
* exception. This method may be useful when processing |
758 |
|
* collections of tasks when some have been cancelled or otherwise |
759 |
|
* known to have aborted. |
760 |
|
*/ |
761 |
|
public final void quietlyInvoke() { |
762 |
< |
if (!tryExec()) |
763 |
< |
quietlyJoin(); |
762 |
> |
if (status >= 0) { |
763 |
> |
boolean completed; |
764 |
> |
try { |
765 |
> |
completed = exec(); |
766 |
> |
} catch (Throwable rex) { |
767 |
> |
setExceptionalCompletion(rex); |
768 |
> |
return; |
769 |
> |
} |
770 |
> |
if (completed) |
771 |
> |
setCompletion(NORMAL); |
772 |
> |
else |
773 |
> |
quietlyJoin(); |
774 |
> |
} |
775 |
|
} |
776 |
|
|
777 |
|
/** |
803 |
|
* pre-constructed trees of subtasks in loops. |
804 |
|
*/ |
805 |
|
public void reinitialize() { |
806 |
< |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
806 |
> |
if (status == EXCEPTIONAL) |
807 |
|
exceptionMap.remove(this); |
808 |
|
status = 0; |
809 |
|
} |
1113 |
|
private void readObject(java.io.ObjectInputStream s) |
1114 |
|
throws java.io.IOException, ClassNotFoundException { |
1115 |
|
s.defaultReadObject(); |
1215 |
– |
status &= ~INTERNAL_SIGNAL_MASK; // clear internal signal counts |
1216 |
– |
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1116 |
|
Object ex = s.readObject(); |
1117 |
|
if (ex != null) |
1118 |
< |
setDoneExceptionally((Throwable) ex); |
1118 |
> |
setExceptionalCompletion((Throwable) ex); |
1119 |
|
} |
1120 |
|
|
1121 |
|
// Unsafe mechanics |