6 |
|
|
7 |
|
package jsr166y; |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
10 |
– |
|
9 |
|
import java.io.Serializable; |
10 |
|
import java.util.Collection; |
11 |
|
import java.util.Collections; |
13 |
|
import java.util.RandomAccess; |
14 |
|
import java.util.Map; |
15 |
|
import java.util.WeakHashMap; |
16 |
+ |
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
+ |
import java.util.concurrent.ExecutionException; |
19 |
+ |
import java.util.concurrent.Executor; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
+ |
import java.util.concurrent.Future; |
22 |
+ |
import java.util.concurrent.RejectedExecutionException; |
23 |
+ |
import java.util.concurrent.RunnableFuture; |
24 |
+ |
import java.util.concurrent.TimeUnit; |
25 |
+ |
import java.util.concurrent.TimeoutException; |
26 |
|
|
27 |
|
/** |
28 |
|
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
36 |
|
* start other subtasks. As indicated by the name of this class, |
37 |
|
* many programs using {@code ForkJoinTask} employ only methods |
38 |
|
* {@link #fork} and {@link #join}, or derivatives such as {@link |
39 |
< |
* #invokeAll}. However, this class also provides a number of other |
40 |
< |
* methods that can come into play in advanced usages, as well as |
41 |
< |
* extension mechanics that allow support of new forms of fork/join |
42 |
< |
* processing. |
39 |
> |
* #invokeAll(ForkJoinTask...) invokeAll}. However, this class also |
40 |
> |
* provides a number of other methods that can come into play in |
41 |
> |
* advanced usages, as well as extension mechanics that allow |
42 |
> |
* support of new forms of fork/join processing. |
43 |
|
* |
44 |
|
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
45 |
|
* The efficiency of {@code ForkJoinTask}s stems from a set of |
64 |
|
* exceptions such as {@code IOExceptions} to be thrown. However, |
65 |
|
* computations may still encounter unchecked exceptions, that are |
66 |
|
* rethrown to callers attempting to join them. These exceptions may |
67 |
< |
* additionally include RejectedExecutionExceptions stemming from |
68 |
< |
* internal resource exhaustion such as failure to allocate internal |
69 |
< |
* task queues. |
67 |
> |
* additionally include {@link RejectedExecutionException} stemming |
68 |
> |
* from internal resource exhaustion, such as failure to allocate |
69 |
> |
* internal task queues. |
70 |
|
* |
71 |
|
* <p>The primary method for awaiting completion and extracting |
72 |
|
* results of a task is {@link #join}, but there are several variants: |
73 |
|
* The {@link Future#get} methods support interruptible and/or timed |
74 |
|
* waits for completion and report results using {@code Future} |
75 |
< |
* 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 |
75 |
> |
* conventions. Method {@link #invoke} is semantically |
76 |
|
* equivalent to {@code fork(); join()} but always attempts to begin |
77 |
|
* execution in the current thread. The "<em>quiet</em>" forms of |
78 |
|
* these methods do not extract results or report exceptions. These |
85 |
|
* <p>The execution status of tasks may be queried at several levels |
86 |
|
* of detail: {@link #isDone} is true if a task completed in any way |
87 |
|
* (including the case where a task was cancelled without executing); |
83 |
– |
* {@link #isCancelled} is true if completion was due to cancellation; |
88 |
|
* {@link #isCompletedNormally} is true if a task completed without |
89 |
< |
* cancellation or encountering an exception; {@link |
90 |
< |
* #isCompletedExceptionally} is true if if the task encountered an |
91 |
< |
* exception (in which case {@link #getException} returns the |
92 |
< |
* exception); {@link #isCancelled} is true if the task was cancelled |
93 |
< |
* (in which case {@link #getException} returns a {@link |
94 |
< |
* java.util.concurrent.CancellationException}); and {@link |
95 |
< |
* #isCompletedAbnormally} is true if a task was either cancelled or |
92 |
< |
* encountered an exception. |
89 |
> |
* cancellation or encountering an exception; {@link #isCancelled} is |
90 |
> |
* true if the task was cancelled (in which case {@link #getException} |
91 |
> |
* returns a {@link java.util.concurrent.CancellationException}); and |
92 |
> |
* {@link #isCompletedAbnormally} is true if a task was either |
93 |
> |
* cancelled or encountered an exception, in which case {@link |
94 |
> |
* #getException} will return either the encountered exception or |
95 |
> |
* {@link java.util.concurrent.CancellationException}. |
96 |
|
* |
97 |
|
* <p>The ForkJoinTask class is not usually directly subclassed. |
98 |
|
* Instead, you subclass one of the abstract classes that support a |
108 |
|
* ForkJoinTasks (as may be determined using method {@link |
109 |
|
* #inForkJoinPool}). Attempts to invoke them in other contexts |
110 |
|
* result in exceptions or errors, possibly including |
111 |
< |
* ClassCastException. |
111 |
> |
* {@code ClassCastException}. |
112 |
> |
* |
113 |
> |
* <p>Method {@link #join} and its variants are appropriate for use |
114 |
> |
* only when completion dependencies are acyclic; that is, the |
115 |
> |
* parallel computation can be described as a directed acyclic graph |
116 |
> |
* (DAG). Otherwise, executions may encounter a form of deadlock as |
117 |
> |
* tasks cyclically wait for each other. However, this framework |
118 |
> |
* supports other methods and techniques (for example the use of |
119 |
> |
* {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that |
120 |
> |
* may be of use in constructing custom subclasses for problems that |
121 |
> |
* are not statically structured as DAGs. |
122 |
|
* |
123 |
|
* <p>Most base support methods are {@code final}, to prevent |
124 |
|
* overriding of implementations that are intrinsically tied to the |
140 |
|
* |
141 |
|
* <p>This class provides {@code adapt} methods for {@link Runnable} |
142 |
|
* and {@link Callable}, that may be of use when mixing execution of |
143 |
< |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks |
144 |
< |
* are of this form, consider using a pool in |
132 |
< |
* {@linkplain ForkJoinPool#setAsyncMode async mode}. |
143 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks are |
144 |
> |
* of this form, consider using a pool constructed in <em>asyncMode</em>. |
145 |
|
* |
146 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
147 |
|
* used in extensions such as remote execution frameworks. It is |
153 |
|
*/ |
154 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
155 |
|
|
156 |
< |
/** |
157 |
< |
* Run control status bits packed into a single int to minimize |
158 |
< |
* footprint and to ensure atomicity (via CAS). Status is |
159 |
< |
* initially zero, and takes on nonnegative values until |
160 |
< |
* completed, upon which status holds COMPLETED. CANCELLED, or |
161 |
< |
* EXCEPTIONAL, which use the top 3 bits. Tasks undergoing |
162 |
< |
* blocking waits by other threads have SIGNAL_MASK bits set -- |
163 |
< |
* bit 15 for external (nonFJ) waits, and the rest a count of |
164 |
< |
* waiting FJ threads. (This representation relies on |
165 |
< |
* ForkJoinPool max thread limits). Completion of a stolen task |
166 |
< |
* with SIGNAL_MASK bits set awakens waiter via notifyAll. Even |
167 |
< |
* though suboptimal for some purposes, we use basic builtin |
168 |
< |
* wait/notify to take advantage of "monitor inflation" in JVMs |
169 |
< |
* that we would otherwise need to emulate to avoid adding further |
170 |
< |
* per-task bookkeeping overhead. Note that bits 16-28 are |
171 |
< |
* currently unused. Also value 0x80000000 is available as spare |
172 |
< |
* completion value. |
156 |
> |
/* |
157 |
> |
* See the internal documentation of class ForkJoinPool for a |
158 |
> |
* general implementation overview. ForkJoinTasks are mainly |
159 |
> |
* responsible for maintaining their "status" field amidst relays |
160 |
> |
* to methods in ForkJoinWorkerThread and ForkJoinPool. The |
161 |
> |
* methods of this class are more-or-less layered into (1) basic |
162 |
> |
* status maintenance (2) execution and awaiting completion (3) |
163 |
> |
* user-level methods that additionally report results. This is |
164 |
> |
* sometimes hard to see because this file orders exported methods |
165 |
> |
* in a way that flows well in javadocs. In particular, most |
166 |
> |
* join mechanics are in method quietlyJoin, below. |
167 |
> |
*/ |
168 |
> |
|
169 |
> |
/* |
170 |
> |
* The status field holds run control status bits packed into a |
171 |
> |
* single int to minimize footprint and to ensure atomicity (via |
172 |
> |
* CAS). Status is initially zero, and takes on nonnegative |
173 |
> |
* values until completed, upon which status holds value |
174 |
> |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
175 |
> |
* waits by other threads have the SIGNAL bit set. Completion of |
176 |
> |
* a stolen task with SIGNAL set awakens any waiters via |
177 |
> |
* notifyAll. Even though suboptimal for some purposes, we use |
178 |
> |
* basic builtin wait/notify to take advantage of "monitor |
179 |
> |
* inflation" in JVMs that we would otherwise need to emulate to |
180 |
> |
* avoid adding further per-task bookkeeping overhead. We want |
181 |
> |
* these monitors to be "fat", i.e., not use biasing or thin-lock |
182 |
> |
* techniques, so use some odd coding idioms that tend to avoid |
183 |
> |
* them. |
184 |
|
*/ |
185 |
+ |
|
186 |
+ |
/** The run status of this task */ |
187 |
|
volatile int status; // accessed directly by pool and workers |
188 |
|
|
189 |
< |
static final int COMPLETION_MASK = 0xe0000000; |
190 |
< |
static final int NORMAL = 0xe0000000; // == mask |
191 |
< |
static final int CANCELLED = 0xc0000000; |
192 |
< |
static final int EXCEPTIONAL = 0xa0000000; |
168 |
< |
static final int SIGNAL_MASK = 0x0000ffff; |
169 |
< |
static final int INTERNAL_SIGNAL_MASK = 0x00007fff; |
170 |
< |
static final int EXTERNAL_SIGNAL = 0x00008000; // top bit of low word |
189 |
> |
private static final int NORMAL = -1; |
190 |
> |
private static final int CANCELLED = -2; |
191 |
> |
private static final int EXCEPTIONAL = -3; |
192 |
> |
private static final int SIGNAL = 1; |
193 |
|
|
194 |
|
/** |
195 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
203 |
|
Collections.synchronizedMap |
204 |
|
(new WeakHashMap<ForkJoinTask<?>, Throwable>()); |
205 |
|
|
206 |
< |
// within-package utilities |
185 |
< |
|
186 |
< |
/** |
187 |
< |
* Gets current worker thread, or null if not a worker thread. |
188 |
< |
*/ |
189 |
< |
static ForkJoinWorkerThread getWorker() { |
190 |
< |
Thread t = Thread.currentThread(); |
191 |
< |
return ((t instanceof ForkJoinWorkerThread) ? |
192 |
< |
(ForkJoinWorkerThread) t : null); |
193 |
< |
} |
194 |
< |
|
195 |
< |
final boolean casStatus(int cmp, int val) { |
196 |
< |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
197 |
< |
} |
206 |
> |
// Maintaining completion status |
207 |
|
|
208 |
|
/** |
209 |
< |
* Workaround for not being able to rethrow unchecked exceptions. |
210 |
< |
*/ |
202 |
< |
static void rethrowException(Throwable ex) { |
203 |
< |
if (ex != null) |
204 |
< |
UNSAFE.throwException(ex); |
205 |
< |
} |
206 |
< |
|
207 |
< |
// Setting completion status |
208 |
< |
|
209 |
< |
/** |
210 |
< |
* Marks completion and wakes up threads waiting to join this task. |
209 |
> |
* Marks completion and wakes up threads waiting to join this task, |
210 |
> |
* also clearing signal request bits. |
211 |
|
* |
212 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
213 |
|
*/ |
214 |
< |
final void setCompletion(int completion) { |
215 |
< |
ForkJoinPool pool = getPool(); |
216 |
< |
if (pool != null) { |
217 |
< |
int s; // Clear signal bits while setting completion status |
218 |
< |
do {} while ((s = status) >= 0 && !casStatus(s, completion)); |
219 |
< |
|
220 |
< |
if ((s & SIGNAL_MASK) != 0) { |
221 |
< |
if ((s &= INTERNAL_SIGNAL_MASK) != 0) |
222 |
< |
pool.updateRunningCount(s); |
223 |
< |
synchronized (this) { notifyAll(); } |
214 |
> |
private void setCompletion(int completion) { |
215 |
> |
int s; |
216 |
> |
while ((s = status) >= 0) { |
217 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
218 |
> |
if (s != 0) |
219 |
> |
synchronized (this) { notifyAll(); } |
220 |
> |
break; |
221 |
|
} |
222 |
|
} |
226 |
– |
else |
227 |
– |
externallySetCompletion(completion); |
228 |
– |
} |
229 |
– |
|
230 |
– |
/** |
231 |
– |
* Version of setCompletion for non-FJ threads. Leaves signal |
232 |
– |
* bits for unblocked threads to adjust, and always notifies. |
233 |
– |
*/ |
234 |
– |
private void externallySetCompletion(int completion) { |
235 |
– |
int s; |
236 |
– |
do {} while ((s = status) >= 0 && |
237 |
– |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
238 |
– |
synchronized (this) { notifyAll(); } |
239 |
– |
} |
240 |
– |
|
241 |
– |
/** |
242 |
– |
* Sets status to indicate normal completion. |
243 |
– |
*/ |
244 |
– |
final void setNormalCompletion() { |
245 |
– |
// Try typical fast case -- single CAS, no signal, not already done. |
246 |
– |
// Manually expand casStatus to improve chances of inlining it |
247 |
– |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
248 |
– |
setCompletion(NORMAL); |
223 |
|
} |
224 |
|
|
251 |
– |
// internal waiting and notification |
252 |
– |
|
225 |
|
/** |
226 |
< |
* Performs the actual monitor wait for awaitDone. |
226 |
> |
* Records exception and sets exceptional completion. |
227 |
> |
* |
228 |
> |
* @return status on exit |
229 |
|
*/ |
230 |
< |
private void doAwaitDone() { |
231 |
< |
// Minimize lock bias and in/de-flation effects by maximizing |
232 |
< |
// 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 |
< |
} |
230 |
> |
private void setExceptionalCompletion(Throwable rex) { |
231 |
> |
exceptionMap.put(this, rex); |
232 |
> |
setCompletion(EXCEPTIONAL); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* Performs the actual timed monitor wait for awaitDone. |
236 |
> |
* Blocks a worker thread until completion. Called only by |
237 |
> |
* pool. Currently unused -- pool-based waits use timeout |
238 |
> |
* version below. |
239 |
|
*/ |
240 |
< |
private void doAwaitDone(long startTime, long nanos) { |
241 |
< |
synchronized (this) { |
240 |
> |
final void internalAwaitDone() { |
241 |
> |
int s; // the odd construction reduces lock bias effects |
242 |
> |
while ((s = status) >= 0) { |
243 |
|
try { |
244 |
< |
while (status >= 0) { |
245 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
246 |
< |
if (nt <= 0) |
276 |
< |
break; |
277 |
< |
wait(nt / 1000000, (int) (nt % 1000000)); |
244 |
> |
synchronized (this) { |
245 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
246 |
> |
wait(); |
247 |
|
} |
248 |
|
} catch (InterruptedException ie) { |
249 |
< |
onInterruptedWait(); |
249 |
> |
cancelIfTerminating(); |
250 |
|
} |
251 |
|
} |
252 |
|
} |
253 |
|
|
285 |
– |
// Awaiting completion |
286 |
– |
|
254 |
|
/** |
255 |
< |
* Sets status to indicate there is joiner, then waits for join, |
256 |
< |
* surrounded with pool notifications. |
255 |
> |
* Blocks a worker thread until completed or timed out. Called |
256 |
> |
* only by pool. |
257 |
|
* |
258 |
< |
* @return status upon exit |
258 |
> |
* @return status on exit |
259 |
|
*/ |
260 |
< |
private int awaitDone(ForkJoinWorkerThread w, |
294 |
< |
boolean maintainParallelism) { |
295 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
260 |
> |
final int internalAwaitDone(long millis, int nanos) { |
261 |
|
int s; |
262 |
< |
while ((s = status) >= 0) { |
263 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
264 |
< |
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
265 |
< |
doAwaitDone(); |
266 |
< |
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
302 |
< |
adjustPoolCountsOnUnblock(pool); |
303 |
< |
break; |
304 |
< |
} |
305 |
< |
} |
306 |
< |
return s; |
307 |
< |
} |
308 |
< |
|
309 |
< |
/** |
310 |
< |
* Timed version of awaitDone |
311 |
< |
* |
312 |
< |
* @return status upon exit |
313 |
< |
*/ |
314 |
< |
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
315 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
316 |
< |
int s; |
317 |
< |
while ((s = status) >= 0) { |
318 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
319 |
< |
long startTime = System.nanoTime(); |
320 |
< |
if (pool == null || !pool.preJoin(this, false)) |
321 |
< |
doAwaitDone(startTime, nanos); |
322 |
< |
if ((s = status) >= 0) { |
323 |
< |
adjustPoolCountsOnCancelledWait(pool); |
324 |
< |
s = status; |
262 |
> |
if ((s = status) >= 0) { |
263 |
> |
try { |
264 |
> |
synchronized (this) { |
265 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
266 |
> |
wait(millis, nanos); |
267 |
|
} |
268 |
< |
if (s < 0 && (s & INTERNAL_SIGNAL_MASK) != 0) |
269 |
< |
adjustPoolCountsOnUnblock(pool); |
328 |
< |
break; |
268 |
> |
} catch (InterruptedException ie) { |
269 |
> |
cancelIfTerminating(); |
270 |
|
} |
271 |
+ |
s = status; |
272 |
|
} |
273 |
|
return s; |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* Notifies pool that thread is unblocked. Called by signalled |
336 |
< |
* threads when woken by non-FJ threads (which is atypical). |
277 |
> |
* Blocks a non-worker-thread until completion. |
278 |
|
*/ |
279 |
< |
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
279 |
> |
private void externalAwaitDone() { |
280 |
|
int s; |
281 |
< |
do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
282 |
< |
if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0) |
283 |
< |
pool.updateRunningCount(s); |
284 |
< |
} |
285 |
< |
|
286 |
< |
/** |
287 |
< |
* Notifies pool to adjust counts on cancelled or timed out wait. |
288 |
< |
*/ |
289 |
< |
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
290 |
< |
if (pool != null) { |
291 |
< |
int s; |
292 |
< |
while ((s = status) >= 0 && (s & INTERNAL_SIGNAL_MASK) != 0) { |
293 |
< |
if (casStatus(s, s - 1)) { |
353 |
< |
pool.updateRunningCount(1); |
281 |
> |
while ((s = status) >= 0) { |
282 |
> |
synchronized (this) { |
283 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
284 |
> |
boolean interrupted = false; |
285 |
> |
while (status >= 0) { |
286 |
> |
try { |
287 |
> |
wait(); |
288 |
> |
} catch (InterruptedException ie) { |
289 |
> |
interrupted = true; |
290 |
> |
} |
291 |
> |
} |
292 |
> |
if (interrupted) |
293 |
> |
Thread.currentThread().interrupt(); |
294 |
|
break; |
295 |
|
} |
296 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* Handles interruptions during waits. |
302 |
< |
*/ |
303 |
< |
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 |
< |
} |
378 |
< |
|
379 |
< |
/** |
380 |
< |
* Throws the exception associated with status s. |
381 |
< |
* |
382 |
< |
* @throws the exception |
383 |
< |
*/ |
384 |
< |
private void reportException(int s) { |
385 |
< |
if ((s &= COMPLETION_MASK) < NORMAL) { |
386 |
< |
if (s == CANCELLED) |
387 |
< |
throw new CancellationException(); |
388 |
< |
else |
389 |
< |
rethrowException(exceptionMap.get(this)); |
390 |
< |
} |
391 |
< |
} |
392 |
< |
|
393 |
< |
/** |
394 |
< |
* Returns result or throws exception using j.u.c.Future conventions. |
395 |
< |
* Only call when {@code isDone} known to be true or thread known |
396 |
< |
* to be interrupted. |
397 |
< |
*/ |
398 |
< |
private V reportFutureResult() |
399 |
< |
throws InterruptedException, ExecutionException { |
400 |
< |
if (Thread.interrupted()) |
401 |
< |
throw new InterruptedException(); |
402 |
< |
int s = status & COMPLETION_MASK; |
403 |
< |
if (s < NORMAL) { |
404 |
< |
Throwable ex; |
405 |
< |
if (s == CANCELLED) |
406 |
< |
throw new CancellationException(); |
407 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
408 |
< |
throw new ExecutionException(ex); |
409 |
< |
} |
410 |
< |
return getRawResult(); |
411 |
< |
} |
412 |
< |
|
413 |
< |
/** |
414 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
415 |
< |
* with timeouts. |
416 |
< |
*/ |
417 |
< |
private V reportTimedFutureResult() |
418 |
< |
throws InterruptedException, ExecutionException, TimeoutException { |
419 |
< |
if (Thread.interrupted()) |
420 |
< |
throw new InterruptedException(); |
421 |
< |
Throwable ex; |
422 |
< |
int s = status & COMPLETION_MASK; |
423 |
< |
if (s == NORMAL) |
424 |
< |
return getRawResult(); |
425 |
< |
else if (s == CANCELLED) |
426 |
< |
throw new CancellationException(); |
427 |
< |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
428 |
< |
throw new ExecutionException(ex); |
429 |
< |
else |
430 |
< |
throw new TimeoutException(); |
431 |
< |
} |
432 |
< |
|
433 |
< |
// internal execution methods |
434 |
< |
|
435 |
< |
/** |
436 |
< |
* Calls exec, recording completion, and rethrowing exception if |
437 |
< |
* encountered. Caller should normally check status before calling. |
438 |
< |
* |
439 |
< |
* @return true if completed normally |
440 |
< |
*/ |
441 |
< |
private boolean tryExec() { |
442 |
< |
try { // try block must contain only call to exec |
443 |
< |
if (!exec()) |
444 |
< |
return false; |
445 |
< |
} catch (Throwable rex) { |
446 |
< |
setDoneExceptionally(rex); |
447 |
< |
rethrowException(rex); |
448 |
< |
return false; // not reached |
449 |
< |
} |
450 |
< |
setNormalCompletion(); |
451 |
< |
return true; |
452 |
< |
} |
453 |
< |
|
454 |
< |
/** |
455 |
< |
* Main execution method used by worker threads. Invokes |
456 |
< |
* base computation unless already complete. |
301 |
> |
* Unless done, calls exec and records status if completed, but |
302 |
> |
* doesn't wait for completion otherwise. Primary execution method |
303 |
> |
* for ForkJoinWorkerThread. |
304 |
|
*/ |
305 |
|
final void quietlyExec() { |
459 |
– |
if (status >= 0) { |
460 |
– |
try { |
461 |
– |
if (!exec()) |
462 |
– |
return; |
463 |
– |
} catch (Throwable rex) { |
464 |
– |
setDoneExceptionally(rex); |
465 |
– |
return; |
466 |
– |
} |
467 |
– |
setNormalCompletion(); |
468 |
– |
} |
469 |
– |
} |
470 |
– |
|
471 |
– |
/** |
472 |
– |
* Calls exec(), recording but not rethrowing exception. |
473 |
– |
* Caller should normally check status before calling. |
474 |
– |
* |
475 |
– |
* @return true if completed normally |
476 |
– |
*/ |
477 |
– |
private boolean tryQuietlyInvoke() { |
306 |
|
try { |
307 |
< |
if (!exec()) |
308 |
< |
return false; |
307 |
> |
if (status < 0 || !exec()) |
308 |
> |
return; |
309 |
|
} catch (Throwable rex) { |
310 |
< |
setDoneExceptionally(rex); |
311 |
< |
return false; |
484 |
< |
} |
485 |
< |
setNormalCompletion(); |
486 |
< |
return true; |
487 |
< |
} |
488 |
< |
|
489 |
< |
/** |
490 |
< |
* Cancels, ignoring any exceptions it throws. |
491 |
< |
*/ |
492 |
< |
final void cancelIgnoringExceptions() { |
493 |
< |
try { |
494 |
< |
cancel(false); |
495 |
< |
} catch (Throwable ignore) { |
310 |
> |
setExceptionalCompletion(rex); |
311 |
> |
return; |
312 |
|
} |
313 |
< |
} |
498 |
< |
|
499 |
< |
/** |
500 |
< |
* Main implementation of helpJoin |
501 |
< |
*/ |
502 |
< |
private int busyJoin(ForkJoinWorkerThread w) { |
503 |
< |
int s; |
504 |
< |
ForkJoinTask<?> t; |
505 |
< |
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
506 |
< |
t.quietlyExec(); |
507 |
< |
return (s >= 0) ? awaitDone(w, false) : s; // block if no work |
313 |
> |
setCompletion(NORMAL); // must be outside try block |
314 |
|
} |
315 |
|
|
316 |
|
// public methods |
319 |
|
* Arranges to asynchronously execute this task. While it is not |
320 |
|
* necessarily enforced, it is a usage error to fork a task more |
321 |
|
* than once unless it has completed and been reinitialized. |
322 |
+ |
* Subsequent modifications to the state of this task or any data |
323 |
+ |
* it operates on are not necessarily consistently observable by |
324 |
+ |
* any thread other than the one executing it unless preceded by a |
325 |
+ |
* call to {@link #join} or related methods, or a call to {@link |
326 |
+ |
* #isDone} returning {@code true}. |
327 |
|
* |
328 |
|
* <p>This method may be invoked only from within {@code |
329 |
|
* ForkJoinTask} computations (as may be determined using method |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* Returns the result of the computation when it is ready. |
344 |
< |
* This method differs from {@link #get()} in that |
343 |
> |
* Returns the result of the computation when it {@link #isDone is |
344 |
> |
* done}. This method differs from {@link #get()} in that |
345 |
|
* abnormal completion results in {@code RuntimeException} or |
346 |
< |
* {@code Error}, not {@code ExecutionException}. |
346 |
> |
* {@code Error}, not {@code ExecutionException}, and that |
347 |
> |
* interrupts of the calling thread do <em>not</em> cause the |
348 |
> |
* method to abruptly return by throwing {@code |
349 |
> |
* InterruptedException}. |
350 |
|
* |
351 |
|
* @return the computed result |
352 |
|
*/ |
353 |
|
public final V join() { |
354 |
< |
ForkJoinWorkerThread w = getWorker(); |
355 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryExec()) |
356 |
< |
reportException(awaitDone(w, true)); |
354 |
> |
quietlyJoin(); |
355 |
> |
Throwable ex; |
356 |
> |
if (status < NORMAL && (ex = getException()) != null) |
357 |
> |
UNSAFE.throwException(ex); |
358 |
|
return getRawResult(); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
|
* Commences performing this task, awaits its completion if |
363 |
< |
* necessary, and return its result, or throws an (unchecked) |
364 |
< |
* exception if the underlying computation did so. |
363 |
> |
* necessary, and returns its result, or throws an (unchecked) |
364 |
> |
* {@code RuntimeException} or {@code Error} if the underlying |
365 |
> |
* computation did so. |
366 |
|
* |
367 |
|
* @return the computed result |
368 |
|
*/ |
369 |
|
public final V invoke() { |
370 |
< |
if (status >= 0 && tryExec()) |
371 |
< |
return getRawResult(); |
372 |
< |
else |
373 |
< |
return join(); |
370 |
> |
quietlyInvoke(); |
371 |
> |
Throwable ex; |
372 |
> |
if (status < NORMAL && (ex = getException()) != null) |
373 |
> |
UNSAFE.throwException(ex); |
374 |
> |
return getRawResult(); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
379 |
|
* each task or an (unchecked) exception is encountered, in which |
380 |
< |
* case the exception is rethrown. If more than one task |
380 |
> |
* case the exception is rethrown. If more than one task |
381 |
|
* encounters an exception, then this method throws any one of |
382 |
< |
* these exceptions. The individual status of each task may be |
383 |
< |
* checked using {@link #getException()} and related methods. |
382 |
> |
* these exceptions. If any task encounters an exception, the |
383 |
> |
* other may be cancelled. However, the execution status of |
384 |
> |
* individual tasks is not guaranteed upon exceptional return. The |
385 |
> |
* status of each task may be obtained using {@link |
386 |
> |
* #getException()} and related methods to check if they have been |
387 |
> |
* cancelled, completed normally or exceptionally, or left |
388 |
> |
* unprocessed. |
389 |
|
* |
390 |
|
* <p>This method may be invoked only from within {@code |
391 |
< |
* ForkJoinTask} computations (as may be determined using method |
391 |
> |
* ForkJoinPool} computations (as may be determined using method |
392 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
393 |
|
* result in exceptions or errors, possibly including {@code |
394 |
|
* ClassCastException}. |
406 |
|
/** |
407 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
408 |
|
* each task or an (unchecked) exception is encountered, in which |
409 |
< |
* case the exception is rethrown. If any task encounters an |
410 |
< |
* exception, others may be, but are not guaranteed to be, |
411 |
< |
* cancelled. If more than one task encounters an exception, then |
412 |
< |
* this method throws any one of these exceptions. The individual |
413 |
< |
* status of each task may be checked using {@link #getException()} |
414 |
< |
* and related methods. |
409 |
> |
* case the exception is rethrown. If more than one task |
410 |
> |
* encounters an exception, then this method throws any one of |
411 |
> |
* these exceptions. If any task encounters an exception, others |
412 |
> |
* may be cancelled. However, the execution status of individual |
413 |
> |
* tasks is not guaranteed upon exceptional return. The status of |
414 |
> |
* each task may be obtained using {@link #getException()} and |
415 |
> |
* related methods to check if they have been cancelled, completed |
416 |
> |
* normally or exceptionally, or left unprocessed. |
417 |
|
* |
418 |
|
* <p>This method may be invoked only from within {@code |
419 |
< |
* ForkJoinTask} computations (as may be determined using method |
419 |
> |
* ForkJoinPool} computations (as may be determined using method |
420 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
421 |
|
* result in exceptions or errors, possibly including {@code |
422 |
|
* ClassCastException}. |
437 |
|
t.fork(); |
438 |
|
else { |
439 |
|
t.quietlyInvoke(); |
440 |
< |
if (ex == null) |
440 |
> |
if (ex == null && t.status < NORMAL) |
441 |
|
ex = t.getException(); |
442 |
|
} |
443 |
|
} |
448 |
|
t.cancel(false); |
449 |
|
else { |
450 |
|
t.quietlyJoin(); |
451 |
< |
if (ex == null) |
451 |
> |
if (ex == null && t.status < NORMAL) |
452 |
|
ex = t.getException(); |
453 |
|
} |
454 |
|
} |
455 |
|
} |
456 |
|
if (ex != null) |
457 |
< |
rethrowException(ex); |
457 |
> |
UNSAFE.throwException(ex); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
|
* Forks all tasks in the specified collection, returning when |
462 |
|
* {@code isDone} holds for each task or an (unchecked) exception |
463 |
< |
* is encountered. If any task encounters an exception, others |
464 |
< |
* may be, but are not guaranteed to be, cancelled. If more than |
465 |
< |
* one task encounters an exception, then this method throws any |
466 |
< |
* one of these exceptions. The individual status of each task |
467 |
< |
* may be checked using {@link #getException()} and related |
468 |
< |
* methods. The behavior of this operation is undefined if the |
469 |
< |
* specified collection is modified while the operation is in |
470 |
< |
* progress. |
463 |
> |
* is encountered, in which case the exception is rethrown. If |
464 |
> |
* more than one task encounters an exception, then this method |
465 |
> |
* throws any one of these exceptions. If any task encounters an |
466 |
> |
* exception, others may be cancelled. However, the execution |
467 |
> |
* status of individual tasks is not guaranteed upon exceptional |
468 |
> |
* return. The status of each task may be obtained using {@link |
469 |
> |
* #getException()} and related methods to check if they have been |
470 |
> |
* cancelled, completed normally or exceptionally, or left |
471 |
> |
* unprocessed. |
472 |
|
* |
473 |
|
* <p>This method may be invoked only from within {@code |
474 |
< |
* ForkJoinTask} computations (as may be determined using method |
474 |
> |
* ForkJoinPool} computations (as may be determined using method |
475 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
476 |
|
* result in exceptions or errors, possibly including {@code |
477 |
|
* ClassCastException}. |
500 |
|
t.fork(); |
501 |
|
else { |
502 |
|
t.quietlyInvoke(); |
503 |
< |
if (ex == null) |
503 |
> |
if (ex == null && t.status < NORMAL) |
504 |
|
ex = t.getException(); |
505 |
|
} |
506 |
|
} |
511 |
|
t.cancel(false); |
512 |
|
else { |
513 |
|
t.quietlyJoin(); |
514 |
< |
if (ex == null) |
514 |
> |
if (ex == null && t.status < NORMAL) |
515 |
|
ex = t.getException(); |
516 |
|
} |
517 |
|
} |
518 |
|
} |
519 |
|
if (ex != null) |
520 |
< |
rethrowException(ex); |
520 |
> |
UNSAFE.throwException(ex); |
521 |
|
return tasks; |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
|
* Attempts to cancel execution of this task. This attempt will |
526 |
< |
* fail if the task has already completed, has already been |
527 |
< |
* cancelled, or could not be cancelled for some other reason. If |
528 |
< |
* successful, and this task has not started when cancel is |
529 |
< |
* called, execution of this task is suppressed, {@link |
530 |
< |
* #isCancelled} will report true, and {@link #join} will result |
531 |
< |
* in a {@code CancellationException} being thrown. |
526 |
> |
* fail if the task has already completed or could not be |
527 |
> |
* cancelled for some other reason. If successful, and this task |
528 |
> |
* has not started when {@code cancel} is called, execution of |
529 |
> |
* this task is suppressed. After this method returns |
530 |
> |
* successfully, unless there is an intervening call to {@link |
531 |
> |
* #reinitialize}, subsequent calls to {@link #isCancelled}, |
532 |
> |
* {@link #isDone}, and {@code cancel} will return {@code true} |
533 |
> |
* and calls to {@link #join} and related methods will result in |
534 |
> |
* {@code CancellationException}. |
535 |
|
* |
536 |
|
* <p>This method may be overridden in subclasses, but if so, must |
537 |
< |
* still ensure that these minimal properties hold. In particular, |
538 |
< |
* the {@code cancel} method itself must not throw exceptions. |
537 |
> |
* still ensure that these properties hold. In particular, the |
538 |
> |
* {@code cancel} method itself must not throw exceptions. |
539 |
|
* |
540 |
|
* <p>This method is designed to be invoked by <em>other</em> |
541 |
|
* tasks. To terminate the current task, you can just return or |
542 |
|
* throw an unchecked exception from its computation method, or |
543 |
|
* invoke {@link #completeExceptionally}. |
544 |
|
* |
545 |
< |
* @param mayInterruptIfRunning this value is ignored in the |
546 |
< |
* default implementation because tasks are not |
547 |
< |
* cancelled via interruption |
545 |
> |
* @param mayInterruptIfRunning this value has no effect in the |
546 |
> |
* default implementation because interrupts are not used to |
547 |
> |
* control cancellation. |
548 |
|
* |
549 |
|
* @return {@code true} if this task is now cancelled |
550 |
|
*/ |
551 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
552 |
|
setCompletion(CANCELLED); |
553 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
553 |
> |
return status == CANCELLED; |
554 |
|
} |
555 |
|
|
556 |
|
/** |
557 |
< |
* Returns {@code true} if the computation performed by this task |
558 |
< |
* has completed (or has been cancelled). |
559 |
< |
* |
560 |
< |
* @return {@code true} if this computation has completed |
557 |
> |
* Cancels, ignoring any exceptions thrown by cancel. Used during |
558 |
> |
* worker and pool shutdown. Cancel is spec'ed not to throw any |
559 |
> |
* exceptions, but if it does anyway, we have no recourse during |
560 |
> |
* shutdown, so guard against this case. |
561 |
|
*/ |
562 |
< |
public final boolean isDone() { |
563 |
< |
return status < 0; |
562 |
> |
final void cancelIgnoringExceptions() { |
563 |
> |
try { |
564 |
> |
cancel(false); |
565 |
> |
} catch (Throwable ignore) { |
566 |
> |
} |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* Returns {@code true} if this task was cancelled. |
571 |
< |
* |
741 |
< |
* @return {@code true} if this task was cancelled |
570 |
> |
* Cancels if current thread is a terminating worker thread, |
571 |
> |
* ignoring any exceptions thrown by cancel. |
572 |
|
*/ |
573 |
+ |
final void cancelIfTerminating() { |
574 |
+ |
Thread t = Thread.currentThread(); |
575 |
+ |
if ((t instanceof ForkJoinWorkerThread) && |
576 |
+ |
((ForkJoinWorkerThread) t).isTerminating()) { |
577 |
+ |
try { |
578 |
+ |
cancel(false); |
579 |
+ |
} catch (Throwable ignore) { |
580 |
+ |
} |
581 |
+ |
} |
582 |
+ |
} |
583 |
+ |
|
584 |
+ |
public final boolean isDone() { |
585 |
+ |
return status < 0; |
586 |
+ |
} |
587 |
+ |
|
588 |
|
public final boolean isCancelled() { |
589 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
589 |
> |
return status == CANCELLED; |
590 |
|
} |
591 |
|
|
592 |
|
/** |
595 |
|
* @return {@code true} if this task threw an exception or was cancelled |
596 |
|
*/ |
597 |
|
public final boolean isCompletedAbnormally() { |
598 |
< |
return (status & COMPLETION_MASK) < NORMAL; |
598 |
> |
return status < NORMAL; |
599 |
|
} |
600 |
|
|
601 |
|
/** |
606 |
|
* exception and was not cancelled |
607 |
|
*/ |
608 |
|
public final boolean isCompletedNormally() { |
609 |
< |
return (status & COMPLETION_MASK) == NORMAL; |
765 |
< |
} |
766 |
< |
|
767 |
< |
/** |
768 |
< |
* Returns {@code true} if this task threw an exception. |
769 |
< |
* |
770 |
< |
* @return {@code true} if this task threw an exception |
771 |
< |
*/ |
772 |
< |
public final boolean isCompletedExceptionally() { |
773 |
< |
return (status & COMPLETION_MASK) == EXCEPTIONAL; |
609 |
> |
return status == NORMAL; |
610 |
|
} |
611 |
|
|
612 |
|
/** |
617 |
|
* @return the exception, or {@code null} if none |
618 |
|
*/ |
619 |
|
public final Throwable getException() { |
620 |
< |
int s = status & COMPLETION_MASK; |
620 |
> |
int s = status; |
621 |
|
return ((s >= NORMAL) ? null : |
622 |
|
(s == CANCELLED) ? new CancellationException() : |
623 |
|
exceptionMap.get(this)); |
633 |
|
* overridable, but overridden versions must invoke {@code super} |
634 |
|
* implementation to maintain guarantees. |
635 |
|
* |
636 |
< |
* @param ex the exception to throw. If this exception is |
637 |
< |
* not a RuntimeException or Error, the actual exception thrown |
638 |
< |
* will be a RuntimeException with cause ex. |
636 |
> |
* @param ex the exception to throw. If this exception is not a |
637 |
> |
* {@code RuntimeException} or {@code Error}, the actual exception |
638 |
> |
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
639 |
|
*/ |
640 |
|
public void completeExceptionally(Throwable ex) { |
641 |
< |
setDoneExceptionally((ex instanceof RuntimeException) || |
642 |
< |
(ex instanceof Error) ? ex : |
643 |
< |
new RuntimeException(ex)); |
641 |
> |
setExceptionalCompletion((ex instanceof RuntimeException) || |
642 |
> |
(ex instanceof Error) ? ex : |
643 |
> |
new RuntimeException(ex)); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
|
* Completes this task, and if not already aborted or cancelled, |
648 |
< |
* returning a {@code null} result upon {@code join} and related |
649 |
< |
* operations. This method may be used to provide results for |
650 |
< |
* asynchronous tasks, or to provide alternative handling for |
651 |
< |
* tasks that would not otherwise complete normally. Its use in |
652 |
< |
* other situations is discouraged. This method is |
653 |
< |
* overridable, but overridden versions must invoke {@code super} |
654 |
< |
* implementation to maintain guarantees. |
648 |
> |
* returning the given value as the result of subsequent |
649 |
> |
* invocations of {@code join} and related operations. This method |
650 |
> |
* may be used to provide results for asynchronous tasks, or to |
651 |
> |
* provide alternative handling for tasks that would not otherwise |
652 |
> |
* complete normally. Its use in other situations is |
653 |
> |
* discouraged. This method is overridable, but overridden |
654 |
> |
* versions must invoke {@code super} implementation to maintain |
655 |
> |
* guarantees. |
656 |
|
* |
657 |
|
* @param value the result value for this task |
658 |
|
*/ |
660 |
|
try { |
661 |
|
setRawResult(value); |
662 |
|
} catch (Throwable rex) { |
663 |
< |
setDoneExceptionally(rex); |
663 |
> |
setExceptionalCompletion(rex); |
664 |
|
return; |
665 |
|
} |
666 |
< |
setNormalCompletion(); |
830 |
< |
} |
831 |
< |
|
832 |
< |
public final V get() throws InterruptedException, ExecutionException { |
833 |
< |
ForkJoinWorkerThread w = getWorker(); |
834 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
835 |
< |
awaitDone(w, true); |
836 |
< |
return reportFutureResult(); |
837 |
< |
} |
838 |
< |
|
839 |
< |
public final V get(long timeout, TimeUnit unit) |
840 |
< |
throws InterruptedException, ExecutionException, TimeoutException { |
841 |
< |
long nanos = unit.toNanos(timeout); |
842 |
< |
ForkJoinWorkerThread w = getWorker(); |
843 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
844 |
< |
awaitDone(w, nanos); |
845 |
< |
return reportTimedFutureResult(); |
666 |
> |
setCompletion(NORMAL); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* Possibly executes other tasks until this task is ready, then |
671 |
< |
* returns the result of the computation. This method may be more |
851 |
< |
* efficient than {@code join}, but is only applicable when |
852 |
< |
* there are no potential dependencies between continuation of the |
853 |
< |
* current task and that of any other task that might be executed |
854 |
< |
* while helping. (This usually holds for pure divide-and-conquer |
855 |
< |
* tasks). |
856 |
< |
* |
857 |
< |
* <p>This method may be invoked only from within {@code |
858 |
< |
* ForkJoinTask} computations (as may be determined using method |
859 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
860 |
< |
* result in exceptions or errors, possibly including {@code |
861 |
< |
* ClassCastException}. |
670 |
> |
* Waits if necessary for the computation to complete, and then |
671 |
> |
* retrieves its result. |
672 |
|
* |
673 |
|
* @return the computed result |
674 |
+ |
* @throws CancellationException if the computation was cancelled |
675 |
+ |
* @throws ExecutionException if the computation threw an |
676 |
+ |
* exception |
677 |
+ |
* @throws InterruptedException if the current thread is not a |
678 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
679 |
|
*/ |
680 |
< |
public final V helpJoin() { |
681 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
682 |
< |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
683 |
< |
reportException(busyJoin(w)); |
680 |
> |
public final V get() throws InterruptedException, ExecutionException { |
681 |
> |
int s; |
682 |
> |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) { |
683 |
> |
quietlyJoin(); |
684 |
> |
s = status; |
685 |
> |
} |
686 |
> |
else { |
687 |
> |
while ((s = status) >= 0) { |
688 |
> |
synchronized (this) { // interruptible form of awaitDone |
689 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
690 |
> |
s, SIGNAL)) { |
691 |
> |
while (status >= 0) |
692 |
> |
wait(); |
693 |
> |
} |
694 |
> |
} |
695 |
> |
} |
696 |
> |
} |
697 |
> |
if (s < NORMAL) { |
698 |
> |
Throwable ex; |
699 |
> |
if (s == CANCELLED) |
700 |
> |
throw new CancellationException(); |
701 |
> |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
702 |
> |
throw new ExecutionException(ex); |
703 |
> |
} |
704 |
|
return getRawResult(); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* Possibly executes other tasks until this task is ready. This |
709 |
< |
* method may be useful when processing collections of tasks when |
875 |
< |
* some have been cancelled or otherwise known to have aborted. |
708 |
> |
* Waits if necessary for at most the given time for the computation |
709 |
> |
* to complete, and then retrieves its result, if available. |
710 |
|
* |
711 |
< |
* <p>This method may be invoked only from within {@code |
712 |
< |
* ForkJoinTask} computations (as may be determined using method |
713 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
714 |
< |
* result in exceptions or errors, possibly including {@code |
715 |
< |
* ClassCastException}. |
711 |
> |
* @param timeout the maximum time to wait |
712 |
> |
* @param unit the time unit of the timeout argument |
713 |
> |
* @return the computed result |
714 |
> |
* @throws CancellationException if the computation was cancelled |
715 |
> |
* @throws ExecutionException if the computation threw an |
716 |
> |
* exception |
717 |
> |
* @throws InterruptedException if the current thread is not a |
718 |
> |
* member of a ForkJoinPool and was interrupted while waiting |
719 |
> |
* @throws TimeoutException if the wait timed out |
720 |
|
*/ |
721 |
< |
public final void quietlyHelpJoin() { |
721 |
> |
public final V get(long timeout, TimeUnit unit) |
722 |
> |
throws InterruptedException, ExecutionException, TimeoutException { |
723 |
> |
long nanos = unit.toNanos(timeout); |
724 |
|
if (status >= 0) { |
725 |
< |
ForkJoinWorkerThread w = |
726 |
< |
(ForkJoinWorkerThread) Thread.currentThread(); |
727 |
< |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
728 |
< |
busyJoin(w); |
725 |
> |
Thread t = Thread.currentThread(); |
726 |
> |
if (t instanceof ForkJoinWorkerThread) { |
727 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
728 |
> |
boolean completed = false; // timed variant of quietlyJoin |
729 |
> |
if (w.unpushTask(this)) { |
730 |
> |
try { |
731 |
> |
completed = exec(); |
732 |
> |
} catch (Throwable rex) { |
733 |
> |
setExceptionalCompletion(rex); |
734 |
> |
} |
735 |
> |
} |
736 |
> |
if (completed) |
737 |
> |
setCompletion(NORMAL); |
738 |
> |
else if (status >= 0) |
739 |
> |
w.joinTask(this, true, nanos); |
740 |
> |
} |
741 |
> |
else if (Thread.interrupted()) |
742 |
> |
throw new InterruptedException(); |
743 |
> |
else { |
744 |
> |
long startTime = System.nanoTime(); |
745 |
> |
int s; long nt; |
746 |
> |
while ((s = status) >= 0 && |
747 |
> |
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
748 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, |
749 |
> |
SIGNAL)) { |
750 |
> |
long ms = nt / 1000000; |
751 |
> |
int ns = (int) (nt % 1000000); |
752 |
> |
synchronized (this) { |
753 |
> |
if (status >= 0) |
754 |
> |
wait(ms, ns); // exit on IE throw |
755 |
> |
} |
756 |
> |
} |
757 |
> |
} |
758 |
> |
} |
759 |
> |
} |
760 |
> |
int es = status; |
761 |
> |
if (es != NORMAL) { |
762 |
> |
Throwable ex; |
763 |
> |
if (es == CANCELLED) |
764 |
> |
throw new CancellationException(); |
765 |
> |
if (es == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
766 |
> |
throw new ExecutionException(ex); |
767 |
> |
throw new TimeoutException(); |
768 |
|
} |
769 |
+ |
return getRawResult(); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* Joins this task, without returning its result or throwing an |
773 |
> |
* Joins this task, without returning its result or throwing its |
774 |
|
* exception. This method may be useful when processing |
775 |
|
* collections of tasks when some have been cancelled or otherwise |
776 |
|
* known to have aborted. |
777 |
|
*/ |
778 |
|
public final void quietlyJoin() { |
779 |
< |
if (status >= 0) { |
780 |
< |
ForkJoinWorkerThread w = getWorker(); |
781 |
< |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
782 |
< |
awaitDone(w, true); |
779 |
> |
Thread t; |
780 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
781 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
782 |
> |
if (status >= 0) { |
783 |
> |
if (w.unpushTask(this)) { |
784 |
> |
boolean completed; |
785 |
> |
try { |
786 |
> |
completed = exec(); |
787 |
> |
} catch (Throwable rex) { |
788 |
> |
setExceptionalCompletion(rex); |
789 |
> |
return; |
790 |
> |
} |
791 |
> |
if (completed) { |
792 |
> |
setCompletion(NORMAL); |
793 |
> |
return; |
794 |
> |
} |
795 |
> |
} |
796 |
> |
w.joinTask(this, false, 0L); |
797 |
> |
} |
798 |
|
} |
799 |
+ |
else |
800 |
+ |
externalAwaitDone(); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* Commences performing this task and awaits its completion if |
805 |
< |
* necessary, without returning its result or throwing an |
806 |
< |
* exception. This method may be useful when processing |
910 |
< |
* collections of tasks when some have been cancelled or otherwise |
911 |
< |
* known to have aborted. |
805 |
> |
* necessary, without returning its result or throwing its |
806 |
> |
* exception. |
807 |
|
*/ |
808 |
|
public final void quietlyInvoke() { |
809 |
< |
if (status >= 0 && !tryQuietlyInvoke()) |
810 |
< |
quietlyJoin(); |
809 |
> |
if (status >= 0) { |
810 |
> |
boolean completed; |
811 |
> |
try { |
812 |
> |
completed = exec(); |
813 |
> |
} catch (Throwable rex) { |
814 |
> |
setExceptionalCompletion(rex); |
815 |
> |
return; |
816 |
> |
} |
817 |
> |
if (completed) |
818 |
> |
setCompletion(NORMAL); |
819 |
> |
else |
820 |
> |
quietlyJoin(); |
821 |
> |
} |
822 |
|
} |
823 |
|
|
824 |
|
/** |
829 |
|
* processed. |
830 |
|
* |
831 |
|
* <p>This method may be invoked only from within {@code |
832 |
< |
* ForkJoinTask} computations (as may be determined using method |
832 |
> |
* ForkJoinPool} computations (as may be determined using method |
833 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
834 |
|
* result in exceptions or errors, possibly including {@code |
835 |
|
* ClassCastException}. |
848 |
|
* under any other usage conditions are not guaranteed. |
849 |
|
* This method may be useful when executing |
850 |
|
* pre-constructed trees of subtasks in loops. |
851 |
+ |
* |
852 |
+ |
* <p>Upon completion of this method, {@code isDone()} reports |
853 |
+ |
* {@code false}, and {@code getException()} reports {@code |
854 |
+ |
* null}. However, the value returned by {@code getRawResult} is |
855 |
+ |
* unaffected. To clear this value, you can invoke {@code |
856 |
+ |
* setRawResult(null)}. |
857 |
|
*/ |
858 |
|
public void reinitialize() { |
859 |
< |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
859 |
> |
if (status == EXCEPTIONAL) |
860 |
|
exceptionMap.remove(this); |
861 |
|
status = 0; |
862 |
|
} |
894 |
|
* were not, stolen. |
895 |
|
* |
896 |
|
* <p>This method may be invoked only from within {@code |
897 |
< |
* ForkJoinTask} computations (as may be determined using method |
897 |
> |
* ForkJoinPool} computations (as may be determined using method |
898 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
899 |
|
* result in exceptions or errors, possibly including {@code |
900 |
|
* ClassCastException}. |
913 |
|
* fork other tasks. |
914 |
|
* |
915 |
|
* <p>This method may be invoked only from within {@code |
916 |
< |
* ForkJoinTask} computations (as may be determined using method |
916 |
> |
* ForkJoinPool} computations (as may be determined using method |
917 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
918 |
|
* result in exceptions or errors, possibly including {@code |
919 |
|
* ClassCastException}. |
936 |
|
* exceeded. |
937 |
|
* |
938 |
|
* <p>This method may be invoked only from within {@code |
939 |
< |
* ForkJoinTask} computations (as may be determined using method |
939 |
> |
* ForkJoinPool} computations (as may be determined using method |
940 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
941 |
|
* result in exceptions or errors, possibly including {@code |
942 |
|
* ClassCastException}. |
994 |
|
* otherwise. |
995 |
|
* |
996 |
|
* <p>This method may be invoked only from within {@code |
997 |
< |
* ForkJoinTask} computations (as may be determined using method |
997 |
> |
* ForkJoinPool} computations (as may be determined using method |
998 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
999 |
|
* result in exceptions or errors, possibly including {@code |
1000 |
|
* ClassCastException}. |
1013 |
|
* be useful otherwise. |
1014 |
|
* |
1015 |
|
* <p>This method may be invoked only from within {@code |
1016 |
< |
* ForkJoinTask} computations (as may be determined using method |
1016 |
> |
* ForkJoinPool} computations (as may be determined using method |
1017 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1018 |
|
* result in exceptions or errors, possibly including {@code |
1019 |
|
* ClassCastException}. |
1036 |
|
* otherwise. |
1037 |
|
* |
1038 |
|
* <p>This method may be invoked only from within {@code |
1039 |
< |
* ForkJoinTask} computations (as may be determined using method |
1039 |
> |
* ForkJoinPool} computations (as may be determined using method |
1040 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1041 |
|
* result in exceptions or errors, possibly including {@code |
1042 |
|
* ClassCastException}. |
1146 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* Save the state to a stream. |
1149 |
> |
* Saves the state to a stream (that is, serializes it). |
1150 |
|
* |
1151 |
|
* @serialData the current run status and the exception thrown |
1152 |
|
* during execution, or {@code null} if none |
1159 |
|
} |
1160 |
|
|
1161 |
|
/** |
1162 |
< |
* Reconstitute the instance from a stream. |
1162 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1163 |
|
* |
1164 |
|
* @param s the stream |
1165 |
|
*/ |
1166 |
|
private void readObject(java.io.ObjectInputStream s) |
1167 |
|
throws java.io.IOException, ClassNotFoundException { |
1168 |
|
s.defaultReadObject(); |
1257 |
– |
status &= ~INTERNAL_SIGNAL_MASK; // clear internal signal counts |
1258 |
– |
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1169 |
|
Object ex = s.readObject(); |
1170 |
|
if (ex != null) |
1171 |
< |
setDoneExceptionally((Throwable) ex); |
1171 |
> |
setExceptionalCompletion((Throwable) ex); |
1172 |
|
} |
1173 |
|
|
1174 |
|
// Unsafe mechanics |