27 |
|
* affect only internal counts; they do not establish any further |
28 |
|
* internal bookkeeping. In particular, the identities of pending |
29 |
|
* tasks are not maintained. As illustrated below, you can create |
30 |
< |
* subclasses that do record some or all pended tasks or their results |
31 |
< |
* when needed. |
30 |
> |
* subclasses that do record some or all pending tasks or their |
31 |
> |
* results when needed. |
32 |
|
* |
33 |
|
* <p>A concrete CountedCompleter class must define method {@link |
34 |
|
* #compute}, that should, in almost all use cases, invoke {@code |
142 |
|
* |
143 |
|
* As a further improvement, notice that the left task need not even |
144 |
|
* exist. Instead of creating a new one, we can iterate using the |
145 |
< |
* original task, and add a pending count for each fork: |
145 |
> |
* original task, and add a pending count for each fork. Additionally, |
146 |
> |
* this version uses {@code helpComplete} to streamline assistance in |
147 |
> |
* the execution of forked tasks. |
148 |
|
* |
149 |
|
* <pre> {@code |
150 |
|
* class ForEach<E> ... |
158 |
|
* } |
159 |
|
* if (h > l) |
160 |
|
* op.apply(array[l]); |
161 |
< |
* tryComplete(); |
161 |
> |
* helpComplete(); |
162 |
|
* } |
163 |
|
* }</pre> |
164 |
|
* |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
+ |
* Returns the root of the current computation; i.e., this |
384 |
+ |
* task if it has no completer, else its completer's root. |
385 |
+ |
* |
386 |
+ |
* @return the root of the current computation |
387 |
+ |
*/ |
388 |
+ |
public final CountedCompleter<?> getRoot() { |
389 |
+ |
CountedCompleter<?> a = this, p; |
390 |
+ |
while ((p = a.completer) != null) |
391 |
+ |
a = p; |
392 |
+ |
return a; |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
/** |
396 |
|
* If the pending count is nonzero, decrements the count; |
397 |
|
* otherwise invokes {@link #onCompletion} and then similarly |
398 |
|
* tries to complete this task's completer, if one exists, |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
+ |
* Identical to {@link #tryComplete}, but may additionally execute |
418 |
+ |
* other tasks within the current computation (i.e., those |
419 |
+ |
* with the same {@link #getRoot}. |
420 |
+ |
*/ |
421 |
+ |
public final void helpComplete() { |
422 |
+ |
CountedCompleter<?> a = this, s = a; |
423 |
+ |
for (int c;;) { |
424 |
+ |
if ((c = a.pending) == 0) { |
425 |
+ |
a.onCompletion(s); |
426 |
+ |
if ((a = (s = a).completer) == null) { |
427 |
+ |
s.quietlyComplete(); |
428 |
+ |
return; |
429 |
+ |
} |
430 |
+ |
} |
431 |
+ |
else if (U.compareAndSwapInt(a, PENDING, c, c - 1)) { |
432 |
+ |
if (!(Thread.currentThread() instanceof ForkJoinWorkerThread)) |
433 |
+ |
ForkJoinPool.popAndExecCCFromCommonPool(a); |
434 |
+ |
return; |
435 |
+ |
} |
436 |
+ |
} |
437 |
+ |
} |
438 |
+ |
|
439 |
+ |
/** |
440 |
|
* Regardless of pending count, invokes {@link #onCompletion}, |
441 |
|
* marks this task as complete and further triggers {@link |
442 |
|
* #tryComplete} on this task's completer, if one exists. This |
503 |
|
} |
504 |
|
} |
505 |
|
|
468 |
– |
|
506 |
|
/** |
507 |
|
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
508 |
|
* Replace with a simple call to Unsafe.getUnsafe when integrating |