--- jsr166/src/jsr166y/CountedCompleter.java 2012/10/21 05:28:08 1.5
+++ jsr166/src/jsr166y/CountedCompleter.java 2012/11/09 03:30:03 1.9
@@ -142,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 ...
@@ -156,7 +158,7 @@ package jsr166y;
* }
* if (h > l)
* op.apply(array[l]);
- * tryComplete();
+ * helpComplete();
* }
* }
*
@@ -378,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,
@@ -399,6 +414,29 @@ 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)) {
+ if (!(Thread.currentThread() instanceof ForkJoinWorkerThread))
+ ForkJoinPool.popAndExecCCFromCommonPool(a);
+ 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
@@ -465,7 +503,6 @@ public abstract class CountedCompleter