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