7 |
|
package jsr166y; |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
– |
|
10 |
|
import java.io.Serializable; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
27 |
|
* start other subtasks. As indicated by the name of this class, |
28 |
|
* many programs using {@code ForkJoinTask} employ only methods |
29 |
|
* {@link #fork} and {@link #join}, or derivatives such as {@link |
30 |
< |
* #invokeAll}. However, this class also provides a number of other |
31 |
< |
* methods that can come into play in advanced usages, as well as |
32 |
< |
* extension mechanics that allow support of new forms of fork/join |
33 |
< |
* processing. |
30 |
> |
* #invokeAll(ForkJoinTask...) invokeAll}. However, this class also |
31 |
> |
* provides a number of other methods that can come into play in |
32 |
> |
* advanced usages, as well as extension mechanics that allow |
33 |
> |
* support of new forms of fork/join processing. |
34 |
|
* |
35 |
|
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
36 |
|
* The efficiency of {@code ForkJoinTask}s stems from a set of |
99 |
|
* ForkJoinTasks (as may be determined using method {@link |
100 |
|
* #inForkJoinPool}). Attempts to invoke them in other contexts |
101 |
|
* result in exceptions or errors, possibly including |
102 |
< |
* ClassCastException. |
102 |
> |
* {@code ClassCastException}. |
103 |
|
* |
104 |
|
* <p>Most base support methods are {@code final}, to prevent |
105 |
|
* overriding of implementations that are intrinsically tied to the |
143 |
|
* status maintenance (2) execution and awaiting completion (3) |
144 |
|
* user-level methods that additionally report results. This is |
145 |
|
* sometimes hard to see because this file orders exported methods |
146 |
< |
* in a way that flows well in javadocs. |
146 |
> |
* in a way that flows well in javadocs. In particular, most |
147 |
> |
* join mechanics are in method quietlyJoin, below. |
148 |
|
*/ |
149 |
|
|
150 |
< |
/** |
151 |
< |
* Run control status bits packed into a single int to minimize |
152 |
< |
* footprint and to ensure atomicity (via CAS). Status is |
153 |
< |
* initially zero, and takes on nonnegative values until |
154 |
< |
* completed, upon which status holds value COMPLETED. CANCELLED, |
155 |
< |
* or EXCEPTIONAL. Tasks undergoing blocking waits by other |
156 |
< |
* threads have the SIGNAL bit set. Completion of a stolen task |
157 |
< |
* with SIGNAL set awakens any waiters via notifyAll. Even though |
158 |
< |
* suboptimal for some purposes, we use basic builtin wait/notify |
159 |
< |
* to take advantage of "monitor inflation" in JVMs that we would |
160 |
< |
* otherwise need to emulate to avoid adding further per-task |
161 |
< |
* bookkeeping overhead. We want these monitors to be "fat", |
162 |
< |
* i.e., not use biasing or thin-lock techniques, so use some odd |
163 |
< |
* coding idioms that tend to avoid them. |
150 |
> |
/* |
151 |
> |
* The status field holds run control status bits packed into a |
152 |
> |
* single int to minimize footprint and to ensure atomicity (via |
153 |
> |
* CAS). Status is initially zero, and takes on nonnegative |
154 |
> |
* values until completed, upon which status holds value |
155 |
> |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
156 |
> |
* waits by other threads have the SIGNAL bit set. Completion of |
157 |
> |
* a stolen task with SIGNAL set awakens any waiters via |
158 |
> |
* notifyAll. Even though suboptimal for some purposes, we use |
159 |
> |
* basic builtin wait/notify to take advantage of "monitor |
160 |
> |
* inflation" in JVMs that we would otherwise need to emulate to |
161 |
> |
* avoid adding further per-task bookkeeping overhead. We want |
162 |
> |
* these monitors to be "fat", i.e., not use biasing or thin-lock |
163 |
> |
* techniques, so use some odd coding idioms that tend to avoid |
164 |
> |
* them. |
165 |
|
*/ |
166 |
+ |
|
167 |
+ |
/** The run status of this task */ |
168 |
|
volatile int status; // accessed directly by pool and workers |
169 |
|
|
170 |
|
private static final int NORMAL = -1; |
191 |
|
* also clearing signal request bits. |
192 |
|
* |
193 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
191 |
– |
* @return status on exit |
194 |
|
*/ |
195 |
< |
private int setCompletion(int completion) { |
195 |
> |
private void setCompletion(int completion) { |
196 |
|
int s; |
197 |
|
while ((s = status) >= 0) { |
198 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
199 |
< |
if (s == SIGNAL) |
199 |
> |
if (s != 0) |
200 |
|
synchronized (this) { notifyAll(); } |
201 |
< |
return completion; |
201 |
> |
break; |
202 |
|
} |
203 |
|
} |
202 |
– |
return s; |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* Record exception and set exceptional completion |
207 |
> |
* Records exception and sets exceptional completion. |
208 |
> |
* |
209 |
|
* @return status on exit |
210 |
|
*/ |
211 |
< |
private int setExceptionalCompletion(Throwable rex) { |
211 |
> |
private void setExceptionalCompletion(Throwable rex) { |
212 |
|
exceptionMap.put(this, rex); |
213 |
< |
return setCompletion(EXCEPTIONAL); |
213 |
> |
setCompletion(EXCEPTIONAL); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* Blocks a worker thread until completion. Called only by pool. |
217 |
> |
* Blocks a worker thread until completion. Called only by |
218 |
> |
* pool. Currently unused -- pool-based waits use timeout |
219 |
> |
* version below. |
220 |
|
*/ |
221 |
< |
final int internalAwaitDone() { |
222 |
< |
int s; |
221 |
> |
final void internalAwaitDone() { |
222 |
> |
int s; // the odd construction reduces lock bias effects |
223 |
|
while ((s = status) >= 0) { |
224 |
< |
synchronized(this) { |
225 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
226 |
< |
do { |
227 |
< |
try { |
228 |
< |
wait(); |
229 |
< |
} catch (InterruptedException ie) { |
230 |
< |
cancelIfTerminating(); |
231 |
< |
} |
232 |
< |
} while ((s = status) >= 0); |
233 |
< |
break; |
224 |
> |
try { |
225 |
> |
synchronized (this) { |
226 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
227 |
> |
wait(); |
228 |
> |
} |
229 |
> |
} catch (InterruptedException ie) { |
230 |
> |
cancelIfTerminating(); |
231 |
> |
} |
232 |
> |
} |
233 |
> |
} |
234 |
> |
|
235 |
> |
/** |
236 |
> |
* Blocks a worker thread until completed or timed out. Called |
237 |
> |
* only by pool. |
238 |
> |
* |
239 |
> |
* @return status on exit |
240 |
> |
*/ |
241 |
> |
final int internalAwaitDone(long millis) { |
242 |
> |
int s; |
243 |
> |
if ((s = status) >= 0) { |
244 |
> |
try { |
245 |
> |
synchronized (this) { |
246 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
247 |
> |
wait(millis, 0); |
248 |
|
} |
249 |
+ |
} catch (InterruptedException ie) { |
250 |
+ |
cancelIfTerminating(); |
251 |
|
} |
252 |
+ |
s = status; |
253 |
|
} |
254 |
|
return s; |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Blocks a non-worker-thread until completion. |
238 |
– |
* @return status on exit |
259 |
|
*/ |
260 |
< |
private int externalAwaitDone() { |
260 |
> |
private void externalAwaitDone() { |
261 |
|
int s; |
262 |
|
while ((s = status) >= 0) { |
263 |
< |
synchronized(this) { |
263 |
> |
synchronized (this) { |
264 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){ |
265 |
|
boolean interrupted = false; |
266 |
< |
do { |
266 |
> |
while (status >= 0) { |
267 |
|
try { |
268 |
|
wait(); |
269 |
|
} catch (InterruptedException ie) { |
270 |
|
interrupted = true; |
271 |
|
} |
272 |
< |
} while ((s = status) >= 0); |
272 |
> |
} |
273 |
|
if (interrupted) |
274 |
|
Thread.currentThread().interrupt(); |
275 |
|
break; |
276 |
|
} |
277 |
|
} |
278 |
|
} |
259 |
– |
return s; |
279 |
|
} |
280 |
|
|
281 |
|
/** |
283 |
|
* doesn't wait for completion otherwise. Primary execution method |
284 |
|
* for ForkJoinWorkerThread. |
285 |
|
*/ |
286 |
< |
final void tryExec() { |
286 |
> |
final void quietlyExec() { |
287 |
|
try { |
288 |
|
if (status < 0 || !exec()) |
289 |
|
return; |
294 |
|
setCompletion(NORMAL); // must be outside try block |
295 |
|
} |
296 |
|
|
278 |
– |
/** |
279 |
– |
* If not done and this task is next in worker queue, runs it, |
280 |
– |
* else waits for it. |
281 |
– |
* @return status on exit |
282 |
– |
*/ |
283 |
– |
private int doJoin() { |
284 |
– |
int stat; |
285 |
– |
if ((stat = status) < 0) |
286 |
– |
return stat; |
287 |
– |
Thread t = Thread.currentThread(); |
288 |
– |
ForkJoinWorkerThread w; |
289 |
– |
if (t instanceof ForkJoinWorkerThread) { |
290 |
– |
if ((w = (ForkJoinWorkerThread) t).unpushTask(this)) { |
291 |
– |
boolean completed; |
292 |
– |
try { |
293 |
– |
completed = exec(); |
294 |
– |
} catch (Throwable rex) { |
295 |
– |
return setExceptionalCompletion(rex); |
296 |
– |
} |
297 |
– |
if (completed) |
298 |
– |
return setCompletion(NORMAL); |
299 |
– |
} |
300 |
– |
w.joinTask(this); |
301 |
– |
return status; |
302 |
– |
} |
303 |
– |
return externalAwaitDone(); |
304 |
– |
} |
305 |
– |
|
306 |
– |
/** |
307 |
– |
* Unless done, calls exec and records status if completed, or |
308 |
– |
* waits for completion otherwise. |
309 |
– |
* @return status on exit |
310 |
– |
*/ |
311 |
– |
private int doInvoke() { |
312 |
– |
int stat; |
313 |
– |
if ((stat = status) >= 0) { |
314 |
– |
boolean completed; |
315 |
– |
try { |
316 |
– |
completed = exec(); |
317 |
– |
} catch (Throwable rex) { |
318 |
– |
return setExceptionalCompletion(rex); |
319 |
– |
} |
320 |
– |
if (completed) |
321 |
– |
stat = setCompletion(NORMAL); |
322 |
– |
else |
323 |
– |
stat = doJoin(); |
324 |
– |
} |
325 |
– |
return stat; |
326 |
– |
} |
327 |
– |
|
328 |
– |
/** |
329 |
– |
* Returns result or throws exception associated with given status. |
330 |
– |
* @param s the status |
331 |
– |
*/ |
332 |
– |
private V reportResult(int s) { |
333 |
– |
Throwable ex; |
334 |
– |
if (s < NORMAL && (ex = getException()) != null) |
335 |
– |
UNSAFE.throwException(ex); |
336 |
– |
return getRawResult(); |
337 |
– |
} |
338 |
– |
|
297 |
|
// public methods |
298 |
|
|
299 |
|
/** |
329 |
|
* @return the computed result |
330 |
|
*/ |
331 |
|
public final V join() { |
332 |
< |
return reportResult(doJoin()); |
332 |
> |
quietlyJoin(); |
333 |
> |
Throwable ex; |
334 |
> |
if (status < NORMAL && (ex = getException()) != null) |
335 |
> |
UNSAFE.throwException(ex); |
336 |
> |
return getRawResult(); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
|
* Commences performing this task, awaits its completion if |
341 |
< |
* necessary, and return its result, or throws an (unchecked) |
342 |
< |
* exception if the underlying computation did so. |
341 |
> |
* necessary, and returns its result, or throws an (unchecked) |
342 |
> |
* {@code RuntimeException} or {@code Error} if the underlying |
343 |
> |
* computation did so. |
344 |
|
* |
345 |
|
* @return the computed result |
346 |
|
*/ |
347 |
|
public final V invoke() { |
348 |
< |
return reportResult(doInvoke()); |
348 |
> |
quietlyInvoke(); |
349 |
> |
Throwable ex; |
350 |
> |
if (status < NORMAL && (ex = getException()) != null) |
351 |
> |
UNSAFE.throwException(ex); |
352 |
> |
return getRawResult(); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
357 |
|
* each task or an (unchecked) exception is encountered, in which |
358 |
< |
* case the exception is rethrown. If either task encounters an |
359 |
< |
* exception, the other one may be, but is not guaranteed to be, |
360 |
< |
* cancelled. If both tasks throw an exception, then this method |
361 |
< |
* throws one of them. The individual status of each task may be |
362 |
< |
* checked using {@link #getException()} and related methods. |
358 |
> |
* case the exception is rethrown. If more than one task |
359 |
> |
* encounters an exception, then this method throws any one of |
360 |
> |
* these exceptions. If any task encounters an exception, the |
361 |
> |
* other may be cancelled. However, the execution status of |
362 |
> |
* individual tasks is not guaranteed upon exceptional return. The |
363 |
> |
* status of each task may be obtained using {@link |
364 |
> |
* #getException()} and related methods to check if they have been |
365 |
> |
* cancelled, completed normally or exceptionally, or left |
366 |
> |
* unprocessed. |
367 |
|
* |
368 |
|
* <p>This method may be invoked only from within {@code |
369 |
|
* ForkJoinTask} computations (as may be determined using method |
384 |
|
/** |
385 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
386 |
|
* each task or an (unchecked) exception is encountered, in which |
387 |
< |
* case the exception is rethrown. If any task encounters an |
388 |
< |
* exception, others may be, but are not guaranteed to be, |
389 |
< |
* cancelled. If more than one task encounters an exception, then |
390 |
< |
* this method throws any one of these exceptions. The individual |
391 |
< |
* status of each task may be checked using {@link #getException()} |
392 |
< |
* and related methods. |
387 |
> |
* case the exception is rethrown. If more than one task |
388 |
> |
* encounters an exception, then this method throws any one of |
389 |
> |
* these exceptions. If any task encounters an exception, others |
390 |
> |
* may be cancelled. However, the execution status of individual |
391 |
> |
* tasks is not guaranteed upon exceptional return. The status of |
392 |
> |
* each task may be obtained using {@link #getException()} and |
393 |
> |
* related methods to check if they have been cancelled, completed |
394 |
> |
* normally or exceptionally, or left unprocessed. |
395 |
|
* |
396 |
|
* <p>This method may be invoked only from within {@code |
397 |
|
* ForkJoinTask} computations (as may be determined using method |
413 |
|
} |
414 |
|
else if (i != 0) |
415 |
|
t.fork(); |
416 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
417 |
< |
ex = t.getException(); |
416 |
> |
else { |
417 |
> |
t.quietlyInvoke(); |
418 |
> |
if (ex == null && t.status < NORMAL) |
419 |
> |
ex = t.getException(); |
420 |
> |
} |
421 |
|
} |
422 |
|
for (int i = 1; i <= last; ++i) { |
423 |
|
ForkJoinTask<?> t = tasks[i]; |
424 |
|
if (t != null) { |
425 |
|
if (ex != null) |
426 |
|
t.cancel(false); |
427 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
428 |
< |
ex = t.getException(); |
427 |
> |
else { |
428 |
> |
t.quietlyJoin(); |
429 |
> |
if (ex == null && t.status < NORMAL) |
430 |
> |
ex = t.getException(); |
431 |
> |
} |
432 |
|
} |
433 |
|
} |
434 |
|
if (ex != null) |
438 |
|
/** |
439 |
|
* Forks all tasks in the specified collection, returning when |
440 |
|
* {@code isDone} holds for each task or an (unchecked) exception |
441 |
< |
* is encountered. If any task encounters an exception, others |
442 |
< |
* may be, but are not guaranteed to be, cancelled. If more than |
443 |
< |
* one task encounters an exception, then this method throws any |
444 |
< |
* one of these exceptions. The individual status of each task |
445 |
< |
* may be checked using {@link #getException()} and related |
446 |
< |
* methods. The behavior of this operation is undefined if the |
447 |
< |
* specified collection is modified while the operation is in |
448 |
< |
* progress. |
441 |
> |
* is encountered, in which case the exception is rethrown. If |
442 |
> |
* more than one task encounters an exception, then this method |
443 |
> |
* throws any one of these exceptions. If any task encounters an |
444 |
> |
* exception, others may be cancelled. However, the execution |
445 |
> |
* status of individual tasks is not guaranteed upon exceptional |
446 |
> |
* return. The status of each task may be obtained using {@link |
447 |
> |
* #getException()} and related methods to check if they have been |
448 |
> |
* cancelled, completed normally or exceptionally, or left |
449 |
> |
* unprocessed. |
450 |
|
* |
451 |
|
* <p>This method may be invoked only from within {@code |
452 |
|
* ForkJoinTask} computations (as may be determined using method |
476 |
|
} |
477 |
|
else if (i != 0) |
478 |
|
t.fork(); |
479 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
480 |
< |
ex = t.getException(); |
479 |
> |
else { |
480 |
> |
t.quietlyInvoke(); |
481 |
> |
if (ex == null && t.status < NORMAL) |
482 |
> |
ex = t.getException(); |
483 |
> |
} |
484 |
|
} |
485 |
|
for (int i = 1; i <= last; ++i) { |
486 |
|
ForkJoinTask<?> t = ts.get(i); |
487 |
|
if (t != null) { |
488 |
|
if (ex != null) |
489 |
|
t.cancel(false); |
490 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
491 |
< |
ex = t.getException(); |
490 |
> |
else { |
491 |
> |
t.quietlyJoin(); |
492 |
> |
if (ex == null && t.status < NORMAL) |
493 |
> |
ex = t.getException(); |
494 |
> |
} |
495 |
|
} |
496 |
|
} |
497 |
|
if (ex != null) |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* Cancels, ignoring any exceptions it throws. Used during worker |
533 |
< |
* and pool shutdown. |
532 |
> |
* Cancels, ignoring any exceptions thrown by cancel. Used during |
533 |
> |
* worker and pool shutdown. Cancel is spec'ed not to throw any |
534 |
> |
* exceptions, but if it does anyway, we have no recourse during |
535 |
> |
* shutdown, so guard against this case. |
536 |
|
*/ |
537 |
|
final void cancelIgnoringExceptions() { |
538 |
|
try { |
542 |
|
} |
543 |
|
|
544 |
|
/** |
545 |
< |
* Cancels ignoring exceptions if worker is terminating |
545 |
> |
* Cancels if current thread is a terminating worker thread, |
546 |
> |
* ignoring any exceptions thrown by cancel. |
547 |
|
*/ |
548 |
< |
private void cancelIfTerminating() { |
548 |
> |
final void cancelIfTerminating() { |
549 |
|
Thread t = Thread.currentThread(); |
550 |
|
if ((t instanceof ForkJoinWorkerThread) && |
551 |
|
((ForkJoinWorkerThread) t).isTerminating()) { |
620 |
|
|
621 |
|
/** |
622 |
|
* Completes this task, and if not already aborted or cancelled, |
623 |
< |
* returning a {@code null} result upon {@code join} and related |
624 |
< |
* operations. This method may be used to provide results for |
625 |
< |
* asynchronous tasks, or to provide alternative handling for |
626 |
< |
* tasks that would not otherwise complete normally. Its use in |
627 |
< |
* other situations is discouraged. This method is |
628 |
< |
* overridable, but overridden versions must invoke {@code super} |
629 |
< |
* implementation to maintain guarantees. |
623 |
> |
* returning the given value as the result of subsequent |
624 |
> |
* invocations of {@code join} and related operations. This method |
625 |
> |
* may be used to provide results for asynchronous tasks, or to |
626 |
> |
* provide alternative handling for tasks that would not otherwise |
627 |
> |
* complete normally. Its use in other situations is |
628 |
> |
* discouraged. This method is overridable, but overridden |
629 |
> |
* versions must invoke {@code super} implementation to maintain |
630 |
> |
* guarantees. |
631 |
|
* |
632 |
|
* @param value the result value for this task |
633 |
|
*/ |
641 |
|
setCompletion(NORMAL); |
642 |
|
} |
643 |
|
|
644 |
+ |
/** |
645 |
+ |
* Waits if necessary for the computation to complete, and then |
646 |
+ |
* retrieves its result. |
647 |
+ |
* |
648 |
+ |
* @return the computed result |
649 |
+ |
* @throws CancellationException if the computation was cancelled |
650 |
+ |
* @throws ExecutionException if the computation threw an |
651 |
+ |
* exception |
652 |
+ |
* @throws InterruptedException if the current thread is not a |
653 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
654 |
+ |
*/ |
655 |
|
public final V get() throws InterruptedException, ExecutionException { |
656 |
< |
int s = doJoin(); |
657 |
< |
if (Thread.interrupted()) |
658 |
< |
throw new InterruptedException(); |
656 |
> |
int s; |
657 |
> |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) { |
658 |
> |
quietlyJoin(); |
659 |
> |
s = status; |
660 |
> |
} |
661 |
> |
else { |
662 |
> |
while ((s = status) >= 0) { |
663 |
> |
synchronized (this) { // interruptible form of awaitDone |
664 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
665 |
> |
s, SIGNAL)) { |
666 |
> |
while (status >= 0) |
667 |
> |
wait(); |
668 |
> |
} |
669 |
> |
} |
670 |
> |
} |
671 |
> |
} |
672 |
|
if (s < NORMAL) { |
673 |
|
Throwable ex; |
674 |
|
if (s == CANCELLED) |
679 |
|
return getRawResult(); |
680 |
|
} |
681 |
|
|
682 |
+ |
/** |
683 |
+ |
* Waits if necessary for at most the given time for the computation |
684 |
+ |
* to complete, and then retrieves its result, if available. |
685 |
+ |
* |
686 |
+ |
* @param timeout the maximum time to wait |
687 |
+ |
* @param unit the time unit of the timeout argument |
688 |
+ |
* @return the computed result |
689 |
+ |
* @throws CancellationException if the computation was cancelled |
690 |
+ |
* @throws ExecutionException if the computation threw an |
691 |
+ |
* exception |
692 |
+ |
* @throws InterruptedException if the current thread is not a |
693 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
694 |
+ |
* @throws TimeoutException if the wait timed out |
695 |
+ |
*/ |
696 |
|
public final V get(long timeout, TimeUnit unit) |
697 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
698 |
|
Thread t = Thread.currentThread(); |
700 |
|
if (t instanceof ForkJoinWorkerThread) { |
701 |
|
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
702 |
|
if (status >= 0 && w.unpushTask(this)) |
703 |
< |
tryExec(); |
703 |
> |
quietlyExec(); |
704 |
|
pool = w.pool; |
705 |
|
} |
706 |
|
else |
707 |
|
pool = null; |
708 |
|
/* |
709 |
< |
* Timed wait loop intermixes cases for fj (pool != null) and |
709 |
> |
* Timed wait loop intermixes cases for FJ (pool != null) and |
710 |
|
* non FJ threads. For FJ, decrement pool count but don't try |
711 |
|
* for replacement; increment count on completion. For non-FJ, |
712 |
|
* deal with interrupts. This is messy, but a little less so |
714 |
|
*/ |
715 |
|
boolean interrupted = false; |
716 |
|
boolean dec = false; // true if pool count decremented |
717 |
+ |
long nanos = unit.toNanos(timeout); |
718 |
|
for (;;) { |
719 |
< |
if (Thread.interrupted() && pool == null) { |
719 |
> |
if (pool == null && Thread.interrupted()) { |
720 |
|
interrupted = true; |
721 |
|
break; |
722 |
|
} |
723 |
|
int s = status; |
724 |
|
if (s < 0) |
725 |
|
break; |
726 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
698 |
< |
s, s | SIGNAL)) { |
726 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
727 |
|
long startTime = System.nanoTime(); |
700 |
– |
long nanos = unit.toNanos(timeout); |
728 |
|
long nt; // wait time |
729 |
|
while (status >= 0 && |
730 |
|
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
734 |
|
long ms = nt / 1000000; |
735 |
|
int ns = (int) (nt % 1000000); |
736 |
|
try { |
737 |
< |
synchronized(this) { |
737 |
> |
synchronized (this) { |
738 |
|
if (status >= 0) |
739 |
|
wait(ms, ns); |
740 |
|
} |
768 |
|
} |
769 |
|
|
770 |
|
/** |
771 |
< |
* Joins this task, without returning its result or throwing an |
771 |
> |
* Joins this task, without returning its result or throwing its |
772 |
|
* exception. This method may be useful when processing |
773 |
|
* collections of tasks when some have been cancelled or otherwise |
774 |
|
* known to have aborted. |
775 |
|
*/ |
776 |
|
public final void quietlyJoin() { |
777 |
< |
doJoin(); |
777 |
> |
Thread t; |
778 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
779 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
780 |
> |
if (status >= 0) { |
781 |
> |
if (w.unpushTask(this)) { |
782 |
> |
boolean completed; |
783 |
> |
try { |
784 |
> |
completed = exec(); |
785 |
> |
} catch (Throwable rex) { |
786 |
> |
setExceptionalCompletion(rex); |
787 |
> |
return; |
788 |
> |
} |
789 |
> |
if (completed) { |
790 |
> |
setCompletion(NORMAL); |
791 |
> |
return; |
792 |
> |
} |
793 |
> |
} |
794 |
> |
w.joinTask(this); |
795 |
> |
} |
796 |
> |
} |
797 |
> |
else |
798 |
> |
externalAwaitDone(); |
799 |
|
} |
800 |
|
|
801 |
|
/** |
802 |
|
* Commences performing this task and awaits its completion if |
803 |
< |
* necessary, without returning its result or throwing an |
804 |
< |
* exception. This method may be useful when processing |
757 |
< |
* collections of tasks when some have been cancelled or otherwise |
758 |
< |
* known to have aborted. |
803 |
> |
* necessary, without returning its result or throwing its |
804 |
> |
* exception. |
805 |
|
*/ |
806 |
|
public final void quietlyInvoke() { |
807 |
< |
doInvoke(); |
807 |
> |
if (status >= 0) { |
808 |
> |
boolean completed; |
809 |
> |
try { |
810 |
> |
completed = exec(); |
811 |
> |
} catch (Throwable rex) { |
812 |
> |
setExceptionalCompletion(rex); |
813 |
> |
return; |
814 |
> |
} |
815 |
> |
if (completed) |
816 |
> |
setCompletion(NORMAL); |
817 |
> |
else |
818 |
> |
quietlyJoin(); |
819 |
> |
} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
1138 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1139 |
|
|
1140 |
|
/** |
1141 |
< |
* Saves the state to a stream. |
1141 |
> |
* Saves the state to a stream (that is, serializes it). |
1142 |
|
* |
1143 |
|
* @serialData the current run status and the exception thrown |
1144 |
|
* during execution, or {@code null} if none |
1151 |
|
} |
1152 |
|
|
1153 |
|
/** |
1154 |
< |
* Reconstitutes the instance from a stream. |
1154 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1155 |
|
* |
1156 |
|
* @param s the stream |
1157 |
|
*/ |
1161 |
|
Object ex = s.readObject(); |
1162 |
|
if (ex != null) |
1163 |
|
setExceptionalCompletion((Throwable) ex); |
1106 |
– |
if (status < 0) |
1107 |
– |
synchronized (this) { notifyAll(); } |
1164 |
|
} |
1165 |
|
|
1166 |
|
// Unsafe mechanics |