56 |
|
* exceptions such as {@code IOExceptions} to be thrown. However, |
57 |
|
* computations may still encounter unchecked exceptions, that are |
58 |
|
* rethrown to callers attempting to join them. These exceptions may |
59 |
< |
* additionally include RejectedExecutionExceptions stemming from |
60 |
< |
* internal resource exhaustion such as failure to allocate internal |
61 |
< |
* task queues. |
59 |
> |
* additionally include {@link RejectedExecutionException} stemming |
60 |
> |
* from internal resource exhaustion, such as failure to allocate |
61 |
> |
* internal task queues. |
62 |
|
* |
63 |
|
* <p>The primary method for awaiting completion and extracting |
64 |
|
* results of a task is {@link #join}, but there are several variants: |
80 |
|
* <p>The execution status of tasks may be queried at several levels |
81 |
|
* of detail: {@link #isDone} is true if a task completed in any way |
82 |
|
* (including the case where a task was cancelled without executing); |
83 |
– |
* {@link #isCancelled} is true if completion was due to cancellation; |
83 |
|
* {@link #isCompletedNormally} is true if a task completed without |
84 |
< |
* cancellation or encountering an exception; {@link |
85 |
< |
* #isCompletedExceptionally} is true if if the task encountered an |
86 |
< |
* exception (in which case {@link #getException} returns the |
87 |
< |
* exception); {@link #isCancelled} is true if the task was cancelled |
88 |
< |
* (in which case {@link #getException} returns a {@link |
89 |
< |
* java.util.concurrent.CancellationException}); and {@link |
90 |
< |
* #isCompletedAbnormally} is true if a task was either cancelled or |
92 |
< |
* encountered an exception. |
84 |
> |
* cancellation or encountering an exception; {@link #isCancelled} is |
85 |
> |
* true if the task was cancelled (in which case {@link #getException} |
86 |
> |
* returns a {@link java.util.concurrent.CancellationException}); and |
87 |
> |
* {@link #isCompletedAbnormally} is true if a task was either |
88 |
> |
* cancelled or encountered an exception, in which case {@link |
89 |
> |
* #getException} will return either the encountered exception or |
90 |
> |
* {@link java.util.concurrent.CancellationException}. |
91 |
|
* |
92 |
|
* <p>The ForkJoinTask class is not usually directly subclassed. |
93 |
|
* Instead, you subclass one of the abstract classes that support a |
123 |
|
* improve throughput. If too small, then memory and internal task |
124 |
|
* maintenance overhead may overwhelm processing. |
125 |
|
* |
126 |
< |
* <p>This class provides {@code adapt} methods for {@link |
127 |
< |
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
128 |
< |
* may be of use when mixing execution of ForkJoinTasks with other |
129 |
< |
* kinds of tasks. When all tasks are of this form, consider using a |
130 |
< |
* pool in {@link ForkJoinPool#setAsyncMode async mode}. |
126 |
> |
* <p>This class provides {@code adapt} methods for {@link Runnable} |
127 |
> |
* and {@link Callable}, that may be of use when mixing execution of |
128 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks |
129 |
> |
* are of this form, consider using a pool in |
130 |
> |
* {@linkplain ForkJoinPool#setAsyncMode async mode}. |
131 |
|
* |
132 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
133 |
|
* used in extensions such as remote execution frameworks. It is |
139 |
|
*/ |
140 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
141 |
|
|
142 |
+ |
/* |
143 |
+ |
* See the internal documentation of class ForkJoinPool for a |
144 |
+ |
* general implementation overview. ForkJoinTasks are mainly |
145 |
+ |
* responsible for maintaining their "status" field amidst relays |
146 |
+ |
* to methods in ForkJoinWorkerThread and ForkJoinPool. The |
147 |
+ |
* methods of this class are more-or-less layered into (1) basic |
148 |
+ |
* status maintenance (2) execution and awaiting completion (3) |
149 |
+ |
* user-level methods that additionally report results. This is |
150 |
+ |
* sometimes hard to see because this file orders exported methods |
151 |
+ |
* in a way that flows well in javadocs. |
152 |
+ |
*/ |
153 |
+ |
|
154 |
|
/** |
155 |
|
* Run control status bits packed into a single int to minimize |
156 |
|
* footprint and to ensure atomicity (via CAS). Status is |
160 |
|
* blocking waits by other threads have SIGNAL_MASK bits set -- |
161 |
|
* bit 15 for external (nonFJ) waits, and the rest a count of |
162 |
|
* waiting FJ threads. (This representation relies on |
163 |
< |
* ForkJoinPool max thread limits). Completion of a stolen task |
164 |
< |
* with SIGNAL_MASK bits set awakens waiter via notifyAll. Even |
165 |
< |
* though suboptimal for some purposes, we use basic builtin |
166 |
< |
* wait/notify to take advantage of "monitor inflation" in JVMs |
167 |
< |
* that we would otherwise need to emulate to avoid adding further |
168 |
< |
* per-task bookkeeping overhead. Note that bits 16-28 are |
169 |
< |
* currently unused. Also value 0x80000000 is available as spare |
170 |
< |
* completion value. |
163 |
> |
* ForkJoinPool max thread limits). Signal counts are not directly |
164 |
> |
* incremented by ForkJoinTask methods, but instead via a call to |
165 |
> |
* requestSignal within ForkJoinPool.preJoin, once their need is |
166 |
> |
* established. |
167 |
> |
* |
168 |
> |
* 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. |
179 |
|
*/ |
180 |
|
volatile int status; // accessed directly by pool and workers |
181 |
|
|
182 |
< |
static final int COMPLETION_MASK = 0xe0000000; |
183 |
< |
static final int NORMAL = 0xe0000000; // == mask |
184 |
< |
static final int CANCELLED = 0xc0000000; |
185 |
< |
static final int EXCEPTIONAL = 0xa0000000; |
186 |
< |
static final int SIGNAL_MASK = 0x0000ffff; |
187 |
< |
static final int INTERNAL_SIGNAL_MASK = 0x00007fff; |
188 |
< |
static final int EXTERNAL_SIGNAL = 0x00008000; // top bit of low word |
182 |
> |
private static final int COMPLETION_MASK = 0xe0000000; |
183 |
> |
private static final int NORMAL = 0xe0000000; // == mask |
184 |
> |
private static final int CANCELLED = 0xc0000000; |
185 |
> |
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; |
189 |
|
|
190 |
|
/** |
191 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
199 |
|
Collections.synchronizedMap |
200 |
|
(new WeakHashMap<ForkJoinTask<?>, Throwable>()); |
201 |
|
|
202 |
< |
// within-package utilities |
202 |
> |
// Maintaining completion status |
203 |
|
|
204 |
|
/** |
205 |
< |
* Gets current worker thread, or null if not a worker thread. |
205 |
> |
* Marks completion and wakes up threads waiting to join this task, |
206 |
> |
* also clearing signal request bits. |
207 |
> |
* |
208 |
> |
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
209 |
|
*/ |
210 |
< |
static ForkJoinWorkerThread getWorker() { |
211 |
< |
Thread t = Thread.currentThread(); |
212 |
< |
return ((t instanceof ForkJoinWorkerThread) ? |
213 |
< |
(ForkJoinWorkerThread) t : null); |
214 |
< |
} |
215 |
< |
|
216 |
< |
final boolean casStatus(int cmp, int val) { |
217 |
< |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
210 |
> |
private void setCompletion(int completion) { |
211 |
> |
int s; |
212 |
> |
while ((s = status) >= 0) { |
213 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
214 |
> |
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); |
219 |
> |
synchronized (this) { notifyAll(); } |
220 |
> |
} |
221 |
> |
return; |
222 |
> |
} |
223 |
> |
} |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* Workaround for not being able to rethrow unchecked exceptions. |
227 |
> |
* Record exception and set exceptional completion |
228 |
|
*/ |
229 |
< |
static void rethrowException(Throwable ex) { |
230 |
< |
if (ex != null) |
231 |
< |
UNSAFE.throwException(ex); |
229 |
> |
private void setDoneExceptionally(Throwable rex) { |
230 |
> |
exceptionMap.put(this, rex); |
231 |
> |
setCompletion(EXCEPTIONAL); |
232 |
|
} |
233 |
|
|
207 |
– |
// Setting completion status |
208 |
– |
|
234 |
|
/** |
235 |
< |
* Marks completion and wakes up threads waiting to join this task. |
235 |
> |
* Main internal execution method: Unless done, calls exec and |
236 |
> |
* records completion. |
237 |
|
* |
238 |
< |
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
238 |
> |
* @return true if ran and completed normally |
239 |
|
*/ |
240 |
< |
final void setCompletion(int completion) { |
241 |
< |
ForkJoinPool pool = getPool(); |
242 |
< |
if (pool != null) { |
243 |
< |
int s; // Clear signal bits while setting completion status |
244 |
< |
do {} while ((s = status) >= 0 && !casStatus(s, completion)); |
245 |
< |
|
246 |
< |
if ((s & SIGNAL_MASK) != 0) { |
221 |
< |
if ((s &= INTERNAL_SIGNAL_MASK) != 0) |
222 |
< |
pool.updateRunningCount(s); |
223 |
< |
synchronized (this) { notifyAll(); } |
224 |
< |
} |
240 |
> |
final boolean tryExec() { |
241 |
> |
try { |
242 |
> |
if (status < 0 || !exec()) |
243 |
> |
return false; |
244 |
> |
} catch (Throwable rex) { |
245 |
> |
setDoneExceptionally(rex); |
246 |
> |
return false; |
247 |
|
} |
248 |
< |
else |
249 |
< |
externallySetCompletion(completion); |
248 |
> |
setCompletion(NORMAL); // must be outside try block |
249 |
> |
return true; |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* Version of setCompletion for non-FJ threads. Leaves signal |
254 |
< |
* bits for unblocked threads to adjust, and always notifies. |
253 |
> |
* Increments internal signal count (thus requesting signal upon |
254 |
> |
* completion) unless already done. Call only once per join. |
255 |
> |
* Used by ForkJoinPool.preJoin. |
256 |
> |
* |
257 |
> |
* @return status |
258 |
|
*/ |
259 |
< |
private void externallySetCompletion(int completion) { |
259 |
> |
final int requestSignal() { |
260 |
|
int s; |
261 |
|
do {} while ((s = status) >= 0 && |
262 |
< |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
263 |
< |
synchronized (this) { notifyAll(); } |
262 |
> |
!UNSAFE.compareAndSwapInt(this, statusOffset, s, s + 1)); |
263 |
> |
return s; |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* Sets status to indicate normal completion. |
267 |
> |
* Sets external signal request unless already done. |
268 |
> |
* |
269 |
> |
* @return status |
270 |
|
*/ |
271 |
< |
final void setNormalCompletion() { |
272 |
< |
// Try typical fast case -- single CAS, no signal, not already done. |
273 |
< |
// Manually expand casStatus to improve chances of inlining it |
274 |
< |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
275 |
< |
setCompletion(NORMAL); |
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 |
< |
// internal waiting and notification |
280 |
< |
|
281 |
< |
/** |
282 |
< |
* Performs the actual monitor wait for awaitDone. |
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 |
|
*/ |
256 |
– |
private void doAwaitDone() { |
257 |
– |
// Minimize lock bias and in/de-flation effects by maximizing |
258 |
– |
// chances of waiting inside sync |
259 |
– |
try { |
260 |
– |
while (status >= 0) |
261 |
– |
synchronized (this) { if (status >= 0) wait(); } |
262 |
– |
} catch (InterruptedException ie) { |
263 |
– |
onInterruptedWait(); |
264 |
– |
} |
265 |
– |
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* Performs the actual timed monitor wait for awaitDone. |
286 |
> |
* Blocks a worker until this task is done, also maintaining pool |
287 |
> |
* and signal counts |
288 |
|
*/ |
289 |
< |
private void doAwaitDone(long startTime, long nanos) { |
290 |
< |
synchronized (this) { |
291 |
< |
try { |
292 |
< |
while (status >= 0) { |
293 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
294 |
< |
if (nt <= 0) |
295 |
< |
break; |
296 |
< |
wait(nt / 1000000, (int) (nt % 1000000)); |
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(); |
304 |
|
} |
279 |
– |
} catch (InterruptedException ie) { |
280 |
– |
onInterruptedWait(); |
305 |
|
} |
306 |
|
} |
307 |
|
} |
308 |
|
|
285 |
– |
// Awaiting completion |
286 |
– |
|
309 |
|
/** |
310 |
< |
* Sets status to indicate there is joiner, then waits for join, |
289 |
< |
* surrounded with pool notifications. |
290 |
< |
* |
291 |
< |
* @return status upon exit |
310 |
> |
* Blocks a non-ForkJoin thread until this task is done. |
311 |
|
*/ |
312 |
< |
private int awaitDone(ForkJoinWorkerThread w, |
313 |
< |
boolean maintainParallelism) { |
314 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
315 |
< |
int s; |
316 |
< |
while ((s = status) >= 0) { |
317 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
318 |
< |
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
319 |
< |
doAwaitDone(); |
320 |
< |
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
321 |
< |
adjustPoolCountsOnUnblock(pool); |
322 |
< |
break; |
312 |
> |
private void externalAwaitDone() { |
313 |
> |
if (requestExternalSignal() >= 0) { |
314 |
> |
boolean interrupted = false; |
315 |
> |
while (status >= 0) { |
316 |
> |
try { |
317 |
> |
synchronized(this) { |
318 |
> |
if (status >= 0) |
319 |
> |
wait(); |
320 |
> |
else { |
321 |
> |
notifyAll(); |
322 |
> |
break; |
323 |
> |
} |
324 |
> |
} |
325 |
> |
} catch (InterruptedException ie) { |
326 |
> |
interrupted = true; |
327 |
> |
} |
328 |
|
} |
329 |
+ |
if (interrupted) |
330 |
+ |
Thread.currentThread().interrupt(); |
331 |
|
} |
306 |
– |
return s; |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
< |
* Timed version of awaitDone |
311 |
< |
* |
312 |
< |
* @return status upon exit |
335 |
> |
* Blocks a worker until this task is done or timeout elapses |
336 |
|
*/ |
337 |
< |
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
338 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
339 |
< |
int s; |
340 |
< |
while ((s = status) >= 0) { |
341 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
342 |
< |
long startTime = System.nanoTime(); |
343 |
< |
if (pool == null || !pool.preJoin(this, false)) |
344 |
< |
doAwaitDone(startTime, nanos); |
345 |
< |
if ((s = status) >= 0) { |
346 |
< |
adjustPoolCountsOnCancelledWait(pool); |
347 |
< |
s = status; |
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; |
361 |
> |
} |
362 |
> |
} |
363 |
> |
break; |
364 |
|
} |
326 |
– |
if (s < 0 && (s & INTERNAL_SIGNAL_MASK) != 0) |
327 |
– |
adjustPoolCountsOnUnblock(pool); |
328 |
– |
break; |
365 |
|
} |
366 |
|
} |
331 |
– |
return s; |
332 |
– |
} |
333 |
– |
|
334 |
– |
/** |
335 |
– |
* Notifies pool that thread is unblocked. Called by signalled |
336 |
– |
* threads when woken by non-FJ threads (which is atypical). |
337 |
– |
*/ |
338 |
– |
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
339 |
– |
int s; |
340 |
– |
do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
341 |
– |
if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0) |
342 |
– |
pool.updateRunningCount(s); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* Notifies pool to adjust counts on cancelled or timed out wait. |
370 |
> |
* Blocks a non-ForkJoin thread until this task is done or timeout elapses |
371 |
|
*/ |
372 |
< |
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
373 |
< |
if (pool != null) { |
374 |
< |
int s; |
375 |
< |
while ((s = status) >= 0 && (s & INTERNAL_SIGNAL_MASK) != 0) { |
376 |
< |
if (casStatus(s, s - 1)) { |
377 |
< |
pool.updateRunningCount(1); |
372 |
> |
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 |
< |
/** |
361 |
< |
* Handles interruptions during waits. |
362 |
< |
*/ |
363 |
< |
private void onInterruptedWait() { |
364 |
< |
ForkJoinWorkerThread w = getWorker(); |
365 |
< |
if (w == null) |
366 |
< |
Thread.currentThread().interrupt(); // re-interrupt |
367 |
< |
else if (w.isTerminating()) |
368 |
< |
cancelIgnoringExceptions(); |
369 |
< |
// else if FJworker, ignore interrupt |
370 |
< |
} |
371 |
< |
|
372 |
< |
// Recording and reporting exceptions |
373 |
< |
|
374 |
< |
private void setDoneExceptionally(Throwable rex) { |
375 |
< |
exceptionMap.put(this, rex); |
376 |
< |
setCompletion(EXCEPTIONAL); |
377 |
< |
} |
393 |
> |
// reporting results |
394 |
|
|
395 |
|
/** |
396 |
< |
* Throws the exception associated with status s. |
397 |
< |
* |
398 |
< |
* @throws the exception |
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. |
399 |
|
*/ |
400 |
< |
private void reportException(int s) { |
401 |
< |
if ((s &= COMPLETION_MASK) < NORMAL) { |
402 |
< |
if (s == CANCELLED) |
403 |
< |
throw new CancellationException(); |
404 |
< |
else |
389 |
< |
rethrowException(exceptionMap.get(this)); |
400 |
> |
private V reportResult() { |
401 |
> |
if ((status & COMPLETION_MASK) < NORMAL) { |
402 |
> |
Throwable ex = getException(); |
403 |
> |
if (ex != null) |
404 |
> |
UNSAFE.throwException(ex); |
405 |
|
} |
406 |
+ |
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. |
411 |
> |
* Only call when {@code isDone} known to be true or thread known |
412 |
> |
* to be interrupted. |
413 |
|
*/ |
414 |
|
private V reportFutureResult() |
415 |
< |
throws ExecutionException, InterruptedException { |
415 |
> |
throws InterruptedException, ExecutionException { |
416 |
|
if (Thread.interrupted()) |
417 |
|
throw new InterruptedException(); |
418 |
|
int s = status & COMPLETION_MASK; |
438 |
|
int s = status & COMPLETION_MASK; |
439 |
|
if (s == NORMAL) |
440 |
|
return getRawResult(); |
441 |
< |
if (s == CANCELLED) |
441 |
> |
else if (s == CANCELLED) |
442 |
|
throw new CancellationException(); |
443 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
443 |
> |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
444 |
|
throw new ExecutionException(ex); |
445 |
< |
throw new TimeoutException(); |
446 |
< |
} |
430 |
< |
|
431 |
< |
// internal execution methods |
432 |
< |
|
433 |
< |
/** |
434 |
< |
* Calls exec, recording completion, and rethrowing exception if |
435 |
< |
* encountered. Caller should normally check status before calling. |
436 |
< |
* |
437 |
< |
* @return true if completed normally |
438 |
< |
*/ |
439 |
< |
private boolean tryExec() { |
440 |
< |
try { // try block must contain only call to exec |
441 |
< |
if (!exec()) |
442 |
< |
return false; |
443 |
< |
} catch (Throwable rex) { |
444 |
< |
setDoneExceptionally(rex); |
445 |
< |
rethrowException(rex); |
446 |
< |
return false; // not reached |
447 |
< |
} |
448 |
< |
setNormalCompletion(); |
449 |
< |
return true; |
450 |
< |
} |
451 |
< |
|
452 |
< |
/** |
453 |
< |
* Main execution method used by worker threads. Invokes |
454 |
< |
* base computation unless already complete. |
455 |
< |
*/ |
456 |
< |
final void quietlyExec() { |
457 |
< |
if (status >= 0) { |
458 |
< |
try { |
459 |
< |
if (!exec()) |
460 |
< |
return; |
461 |
< |
} catch (Throwable rex) { |
462 |
< |
setDoneExceptionally(rex); |
463 |
< |
return; |
464 |
< |
} |
465 |
< |
setNormalCompletion(); |
466 |
< |
} |
467 |
< |
} |
468 |
< |
|
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) { |
494 |
< |
} |
495 |
< |
} |
496 |
< |
|
497 |
< |
/** |
498 |
< |
* Main implementation of helpJoin |
499 |
< |
*/ |
500 |
< |
private int busyJoin(ForkJoinWorkerThread w) { |
501 |
< |
int s; |
502 |
< |
ForkJoinTask<?> t; |
503 |
< |
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
504 |
< |
t.quietlyExec(); |
505 |
< |
return (s >= 0) ? awaitDone(w, false) : s; // block if no work |
445 |
> |
else |
446 |
> |
throw new TimeoutException(); |
447 |
|
} |
448 |
|
|
449 |
|
// public methods |
452 |
|
* Arranges to asynchronously execute this task. While it is not |
453 |
|
* necessarily enforced, it is a usage error to fork a task more |
454 |
|
* than once unless it has completed and been reinitialized. |
455 |
+ |
* Subsequent modifications to the state of this task or any data |
456 |
+ |
* it operates on are not necessarily consistently observable by |
457 |
+ |
* any thread other than the one executing it unless preceded by a |
458 |
+ |
* call to {@link #join} or related methods, or a call to {@link |
459 |
+ |
* #isDone} returning {@code true}. |
460 |
|
* |
461 |
|
* <p>This method may be invoked only from within {@code |
462 |
|
* ForkJoinTask} computations (as may be determined using method |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
< |
* Returns the result of the computation when it is ready. |
476 |
> |
* Returns the result of the computation when it {@link #isDone is done}. |
477 |
|
* This method differs from {@link #get()} in that |
478 |
|
* abnormal completion results in {@code RuntimeException} or |
479 |
|
* {@code Error}, not {@code ExecutionException}. |
481 |
|
* @return the computed result |
482 |
|
*/ |
483 |
|
public final V join() { |
484 |
< |
ForkJoinWorkerThread w = getWorker(); |
485 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryExec()) |
540 |
< |
reportException(awaitDone(w, true)); |
541 |
< |
return getRawResult(); |
484 |
> |
quietlyJoin(); |
485 |
> |
return reportResult(); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
493 |
|
* @return the computed result |
494 |
|
*/ |
495 |
|
public final V invoke() { |
496 |
< |
if (status >= 0 && tryExec()) |
497 |
< |
return getRawResult(); |
498 |
< |
else |
555 |
< |
return join(); |
496 |
> |
if (!tryExec()) |
497 |
> |
quietlyJoin(); |
498 |
> |
return reportResult(); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
503 |
|
* each task or an (unchecked) exception is encountered, in which |
504 |
< |
* case the exception is rethrown. If more than one task |
505 |
< |
* encounters an exception, then this method throws any one of |
506 |
< |
* these exceptions. The individual status of each task may be |
504 |
> |
* case the exception is rethrown. If either task encounters an |
505 |
> |
* exception, the other one may be, but is not guaranteed to be, |
506 |
> |
* cancelled. If both tasks throw an exception, then this method |
507 |
> |
* throws one of them. The individual status of each task may be |
508 |
|
* checked using {@link #getException()} and related methods. |
509 |
|
* |
510 |
|
* <p>This method may be invoked only from within {@code |
572 |
|
} |
573 |
|
} |
574 |
|
if (ex != null) |
575 |
< |
rethrowException(ex); |
575 |
> |
UNSAFE.throwException(ex); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
634 |
|
} |
635 |
|
} |
636 |
|
if (ex != null) |
637 |
< |
rethrowException(ex); |
637 |
> |
UNSAFE.throwException(ex); |
638 |
|
return tasks; |
639 |
|
} |
640 |
|
|
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
< |
* Returns {@code true} if the computation performed by this task |
672 |
< |
* has completed (or has been cancelled). |
729 |
< |
* |
730 |
< |
* @return {@code true} if this computation has completed |
671 |
> |
* Cancels, ignoring any exceptions it throws. Used during worker |
672 |
> |
* and pool shutdown. |
673 |
|
*/ |
674 |
< |
public final boolean isDone() { |
675 |
< |
return status < 0; |
674 |
> |
final void cancelIgnoringExceptions() { |
675 |
> |
try { |
676 |
> |
cancel(false); |
677 |
> |
} catch (Throwable ignore) { |
678 |
> |
} |
679 |
|
} |
680 |
|
|
681 |
|
/** |
682 |
< |
* Returns {@code true} if this task was cancelled. |
738 |
< |
* |
739 |
< |
* @return {@code true} if this task was cancelled |
682 |
> |
* Cancels ignoring exceptions if worker is terminating |
683 |
|
*/ |
684 |
+ |
private void cancelIfTerminating() { |
685 |
+ |
Thread t = Thread.currentThread(); |
686 |
+ |
if ((t instanceof ForkJoinWorkerThread) && |
687 |
+ |
((ForkJoinWorkerThread) t).isTerminating()) { |
688 |
+ |
try { |
689 |
+ |
cancel(false); |
690 |
+ |
} catch (Throwable ignore) { |
691 |
+ |
} |
692 |
+ |
} |
693 |
+ |
} |
694 |
+ |
|
695 |
+ |
public final boolean isDone() { |
696 |
+ |
return status < 0; |
697 |
+ |
} |
698 |
+ |
|
699 |
|
public final boolean isCancelled() { |
700 |
|
return (status & COMPLETION_MASK) == CANCELLED; |
701 |
|
} |
721 |
|
} |
722 |
|
|
723 |
|
/** |
766 |
– |
* Returns {@code true} if this task threw an exception. |
767 |
– |
* |
768 |
– |
* @return {@code true} if this task threw an exception |
769 |
– |
*/ |
770 |
– |
public final boolean isCompletedExceptionally() { |
771 |
– |
return (status & COMPLETION_MASK) == EXCEPTIONAL; |
772 |
– |
} |
773 |
– |
|
774 |
– |
/** |
724 |
|
* Returns the exception thrown by the base computation, or a |
725 |
|
* {@code CancellationException} if cancelled, or {@code null} if |
726 |
|
* none or if the method has not yet completed. |
729 |
|
*/ |
730 |
|
public final Throwable getException() { |
731 |
|
int s = status & COMPLETION_MASK; |
732 |
< |
if (s >= NORMAL) |
733 |
< |
return null; |
734 |
< |
if (s == CANCELLED) |
786 |
< |
return new CancellationException(); |
787 |
< |
return exceptionMap.get(this); |
732 |
> |
return ((s >= NORMAL) ? null : |
733 |
> |
(s == CANCELLED) ? new CancellationException() : |
734 |
> |
exceptionMap.get(this)); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
744 |
|
* overridable, but overridden versions must invoke {@code super} |
745 |
|
* implementation to maintain guarantees. |
746 |
|
* |
747 |
< |
* @param ex the exception to throw. If this exception is |
748 |
< |
* not a RuntimeException or Error, the actual exception thrown |
749 |
< |
* will be a RuntimeException with cause ex. |
747 |
> |
* @param ex the exception to throw. If this exception is not a |
748 |
> |
* {@code RuntimeException} or {@code Error}, the actual exception |
749 |
> |
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
750 |
|
*/ |
751 |
|
public void completeExceptionally(Throwable ex) { |
752 |
|
setDoneExceptionally((ex instanceof RuntimeException) || |
773 |
|
setDoneExceptionally(rex); |
774 |
|
return; |
775 |
|
} |
776 |
< |
setNormalCompletion(); |
776 |
> |
setCompletion(NORMAL); |
777 |
|
} |
778 |
|
|
779 |
|
public final V get() throws InterruptedException, ExecutionException { |
780 |
< |
ForkJoinWorkerThread w = getWorker(); |
834 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
835 |
< |
awaitDone(w, true); |
780 |
> |
quietlyJoin(); |
781 |
|
return reportFutureResult(); |
782 |
|
} |
783 |
|
|
784 |
|
public final V get(long timeout, TimeUnit unit) |
785 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
786 |
|
long nanos = unit.toNanos(timeout); |
787 |
< |
ForkJoinWorkerThread w = getWorker(); |
788 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
789 |
< |
awaitDone(w, nanos); |
787 |
> |
Thread t = Thread.currentThread(); |
788 |
> |
if (t instanceof ForkJoinWorkerThread) { |
789 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
790 |
> |
if (!w.unpushTask(this) || !tryExec()) |
791 |
> |
timedAwaitDone(w, nanos); |
792 |
> |
} |
793 |
> |
else |
794 |
> |
externalTimedAwaitDone(nanos); |
795 |
|
return reportTimedFutureResult(); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
< |
* Possibly executes other tasks until this task is ready, then |
800 |
< |
* returns the result of the computation. This method may be more |
801 |
< |
* efficient than {@code join}, but is only applicable when |
802 |
< |
* there are no potential dependencies between continuation of the |
803 |
< |
* current task and that of any other task that might be executed |
804 |
< |
* while helping. (This usually holds for pure divide-and-conquer |
805 |
< |
* tasks). |
799 |
> |
* Possibly executes other tasks until this task {@link #isDone is |
800 |
> |
* done}, then returns the result of the computation. This method |
801 |
> |
* may be more efficient than {@code join}, but is only applicable |
802 |
> |
* when there are no potential dependencies between continuation |
803 |
> |
* of the current task and that of any other task that might be |
804 |
> |
* executed while helping. (This usually holds for pure |
805 |
> |
* divide-and-conquer tasks). |
806 |
|
* |
807 |
|
* <p>This method may be invoked only from within {@code |
808 |
|
* ForkJoinTask} computations (as may be determined using method |
813 |
|
* @return the computed result |
814 |
|
*/ |
815 |
|
public final V helpJoin() { |
816 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
817 |
< |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
868 |
< |
reportException(busyJoin(w)); |
869 |
< |
return getRawResult(); |
816 |
> |
quietlyHelpJoin(); |
817 |
> |
return reportResult(); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
< |
* Possibly executes other tasks until this task is ready. This |
822 |
< |
* method may be useful when processing collections of tasks when |
823 |
< |
* some have been cancelled or otherwise known to have aborted. |
821 |
> |
* Possibly executes other tasks until this task {@link #isDone is |
822 |
> |
* done}. This method may be useful when processing collections |
823 |
> |
* of tasks when some have been cancelled or otherwise known to |
824 |
> |
* have aborted. |
825 |
|
* |
826 |
|
* <p>This method may be invoked only from within {@code |
827 |
|
* ForkJoinTask} computations (as may be determined using method |
830 |
|
* ClassCastException}. |
831 |
|
*/ |
832 |
|
public final void quietlyHelpJoin() { |
833 |
< |
if (status >= 0) { |
834 |
< |
ForkJoinWorkerThread w = |
835 |
< |
(ForkJoinWorkerThread) Thread.currentThread(); |
836 |
< |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
837 |
< |
busyJoin(w); |
833 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
834 |
> |
if (!w.unpushTask(this) || !tryExec()) { |
835 |
> |
for (;;) { |
836 |
> |
ForkJoinTask<?> t; |
837 |
> |
if (status < 0) |
838 |
> |
return; |
839 |
> |
else if ((t = w.scanWhileJoining(this)) != null) |
840 |
> |
t.tryExec(); |
841 |
> |
else if (status < 0) |
842 |
> |
return; |
843 |
> |
else if (w.pool.preBlockHelpingJoin(this)) { |
844 |
> |
while (status >= 0) { // variant of awaitDone |
845 |
> |
try { |
846 |
> |
synchronized(this) { |
847 |
> |
if (status >= 0) |
848 |
> |
wait(); |
849 |
> |
else { |
850 |
> |
notifyAll(); |
851 |
> |
break; |
852 |
> |
} |
853 |
> |
} |
854 |
> |
} catch (InterruptedException ie) { |
855 |
> |
cancelIfTerminating(); |
856 |
> |
} |
857 |
> |
} |
858 |
> |
return; |
859 |
> |
} |
860 |
> |
} |
861 |
|
} |
862 |
|
} |
863 |
|
|
868 |
|
* known to have aborted. |
869 |
|
*/ |
870 |
|
public final void quietlyJoin() { |
871 |
< |
if (status >= 0) { |
872 |
< |
ForkJoinWorkerThread w = getWorker(); |
873 |
< |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
874 |
< |
awaitDone(w, true); |
871 |
> |
Thread t = Thread.currentThread(); |
872 |
> |
if (t instanceof ForkJoinWorkerThread) { |
873 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
874 |
> |
if (!w.unpushTask(this) || !tryExec()) |
875 |
> |
awaitDone(w); |
876 |
|
} |
877 |
+ |
else |
878 |
+ |
externalAwaitDone(); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
886 |
|
* known to have aborted. |
887 |
|
*/ |
888 |
|
public final void quietlyInvoke() { |
889 |
< |
if (status >= 0 && !tryQuietlyInvoke()) |
889 |
> |
if (!tryExec()) |
890 |
|
quietlyJoin(); |
891 |
|
} |
892 |
|
|
1209 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1210 |
|
|
1211 |
|
/** |
1212 |
< |
* Save the state to a stream. |
1212 |
> |
* Saves the state to a stream. |
1213 |
|
* |
1214 |
|
* @serialData the current run status and the exception thrown |
1215 |
|
* during execution, or {@code null} if none |
1222 |
|
} |
1223 |
|
|
1224 |
|
/** |
1225 |
< |
* Reconstitute the instance from a stream. |
1225 |
> |
* Reconstitutes the instance from a stream. |
1226 |
|
* |
1227 |
|
* @param s the stream |
1228 |
|
*/ |