--- jsr166/src/jsr166y/CountedCompleter.java 2012/08/16 12:25:03 1.3 +++ jsr166/src/jsr166y/CountedCompleter.java 2012/10/30 16:05:35 1.7 @@ -22,12 +22,13 @@ package jsr166y; * decremented; otherwise, the completion action is performed, and if * this completer itself has a completer, the process is continued * with its completer. As is the case with related synchronization - * components such as {@link Phaser} and {@link - * java.util.concurrent.Semaphore} these methods affect only internal - * counts; they do not establish any further internal bookkeeping. In - * particular, the identities of pending tasks are not maintained. As - * illustrated below, you can create subclasses that do record some or - * all pended tasks or their results when needed. + * components such as {@link java.util.concurrent.Phaser Phaser} and + * {@link java.util.concurrent.Semaphore Semaphore}, these methods + * affect only internal counts; they do not establish any further + * internal bookkeeping. In particular, the identities of pending + * tasks are not maintained. As illustrated below, you can create + * subclasses that do record some or all pending tasks or their + * results when needed. * *

A concrete CountedCompleter class must define method {@link * #compute}, that should, in almost all use cases, invoke {@code @@ -141,7 +142,9 @@ package jsr166y; * * As a further improvement, notice that the left task need not even * exist. Instead of creating a new one, we can iterate using the - * original task, and add a pending count for each fork: + * original task, and add a pending count for each fork. Additionally, + * this version uses {@code helpComplete} to streamline assistance in + * the execution of forked tasks. * *

 {@code
  * class ForEach ...
@@ -155,7 +158,7 @@ package jsr166y;
  *         }
  *         if (h > l)
  *             op.apply(array[l]);
- *         tryComplete();
+ *         helpComplete();
  *     }
  * }
* @@ -377,6 +380,19 @@ public abstract class CountedCompleter getRoot() { + CountedCompleter a = this, p; + while ((p = a.completer) != null) + a = p; + return a; + } + + /** * If the pending count is nonzero, decrements the count; * otherwise invokes {@link #onCompletion} and then similarly * tries to complete this task's completer, if one exists, @@ -398,6 +414,39 @@ public abstract class CountedCompleter a = this, s = a; + for (int c;;) { + if ((c = a.pending) == 0) { + a.onCompletion(s); + if ((a = (s = a).completer) == null) { + s.quietlyComplete(); + return; + } + } + else if (U.compareAndSwapInt(a, PENDING, c, c - 1)) { + CountedCompleter root = a.getRoot(); + Thread thread = Thread.currentThread(); + ForkJoinPool.WorkQueue wq = + (thread instanceof ForkJoinWorkerThread) ? + ((ForkJoinWorkerThread)thread).workQueue : null; + ForkJoinTask t; + while ((t = (wq != null) ? wq.popCC(root) : + ForkJoinPool.popCCFromCommonPool(root)) != null) { + t.doExec(); + if (root.isDone()) + break; + } + return; + } + } + } + + /** * Regardless of pending count, invokes {@link #onCompletion}, * marks this task as complete and further triggers {@link * #tryComplete} on this task's completer, if one exists. This @@ -464,7 +513,6 @@ public abstract class CountedCompleter