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 |
135 |
|
*/ |
136 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
137 |
|
|
138 |
+ |
/* |
139 |
+ |
* See the internal documentation of class ForkJoinPool for a |
140 |
+ |
* general implementation overview. ForkJoinTasks are mainly |
141 |
+ |
* responsible for maintaining their "status" field amidst relays |
142 |
+ |
* to methods in ForkJoinWorkerThread and ForkJoinPool. The |
143 |
+ |
* methods of this class are more-or-less layered into (1) basic |
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. |
148 |
+ |
*/ |
149 |
+ |
|
150 |
|
/** |
151 |
|
* Run control status bits packed into a single int to minimize |
152 |
|
* footprint and to ensure atomicity (via CAS). Status is |
153 |
|
* initially zero, and takes on nonnegative values until |
154 |
< |
* completed, upon which status holds COMPLETED. CANCELLED, or |
155 |
< |
* EXCEPTIONAL, which use the top 3 bits. Tasks undergoing |
156 |
< |
* blocking waits by other threads have SIGNAL_MASK bits set -- |
157 |
< |
* bit 15 for external (nonFJ) waits, and the rest a count of |
158 |
< |
* waiting FJ threads. (This representation relies on |
159 |
< |
* ForkJoinPool max thread limits). Completion of a stolen task |
160 |
< |
* with SIGNAL_MASK bits set awakens waiter via notifyAll. Even |
161 |
< |
* though suboptimal for some purposes, we use basic builtin |
162 |
< |
* wait/notify to take advantage of "monitor inflation" in JVMs |
163 |
< |
* that we would otherwise need to emulate to avoid adding further |
156 |
< |
* per-task bookkeeping overhead. Note that bits 16-28 are |
157 |
< |
* currently unused. Also value 0x80000000 is available as spare |
158 |
< |
* completion value. |
154 |
> |
* completed, upon which status holds value COMPLETED. CANCELLED, |
155 |
> |
* or EXCEPTIONAL. Tasks undergoing blocking waits by other |
156 |
> |
* threads have the SIGNAL bit set. Completion of a stolen task |
157 |
> |
* with SIGNAL set awakens any waiters via notifyAll. Even though |
158 |
> |
* suboptimal for some purposes, we use basic builtin wait/notify |
159 |
> |
* to take advantage of "monitor inflation" in JVMs that we would |
160 |
> |
* otherwise need to emulate to avoid adding further per-task |
161 |
> |
* bookkeeping overhead. We want these monitors to be "fat", |
162 |
> |
* i.e., not use biasing or thin-lock techniques, so use some odd |
163 |
> |
* coding idioms that tend to avoid them. |
164 |
|
*/ |
165 |
|
volatile int status; // accessed directly by pool and workers |
166 |
|
|
167 |
< |
static final int COMPLETION_MASK = 0xe0000000; |
168 |
< |
static final int NORMAL = 0xe0000000; // == mask |
169 |
< |
static final int CANCELLED = 0xc0000000; |
170 |
< |
static final int EXCEPTIONAL = 0xa0000000; |
166 |
< |
static final int SIGNAL_MASK = 0x0000ffff; |
167 |
< |
static final int INTERNAL_SIGNAL_MASK = 0x00007fff; |
168 |
< |
static final int EXTERNAL_SIGNAL = 0x00008000; // top bit of low word |
167 |
> |
private static final int NORMAL = -1; |
168 |
> |
private static final int CANCELLED = -2; |
169 |
> |
private static final int EXCEPTIONAL = -3; |
170 |
> |
private static final int SIGNAL = 1; |
171 |
|
|
172 |
|
/** |
173 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
181 |
|
Collections.synchronizedMap |
182 |
|
(new WeakHashMap<ForkJoinTask<?>, Throwable>()); |
183 |
|
|
184 |
< |
// within-package utilities |
183 |
< |
|
184 |
< |
/** |
185 |
< |
* Gets current worker thread, or null if not a worker thread. |
186 |
< |
*/ |
187 |
< |
static ForkJoinWorkerThread getWorker() { |
188 |
< |
Thread t = Thread.currentThread(); |
189 |
< |
return ((t instanceof ForkJoinWorkerThread) ? |
190 |
< |
(ForkJoinWorkerThread) t : null); |
191 |
< |
} |
192 |
< |
|
193 |
< |
final boolean casStatus(int cmp, int val) { |
194 |
< |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
195 |
< |
} |
184 |
> |
// Maintaining completion status |
185 |
|
|
186 |
|
/** |
187 |
< |
* Workaround for not being able to rethrow unchecked exceptions. |
188 |
< |
*/ |
200 |
< |
static void rethrowException(Throwable ex) { |
201 |
< |
if (ex != null) |
202 |
< |
UNSAFE.throwException(ex); |
203 |
< |
} |
204 |
< |
|
205 |
< |
// Setting completion status |
206 |
< |
|
207 |
< |
/** |
208 |
< |
* Marks completion and wakes up threads waiting to join this task. |
187 |
> |
* Marks completion and wakes up threads waiting to join this task, |
188 |
> |
* also clearing signal request bits. |
189 |
|
* |
190 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
191 |
+ |
* @return status on exit |
192 |
|
*/ |
193 |
< |
final void setCompletion(int completion) { |
213 |
< |
ForkJoinPool pool = getPool(); |
214 |
< |
if (pool != null) { |
215 |
< |
int s; // Clear signal bits while setting completion status |
216 |
< |
do {} while ((s = status) >= 0 && !casStatus(s, completion)); |
217 |
< |
|
218 |
< |
if ((s & SIGNAL_MASK) != 0) { |
219 |
< |
if ((s &= INTERNAL_SIGNAL_MASK) != 0) |
220 |
< |
pool.updateRunningCount(s); |
221 |
< |
synchronized (this) { notifyAll(); } |
222 |
< |
} |
223 |
< |
} |
224 |
< |
else |
225 |
< |
externallySetCompletion(completion); |
226 |
< |
} |
227 |
< |
|
228 |
< |
/** |
229 |
< |
* Version of setCompletion for non-FJ threads. Leaves signal |
230 |
< |
* bits for unblocked threads to adjust, and always notifies. |
231 |
< |
*/ |
232 |
< |
private void externallySetCompletion(int completion) { |
193 |
> |
private int setCompletion(int completion) { |
194 |
|
int s; |
195 |
< |
do {} while ((s = status) >= 0 && |
196 |
< |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
197 |
< |
synchronized (this) { notifyAll(); } |
198 |
< |
} |
199 |
< |
|
239 |
< |
/** |
240 |
< |
* Sets status to indicate normal completion. |
241 |
< |
*/ |
242 |
< |
final void setNormalCompletion() { |
243 |
< |
// Try typical fast case -- single CAS, no signal, not already done. |
244 |
< |
// Manually expand casStatus to improve chances of inlining it |
245 |
< |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
246 |
< |
setCompletion(NORMAL); |
247 |
< |
} |
248 |
< |
|
249 |
< |
// internal waiting and notification |
250 |
< |
|
251 |
< |
/** |
252 |
< |
* Performs the actual monitor wait for awaitDone. |
253 |
< |
*/ |
254 |
< |
private void doAwaitDone() { |
255 |
< |
// Minimize lock bias and in/de-flation effects by maximizing |
256 |
< |
// chances of waiting inside sync |
257 |
< |
try { |
258 |
< |
while (status >= 0) |
259 |
< |
synchronized (this) { if (status >= 0) wait(); } |
260 |
< |
} catch (InterruptedException ie) { |
261 |
< |
onInterruptedWait(); |
262 |
< |
} |
263 |
< |
} |
264 |
< |
|
265 |
< |
/** |
266 |
< |
* Performs the actual timed monitor wait for awaitDone. |
267 |
< |
*/ |
268 |
< |
private void doAwaitDone(long startTime, long nanos) { |
269 |
< |
synchronized (this) { |
270 |
< |
try { |
271 |
< |
while (status >= 0) { |
272 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
273 |
< |
if (nt <= 0) |
274 |
< |
break; |
275 |
< |
wait(nt / 1000000, (int) (nt % 1000000)); |
276 |
< |
} |
277 |
< |
} catch (InterruptedException ie) { |
278 |
< |
onInterruptedWait(); |
195 |
> |
while ((s = status) >= 0) { |
196 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
197 |
> |
if (s == SIGNAL) |
198 |
> |
synchronized (this) { notifyAll(); } |
199 |
> |
return completion; |
200 |
|
} |
201 |
|
} |
202 |
+ |
return s; |
203 |
|
} |
204 |
|
|
283 |
– |
// Awaiting completion |
284 |
– |
|
205 |
|
/** |
206 |
< |
* Sets status to indicate there is joiner, then waits for join, |
207 |
< |
* surrounded with pool notifications. |
288 |
< |
* |
289 |
< |
* @return status upon exit |
206 |
> |
* Record exception and set exceptional completion |
207 |
> |
* @return status on exit |
208 |
|
*/ |
209 |
< |
private int awaitDone(ForkJoinWorkerThread w, |
210 |
< |
boolean maintainParallelism) { |
211 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
294 |
< |
int s; |
295 |
< |
while ((s = status) >= 0) { |
296 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
297 |
< |
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
298 |
< |
doAwaitDone(); |
299 |
< |
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
300 |
< |
adjustPoolCountsOnUnblock(pool); |
301 |
< |
break; |
302 |
< |
} |
303 |
< |
} |
304 |
< |
return s; |
209 |
> |
private int setExceptionalCompletion(Throwable rex) { |
210 |
> |
exceptionMap.put(this, rex); |
211 |
> |
return setCompletion(EXCEPTIONAL); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* Timed version of awaitDone |
309 |
< |
* |
310 |
< |
* @return status upon exit |
215 |
> |
* Blocks a worker thread until completion. Called only by pool. |
216 |
|
*/ |
217 |
< |
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
313 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
217 |
> |
final int internalAwaitDone() { |
218 |
|
int s; |
219 |
|
while ((s = status) >= 0) { |
220 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
221 |
< |
long startTime = System.nanoTime(); |
222 |
< |
if (pool == null || !pool.preJoin(this, false)) |
223 |
< |
doAwaitDone(startTime, nanos); |
224 |
< |
if ((s = status) >= 0) { |
225 |
< |
adjustPoolCountsOnCancelledWait(pool); |
226 |
< |
s = status; |
220 |
> |
synchronized(this) { |
221 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
222 |
> |
do { |
223 |
> |
try { |
224 |
> |
wait(); |
225 |
> |
} catch (InterruptedException ie) { |
226 |
> |
cancelIfTerminating(); |
227 |
> |
} |
228 |
> |
} while ((s = status) >= 0); |
229 |
> |
break; |
230 |
|
} |
324 |
– |
if (s < 0 && (s & INTERNAL_SIGNAL_MASK) != 0) |
325 |
– |
adjustPoolCountsOnUnblock(pool); |
326 |
– |
break; |
231 |
|
} |
232 |
|
} |
233 |
|
return s; |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* Notifies pool that thread is unblocked. Called by signalled |
238 |
< |
* threads when woken by non-FJ threads (which is atypical). |
237 |
> |
* Blocks a non-worker-thread until completion. |
238 |
> |
* @return status on exit |
239 |
|
*/ |
240 |
< |
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
240 |
> |
private int externalAwaitDone() { |
241 |
|
int s; |
242 |
< |
do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
243 |
< |
if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0) |
244 |
< |
pool.updateRunningCount(s); |
245 |
< |
} |
246 |
< |
|
247 |
< |
/** |
248 |
< |
* Notifies pool to adjust counts on cancelled or timed out wait. |
249 |
< |
*/ |
250 |
< |
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
251 |
< |
if (pool != null) { |
252 |
< |
int s; |
253 |
< |
while ((s = status) >= 0 && (s & INTERNAL_SIGNAL_MASK) != 0) { |
254 |
< |
if (casStatus(s, s - 1)) { |
351 |
< |
pool.updateRunningCount(1); |
242 |
> |
while ((s = status) >= 0) { |
243 |
> |
synchronized(this) { |
244 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){ |
245 |
> |
boolean interrupted = false; |
246 |
> |
do { |
247 |
> |
try { |
248 |
> |
wait(); |
249 |
> |
} catch (InterruptedException ie) { |
250 |
> |
interrupted = true; |
251 |
> |
} |
252 |
> |
} while ((s = status) >= 0); |
253 |
> |
if (interrupted) |
254 |
> |
Thread.currentThread().interrupt(); |
255 |
|
break; |
256 |
|
} |
257 |
|
} |
258 |
|
} |
259 |
+ |
return s; |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
< |
* Handles interruptions during waits. |
264 |
< |
*/ |
265 |
< |
private void onInterruptedWait() { |
362 |
< |
ForkJoinWorkerThread w = getWorker(); |
363 |
< |
if (w == null) |
364 |
< |
Thread.currentThread().interrupt(); // re-interrupt |
365 |
< |
else if (w.isTerminating()) |
366 |
< |
cancelIgnoringExceptions(); |
367 |
< |
// else if FJworker, ignore interrupt |
368 |
< |
} |
369 |
< |
|
370 |
< |
// Recording and reporting exceptions |
371 |
< |
|
372 |
< |
private void setDoneExceptionally(Throwable rex) { |
373 |
< |
exceptionMap.put(this, rex); |
374 |
< |
setCompletion(EXCEPTIONAL); |
375 |
< |
} |
376 |
< |
|
377 |
< |
/** |
378 |
< |
* Throws the exception associated with status s. |
379 |
< |
* |
380 |
< |
* @throws the exception |
381 |
< |
*/ |
382 |
< |
private void reportException(int s) { |
383 |
< |
if ((s &= COMPLETION_MASK) < NORMAL) { |
384 |
< |
if (s == CANCELLED) |
385 |
< |
throw new CancellationException(); |
386 |
< |
else |
387 |
< |
rethrowException(exceptionMap.get(this)); |
388 |
< |
} |
389 |
< |
} |
390 |
< |
|
391 |
< |
/** |
392 |
< |
* Returns result or throws exception using j.u.c.Future conventions. |
393 |
< |
* Only call when {@code isDone} known to be true or thread known |
394 |
< |
* to be interrupted. |
263 |
> |
* Unless done, calls exec and records status if completed, but |
264 |
> |
* doesn't wait for completion otherwise. Primary execution method |
265 |
> |
* for ForkJoinWorkerThread. |
266 |
|
*/ |
267 |
< |
private V reportFutureResult() |
268 |
< |
throws InterruptedException, ExecutionException { |
269 |
< |
if (Thread.interrupted()) |
270 |
< |
throw new InterruptedException(); |
271 |
< |
int s = status & COMPLETION_MASK; |
272 |
< |
if (s < NORMAL) { |
273 |
< |
Throwable ex; |
403 |
< |
if (s == CANCELLED) |
404 |
< |
throw new CancellationException(); |
405 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
406 |
< |
throw new ExecutionException(ex); |
267 |
> |
final void tryExec() { |
268 |
> |
try { |
269 |
> |
if (status < 0 || !exec()) |
270 |
> |
return; |
271 |
> |
} catch (Throwable rex) { |
272 |
> |
setExceptionalCompletion(rex); |
273 |
> |
return; |
274 |
|
} |
275 |
< |
return getRawResult(); |
409 |
< |
} |
410 |
< |
|
411 |
< |
/** |
412 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
413 |
< |
* with timeouts. |
414 |
< |
*/ |
415 |
< |
private V reportTimedFutureResult() |
416 |
< |
throws InterruptedException, ExecutionException, TimeoutException { |
417 |
< |
if (Thread.interrupted()) |
418 |
< |
throw new InterruptedException(); |
419 |
< |
Throwable ex; |
420 |
< |
int s = status & COMPLETION_MASK; |
421 |
< |
if (s == NORMAL) |
422 |
< |
return getRawResult(); |
423 |
< |
else if (s == CANCELLED) |
424 |
< |
throw new CancellationException(); |
425 |
< |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
426 |
< |
throw new ExecutionException(ex); |
427 |
< |
else |
428 |
< |
throw new TimeoutException(); |
275 |
> |
setCompletion(NORMAL); // must be outside try block |
276 |
|
} |
277 |
|
|
431 |
– |
// internal execution methods |
432 |
– |
|
278 |
|
/** |
279 |
< |
* Calls exec, recording completion, and rethrowing exception if |
280 |
< |
* encountered. Caller should normally check status before calling. |
281 |
< |
* |
437 |
< |
* @return true if completed normally |
279 |
> |
* If not done and this task is next in worker queue, runs it, |
280 |
> |
* else waits for it. |
281 |
> |
* @return status on exit |
282 |
|
*/ |
283 |
< |
private boolean tryExec() { |
284 |
< |
try { // try block must contain only call to exec |
285 |
< |
if (!exec()) |
286 |
< |
return false; |
287 |
< |
} catch (Throwable rex) { |
288 |
< |
setDoneExceptionally(rex); |
289 |
< |
rethrowException(rex); |
290 |
< |
return false; // not reached |
283 |
> |
private int doJoin() { |
284 |
> |
int stat; |
285 |
> |
if ((stat = status) < 0) |
286 |
> |
return stat; |
287 |
> |
Thread t = Thread.currentThread(); |
288 |
> |
ForkJoinWorkerThread w; |
289 |
> |
if (t instanceof ForkJoinWorkerThread) { |
290 |
> |
if ((w = (ForkJoinWorkerThread) t).unpushTask(this)) { |
291 |
> |
boolean completed; |
292 |
> |
try { |
293 |
> |
completed = exec(); |
294 |
> |
} catch (Throwable rex) { |
295 |
> |
return setExceptionalCompletion(rex); |
296 |
> |
} |
297 |
> |
if (completed) |
298 |
> |
return setCompletion(NORMAL); |
299 |
> |
} |
300 |
> |
w.joinTask(this); |
301 |
> |
return status; |
302 |
|
} |
303 |
< |
setNormalCompletion(); |
449 |
< |
return true; |
303 |
> |
return externalAwaitDone(); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* Main execution method used by worker threads. Invokes |
308 |
< |
* base computation unless already complete. |
307 |
> |
* Unless done, calls exec and records status if completed, or |
308 |
> |
* waits for completion otherwise. |
309 |
> |
* @return status on exit |
310 |
|
*/ |
311 |
< |
final void quietlyExec() { |
312 |
< |
if (status >= 0) { |
311 |
> |
private int doInvoke() { |
312 |
> |
int stat; |
313 |
> |
if ((stat = status) >= 0) { |
314 |
> |
boolean completed; |
315 |
|
try { |
316 |
< |
if (!exec()) |
460 |
< |
return; |
316 |
> |
completed = exec(); |
317 |
|
} catch (Throwable rex) { |
318 |
< |
setDoneExceptionally(rex); |
463 |
< |
return; |
318 |
> |
return setExceptionalCompletion(rex); |
319 |
|
} |
320 |
< |
setNormalCompletion(); |
321 |
< |
} |
322 |
< |
} |
323 |
< |
|
469 |
< |
/** |
470 |
< |
* Calls exec(), recording but not rethrowing exception. |
471 |
< |
* Caller should normally check status before calling. |
472 |
< |
* |
473 |
< |
* @return true if completed normally |
474 |
< |
*/ |
475 |
< |
private boolean tryQuietlyInvoke() { |
476 |
< |
try { |
477 |
< |
if (!exec()) |
478 |
< |
return false; |
479 |
< |
} catch (Throwable rex) { |
480 |
< |
setDoneExceptionally(rex); |
481 |
< |
return false; |
482 |
< |
} |
483 |
< |
setNormalCompletion(); |
484 |
< |
return true; |
485 |
< |
} |
486 |
< |
|
487 |
< |
/** |
488 |
< |
* Cancels, ignoring any exceptions it throws. |
489 |
< |
*/ |
490 |
< |
final void cancelIgnoringExceptions() { |
491 |
< |
try { |
492 |
< |
cancel(false); |
493 |
< |
} catch (Throwable ignore) { |
320 |
> |
if (completed) |
321 |
> |
stat = setCompletion(NORMAL); |
322 |
> |
else |
323 |
> |
stat = doJoin(); |
324 |
|
} |
325 |
+ |
return stat; |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* Main implementation of helpJoin |
329 |
> |
* Returns result or throws exception associated with given status. |
330 |
> |
* @param s the status |
331 |
|
*/ |
332 |
< |
private int busyJoin(ForkJoinWorkerThread w) { |
333 |
< |
int s; |
334 |
< |
ForkJoinTask<?> t; |
335 |
< |
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
336 |
< |
t.quietlyExec(); |
505 |
< |
return (s >= 0) ? awaitDone(w, false) : s; // block if no work |
332 |
> |
private V reportResult(int s) { |
333 |
> |
Throwable ex; |
334 |
> |
if (s < NORMAL && (ex = getException()) != null) |
335 |
> |
UNSAFE.throwException(ex); |
336 |
> |
return getRawResult(); |
337 |
|
} |
338 |
|
|
339 |
|
// public methods |
371 |
|
* @return the computed result |
372 |
|
*/ |
373 |
|
public final V join() { |
374 |
< |
ForkJoinWorkerThread w = getWorker(); |
544 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryExec()) |
545 |
< |
reportException(awaitDone(w, true)); |
546 |
< |
return getRawResult(); |
374 |
> |
return reportResult(doJoin()); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
382 |
|
* @return the computed result |
383 |
|
*/ |
384 |
|
public final V invoke() { |
385 |
< |
if (status >= 0 && tryExec()) |
558 |
< |
return getRawResult(); |
559 |
< |
else |
560 |
< |
return join(); |
385 |
> |
return reportResult(doInvoke()); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
440 |
|
} |
441 |
|
else if (i != 0) |
442 |
|
t.fork(); |
443 |
< |
else { |
444 |
< |
t.quietlyInvoke(); |
620 |
< |
if (ex == null) |
621 |
< |
ex = t.getException(); |
622 |
< |
} |
443 |
> |
else if (t.doInvoke() < NORMAL && ex == null) |
444 |
> |
ex = t.getException(); |
445 |
|
} |
446 |
|
for (int i = 1; i <= last; ++i) { |
447 |
|
ForkJoinTask<?> t = tasks[i]; |
448 |
|
if (t != null) { |
449 |
|
if (ex != null) |
450 |
|
t.cancel(false); |
451 |
< |
else { |
452 |
< |
t.quietlyJoin(); |
631 |
< |
if (ex == null) |
632 |
< |
ex = t.getException(); |
633 |
< |
} |
451 |
> |
else if (t.doJoin() < NORMAL && ex == null) |
452 |
> |
ex = t.getException(); |
453 |
|
} |
454 |
|
} |
455 |
|
if (ex != null) |
456 |
< |
rethrowException(ex); |
456 |
> |
UNSAFE.throwException(ex); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
496 |
|
} |
497 |
|
else if (i != 0) |
498 |
|
t.fork(); |
499 |
< |
else { |
500 |
< |
t.quietlyInvoke(); |
682 |
< |
if (ex == null) |
683 |
< |
ex = t.getException(); |
684 |
< |
} |
499 |
> |
else if (t.doInvoke() < NORMAL && ex == null) |
500 |
> |
ex = t.getException(); |
501 |
|
} |
502 |
|
for (int i = 1; i <= last; ++i) { |
503 |
|
ForkJoinTask<?> t = ts.get(i); |
504 |
|
if (t != null) { |
505 |
|
if (ex != null) |
506 |
|
t.cancel(false); |
507 |
< |
else { |
508 |
< |
t.quietlyJoin(); |
693 |
< |
if (ex == null) |
694 |
< |
ex = t.getException(); |
695 |
< |
} |
507 |
> |
else if (t.doJoin() < NORMAL && ex == null) |
508 |
> |
ex = t.getException(); |
509 |
|
} |
510 |
|
} |
511 |
|
if (ex != null) |
512 |
< |
rethrowException(ex); |
512 |
> |
UNSAFE.throwException(ex); |
513 |
|
return tasks; |
514 |
|
} |
515 |
|
|
539 |
|
*/ |
540 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
541 |
|
setCompletion(CANCELLED); |
542 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
542 |
> |
return status == CANCELLED; |
543 |
> |
} |
544 |
> |
|
545 |
> |
/** |
546 |
> |
* Cancels, ignoring any exceptions it throws. Used during worker |
547 |
> |
* and pool shutdown. |
548 |
> |
*/ |
549 |
> |
final void cancelIgnoringExceptions() { |
550 |
> |
try { |
551 |
> |
cancel(false); |
552 |
> |
} catch (Throwable ignore) { |
553 |
> |
} |
554 |
> |
} |
555 |
> |
|
556 |
> |
/** |
557 |
> |
* Cancels ignoring exceptions if worker is terminating |
558 |
> |
*/ |
559 |
> |
private void cancelIfTerminating() { |
560 |
> |
Thread t = Thread.currentThread(); |
561 |
> |
if ((t instanceof ForkJoinWorkerThread) && |
562 |
> |
((ForkJoinWorkerThread) t).isTerminating()) { |
563 |
> |
try { |
564 |
> |
cancel(false); |
565 |
> |
} catch (Throwable ignore) { |
566 |
> |
} |
567 |
> |
} |
568 |
|
} |
569 |
|
|
570 |
|
public final boolean isDone() { |
572 |
|
} |
573 |
|
|
574 |
|
public final boolean isCancelled() { |
575 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
575 |
> |
return status == CANCELLED; |
576 |
|
} |
577 |
|
|
578 |
|
/** |
581 |
|
* @return {@code true} if this task threw an exception or was cancelled |
582 |
|
*/ |
583 |
|
public final boolean isCompletedAbnormally() { |
584 |
< |
return (status & COMPLETION_MASK) < NORMAL; |
584 |
> |
return status < NORMAL; |
585 |
|
} |
586 |
|
|
587 |
|
/** |
592 |
|
* exception and was not cancelled |
593 |
|
*/ |
594 |
|
public final boolean isCompletedNormally() { |
595 |
< |
return (status & COMPLETION_MASK) == NORMAL; |
595 |
> |
return status == NORMAL; |
596 |
|
} |
597 |
|
|
598 |
|
/** |
603 |
|
* @return the exception, or {@code null} if none |
604 |
|
*/ |
605 |
|
public final Throwable getException() { |
606 |
< |
int s = status & COMPLETION_MASK; |
606 |
> |
int s = status; |
607 |
|
return ((s >= NORMAL) ? null : |
608 |
|
(s == CANCELLED) ? new CancellationException() : |
609 |
|
exceptionMap.get(this)); |
624 |
|
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
625 |
|
*/ |
626 |
|
public void completeExceptionally(Throwable ex) { |
627 |
< |
setDoneExceptionally((ex instanceof RuntimeException) || |
628 |
< |
(ex instanceof Error) ? ex : |
629 |
< |
new RuntimeException(ex)); |
627 |
> |
setExceptionalCompletion((ex instanceof RuntimeException) || |
628 |
> |
(ex instanceof Error) ? ex : |
629 |
> |
new RuntimeException(ex)); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
645 |
|
try { |
646 |
|
setRawResult(value); |
647 |
|
} catch (Throwable rex) { |
648 |
< |
setDoneExceptionally(rex); |
648 |
> |
setExceptionalCompletion(rex); |
649 |
|
return; |
650 |
|
} |
651 |
< |
setNormalCompletion(); |
651 |
> |
setCompletion(NORMAL); |
652 |
|
} |
653 |
|
|
654 |
|
public final V get() throws InterruptedException, ExecutionException { |
655 |
< |
ForkJoinWorkerThread w = getWorker(); |
656 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
657 |
< |
awaitDone(w, true); |
658 |
< |
return reportFutureResult(); |
655 |
> |
int s = doJoin(); |
656 |
> |
if (Thread.interrupted()) |
657 |
> |
throw new InterruptedException(); |
658 |
> |
if (s < NORMAL) { |
659 |
> |
Throwable ex; |
660 |
> |
if (s == CANCELLED) |
661 |
> |
throw new CancellationException(); |
662 |
> |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
663 |
> |
throw new ExecutionException(ex); |
664 |
> |
} |
665 |
> |
return getRawResult(); |
666 |
|
} |
667 |
|
|
668 |
|
public final V get(long timeout, TimeUnit unit) |
669 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
670 |
< |
long nanos = unit.toNanos(timeout); |
671 |
< |
ForkJoinWorkerThread w = getWorker(); |
672 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
673 |
< |
awaitDone(w, nanos); |
674 |
< |
return reportTimedFutureResult(); |
675 |
< |
} |
676 |
< |
|
677 |
< |
/** |
678 |
< |
* Possibly executes other tasks until this task {@link #isDone is |
679 |
< |
* done}, then returns the result of the computation. This method |
680 |
< |
* may be more efficient than {@code join}, but is only applicable |
681 |
< |
* when there are no potential dependencies between continuation |
682 |
< |
* of the current task and that of any other task that might be |
683 |
< |
* executed while helping. (This usually holds for pure |
684 |
< |
* divide-and-conquer tasks). |
685 |
< |
* |
686 |
< |
* <p>This method may be invoked only from within {@code |
687 |
< |
* ForkJoinTask} computations (as may be determined using method |
688 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
689 |
< |
* result in exceptions or errors, possibly including {@code |
690 |
< |
* ClassCastException}. |
691 |
< |
* |
692 |
< |
* @return the computed result |
693 |
< |
*/ |
694 |
< |
public final V helpJoin() { |
695 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
696 |
< |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
697 |
< |
reportException(busyJoin(w)); |
698 |
< |
return getRawResult(); |
699 |
< |
} |
700 |
< |
|
701 |
< |
/** |
702 |
< |
* Possibly executes other tasks until this task {@link #isDone is |
703 |
< |
* done}. This method may be useful when processing collections |
704 |
< |
* of tasks when some have been cancelled or otherwise known to |
705 |
< |
* have aborted. |
706 |
< |
* |
707 |
< |
* <p>This method may be invoked only from within {@code |
708 |
< |
* ForkJoinTask} computations (as may be determined using method |
709 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
710 |
< |
* result in exceptions or errors, possibly including {@code |
711 |
< |
* ClassCastException}. |
712 |
< |
*/ |
713 |
< |
public final void quietlyHelpJoin() { |
714 |
< |
if (status >= 0) { |
715 |
< |
ForkJoinWorkerThread w = |
716 |
< |
(ForkJoinWorkerThread) Thread.currentThread(); |
717 |
< |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
718 |
< |
busyJoin(w); |
670 |
> |
Thread t = Thread.currentThread(); |
671 |
> |
ForkJoinPool pool; |
672 |
> |
if (t instanceof ForkJoinWorkerThread) { |
673 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
674 |
> |
if (status >= 0 && w.unpushTask(this)) |
675 |
> |
tryExec(); |
676 |
> |
pool = w.pool; |
677 |
> |
} |
678 |
> |
else |
679 |
> |
pool = null; |
680 |
> |
/* |
681 |
> |
* Timed wait loop intermixes cases for fj (pool != null) and |
682 |
> |
* non FJ threads. For FJ, decrement pool count but don't try |
683 |
> |
* for replacement; increment count on completion. For non-FJ, |
684 |
> |
* deal with interrupts. This is messy, but a little less so |
685 |
> |
* than is splitting the FJ and nonFJ cases. |
686 |
> |
*/ |
687 |
> |
boolean interrupted = false; |
688 |
> |
boolean dec = false; // true if pool count decremented |
689 |
> |
for (;;) { |
690 |
> |
if (Thread.interrupted() && pool == null) { |
691 |
> |
interrupted = true; |
692 |
> |
break; |
693 |
> |
} |
694 |
> |
int s = status; |
695 |
> |
if (s < 0) |
696 |
> |
break; |
697 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
698 |
> |
s, s | SIGNAL)) { |
699 |
> |
long startTime = System.nanoTime(); |
700 |
> |
long nanos = unit.toNanos(timeout); |
701 |
> |
long nt; // wait time |
702 |
> |
while (status >= 0 && |
703 |
> |
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
704 |
> |
if (pool != null && !dec) |
705 |
> |
dec = pool.tryDecrementRunningCount(); |
706 |
> |
else { |
707 |
> |
long ms = nt / 1000000; |
708 |
> |
int ns = (int) (nt % 1000000); |
709 |
> |
try { |
710 |
> |
synchronized(this) { |
711 |
> |
if (status >= 0) |
712 |
> |
wait(ms, ns); |
713 |
> |
} |
714 |
> |
} catch (InterruptedException ie) { |
715 |
> |
if (pool != null) |
716 |
> |
cancelIfTerminating(); |
717 |
> |
else { |
718 |
> |
interrupted = true; |
719 |
> |
break; |
720 |
> |
} |
721 |
> |
} |
722 |
> |
} |
723 |
> |
} |
724 |
> |
break; |
725 |
> |
} |
726 |
|
} |
727 |
+ |
if (pool != null && dec) |
728 |
+ |
pool.incrementRunningCount(); |
729 |
+ |
if (interrupted) |
730 |
+ |
throw new InterruptedException(); |
731 |
+ |
int es = status; |
732 |
+ |
if (es != NORMAL) { |
733 |
+ |
Throwable ex; |
734 |
+ |
if (es == CANCELLED) |
735 |
+ |
throw new CancellationException(); |
736 |
+ |
if (es == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
737 |
+ |
throw new ExecutionException(ex); |
738 |
+ |
throw new TimeoutException(); |
739 |
+ |
} |
740 |
+ |
return getRawResult(); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
747 |
|
* known to have aborted. |
748 |
|
*/ |
749 |
|
public final void quietlyJoin() { |
750 |
< |
if (status >= 0) { |
885 |
< |
ForkJoinWorkerThread w = getWorker(); |
886 |
< |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
887 |
< |
awaitDone(w, true); |
888 |
< |
} |
750 |
> |
doJoin(); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
758 |
|
* known to have aborted. |
759 |
|
*/ |
760 |
|
public final void quietlyInvoke() { |
761 |
< |
if (status >= 0 && !tryQuietlyInvoke()) |
900 |
< |
quietlyJoin(); |
761 |
> |
doInvoke(); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
790 |
|
* pre-constructed trees of subtasks in loops. |
791 |
|
*/ |
792 |
|
public void reinitialize() { |
793 |
< |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
793 |
> |
if (status == EXCEPTIONAL) |
794 |
|
exceptionMap.remove(this); |
795 |
|
status = 0; |
796 |
|
} |
1080 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1081 |
|
|
1082 |
|
/** |
1083 |
< |
* Save the state to a stream. |
1083 |
> |
* Saves the state to a stream. |
1084 |
|
* |
1085 |
|
* @serialData the current run status and the exception thrown |
1086 |
|
* during execution, or {@code null} if none |
1093 |
|
} |
1094 |
|
|
1095 |
|
/** |
1096 |
< |
* Reconstitute the instance from a stream. |
1096 |
> |
* Reconstitutes the instance from a stream. |
1097 |
|
* |
1098 |
|
* @param s the stream |
1099 |
|
*/ |
1100 |
|
private void readObject(java.io.ObjectInputStream s) |
1101 |
|
throws java.io.IOException, ClassNotFoundException { |
1102 |
|
s.defaultReadObject(); |
1242 |
– |
status &= ~INTERNAL_SIGNAL_MASK; // clear internal signal counts |
1243 |
– |
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1103 |
|
Object ex = s.readObject(); |
1104 |
|
if (ex != null) |
1105 |
< |
setDoneExceptionally((Throwable) ex); |
1105 |
> |
setExceptionalCompletion((Throwable) ex); |
1106 |
> |
if (status < 0) |
1107 |
> |
synchronized (this) { notifyAll(); } |
1108 |
|
} |
1109 |
|
|
1110 |
|
// Unsafe mechanics |