1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
12 |
|
import java.util.List; |
13 |
|
import java.util.RandomAccess; |
14 |
|
import java.util.Map; |
15 |
< |
import java.util.WeakHashMap; |
15 |
> |
import java.lang.ref.WeakReference; |
16 |
> |
import java.lang.ref.ReferenceQueue; |
17 |
|
import java.util.concurrent.Callable; |
18 |
|
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.ExecutionException; |
24 |
|
import java.util.concurrent.RunnableFuture; |
25 |
|
import java.util.concurrent.TimeUnit; |
26 |
|
import java.util.concurrent.TimeoutException; |
27 |
+ |
import java.util.concurrent.locks.ReentrantLock; |
28 |
+ |
import java.lang.reflect.Constructor; |
29 |
|
|
30 |
|
/** |
31 |
|
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
69 |
|
* rethrown to callers attempting to join them. These exceptions may |
70 |
|
* additionally include {@link RejectedExecutionException} stemming |
71 |
|
* from internal resource exhaustion, such as failure to allocate |
72 |
< |
* internal task queues. |
72 |
> |
* internal task queues. Rethrown exceptions behave in the same way as |
73 |
> |
* regular exceptions, but, when possible, contain stack traces (as |
74 |
> |
* displayed for example using {@code ex.printStackTrace()}) of both |
75 |
> |
* the thread that initiated the computation as well as the thread |
76 |
> |
* actually encountering the exception; minimally only the latter. |
77 |
|
* |
78 |
|
* <p>The primary method for awaiting completion and extracting |
79 |
|
* results of a task is {@link #join}, but there are several variants: |
170 |
|
* status maintenance (2) execution and awaiting completion (3) |
171 |
|
* user-level methods that additionally report results. This is |
172 |
|
* sometimes hard to see because this file orders exported methods |
173 |
< |
* in a way that flows well in javadocs. In particular, most |
167 |
< |
* join mechanics are in method quietlyJoin, below. |
173 |
> |
* in a way that flows well in javadocs. |
174 |
|
*/ |
175 |
|
|
176 |
|
/* |
192 |
|
|
193 |
|
/** The run status of this task */ |
194 |
|
volatile int status; // accessed directly by pool and workers |
189 |
– |
|
195 |
|
private static final int NORMAL = -1; |
196 |
|
private static final int CANCELLED = -2; |
197 |
|
private static final int EXCEPTIONAL = -3; |
198 |
|
private static final int SIGNAL = 1; |
199 |
|
|
200 |
|
/** |
196 |
– |
* Table of exceptions thrown by tasks, to enable reporting by |
197 |
– |
* callers. Because exceptions are rare, we don't directly keep |
198 |
– |
* them with task objects, but instead use a weak ref table. Note |
199 |
– |
* that cancellation exceptions don't appear in the table, but are |
200 |
– |
* instead recorded as status values. |
201 |
– |
* TODO: Use ConcurrentReferenceHashMap |
202 |
– |
*/ |
203 |
– |
static final Map<ForkJoinTask<?>, Throwable> exceptionMap = |
204 |
– |
Collections.synchronizedMap |
205 |
– |
(new WeakHashMap<ForkJoinTask<?>, Throwable>()); |
206 |
– |
|
207 |
– |
// Maintaining completion status |
208 |
– |
|
209 |
– |
/** |
201 |
|
* Marks completion and wakes up threads waiting to join this task, |
202 |
|
* also clearing signal request bits. |
203 |
|
* |
204 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
205 |
+ |
* @return completion status on exit |
206 |
|
*/ |
207 |
< |
private void setCompletion(int completion) { |
208 |
< |
int s; |
209 |
< |
while ((s = status) >= 0) { |
207 |
> |
private int setCompletion(int completion) { |
208 |
> |
for (int s;;) { |
209 |
> |
if ((s = status) < 0) |
210 |
> |
return s; |
211 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
212 |
|
if (s != 0) |
213 |
|
synchronized (this) { notifyAll(); } |
214 |
< |
break; |
214 |
> |
return completion; |
215 |
|
} |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* Records exception and sets exceptional completion. |
220 |
> |
* Tries to block a worker thread until completed or timed out. |
221 |
> |
* Uses Object.wait time argument conventions. |
222 |
> |
* May fail on contention or interrupt. |
223 |
|
* |
224 |
< |
* @return status on exit |
224 |
> |
* @param millis if > 0, wait time. |
225 |
|
*/ |
226 |
< |
private void setExceptionalCompletion(Throwable rex) { |
227 |
< |
exceptionMap.put(this, rex); |
228 |
< |
setCompletion(EXCEPTIONAL); |
229 |
< |
} |
230 |
< |
|
231 |
< |
/** |
232 |
< |
* Blocks a worker thread until completed or timed out. Called |
238 |
< |
* only by pool. |
239 |
< |
*/ |
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 |
226 |
> |
final void tryAwaitDone(long millis) { |
227 |
> |
int s; |
228 |
> |
try { |
229 |
> |
if (((s = status) > 0 || |
230 |
> |
(s == 0 && |
231 |
> |
UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL))) && |
232 |
> |
status > 0) { |
233 |
|
synchronized (this) { |
234 |
|
if (status > 0) |
235 |
< |
wait(millis, nanos); |
249 |
< |
else |
250 |
< |
notifyAll(); |
235 |
> |
wait(millis); |
236 |
|
} |
252 |
– |
} catch (InterruptedException ie) { |
253 |
– |
cancelIfTerminating(); |
237 |
|
} |
238 |
+ |
} catch (InterruptedException ie) { |
239 |
+ |
// caller must check termination |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
|
* Blocks a non-worker-thread until completion. |
245 |
+ |
* @return status upon completion |
246 |
|
*/ |
247 |
< |
private void externalAwaitDone() { |
248 |
< |
if (status >= 0) { |
247 |
> |
private int externalAwaitDone() { |
248 |
> |
int s; |
249 |
> |
if ((s = status) >= 0) { |
250 |
|
boolean interrupted = false; |
251 |
|
synchronized (this) { |
252 |
< |
for (;;) { |
266 |
< |
int s = status; |
252 |
> |
while ((s = status) >= 0) { |
253 |
|
if (s == 0) |
254 |
|
UNSAFE.compareAndSwapInt(this, statusOffset, |
255 |
|
0, SIGNAL); |
270 |
– |
else if (s < 0) { |
271 |
– |
notifyAll(); |
272 |
– |
break; |
273 |
– |
} |
256 |
|
else { |
257 |
|
try { |
258 |
|
wait(); |
265 |
|
if (interrupted) |
266 |
|
Thread.currentThread().interrupt(); |
267 |
|
} |
268 |
+ |
return s; |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
|
* Blocks a non-worker-thread until completion or interruption or timeout. |
273 |
|
*/ |
274 |
< |
private void externalInterruptibleAwaitDone(boolean timed, long nanos) |
274 |
> |
private int externalInterruptibleAwaitDone(long millis) |
275 |
|
throws InterruptedException { |
276 |
+ |
int s; |
277 |
|
if (Thread.interrupted()) |
278 |
|
throw new InterruptedException(); |
279 |
< |
if (status >= 0) { |
296 |
< |
long startTime = timed ? System.nanoTime() : 0L; |
279 |
> |
if ((s = status) >= 0) { |
280 |
|
synchronized (this) { |
281 |
< |
for (;;) { |
299 |
< |
long nt; |
300 |
< |
int s = status; |
281 |
> |
while ((s = status) >= 0) { |
282 |
|
if (s == 0) |
283 |
|
UNSAFE.compareAndSwapInt(this, statusOffset, |
284 |
|
0, SIGNAL); |
285 |
< |
else if (s < 0) { |
286 |
< |
notifyAll(); |
287 |
< |
break; |
285 |
> |
else { |
286 |
> |
wait(millis); |
287 |
> |
if (millis > 0L) |
288 |
> |
break; |
289 |
|
} |
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; |
290 |
|
} |
291 |
|
} |
292 |
|
} |
293 |
+ |
return s; |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
< |
* Unless done, calls exec and records status if completed, but |
298 |
< |
* doesn't wait for completion otherwise. Primary execution method |
299 |
< |
* for ForkJoinWorkerThread. |
297 |
> |
* Primary execution method for stolen tasks. Unless done, calls |
298 |
> |
* exec and records status if completed, but doesn't wait for |
299 |
> |
* completion otherwise. |
300 |
|
*/ |
301 |
< |
final void quietlyExec() { |
302 |
< |
try { |
303 |
< |
if (status < 0 || !exec()) |
301 |
> |
final void doExec() { |
302 |
> |
if (status >= 0) { |
303 |
> |
boolean completed; |
304 |
> |
try { |
305 |
> |
completed = exec(); |
306 |
> |
} catch (Throwable rex) { |
307 |
> |
setExceptionalCompletion(rex); |
308 |
|
return; |
309 |
+ |
} |
310 |
+ |
if (completed) |
311 |
+ |
setCompletion(NORMAL); // must be outside try block |
312 |
+ |
} |
313 |
+ |
} |
314 |
+ |
|
315 |
+ |
/** |
316 |
+ |
* Primary mechanics for join, get, quietlyJoin. |
317 |
+ |
* @return status upon completion |
318 |
+ |
*/ |
319 |
+ |
private int doJoin() { |
320 |
+ |
Thread t; ForkJoinWorkerThread w; int s; boolean completed; |
321 |
+ |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
322 |
+ |
if ((s = status) < 0) |
323 |
+ |
return s; |
324 |
+ |
if ((w = (ForkJoinWorkerThread)t).unpushTask(this)) { |
325 |
+ |
try { |
326 |
+ |
completed = exec(); |
327 |
+ |
} catch (Throwable rex) { |
328 |
+ |
return setExceptionalCompletion(rex); |
329 |
+ |
} |
330 |
+ |
if (completed) |
331 |
+ |
return setCompletion(NORMAL); |
332 |
+ |
} |
333 |
+ |
return w.joinTask(this); |
334 |
+ |
} |
335 |
+ |
else |
336 |
+ |
return externalAwaitDone(); |
337 |
+ |
} |
338 |
+ |
|
339 |
+ |
/** |
340 |
+ |
* Primary mechanics for invoke, quietlyInvoke. |
341 |
+ |
* @return status upon completion |
342 |
+ |
*/ |
343 |
+ |
private int doInvoke() { |
344 |
+ |
int s; boolean completed; |
345 |
+ |
if ((s = status) < 0) |
346 |
+ |
return s; |
347 |
+ |
try { |
348 |
+ |
completed = exec(); |
349 |
|
} catch (Throwable rex) { |
350 |
< |
setExceptionalCompletion(rex); |
351 |
< |
return; |
350 |
> |
return setExceptionalCompletion(rex); |
351 |
> |
} |
352 |
> |
if (completed) |
353 |
> |
return setCompletion(NORMAL); |
354 |
> |
else |
355 |
> |
return doJoin(); |
356 |
> |
} |
357 |
> |
|
358 |
> |
// Exception table support |
359 |
> |
|
360 |
> |
/** |
361 |
> |
* Table of exceptions thrown by tasks, to enable reporting by |
362 |
> |
* callers. Because exceptions are rare, we don't directly keep |
363 |
> |
* them with task objects, but instead use a weak ref table. Note |
364 |
> |
* that cancellation exceptions don't appear in the table, but are |
365 |
> |
* instead recorded as status values. |
366 |
> |
* |
367 |
> |
* Note: These statics are initialized below in static block. |
368 |
> |
*/ |
369 |
> |
private static final ExceptionNode[] exceptionTable; |
370 |
> |
private static final ReentrantLock exceptionTableLock; |
371 |
> |
private static final ReferenceQueue<Object> exceptionTableRefQueue; |
372 |
> |
|
373 |
> |
/** |
374 |
> |
* Fixed capacity for exceptionTable. |
375 |
> |
*/ |
376 |
> |
private static final int EXCEPTION_MAP_CAPACITY = 32; |
377 |
> |
|
378 |
> |
/** |
379 |
> |
* Key-value nodes for exception table. The chained hash table |
380 |
> |
* uses identity comparisons, full locking, and weak references |
381 |
> |
* for keys. The table has a fixed capacity because it only |
382 |
> |
* maintains task exceptions long enough for joiners to access |
383 |
> |
* them, so should never become very large for sustained |
384 |
> |
* periods. However, since we do not know when the last joiner |
385 |
> |
* completes, we must use weak references and expunge them. We do |
386 |
> |
* so on each operation (hence full locking). Also, some thread in |
387 |
> |
* any ForkJoinPool will call helpExpungeStaleExceptions when its |
388 |
> |
* pool becomes isQuiescent. |
389 |
> |
*/ |
390 |
> |
static final class ExceptionNode extends WeakReference<ForkJoinTask<?>>{ |
391 |
> |
final Throwable ex; |
392 |
> |
ExceptionNode next; |
393 |
> |
final long thrower; // use id not ref to avoid weak cycles |
394 |
> |
ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next) { |
395 |
> |
super(task, exceptionTableRefQueue); |
396 |
> |
this.ex = ex; |
397 |
> |
this.next = next; |
398 |
> |
this.thrower = Thread.currentThread().getId(); |
399 |
> |
} |
400 |
> |
} |
401 |
> |
|
402 |
> |
/** |
403 |
> |
* Records exception and sets exceptional completion. |
404 |
> |
* |
405 |
> |
* @return status on exit |
406 |
> |
*/ |
407 |
> |
private int setExceptionalCompletion(Throwable ex) { |
408 |
> |
int h = System.identityHashCode(this); |
409 |
> |
final ReentrantLock lock = exceptionTableLock; |
410 |
> |
lock.lock(); |
411 |
> |
try { |
412 |
> |
expungeStaleExceptions(); |
413 |
> |
ExceptionNode[] t = exceptionTable; |
414 |
> |
int i = h & (t.length - 1); |
415 |
> |
for (ExceptionNode e = t[i]; ; e = e.next) { |
416 |
> |
if (e == null) { |
417 |
> |
t[i] = new ExceptionNode(this, ex, t[i]); |
418 |
> |
break; |
419 |
> |
} |
420 |
> |
if (e.get() == this) // already present |
421 |
> |
break; |
422 |
> |
} |
423 |
> |
} finally { |
424 |
> |
lock.unlock(); |
425 |
> |
} |
426 |
> |
return setCompletion(EXCEPTIONAL); |
427 |
> |
} |
428 |
> |
|
429 |
> |
/** |
430 |
> |
* Removes exception node and clears status |
431 |
> |
*/ |
432 |
> |
private void clearExceptionalCompletion() { |
433 |
> |
int h = System.identityHashCode(this); |
434 |
> |
final ReentrantLock lock = exceptionTableLock; |
435 |
> |
lock.lock(); |
436 |
> |
try { |
437 |
> |
ExceptionNode[] t = exceptionTable; |
438 |
> |
int i = h & (t.length - 1); |
439 |
> |
ExceptionNode e = t[i]; |
440 |
> |
ExceptionNode pred = null; |
441 |
> |
while (e != null) { |
442 |
> |
ExceptionNode next = e.next; |
443 |
> |
if (e.get() == this) { |
444 |
> |
if (pred == null) |
445 |
> |
t[i] = next; |
446 |
> |
else |
447 |
> |
pred.next = next; |
448 |
> |
break; |
449 |
> |
} |
450 |
> |
pred = e; |
451 |
> |
e = next; |
452 |
> |
} |
453 |
> |
expungeStaleExceptions(); |
454 |
> |
status = 0; |
455 |
> |
} finally { |
456 |
> |
lock.unlock(); |
457 |
> |
} |
458 |
> |
} |
459 |
> |
|
460 |
> |
/** |
461 |
> |
* Returns a rethrowable exception for the given task, if |
462 |
> |
* available. To provide accurate stack traces, if the exception |
463 |
> |
* was not thrown by the current thread, we try to create a new |
464 |
> |
* exception of the same type as the one thrown, but with the |
465 |
> |
* recorded exception as its cause. If there is no such |
466 |
> |
* constructor, we instead try to use a no-arg constructor, |
467 |
> |
* followed by initCause, to the same effect. If none of these |
468 |
> |
* apply, or any fail due to other exceptions, we return the |
469 |
> |
* recorded exception, which is still correct, although it may |
470 |
> |
* contain a misleading stack trace. |
471 |
> |
* |
472 |
> |
* @return the exception, or null if none |
473 |
> |
*/ |
474 |
> |
private Throwable getThrowableException() { |
475 |
> |
if (status != EXCEPTIONAL) |
476 |
> |
return null; |
477 |
> |
int h = System.identityHashCode(this); |
478 |
> |
ExceptionNode e; |
479 |
> |
final ReentrantLock lock = exceptionTableLock; |
480 |
> |
lock.lock(); |
481 |
> |
try { |
482 |
> |
expungeStaleExceptions(); |
483 |
> |
ExceptionNode[] t = exceptionTable; |
484 |
> |
e = t[h & (t.length - 1)]; |
485 |
> |
while (e != null && e.get() != this) |
486 |
> |
e = e.next; |
487 |
> |
} finally { |
488 |
> |
lock.unlock(); |
489 |
> |
} |
490 |
> |
Throwable ex; |
491 |
> |
if (e == null || (ex = e.ex) == null) |
492 |
> |
return null; |
493 |
> |
if (e.thrower != Thread.currentThread().getId()) { |
494 |
> |
Class ec = ex.getClass(); |
495 |
> |
try { |
496 |
> |
Constructor<?> noArgCtor = null; |
497 |
> |
Constructor<?>[] cs = ec.getConstructors();// public ctors only |
498 |
> |
for (int i = 0; i < cs.length; ++i) { |
499 |
> |
Constructor<?> c = cs[i]; |
500 |
> |
Class<?>[] ps = c.getParameterTypes(); |
501 |
> |
if (ps.length == 0) |
502 |
> |
noArgCtor = c; |
503 |
> |
else if (ps.length == 1 && ps[0] == Throwable.class) |
504 |
> |
return (Throwable)(c.newInstance(ex)); |
505 |
> |
} |
506 |
> |
if (noArgCtor != null) { |
507 |
> |
Throwable wx = (Throwable)(noArgCtor.newInstance()); |
508 |
> |
wx.initCause(ex); |
509 |
> |
return wx; |
510 |
> |
} |
511 |
> |
} catch (Exception ignore) { |
512 |
> |
} |
513 |
|
} |
514 |
< |
setCompletion(NORMAL); // must be outside try block |
514 |
> |
return ex; |
515 |
> |
} |
516 |
> |
|
517 |
> |
/** |
518 |
> |
* Poll stale refs and remove them. Call only while holding lock. |
519 |
> |
*/ |
520 |
> |
private static void expungeStaleExceptions() { |
521 |
> |
for (Object x; (x = exceptionTableRefQueue.poll()) != null;) { |
522 |
> |
if (x instanceof ExceptionNode) { |
523 |
> |
ForkJoinTask<?> key = ((ExceptionNode)x).get(); |
524 |
> |
ExceptionNode[] t = exceptionTable; |
525 |
> |
int i = System.identityHashCode(key) & (t.length - 1); |
526 |
> |
ExceptionNode e = t[i]; |
527 |
> |
ExceptionNode pred = null; |
528 |
> |
while (e != null) { |
529 |
> |
ExceptionNode next = e.next; |
530 |
> |
if (e == x) { |
531 |
> |
if (pred == null) |
532 |
> |
t[i] = next; |
533 |
> |
else |
534 |
> |
pred.next = next; |
535 |
> |
break; |
536 |
> |
} |
537 |
> |
pred = e; |
538 |
> |
e = next; |
539 |
> |
} |
540 |
> |
} |
541 |
> |
} |
542 |
> |
} |
543 |
> |
|
544 |
> |
/** |
545 |
> |
* If lock is available, poll stale refs and remove them. |
546 |
> |
* Called from ForkJoinPool when pools become quiescent. |
547 |
> |
*/ |
548 |
> |
static final void helpExpungeStaleExceptions() { |
549 |
> |
final ReentrantLock lock = exceptionTableLock; |
550 |
> |
if (lock.tryLock()) { |
551 |
> |
try { |
552 |
> |
expungeStaleExceptions(); |
553 |
> |
} finally { |
554 |
> |
lock.unlock(); |
555 |
> |
} |
556 |
> |
} |
557 |
> |
} |
558 |
> |
|
559 |
> |
/** |
560 |
> |
* Report the result of invoke or join; called only upon |
561 |
> |
* non-normal return of internal versions. |
562 |
> |
*/ |
563 |
> |
private V reportResult() { |
564 |
> |
int s; Throwable ex; |
565 |
> |
if ((s = status) == CANCELLED) |
566 |
> |
throw new CancellationException(); |
567 |
> |
if (s == EXCEPTIONAL && (ex = getThrowableException()) != null) |
568 |
> |
UNSAFE.throwException(ex); |
569 |
> |
return getRawResult(); |
570 |
|
} |
571 |
|
|
572 |
|
// public methods |
607 |
|
* @return the computed result |
608 |
|
*/ |
609 |
|
public final V join() { |
610 |
< |
quietlyJoin(); |
611 |
< |
Throwable ex; |
612 |
< |
if (status < NORMAL && (ex = getException()) != null) |
613 |
< |
UNSAFE.throwException(ex); |
377 |
< |
return getRawResult(); |
610 |
> |
if (doJoin() != NORMAL) |
611 |
> |
return reportResult(); |
612 |
> |
else |
613 |
> |
return getRawResult(); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
622 |
|
* @return the computed result |
623 |
|
*/ |
624 |
|
public final V invoke() { |
625 |
< |
quietlyInvoke(); |
626 |
< |
Throwable ex; |
627 |
< |
if (status < NORMAL && (ex = getException()) != null) |
628 |
< |
UNSAFE.throwException(ex); |
393 |
< |
return getRawResult(); |
625 |
> |
if (doInvoke() != NORMAL) |
626 |
> |
return reportResult(); |
627 |
> |
else |
628 |
> |
return getRawResult(); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
689 |
|
} |
690 |
|
else if (i != 0) |
691 |
|
t.fork(); |
692 |
< |
else { |
693 |
< |
t.quietlyInvoke(); |
459 |
< |
if (ex == null && t.status < NORMAL) |
460 |
< |
ex = t.getException(); |
461 |
< |
} |
692 |
> |
else if (t.doInvoke() < NORMAL && ex == null) |
693 |
> |
ex = t.getException(); |
694 |
|
} |
695 |
|
for (int i = 1; i <= last; ++i) { |
696 |
|
ForkJoinTask<?> t = tasks[i]; |
697 |
|
if (t != null) { |
698 |
|
if (ex != null) |
699 |
|
t.cancel(false); |
700 |
< |
else { |
701 |
< |
t.quietlyJoin(); |
470 |
< |
if (ex == null && t.status < NORMAL) |
471 |
< |
ex = t.getException(); |
472 |
< |
} |
700 |
> |
else if (t.doJoin() < NORMAL && ex == null) |
701 |
> |
ex = t.getException(); |
702 |
|
} |
703 |
|
} |
704 |
|
if (ex != null) |
746 |
|
} |
747 |
|
else if (i != 0) |
748 |
|
t.fork(); |
749 |
< |
else { |
750 |
< |
t.quietlyInvoke(); |
522 |
< |
if (ex == null && t.status < NORMAL) |
523 |
< |
ex = t.getException(); |
524 |
< |
} |
749 |
> |
else if (t.doInvoke() < NORMAL && ex == null) |
750 |
> |
ex = t.getException(); |
751 |
|
} |
752 |
|
for (int i = 1; i <= last; ++i) { |
753 |
|
ForkJoinTask<?> t = ts.get(i); |
754 |
|
if (t != null) { |
755 |
|
if (ex != null) |
756 |
|
t.cancel(false); |
757 |
< |
else { |
758 |
< |
t.quietlyJoin(); |
533 |
< |
if (ex == null && t.status < NORMAL) |
534 |
< |
ex = t.getException(); |
535 |
< |
} |
757 |
> |
else if (t.doJoin() < NORMAL && ex == null) |
758 |
> |
ex = t.getException(); |
759 |
|
} |
760 |
|
} |
761 |
|
if (ex != null) |
791 |
|
* @return {@code true} if this task is now cancelled |
792 |
|
*/ |
793 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
794 |
< |
setCompletion(CANCELLED); |
572 |
< |
return status == CANCELLED; |
794 |
> |
return setCompletion(CANCELLED) == CANCELLED; |
795 |
|
} |
796 |
|
|
797 |
|
/** |
807 |
|
} |
808 |
|
} |
809 |
|
|
588 |
– |
/** |
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 |
– |
|
810 |
|
public final boolean isDone() { |
811 |
|
return status < 0; |
812 |
|
} |
846 |
|
int s = status; |
847 |
|
return ((s >= NORMAL) ? null : |
848 |
|
(s == CANCELLED) ? new CancellationException() : |
849 |
< |
exceptionMap.get(this)); |
849 |
> |
getThrowableException()); |
850 |
|
} |
851 |
|
|
852 |
|
/** |
904 |
|
* member of a ForkJoinPool and was interrupted while waiting |
905 |
|
*/ |
906 |
|
public final V get() throws InterruptedException, ExecutionException { |
907 |
< |
Thread t = Thread.currentThread(); |
908 |
< |
if (t instanceof ForkJoinWorkerThread) |
909 |
< |
quietlyJoin(); |
910 |
< |
else |
911 |
< |
externalInterruptibleAwaitDone(false, 0L); |
912 |
< |
int s = status; |
913 |
< |
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 |
< |
} |
907 |
> |
int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ? |
908 |
> |
doJoin() : externalInterruptibleAwaitDone(0L); |
909 |
> |
Throwable ex; |
910 |
> |
if (s == CANCELLED) |
911 |
> |
throw new CancellationException(); |
912 |
> |
if (s == EXCEPTIONAL && (ex = getThrowableException()) != null) |
913 |
> |
throw new ExecutionException(ex); |
914 |
|
return getRawResult(); |
915 |
|
} |
916 |
|
|
930 |
|
*/ |
931 |
|
public final V get(long timeout, TimeUnit unit) |
932 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
732 |
– |
long nanos = unit.toNanos(timeout); |
933 |
|
Thread t = Thread.currentThread(); |
934 |
< |
if (t instanceof ForkJoinWorkerThread) |
935 |
< |
((ForkJoinWorkerThread)t).joinTask(this, true, nanos); |
936 |
< |
else |
937 |
< |
externalInterruptibleAwaitDone(true, nanos); |
934 |
> |
if (t instanceof ForkJoinWorkerThread) { |
935 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
936 |
> |
long nanos = unit.toNanos(timeout); |
937 |
> |
if (status >= 0) { |
938 |
> |
boolean completed = false; |
939 |
> |
if (w.unpushTask(this)) { |
940 |
> |
try { |
941 |
> |
completed = exec(); |
942 |
> |
} catch (Throwable rex) { |
943 |
> |
setExceptionalCompletion(rex); |
944 |
> |
} |
945 |
> |
} |
946 |
> |
if (completed) |
947 |
> |
setCompletion(NORMAL); |
948 |
> |
else if (status >= 0 && nanos > 0) |
949 |
> |
w.pool.timedAwaitJoin(this, nanos); |
950 |
> |
} |
951 |
> |
} |
952 |
> |
else { |
953 |
> |
long millis = unit.toMillis(timeout); |
954 |
> |
if (millis > 0) |
955 |
> |
externalInterruptibleAwaitDone(millis); |
956 |
> |
} |
957 |
|
int s = status; |
958 |
|
if (s != NORMAL) { |
959 |
|
Throwable ex; |
960 |
|
if (s == CANCELLED) |
961 |
|
throw new CancellationException(); |
962 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
962 |
> |
if (s != EXCEPTIONAL) |
963 |
> |
throw new TimeoutException(); |
964 |
> |
if ((ex = getThrowableException()) != null) |
965 |
|
throw new ExecutionException(ex); |
745 |
– |
throw new TimeoutException(); |
966 |
|
} |
967 |
|
return getRawResult(); |
968 |
|
} |
974 |
|
* known to have aborted. |
975 |
|
*/ |
976 |
|
public final void quietlyJoin() { |
977 |
< |
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(); |
977 |
> |
doJoin(); |
978 |
|
} |
979 |
|
|
980 |
|
/** |
983 |
|
* exception. |
984 |
|
*/ |
985 |
|
public final void quietlyInvoke() { |
986 |
< |
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 |
< |
} |
986 |
> |
doInvoke(); |
987 |
|
} |
988 |
|
|
989 |
|
/** |
1022 |
|
*/ |
1023 |
|
public void reinitialize() { |
1024 |
|
if (status == EXCEPTIONAL) |
1025 |
< |
exceptionMap.remove(this); |
1026 |
< |
status = 0; |
1025 |
> |
clearExceptionalCompletion(); |
1026 |
> |
else |
1027 |
> |
status = 0; |
1028 |
|
} |
1029 |
|
|
1030 |
|
/** |
1335 |
|
s.defaultReadObject(); |
1336 |
|
Object ex = s.readObject(); |
1337 |
|
if (ex != null) |
1338 |
< |
setExceptionalCompletion((Throwable) ex); |
1338 |
> |
setExceptionalCompletion((Throwable)ex); |
1339 |
|
} |
1340 |
|
|
1341 |
|
// Unsafe mechanics |
1342 |
< |
|
1343 |
< |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1344 |
< |
private static final long statusOffset = |
1345 |
< |
objectFieldOffset("status", ForkJoinTask.class); |
1346 |
< |
|
1347 |
< |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1342 |
> |
private static final sun.misc.Unsafe UNSAFE; |
1343 |
> |
private static final long statusOffset; |
1344 |
> |
static { |
1345 |
> |
exceptionTableLock = new ReentrantLock(); |
1346 |
> |
exceptionTableRefQueue = new ReferenceQueue<Object>(); |
1347 |
> |
exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY]; |
1348 |
|
try { |
1349 |
< |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1350 |
< |
} catch (NoSuchFieldException e) { |
1351 |
< |
// Convert Exception to corresponding Error |
1352 |
< |
NoSuchFieldError error = new NoSuchFieldError(field); |
1353 |
< |
error.initCause(e); |
1166 |
< |
throw error; |
1349 |
> |
UNSAFE = getUnsafe(); |
1350 |
> |
statusOffset = UNSAFE.objectFieldOffset |
1351 |
> |
(ForkJoinTask.class.getDeclaredField("status")); |
1352 |
> |
} catch (Exception e) { |
1353 |
> |
throw new Error(e); |
1354 |
|
} |
1355 |
|
} |
1356 |
|
|