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 |
134 |
|
* computation. Large tasks should be split into smaller subtasks, |
135 |
|
* usually via recursive decomposition. As a very rough rule of thumb, |
136 |
|
* a task should perform more than 100 and less than 10000 basic |
137 |
< |
* computational steps. If tasks are too big, then parallelism cannot |
138 |
< |
* improve throughput. If too small, then memory and internal task |
139 |
< |
* maintenance overhead may overwhelm processing. |
137 |
> |
* computational steps, and should avoid indefinite looping. If tasks |
138 |
> |
* are too big, then parallelism cannot improve throughput. If too |
139 |
> |
* small, then memory and internal task maintenance overhead may |
140 |
> |
* overwhelm processing. |
141 |
|
* |
142 |
< |
* <p>This class provides {@code adapt} methods for {@link |
143 |
< |
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
144 |
< |
* may be of use when mixing execution of ForkJoinTasks with other |
145 |
< |
* kinds of tasks. When all tasks are of this form, consider using a |
132 |
< |
* pool in {@link ForkJoinPool#setAsyncMode async mode}. |
142 |
> |
* <p>This class provides {@code adapt} methods for {@link Runnable} |
143 |
> |
* and {@link Callable}, that may be of use when mixing execution of |
144 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks are |
145 |
> |
* of this form, consider using a pool constructed in <em>asyncMode</em>. |
146 |
|
* |
147 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
148 |
|
* used in extensions such as remote execution frameworks. It is |
154 |
|
*/ |
155 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
156 |
|
|
157 |
< |
/** |
158 |
< |
* Run control status bits packed into a single int to minimize |
159 |
< |
* footprint and to ensure atomicity (via CAS). Status is |
160 |
< |
* initially zero, and takes on nonnegative values until |
161 |
< |
* completed, upon which status holds COMPLETED. CANCELLED, or |
162 |
< |
* EXCEPTIONAL, which use the top 3 bits. Tasks undergoing |
163 |
< |
* blocking waits by other threads have SIGNAL_MASK bits set -- |
164 |
< |
* bit 15 for external (nonFJ) waits, and the rest a count of |
165 |
< |
* waiting FJ threads. (This representation relies on |
166 |
< |
* ForkJoinPool max thread limits). Completion of a stolen task |
167 |
< |
* with SIGNAL_MASK bits set awakens waiter via notifyAll. Even |
168 |
< |
* though suboptimal for some purposes, we use basic builtin |
169 |
< |
* wait/notify to take advantage of "monitor inflation" in JVMs |
170 |
< |
* that we would otherwise need to emulate to avoid adding further |
171 |
< |
* per-task bookkeeping overhead. Note that bits 16-28 are |
172 |
< |
* currently unused. Also value 0x80000000 is available as spare |
173 |
< |
* completion value. |
157 |
> |
/* |
158 |
> |
* See the internal documentation of class ForkJoinPool for a |
159 |
> |
* general implementation overview. ForkJoinTasks are mainly |
160 |
> |
* responsible for maintaining their "status" field amidst relays |
161 |
> |
* to methods in ForkJoinWorkerThread and ForkJoinPool. The |
162 |
> |
* methods of this class are more-or-less layered into (1) basic |
163 |
> |
* status maintenance (2) execution and awaiting completion (3) |
164 |
> |
* user-level methods that additionally report results. This is |
165 |
> |
* sometimes hard to see because this file orders exported methods |
166 |
> |
* in a way that flows well in javadocs. In particular, most |
167 |
> |
* join mechanics are in method quietlyJoin, below. |
168 |
> |
*/ |
169 |
> |
|
170 |
> |
/* |
171 |
> |
* The status field holds run control status bits packed into a |
172 |
> |
* single int to minimize footprint and to ensure atomicity (via |
173 |
> |
* CAS). Status is initially zero, and takes on nonnegative |
174 |
> |
* values until completed, upon which status holds value |
175 |
> |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
176 |
> |
* waits by other threads have the SIGNAL bit set. Completion of |
177 |
> |
* a stolen task with SIGNAL set awakens any waiters via |
178 |
> |
* notifyAll. Even though suboptimal for some purposes, we use |
179 |
> |
* basic builtin wait/notify to take advantage of "monitor |
180 |
> |
* inflation" in JVMs that we would otherwise need to emulate to |
181 |
> |
* avoid adding further per-task bookkeeping overhead. We want |
182 |
> |
* these monitors to be "fat", i.e., not use biasing or thin-lock |
183 |
> |
* techniques, so use some odd coding idioms that tend to avoid |
184 |
> |
* them. |
185 |
|
*/ |
186 |
+ |
|
187 |
+ |
/** The run status of this task */ |
188 |
|
volatile int status; // accessed directly by pool and workers |
189 |
|
|
190 |
< |
static final int COMPLETION_MASK = 0xe0000000; |
191 |
< |
static final int NORMAL = 0xe0000000; // == mask |
192 |
< |
static final int CANCELLED = 0xc0000000; |
193 |
< |
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 |
190 |
> |
private static final int NORMAL = -1; |
191 |
> |
private static final int CANCELLED = -2; |
192 |
> |
private static final int EXCEPTIONAL = -3; |
193 |
> |
private static final int SIGNAL = 1; |
194 |
|
|
195 |
|
/** |
196 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
204 |
|
Collections.synchronizedMap |
205 |
|
(new WeakHashMap<ForkJoinTask<?>, Throwable>()); |
206 |
|
|
207 |
< |
// within-package utilities |
207 |
> |
// Maintaining completion status |
208 |
|
|
209 |
|
/** |
210 |
< |
* Gets current worker thread, or null if not a worker thread. |
211 |
< |
*/ |
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 |
< |
} |
198 |
< |
|
199 |
< |
/** |
200 |
< |
* Workaround for not being able to rethrow unchecked exceptions. |
201 |
< |
*/ |
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. |
210 |
> |
* Marks completion and wakes up threads waiting to join this task, |
211 |
> |
* also clearing signal request bits. |
212 |
|
* |
213 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
214 |
|
*/ |
215 |
< |
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(); } |
224 |
< |
} |
225 |
< |
} |
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); |
249 |
< |
} |
250 |
< |
|
251 |
< |
// internal waiting and notification |
252 |
< |
|
253 |
< |
/** |
254 |
< |
* Performs the actual monitor wait for awaitDone. |
255 |
< |
*/ |
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 |
< |
} |
266 |
< |
|
267 |
< |
/** |
268 |
< |
* Performs the actual timed monitor wait for awaitDone. |
269 |
< |
*/ |
270 |
< |
private void doAwaitDone(long startTime, long nanos) { |
271 |
< |
synchronized (this) { |
272 |
< |
try { |
273 |
< |
while (status >= 0) { |
274 |
< |
long nt = nanos - (System.nanoTime() - startTime); |
275 |
< |
if (nt <= 0) |
276 |
< |
break; |
277 |
< |
wait(nt / 1000000, (int) (nt % 1000000)); |
278 |
< |
} |
279 |
< |
} catch (InterruptedException ie) { |
280 |
< |
onInterruptedWait(); |
281 |
< |
} |
282 |
< |
} |
283 |
< |
} |
284 |
< |
|
285 |
< |
// Awaiting completion |
286 |
< |
|
287 |
< |
/** |
288 |
< |
* Sets status to indicate there is joiner, then waits for join, |
289 |
< |
* surrounded with pool notifications. |
290 |
< |
* |
291 |
< |
* @return status upon exit |
292 |
< |
*/ |
293 |
< |
private int awaitDone(ForkJoinWorkerThread w, |
294 |
< |
boolean maintainParallelism) { |
295 |
< |
ForkJoinPool pool = (w == null) ? null : w.pool; |
215 |
> |
private void setCompletion(int completion) { |
216 |
|
int s; |
217 |
|
while ((s = status) >= 0) { |
218 |
< |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
219 |
< |
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
220 |
< |
doAwaitDone(); |
301 |
< |
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
302 |
< |
adjustPoolCountsOnUnblock(pool); |
218 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
219 |
> |
if (s != 0) |
220 |
> |
synchronized (this) { notifyAll(); } |
221 |
|
break; |
222 |
|
} |
223 |
|
} |
306 |
– |
return s; |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* Timed version of awaitDone |
227 |
> |
* Records exception and sets exceptional completion. |
228 |
|
* |
229 |
< |
* @return status upon exit |
229 |
> |
* @return status on exit |
230 |
|
*/ |
231 |
< |
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; |
325 |
< |
} |
326 |
< |
if (s < 0 && (s & INTERNAL_SIGNAL_MASK) != 0) |
327 |
< |
adjustPoolCountsOnUnblock(pool); |
328 |
< |
break; |
329 |
< |
} |
330 |
< |
} |
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); |
343 |
< |
} |
344 |
< |
|
345 |
< |
/** |
346 |
< |
* Notifies pool to adjust counts on cancelled or timed out wait. |
347 |
< |
*/ |
348 |
< |
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
349 |
< |
if (pool != null) { |
350 |
< |
int s; |
351 |
< |
while ((s = status) >= 0 && (s & INTERNAL_SIGNAL_MASK) != 0) { |
352 |
< |
if (casStatus(s, s - 1)) { |
353 |
< |
pool.updateRunningCount(1); |
354 |
< |
break; |
355 |
< |
} |
356 |
< |
} |
357 |
< |
} |
358 |
< |
} |
359 |
< |
|
360 |
< |
/** |
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) { |
231 |
> |
private void setExceptionalCompletion(Throwable rex) { |
232 |
|
exceptionMap.put(this, rex); |
233 |
|
setCompletion(EXCEPTIONAL); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* Throws the exception associated with status s. |
238 |
< |
* |
382 |
< |
* @throws the exception |
237 |
> |
* Blocks a worker thread until completed or timed out. Called |
238 |
> |
* only by pool. |
239 |
|
*/ |
240 |
< |
private void reportException(int s) { |
241 |
< |
if ((s &= COMPLETION_MASK) < NORMAL) { |
242 |
< |
if (s == CANCELLED) |
243 |
< |
throw new CancellationException(); |
244 |
< |
else |
245 |
< |
rethrowException(exceptionMap.get(this)); |
240 |
> |
final void internalAwaitDone(long millis, int nanos) { |
241 |
> |
int s = status; |
242 |
> |
if ((s == 0 && |
243 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL)) || |
244 |
> |
s > 0) { |
245 |
> |
try { // the odd construction reduces lock bias effects |
246 |
> |
synchronized (this) { |
247 |
> |
if (status > 0) |
248 |
> |
wait(millis, nanos); |
249 |
> |
else |
250 |
> |
notifyAll(); |
251 |
> |
} |
252 |
> |
} catch (InterruptedException ie) { |
253 |
> |
cancelIfTerminating(); |
254 |
> |
} |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* Returns result or throws exception using j.u.c.Future conventions. |
395 |
< |
* Only call when {@code isDone} known to be true. |
259 |
> |
* Blocks a non-worker-thread until completion. |
260 |
|
*/ |
261 |
< |
private V reportFutureResult() |
262 |
< |
throws ExecutionException, InterruptedException { |
263 |
< |
if (Thread.interrupted()) |
264 |
< |
throw new InterruptedException(); |
265 |
< |
int s = status & COMPLETION_MASK; |
266 |
< |
if (s < NORMAL) { |
267 |
< |
Throwable ex; |
268 |
< |
if (s == CANCELLED) |
269 |
< |
throw new CancellationException(); |
270 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
271 |
< |
throw new ExecutionException(ex); |
261 |
> |
private void externalAwaitDone() { |
262 |
> |
if (status >= 0) { |
263 |
> |
boolean interrupted = false; |
264 |
> |
synchronized(this) { |
265 |
> |
for (;;) { |
266 |
> |
int s = status; |
267 |
> |
if (s == 0) |
268 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, |
269 |
> |
0, SIGNAL); |
270 |
> |
else if (s < 0) { |
271 |
> |
notifyAll(); |
272 |
> |
break; |
273 |
> |
} |
274 |
> |
else { |
275 |
> |
try { |
276 |
> |
wait(); |
277 |
> |
} catch (InterruptedException ie) { |
278 |
> |
interrupted = true; |
279 |
> |
} |
280 |
> |
} |
281 |
> |
} |
282 |
> |
} |
283 |
> |
if (interrupted) |
284 |
> |
Thread.currentThread().interrupt(); |
285 |
|
} |
409 |
– |
return getRawResult(); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
414 |
< |
* with timeouts. |
289 |
> |
* Blocks a non-worker-thread until completion or interruption or timeout. |
290 |
|
*/ |
291 |
< |
private V reportTimedFutureResult() |
292 |
< |
throws InterruptedException, ExecutionException, TimeoutException { |
291 |
> |
private void externalInterruptibleAwaitDone(boolean timed, long nanos) |
292 |
> |
throws InterruptedException { |
293 |
|
if (Thread.interrupted()) |
294 |
|
throw new InterruptedException(); |
420 |
– |
Throwable ex; |
421 |
– |
int s = status & COMPLETION_MASK; |
422 |
– |
if (s == NORMAL) |
423 |
– |
return getRawResult(); |
424 |
– |
if (s == CANCELLED) |
425 |
– |
throw new CancellationException(); |
426 |
– |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
427 |
– |
throw new ExecutionException(ex); |
428 |
– |
throw new TimeoutException(); |
429 |
– |
} |
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() { |
295 |
|
if (status >= 0) { |
296 |
< |
try { |
297 |
< |
if (!exec()) |
298 |
< |
return; |
299 |
< |
} catch (Throwable rex) { |
300 |
< |
setDoneExceptionally(rex); |
301 |
< |
return; |
296 |
> |
long startTime = timed ? System.nanoTime() : 0L; |
297 |
> |
synchronized(this) { |
298 |
> |
for (;;) { |
299 |
> |
long nt; |
300 |
> |
int s = status; |
301 |
> |
if (s == 0) |
302 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, |
303 |
> |
0, SIGNAL); |
304 |
> |
else if (s < 0) { |
305 |
> |
notifyAll(); |
306 |
> |
break; |
307 |
> |
} |
308 |
> |
else if (!timed) |
309 |
> |
wait(); |
310 |
> |
else if ((nt = nanos - (System.nanoTime()-startTime)) > 0L) |
311 |
> |
wait(nt / 1000000, (int)(nt % 1000000)); |
312 |
> |
else |
313 |
> |
break; |
314 |
> |
} |
315 |
|
} |
465 |
– |
setNormalCompletion(); |
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* Calls exec(), recording but not rethrowing exception. |
321 |
< |
* Caller should normally check status before calling. |
322 |
< |
* |
473 |
< |
* @return true if completed normally |
320 |
> |
* Unless done, calls exec and records status if completed, but |
321 |
> |
* doesn't wait for completion otherwise. Primary execution method |
322 |
> |
* for ForkJoinWorkerThread. |
323 |
|
*/ |
324 |
< |
private boolean tryQuietlyInvoke() { |
324 |
> |
final void quietlyExec() { |
325 |
|
try { |
326 |
< |
if (!exec()) |
327 |
< |
return false; |
326 |
> |
if (status < 0 || !exec()) |
327 |
> |
return; |
328 |
|
} catch (Throwable rex) { |
329 |
< |
setDoneExceptionally(rex); |
330 |
< |
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) { |
329 |
> |
setExceptionalCompletion(rex); |
330 |
> |
return; |
331 |
|
} |
332 |
< |
} |
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 |
332 |
> |
setCompletion(NORMAL); // must be outside try block |
333 |
|
} |
334 |
|
|
335 |
|
// public methods |
338 |
|
* Arranges to asynchronously execute this task. While it is not |
339 |
|
* necessarily enforced, it is a usage error to fork a task more |
340 |
|
* than once unless it has completed and been reinitialized. |
341 |
+ |
* Subsequent modifications to the state of this task or any data |
342 |
+ |
* it operates on are not necessarily consistently observable by |
343 |
+ |
* any thread other than the one executing it unless preceded by a |
344 |
+ |
* call to {@link #join} or related methods, or a call to {@link |
345 |
+ |
* #isDone} returning {@code true}. |
346 |
|
* |
347 |
|
* <p>This method may be invoked only from within {@code |
348 |
< |
* ForkJoinTask} computations (as may be determined using method |
348 |
> |
* ForkJoinPool} computations (as may be determined using method |
349 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
350 |
|
* result in exceptions or errors, possibly including {@code |
351 |
|
* ClassCastException}. |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* Returns the result of the computation when it is ready. |
363 |
< |
* This method differs from {@link #get()} in that |
362 |
> |
* Returns the result of the computation when it {@link #isDone is |
363 |
> |
* done}. This method differs from {@link #get()} in that |
364 |
|
* abnormal completion results in {@code RuntimeException} or |
365 |
< |
* {@code Error}, not {@code ExecutionException}. |
365 |
> |
* {@code Error}, not {@code ExecutionException}, and that |
366 |
> |
* interrupts of the calling thread do <em>not</em> cause the |
367 |
> |
* method to abruptly return by throwing {@code |
368 |
> |
* InterruptedException}. |
369 |
|
* |
370 |
|
* @return the computed result |
371 |
|
*/ |
372 |
|
public final V join() { |
373 |
< |
ForkJoinWorkerThread w = getWorker(); |
374 |
< |
if (w == null || status < 0 || !w.unpushTask(this) || !tryExec()) |
375 |
< |
reportException(awaitDone(w, true)); |
373 |
> |
quietlyJoin(); |
374 |
> |
Throwable ex; |
375 |
> |
if (status < NORMAL && (ex = getException()) != null) |
376 |
> |
UNSAFE.throwException(ex); |
377 |
|
return getRawResult(); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
381 |
|
* Commences performing this task, awaits its completion if |
382 |
< |
* necessary, and return its result, or throws an (unchecked) |
383 |
< |
* exception if the underlying computation did so. |
382 |
> |
* necessary, and returns its result, or throws an (unchecked) |
383 |
> |
* {@code RuntimeException} or {@code Error} if the underlying |
384 |
> |
* computation did so. |
385 |
|
* |
386 |
|
* @return the computed result |
387 |
|
*/ |
388 |
|
public final V invoke() { |
389 |
< |
if (status >= 0 && tryExec()) |
390 |
< |
return getRawResult(); |
391 |
< |
else |
392 |
< |
return join(); |
389 |
> |
quietlyInvoke(); |
390 |
> |
Throwable ex; |
391 |
> |
if (status < NORMAL && (ex = getException()) != null) |
392 |
> |
UNSAFE.throwException(ex); |
393 |
> |
return getRawResult(); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
398 |
|
* each task or an (unchecked) exception is encountered, in which |
399 |
< |
* case the exception is rethrown. If more than one task |
399 |
> |
* case the exception is rethrown. If more than one task |
400 |
|
* encounters an exception, then this method throws any one of |
401 |
< |
* these exceptions. The individual status of each task may be |
402 |
< |
* checked using {@link #getException()} and related methods. |
401 |
> |
* these exceptions. If any task encounters an exception, the |
402 |
> |
* other may be cancelled. However, the execution status of |
403 |
> |
* individual tasks is not guaranteed upon exceptional return. The |
404 |
> |
* status of each task may be obtained using {@link |
405 |
> |
* #getException()} and related methods to check if they have been |
406 |
> |
* cancelled, completed normally or exceptionally, or left |
407 |
> |
* unprocessed. |
408 |
|
* |
409 |
|
* <p>This method may be invoked only from within {@code |
410 |
< |
* ForkJoinTask} computations (as may be determined using method |
410 |
> |
* ForkJoinPool} computations (as may be determined using method |
411 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
412 |
|
* result in exceptions or errors, possibly including {@code |
413 |
|
* ClassCastException}. |
425 |
|
/** |
426 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
427 |
|
* each task or an (unchecked) exception is encountered, in which |
428 |
< |
* case the exception is rethrown. If any task encounters an |
429 |
< |
* exception, others may be, but are not guaranteed to be, |
430 |
< |
* cancelled. If more than one task encounters an exception, then |
431 |
< |
* this method throws any one of these exceptions. The individual |
432 |
< |
* status of each task may be checked using {@link #getException()} |
433 |
< |
* and related methods. |
428 |
> |
* case the exception is rethrown. If more than one task |
429 |
> |
* encounters an exception, then this method throws any one of |
430 |
> |
* these exceptions. If any task encounters an exception, others |
431 |
> |
* may be cancelled. However, the execution status of individual |
432 |
> |
* tasks is not guaranteed upon exceptional return. The status of |
433 |
> |
* each task may be obtained using {@link #getException()} and |
434 |
> |
* related methods to check if they have been cancelled, completed |
435 |
> |
* normally or exceptionally, or left unprocessed. |
436 |
|
* |
437 |
|
* <p>This method may be invoked only from within {@code |
438 |
< |
* ForkJoinTask} computations (as may be determined using method |
438 |
> |
* ForkJoinPool} computations (as may be determined using method |
439 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
440 |
|
* result in exceptions or errors, possibly including {@code |
441 |
|
* ClassCastException}. |
456 |
|
t.fork(); |
457 |
|
else { |
458 |
|
t.quietlyInvoke(); |
459 |
< |
if (ex == null) |
459 |
> |
if (ex == null && t.status < NORMAL) |
460 |
|
ex = t.getException(); |
461 |
|
} |
462 |
|
} |
467 |
|
t.cancel(false); |
468 |
|
else { |
469 |
|
t.quietlyJoin(); |
470 |
< |
if (ex == null) |
470 |
> |
if (ex == null && t.status < NORMAL) |
471 |
|
ex = t.getException(); |
472 |
|
} |
473 |
|
} |
474 |
|
} |
475 |
|
if (ex != null) |
476 |
< |
rethrowException(ex); |
476 |
> |
UNSAFE.throwException(ex); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
|
* Forks all tasks in the specified collection, returning when |
481 |
|
* {@code isDone} holds for each task or an (unchecked) exception |
482 |
< |
* is encountered. If any task encounters an exception, others |
483 |
< |
* may be, but are not guaranteed to be, cancelled. If more than |
484 |
< |
* one task encounters an exception, then this method throws any |
485 |
< |
* one of these exceptions. The individual status of each task |
486 |
< |
* may be checked using {@link #getException()} and related |
487 |
< |
* methods. The behavior of this operation is undefined if the |
488 |
< |
* specified collection is modified while the operation is in |
489 |
< |
* progress. |
482 |
> |
* is encountered, in which case the exception is rethrown. If |
483 |
> |
* more than one task encounters an exception, then this method |
484 |
> |
* throws any one of these exceptions. If any task encounters an |
485 |
> |
* exception, others may be cancelled. However, the execution |
486 |
> |
* status of individual tasks is not guaranteed upon exceptional |
487 |
> |
* return. The status of each task may be obtained using {@link |
488 |
> |
* #getException()} and related methods to check if they have been |
489 |
> |
* cancelled, completed normally or exceptionally, or left |
490 |
> |
* unprocessed. |
491 |
|
* |
492 |
|
* <p>This method may be invoked only from within {@code |
493 |
< |
* ForkJoinTask} computations (as may be determined using method |
493 |
> |
* ForkJoinPool} computations (as may be determined using method |
494 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
495 |
|
* result in exceptions or errors, possibly including {@code |
496 |
|
* ClassCastException}. |
519 |
|
t.fork(); |
520 |
|
else { |
521 |
|
t.quietlyInvoke(); |
522 |
< |
if (ex == null) |
522 |
> |
if (ex == null && t.status < NORMAL) |
523 |
|
ex = t.getException(); |
524 |
|
} |
525 |
|
} |
530 |
|
t.cancel(false); |
531 |
|
else { |
532 |
|
t.quietlyJoin(); |
533 |
< |
if (ex == null) |
533 |
> |
if (ex == null && t.status < NORMAL) |
534 |
|
ex = t.getException(); |
535 |
|
} |
536 |
|
} |
537 |
|
} |
538 |
|
if (ex != null) |
539 |
< |
rethrowException(ex); |
539 |
> |
UNSAFE.throwException(ex); |
540 |
|
return tasks; |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
|
* Attempts to cancel execution of this task. This attempt will |
545 |
< |
* fail if the task has already completed, has already been |
546 |
< |
* cancelled, or could not be cancelled for some other reason. If |
547 |
< |
* successful, and this task has not started when cancel is |
548 |
< |
* called, execution of this task is suppressed, {@link |
549 |
< |
* #isCancelled} will report true, and {@link #join} will result |
550 |
< |
* in a {@code CancellationException} being thrown. |
545 |
> |
* fail if the task has already completed or could not be |
546 |
> |
* cancelled for some other reason. If successful, and this task |
547 |
> |
* has not started when {@code cancel} is called, execution of |
548 |
> |
* this task is suppressed. After this method returns |
549 |
> |
* successfully, unless there is an intervening call to {@link |
550 |
> |
* #reinitialize}, subsequent calls to {@link #isCancelled}, |
551 |
> |
* {@link #isDone}, and {@code cancel} will return {@code true} |
552 |
> |
* and calls to {@link #join} and related methods will result in |
553 |
> |
* {@code CancellationException}. |
554 |
|
* |
555 |
|
* <p>This method may be overridden in subclasses, but if so, must |
556 |
< |
* still ensure that these minimal properties hold. In particular, |
557 |
< |
* the {@code cancel} method itself must not throw exceptions. |
556 |
> |
* still ensure that these properties hold. In particular, the |
557 |
> |
* {@code cancel} method itself must not throw exceptions. |
558 |
|
* |
559 |
|
* <p>This method is designed to be invoked by <em>other</em> |
560 |
|
* tasks. To terminate the current task, you can just return or |
561 |
|
* throw an unchecked exception from its computation method, or |
562 |
|
* invoke {@link #completeExceptionally}. |
563 |
|
* |
564 |
< |
* @param mayInterruptIfRunning this value is ignored in the |
565 |
< |
* default implementation because tasks are not |
566 |
< |
* cancelled via interruption |
564 |
> |
* @param mayInterruptIfRunning this value has no effect in the |
565 |
> |
* default implementation because interrupts are not used to |
566 |
> |
* control cancellation. |
567 |
|
* |
568 |
|
* @return {@code true} if this task is now cancelled |
569 |
|
*/ |
570 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
571 |
|
setCompletion(CANCELLED); |
572 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
572 |
> |
return status == CANCELLED; |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* Returns {@code true} if the computation performed by this task |
577 |
< |
* has completed (or has been cancelled). |
578 |
< |
* |
579 |
< |
* @return {@code true} if this computation has completed |
576 |
> |
* Cancels, ignoring any exceptions thrown by cancel. Used during |
577 |
> |
* worker and pool shutdown. Cancel is spec'ed not to throw any |
578 |
> |
* exceptions, but if it does anyway, we have no recourse during |
579 |
> |
* shutdown, so guard against this case. |
580 |
|
*/ |
581 |
< |
public final boolean isDone() { |
582 |
< |
return status < 0; |
581 |
> |
final void cancelIgnoringExceptions() { |
582 |
> |
try { |
583 |
> |
cancel(false); |
584 |
> |
} catch (Throwable ignore) { |
585 |
> |
} |
586 |
|
} |
587 |
|
|
588 |
|
/** |
589 |
< |
* Returns {@code true} if this task was cancelled. |
590 |
< |
* |
739 |
< |
* @return {@code true} if this task was cancelled |
589 |
> |
* Cancels if current thread is a terminating worker thread, |
590 |
> |
* ignoring any exceptions thrown by cancel. |
591 |
|
*/ |
592 |
+ |
final void cancelIfTerminating() { |
593 |
+ |
Thread t = Thread.currentThread(); |
594 |
+ |
if ((t instanceof ForkJoinWorkerThread) && |
595 |
+ |
((ForkJoinWorkerThread) t).isTerminating()) { |
596 |
+ |
try { |
597 |
+ |
cancel(false); |
598 |
+ |
} catch (Throwable ignore) { |
599 |
+ |
} |
600 |
+ |
} |
601 |
+ |
} |
602 |
+ |
|
603 |
+ |
public final boolean isDone() { |
604 |
+ |
return status < 0; |
605 |
+ |
} |
606 |
+ |
|
607 |
|
public final boolean isCancelled() { |
608 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
608 |
> |
return status == CANCELLED; |
609 |
|
} |
610 |
|
|
611 |
|
/** |
614 |
|
* @return {@code true} if this task threw an exception or was cancelled |
615 |
|
*/ |
616 |
|
public final boolean isCompletedAbnormally() { |
617 |
< |
return (status & COMPLETION_MASK) < NORMAL; |
617 |
> |
return status < NORMAL; |
618 |
|
} |
619 |
|
|
620 |
|
/** |
625 |
|
* exception and was not cancelled |
626 |
|
*/ |
627 |
|
public final boolean isCompletedNormally() { |
628 |
< |
return (status & COMPLETION_MASK) == NORMAL; |
763 |
< |
} |
764 |
< |
|
765 |
< |
/** |
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; |
628 |
> |
return status == NORMAL; |
629 |
|
} |
630 |
|
|
631 |
|
/** |
636 |
|
* @return the exception, or {@code null} if none |
637 |
|
*/ |
638 |
|
public final Throwable getException() { |
639 |
< |
int s = status & COMPLETION_MASK; |
640 |
< |
if (s >= NORMAL) |
641 |
< |
return null; |
642 |
< |
if (s == CANCELLED) |
786 |
< |
return new CancellationException(); |
787 |
< |
return exceptionMap.get(this); |
639 |
> |
int s = status; |
640 |
> |
return ((s >= NORMAL) ? null : |
641 |
> |
(s == CANCELLED) ? new CancellationException() : |
642 |
> |
exceptionMap.get(this)); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
652 |
|
* overridable, but overridden versions must invoke {@code super} |
653 |
|
* implementation to maintain guarantees. |
654 |
|
* |
655 |
< |
* @param ex the exception to throw. If this exception is |
656 |
< |
* not a RuntimeException or Error, the actual exception thrown |
657 |
< |
* will be a RuntimeException with cause ex. |
655 |
> |
* @param ex the exception to throw. If this exception is not a |
656 |
> |
* {@code RuntimeException} or {@code Error}, the actual exception |
657 |
> |
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
658 |
|
*/ |
659 |
|
public void completeExceptionally(Throwable ex) { |
660 |
< |
setDoneExceptionally((ex instanceof RuntimeException) || |
661 |
< |
(ex instanceof Error) ? ex : |
662 |
< |
new RuntimeException(ex)); |
660 |
> |
setExceptionalCompletion((ex instanceof RuntimeException) || |
661 |
> |
(ex instanceof Error) ? ex : |
662 |
> |
new RuntimeException(ex)); |
663 |
|
} |
664 |
|
|
665 |
|
/** |
666 |
|
* Completes this task, and if not already aborted or cancelled, |
667 |
< |
* returning a {@code null} result upon {@code join} and related |
668 |
< |
* operations. This method may be used to provide results for |
669 |
< |
* asynchronous tasks, or to provide alternative handling for |
670 |
< |
* tasks that would not otherwise complete normally. Its use in |
671 |
< |
* other situations is discouraged. This method is |
672 |
< |
* overridable, but overridden versions must invoke {@code super} |
673 |
< |
* implementation to maintain guarantees. |
667 |
> |
* returning the given value as the result of subsequent |
668 |
> |
* invocations of {@code join} and related operations. This method |
669 |
> |
* may be used to provide results for asynchronous tasks, or to |
670 |
> |
* provide alternative handling for tasks that would not otherwise |
671 |
> |
* complete normally. Its use in other situations is |
672 |
> |
* discouraged. This method is overridable, but overridden |
673 |
> |
* versions must invoke {@code super} implementation to maintain |
674 |
> |
* guarantees. |
675 |
|
* |
676 |
|
* @param value the result value for this task |
677 |
|
*/ |
679 |
|
try { |
680 |
|
setRawResult(value); |
681 |
|
} catch (Throwable rex) { |
682 |
< |
setDoneExceptionally(rex); |
682 |
> |
setExceptionalCompletion(rex); |
683 |
|
return; |
684 |
|
} |
685 |
< |
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(); |
685 |
> |
setCompletion(NORMAL); |
686 |
|
} |
687 |
|
|
688 |
|
/** |
689 |
< |
* Possibly executes other tasks until this task is ready, then |
690 |
< |
* 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}. |
689 |
> |
* Waits if necessary for the computation to complete, and then |
690 |
> |
* retrieves its result. |
691 |
|
* |
692 |
|
* @return the computed result |
693 |
+ |
* @throws CancellationException if the computation was cancelled |
694 |
+ |
* @throws ExecutionException if the computation threw an |
695 |
+ |
* exception |
696 |
+ |
* @throws InterruptedException if the current thread is not a |
697 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
698 |
|
*/ |
699 |
< |
public final V helpJoin() { |
700 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
701 |
< |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
702 |
< |
reportException(busyJoin(w)); |
699 |
> |
public final V get() throws InterruptedException, ExecutionException { |
700 |
> |
Thread t = Thread.currentThread(); |
701 |
> |
if (t instanceof ForkJoinWorkerThread) |
702 |
> |
quietlyJoin(); |
703 |
> |
else |
704 |
> |
externalInterruptibleAwaitDone(false, 0L); |
705 |
> |
int s = status; |
706 |
> |
if (s != NORMAL) { |
707 |
> |
Throwable ex; |
708 |
> |
if (s == CANCELLED) |
709 |
> |
throw new CancellationException(); |
710 |
> |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
711 |
> |
throw new ExecutionException(ex); |
712 |
> |
} |
713 |
|
return getRawResult(); |
714 |
|
} |
715 |
|
|
716 |
|
/** |
717 |
< |
* Possibly executes other tasks until this task is ready. This |
718 |
< |
* method may be useful when processing collections of tasks when |
875 |
< |
* some have been cancelled or otherwise known to have aborted. |
717 |
> |
* Waits if necessary for at most the given time for the computation |
718 |
> |
* to complete, and then retrieves its result, if available. |
719 |
|
* |
720 |
< |
* <p>This method may be invoked only from within {@code |
721 |
< |
* ForkJoinTask} computations (as may be determined using method |
722 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
723 |
< |
* result in exceptions or errors, possibly including {@code |
724 |
< |
* ClassCastException}. |
720 |
> |
* @param timeout the maximum time to wait |
721 |
> |
* @param unit the time unit of the timeout argument |
722 |
> |
* @return the computed result |
723 |
> |
* @throws CancellationException if the computation was cancelled |
724 |
> |
* @throws ExecutionException if the computation threw an |
725 |
> |
* exception |
726 |
> |
* @throws InterruptedException if the current thread is not a |
727 |
> |
* member of a ForkJoinPool and was interrupted while waiting |
728 |
> |
* @throws TimeoutException if the wait timed out |
729 |
|
*/ |
730 |
< |
public final void quietlyHelpJoin() { |
731 |
< |
if (status >= 0) { |
732 |
< |
ForkJoinWorkerThread w = |
733 |
< |
(ForkJoinWorkerThread) Thread.currentThread(); |
734 |
< |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
735 |
< |
busyJoin(w); |
730 |
> |
public final V get(long timeout, TimeUnit unit) |
731 |
> |
throws InterruptedException, ExecutionException, TimeoutException { |
732 |
> |
long nanos = unit.toNanos(timeout); |
733 |
> |
Thread t = Thread.currentThread(); |
734 |
> |
if (t instanceof ForkJoinWorkerThread) |
735 |
> |
((ForkJoinWorkerThread)t).joinTask(this, true, nanos); |
736 |
> |
else |
737 |
> |
externalInterruptibleAwaitDone(true, nanos); |
738 |
> |
int s = status; |
739 |
> |
if (s != NORMAL) { |
740 |
> |
Throwable ex; |
741 |
> |
if (s == CANCELLED) |
742 |
> |
throw new CancellationException(); |
743 |
> |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
744 |
> |
throw new ExecutionException(ex); |
745 |
> |
throw new TimeoutException(); |
746 |
|
} |
747 |
+ |
return getRawResult(); |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
< |
* Joins this task, without returning its result or throwing an |
751 |
> |
* Joins this task, without returning its result or throwing its |
752 |
|
* exception. This method may be useful when processing |
753 |
|
* collections of tasks when some have been cancelled or otherwise |
754 |
|
* known to have aborted. |
755 |
|
*/ |
756 |
|
public final void quietlyJoin() { |
757 |
< |
if (status >= 0) { |
758 |
< |
ForkJoinWorkerThread w = getWorker(); |
759 |
< |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
760 |
< |
awaitDone(w, true); |
757 |
> |
Thread t; |
758 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
759 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
760 |
> |
if (status >= 0) { |
761 |
> |
if (w.unpushTask(this)) { |
762 |
> |
boolean completed; |
763 |
> |
try { |
764 |
> |
completed = exec(); |
765 |
> |
} catch (Throwable rex) { |
766 |
> |
setExceptionalCompletion(rex); |
767 |
> |
return; |
768 |
> |
} |
769 |
> |
if (completed) { |
770 |
> |
setCompletion(NORMAL); |
771 |
> |
return; |
772 |
> |
} |
773 |
> |
} |
774 |
> |
w.joinTask(this, false, 0L); |
775 |
> |
} |
776 |
|
} |
777 |
+ |
else |
778 |
+ |
externalAwaitDone(); |
779 |
|
} |
780 |
|
|
781 |
|
/** |
782 |
|
* Commences performing this task and awaits its completion if |
783 |
< |
* necessary, without returning its result or throwing an |
784 |
< |
* exception. This method may be useful when processing |
910 |
< |
* collections of tasks when some have been cancelled or otherwise |
911 |
< |
* known to have aborted. |
783 |
> |
* necessary, without returning its result or throwing its |
784 |
> |
* exception. |
785 |
|
*/ |
786 |
|
public final void quietlyInvoke() { |
787 |
< |
if (status >= 0 && !tryQuietlyInvoke()) |
788 |
< |
quietlyJoin(); |
787 |
> |
if (status >= 0) { |
788 |
> |
boolean completed; |
789 |
> |
try { |
790 |
> |
completed = exec(); |
791 |
> |
} catch (Throwable rex) { |
792 |
> |
setExceptionalCompletion(rex); |
793 |
> |
return; |
794 |
> |
} |
795 |
> |
if (completed) |
796 |
> |
setCompletion(NORMAL); |
797 |
> |
else |
798 |
> |
quietlyJoin(); |
799 |
> |
} |
800 |
|
} |
801 |
|
|
802 |
|
/** |
807 |
|
* processed. |
808 |
|
* |
809 |
|
* <p>This method may be invoked only from within {@code |
810 |
< |
* ForkJoinTask} computations (as may be determined using method |
810 |
> |
* ForkJoinPool} computations (as may be determined using method |
811 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
812 |
|
* result in exceptions or errors, possibly including {@code |
813 |
|
* ClassCastException}. |
826 |
|
* under any other usage conditions are not guaranteed. |
827 |
|
* This method may be useful when executing |
828 |
|
* pre-constructed trees of subtasks in loops. |
829 |
+ |
* |
830 |
+ |
* <p>Upon completion of this method, {@code isDone()} reports |
831 |
+ |
* {@code false}, and {@code getException()} reports {@code |
832 |
+ |
* null}. However, the value returned by {@code getRawResult} is |
833 |
+ |
* unaffected. To clear this value, you can invoke {@code |
834 |
+ |
* setRawResult(null)}. |
835 |
|
*/ |
836 |
|
public void reinitialize() { |
837 |
< |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
837 |
> |
if (status == EXCEPTIONAL) |
838 |
|
exceptionMap.remove(this); |
839 |
|
status = 0; |
840 |
|
} |
853 |
|
} |
854 |
|
|
855 |
|
/** |
856 |
< |
* Returns {@code true} if the current thread is executing as a |
857 |
< |
* ForkJoinPool computation. |
856 |
> |
* Returns {@code true} if the current thread is a {@link |
857 |
> |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation. |
858 |
|
* |
859 |
< |
* @return {@code true} if the current thread is executing as a |
860 |
< |
* ForkJoinPool computation, or false otherwise |
859 |
> |
* @return {@code true} if the current thread is a {@link |
860 |
> |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation, |
861 |
> |
* or {@code false} otherwise |
862 |
|
*/ |
863 |
|
public static boolean inForkJoinPool() { |
864 |
|
return Thread.currentThread() instanceof ForkJoinWorkerThread; |
873 |
|
* were not, stolen. |
874 |
|
* |
875 |
|
* <p>This method may be invoked only from within {@code |
876 |
< |
* ForkJoinTask} computations (as may be determined using method |
876 |
> |
* ForkJoinPool} computations (as may be determined using method |
877 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
878 |
|
* result in exceptions or errors, possibly including {@code |
879 |
|
* ClassCastException}. |
892 |
|
* fork other tasks. |
893 |
|
* |
894 |
|
* <p>This method may be invoked only from within {@code |
895 |
< |
* ForkJoinTask} computations (as may be determined using method |
895 |
> |
* ForkJoinPool} computations (as may be determined using method |
896 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
897 |
|
* result in exceptions or errors, possibly including {@code |
898 |
|
* ClassCastException}. |
915 |
|
* exceeded. |
916 |
|
* |
917 |
|
* <p>This method may be invoked only from within {@code |
918 |
< |
* ForkJoinTask} computations (as may be determined using method |
918 |
> |
* ForkJoinPool} computations (as may be determined using method |
919 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
920 |
|
* result in exceptions or errors, possibly including {@code |
921 |
|
* ClassCastException}. |
973 |
|
* otherwise. |
974 |
|
* |
975 |
|
* <p>This method may be invoked only from within {@code |
976 |
< |
* ForkJoinTask} computations (as may be determined using method |
976 |
> |
* ForkJoinPool} computations (as may be determined using method |
977 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
978 |
|
* result in exceptions or errors, possibly including {@code |
979 |
|
* ClassCastException}. |
992 |
|
* be useful otherwise. |
993 |
|
* |
994 |
|
* <p>This method may be invoked only from within {@code |
995 |
< |
* ForkJoinTask} computations (as may be determined using method |
995 |
> |
* ForkJoinPool} computations (as may be determined using method |
996 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
997 |
|
* result in exceptions or errors, possibly including {@code |
998 |
|
* ClassCastException}. |
1015 |
|
* otherwise. |
1016 |
|
* |
1017 |
|
* <p>This method may be invoked only from within {@code |
1018 |
< |
* ForkJoinTask} computations (as may be determined using method |
1018 |
> |
* ForkJoinPool} computations (as may be determined using method |
1019 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1020 |
|
* result in exceptions or errors, possibly including {@code |
1021 |
|
* ClassCastException}. |
1125 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1126 |
|
|
1127 |
|
/** |
1128 |
< |
* Save the state to a stream. |
1128 |
> |
* Saves the state to a stream (that is, serializes it). |
1129 |
|
* |
1130 |
|
* @serialData the current run status and the exception thrown |
1131 |
|
* during execution, or {@code null} if none |
1138 |
|
} |
1139 |
|
|
1140 |
|
/** |
1141 |
< |
* Reconstitute the instance from a stream. |
1141 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1142 |
|
* |
1143 |
|
* @param s the stream |
1144 |
|
*/ |
1145 |
|
private void readObject(java.io.ObjectInputStream s) |
1146 |
|
throws java.io.IOException, ClassNotFoundException { |
1147 |
|
s.defaultReadObject(); |
1257 |
– |
status &= ~INTERNAL_SIGNAL_MASK; // clear internal signal counts |
1258 |
– |
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1148 |
|
Object ex = s.readObject(); |
1149 |
|
if (ex != null) |
1150 |
< |
setDoneExceptionally((Throwable) ex); |
1150 |
> |
setExceptionalCompletion((Throwable) ex); |
1151 |
|
} |
1152 |
|
|
1153 |
|
// Unsafe mechanics |