--- jsr166/src/jsr166y/CountedCompleter.java 2012/04/09 13:12:18 1.1 +++ jsr166/src/jsr166y/CountedCompleter.java 2012/11/19 18:12:42 1.14 @@ -7,32 +7,52 @@ package jsr166y; /** - * A resultless {@link ForkJoinTask} with a completion action - * performed when triggered and there are no remaining pending - * actions. Uses of CountedCompleter are similar to those of other - * completion based components (such as {@link - * java.nio.channels.CompletionHandler}) except that multiple - * pending completions may be necessary to trigger the {@link - * #onCompletion} action, not just one. Unless initialized otherwise, - * the {@link #getPendingCount pending count} starts at zero, but may - * be (atomically) changed using methods {@link #setPendingCount}, - * {@link #addToPendingCount}, and {@link + * A {@link ForkJoinTask} with a completion action performed when + * triggered and there are no remaining pending + * actions. CountedCompleters are in general more robust in the + * presence of subtask stalls and blockage than are other forms for + * ForkJoinTasks, but are in general less intuitive to program. Uses + * of CountedCompleter are similar to those of other completion based + * components (such as {@link java.nio.channels.CompletionHandler}) + * except that multiple pending completions may be necessary + * to trigger the {@link #onCompletion} action, not just one. Unless + * initialized otherwise, the {@link #getPendingCount pending count} + * starts at zero, but may be (atomically) changed using methods + * {@link #setPendingCount}, {@link #addToPendingCount}, and {@link * #compareAndSetPendingCount}. Upon invocation of {@link * #tryComplete}, if the pending action count is nonzero, it is * 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 most basic synchronization - * constructs, 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. + * with its completer. As is the case with related synchronization + * 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. Because CountedCompleters provide only basic + * synchronization mechanisms, it may be useful to create further + * abstract subclasses that maintain linkages and fields and support + * methods appropriate for a set of related usages. * *

A concrete CountedCompleter class must define method {@link - * #compute}, that should, in almost all use cases, invoke {@code - * tryComplete()} before returning. The class may also optionally - * override method {@link #onCompletion} to perform an action upon - * normal completion. + * #compute}, that should in most cases (as illustrated below), invoke + * {@code tryComplete()} once before returning. The class may also + * optionally override method {@link #onCompletion} to perform an + * action upon normal completion, and method {@link + * #onExceptionalCompletion} to perform an action upon any exception. + * + *

CountedCompleters most often do not bear results, in which case + * they are normally declared as {@code CountedCompleter}, and + * will always return {@code null} as a result value. In other cases, + * you should override method {@link #getRawResult} to provide a + * result from {@code join(), invoke()}, and related methods. In + * general, this method should return the value of a field (or a + * function of one or more fields) of the CountedCompleter object that + * holds the result upon completion. Method {@link #setRawResult} by + * default plays no role in CountedCompleters. It is possible, but + * not usually applicable, to override this method to maintain other + * objects or fields holding result data. * *

A CountedCompleter that does not itself have a completer (i.e., * one for which {@link #getCompleter} returns {@code null}) can be @@ -44,15 +64,18 @@ package jsr166y; * {@link #complete}, {@link ForkJoinTask#cancel}, {@link * ForkJoinTask#completeExceptionally} or upon exceptional completion * of method {@code compute}. Upon any exceptional completion, the - * exception is relayed to a task's completer (and its completer, and - * so on), if one exists and it has not otherwise already completed. + * exception may be relayed to a task's completer (and its completer, + * and so on), if one exists and it has not otherwise already + * completed. Similarly, cancelling an internal CountedCompleter has + * only a local effect on that completer, so is not often useful. * *

Sample Usages. * *

Parallel recursive decomposition. CountedCompleters may * be arranged in trees similar to those often used with {@link * RecursiveAction}s, although the constructions involved in setting - * them up typically vary. Even though they entail a bit more + * them up typically vary. Here, the completer of each task is its + * parent in the computation tree. Even though they entail a bit more * bookkeeping, CountedCompleters may be better choices when applying * a possibly time-consuming operation (that cannot be further * subdivided) to each element of an array or collection; especially @@ -63,7 +86,7 @@ package jsr166y; * continuations, other threads need not block waiting to perform * them. * - *

For example, here is an initial version of a class that uses + *

For example, here is an initial version of a class that uses * divide-by-two recursive decomposition to divide work into single * pieces (leaf tasks). Even when work is split into individual calls, * tree-based techniques are usually preferable to directly forking @@ -72,19 +95,20 @@ package jsr166y; * pair of subtasks to finish triggers completion of its parent * (because no result combination is performed, the default no-op * implementation of method {@code onCompletion} is not overridden). A - * static utility method sets up the base task and invokes it: + * static utility method sets up the base task and invokes it + * (here, implicitly using the {@link ForkJoinPool#commonPool()}). * *

 {@code
  * class MyOperation { void apply(E e) { ... }  }
  *
- * class ForEach extends CountedCompleter {
+ * class ForEach extends CountedCompleter {
  *
- *     public static  void forEach(ForkJoinPool pool, E[] array, MyOperation op) {
- *         pool.invoke(new ForEach(null, array, op, 0, array.length));
+ *     public static  void forEach(E[] array, MyOperation op) {
+ *         new ForEach(null, array, op, 0, array.length).invoke();
  *     }
  *
  *     final E[] array; final MyOperation op; final int lo, hi;
- *     ForEach(CountedCompleter p, E[] array, MyOperation op, int lo, int hi) {
+ *     ForEach(CountedCompleter p, E[] array, MyOperation op, int lo, int hi) {
  *         super(p);
  *         this.array = array; this.op = op; this.lo = lo; this.hi = hi;
  *     }
@@ -128,7 +152,7 @@ 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.
  *
  * 
 {@code
  * class ForEach ...
@@ -159,17 +183,18 @@ package jsr166y;
  * and reductions are all of type {@code E}), one way to do this in
  * divide and conquer designs is to have each subtask record its
  * sibling, so that it can be accessed in method {@code onCompletion}.
- * For clarity, this class uses explicit left and right subtasks, but
- * variants of other streamlinings seen in the above example may also
- * apply.
+ * This technique applies to reductions in which the order of
+ * combining left and right results does not matter; ordered
+ * reductions require explicit left/right designations.  Variants of
+ * other streamlinings seen in the above examples may also apply.
  *
  * 
 {@code
  * class MyMapper { E apply(E v) {  ...  } }
  * class MyReducer { E apply(E x, E y) {  ...  } }
- * class MapReducer extends CountedCompleter {
+ * class MapReducer extends CountedCompleter {
  *     final E[] array; final MyMapper mapper;
  *     final MyReducer reducer; final int lo, hi;
- *     MapReducer sibling;
+ *     MapReducer sibling;
  *     E result;
  *     MapReducer(CountedCompleter p, E[] array, MyMapper mapper,
  *                MyReducer reducer, int lo, int hi) {
@@ -204,28 +229,89 @@ package jsr166y;
  *                result = reducer.apply(child.result, sib.result);
  *         }
  *     }
+ *     public E getRawResult() { return result; }
  *
- *     public static  E mapReduce(ForkJoinPool pool, E[] array,
- *                                   MyMapper mapper, MyReducer reducer) {
- *         MapReducer mr = new MapReducer(null, array, mapper,
- *                                              reducer, 0, array.length);
- *         pool.invoke(mr);
- *         return mr.result;
+ *     public static  E mapReduce(E[] array, MyMapper mapper, MyReducer reducer) {
+ *         return new MapReducer(null, array, mapper, reducer,
+ *                                  0, array.length).invoke();
  *     }
  * } }
* + * Here, method {@code onCompletion} takes a form common to many + * completion designs that combine results. This callback-style method + * is triggered once per task, in either of the two different contexts + * in which the pending count is, or becomes, zero: (1) by a task + * itself, if its pending count is zero upon invocation of {@code + * tryComplete}, or (2) by any of its subtasks when they complete and + * decrement the pending count to zero. The {@code caller} argument + * distinguishes cases. Most often, when the caller is {@code this}, + * no action is necessary. Otherwise the caller argument can be used + * (usually via a cast) to supply a value (and/or links to other + * values) to be combined. Asuuming proper use of pending counts, the + * actions inside {@code onCompletion} occur (once) upon completion of + * a task and its subtasks. No additional synchronization is required + * within this method to ensure thread safety of accesses to fields of + * this task or other completed tasks. + * + *

Searching. A tree of CountedCompleters can search for a + * value or property in different parts of a data structure, and + * report a result in an {@link java.util.concurrent.AtomicReference} + * as soon as one is found. The others can poll the result to avoid + * unnecessary work. (You could additionally {@link #cancel} other + * tasks, but it is usually simpler and more efficient to just let + * them notice that the result is set and if so skip further + * processing.) Illustrating again with an array using full + * partitioning (again, in practice, leaf tasks will almost always + * process more than one element): + * + *

 {@code
+ * class Searcher extends CountedCompleter {
+ *     final E[] array; final AtomicReference result; final int lo, hi;
+ *     Searcher(CountedCompleter p, E[] array, AtomicReference result, int lo, int hi) {
+ *         super(p);
+ *         this.array = array; this.result = result; this.lo = lo; this.hi = hi;
+ *     }
+ *     public E getRawResult() { return result.get(); }
+ *     public void compute() { // similar to ForEach version 3
+ *         int l = lo,  h = hi;
+ *         while (h - l >= 2 && result.get() == null) {
+ *             int mid = (l + h) >>> 1;
+ *             addToPendingCount(1);
+ *             new Searcher(this, array, result, mid, h).fork();
+ *             h = mid;
+ *         }
+ *         if (h > l && result.get() == null && matches(array[l]) &&
+ *             result.compareAndSet(null, array[l]))
+ *             getRoot().quietlyComplete(); // root task is now joinable
+ *
+ *         tryComplete(); // normally complete whether or not found
+ *     }
+ *     boolean matches(E e) { ... } // return true if found
+ *
+ *     public static  E search(E[] array) {
+ *         return new Searcher(null, array, new AtomicReference(), 0, array.length).invoke();
+ *     }
+ *}}
+ * + * In this example, as well as others in which tasks have no other + * effects except to compareAndSet a common result, the trailing + * unconditional invocation of {@code tryComplete} could be made + * conditional ({@code if (result.get() == null) tryComplete();}) + * because no further bookkeeping is required to manage completions + * once the root task completes. + * *

Triggers. Some CountedCompleters are themselves never * forked, but instead serve as bits of plumbing in other designs; * including those in which the completion of one of more async tasks * triggers another async task. For example: * *

 {@code
- * class HeaderBuilder extends CountedCompleter { ... }
- * class BodyBuilder extends CountedCompleter { ... }
- * class PacketSender extends CountedCompleter {
+ * class HeaderBuilder extends CountedCompleter<...> { ... }
+ * class BodyBuilder extends CountedCompleter<...> { ... }
+ * class PacketSender extends CountedCompleter<...> {
  *     PacketSender(...) { super(null, 1); ... } // trigger on second completion
  *     public void compute() { } // never called
- *     public void onCompletion(CountedCompleter caller) { sendPacket(); }
+ *     public void onCompletion(CountedCompleter caller) { sendPacket(); }
  * }
  * // sample use:
  * PacketSender p = new PacketSender();
@@ -236,9 +322,11 @@ package jsr166y;
  * @since 1.8
  * @author Doug Lea
  */
-public abstract class CountedCompleter extends ForkJoinTask {
+public abstract class CountedCompleter extends ForkJoinTask {
+    private static final long serialVersionUID = 5232453752276485070L;
+
     /** This task's completer, or null if none */
-    final CountedCompleter completer;
+    final CountedCompleter completer;
     /** The number of pending tasks until completion */
     volatile int pending;
 
@@ -249,7 +337,7 @@ public abstract class CountedCompleter e
      * @param completer this tasks completer, or {@code null} if none
      * @param initialPendingCount the initial pending count
      */
-    protected CountedCompleter(CountedCompleter completer,
+    protected CountedCompleter(CountedCompleter completer,
                                int initialPendingCount) {
         this.completer = completer;
         this.pending = initialPendingCount;
@@ -261,7 +349,7 @@ public abstract class CountedCompleter e
      *
      * @param completer this tasks completer, or {@code null} if none
      */
-    protected CountedCompleter(CountedCompleter completer) {
+    protected CountedCompleter(CountedCompleter completer) {
         this.completer = completer;
     }
 
@@ -279,15 +367,39 @@ public abstract class CountedCompleter e
     public abstract void compute();
 
     /**
-     * Executes the completion action when method {@link #tryComplete}
-     * is invoked and there are no pending counts, or when the
-     * unconditional method {@link #complete} is invoked.  By default,
-     * this method does nothing.
+     * Performs an action when method {@link #tryComplete} is invoked
+     * and there are no pending counts, or when the unconditional
+     * method {@link #complete} is invoked.  By default, this method
+     * does nothing. You can distinguish cases by checking the
+     * identity of the given caller argument. If not equal to {@code
+     * this}, then it is typically a subtask that may contain results
+     * (and/or links to other results) to combine.
      *
      * @param caller the task invoking this method (which may
      * be this task itself).
      */
-    public void onCompletion(CountedCompleter caller) {
+    public void onCompletion(CountedCompleter caller) {
+    }
+
+    /**
+     * Performs an action when method {@link #completeExceptionally}
+     * is invoked or method {@link #compute} throws an exception, and
+     * this task has not otherwise already completed normally. On
+     * entry to this method, this task {@link
+     * ForkJoinTask#isCompletedAbnormally}.  The return value of this
+     * method controls further propagation: If {@code true} and this
+     * task has a completer, then this completer is also completed
+     * exceptionally.  The default implementation of this method does
+     * nothing except return {@code true}.
+     *
+     * @param ex the exception
+     * @param caller the task invoking this method (which may
+     * be this task itself).
+     * @return true if this exception should be propagated to this
+     * tasks completer, if one exists.
+     */
+    public boolean onExceptionalCompletion(Throwable ex, CountedCompleter caller) {
+        return true;
     }
 
     /**
@@ -296,7 +408,7 @@ public abstract class CountedCompleter e
      *
      * @return the completer
      */
-    public final CountedCompleter getCompleter() {
+    public final CountedCompleter getCompleter() {
         return completer;
     }
 
@@ -341,14 +453,27 @@ public abstract class CountedCompleter e
     }
 
     /**
+     * Returns the root of the current computation; i.e., this
+     * task if it has no completer, else its completer's root.
+     *
+     * @return the root of the current computation
+     */
+    public final 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,
      * else marks this task as complete.
      */
     public final void tryComplete() {
-        for (CountedCompleter a = this, s = a;;) {
-            int c;
+        CountedCompleter a = this, s = a;
+        for (int c;;) {
             if ((c = a.pending) == 0) {
                 a.onCompletion(s);
                 if ((a = (s = a).completer) == null) {
@@ -363,16 +488,24 @@ public abstract class CountedCompleter e
 
     /**
      * Regardless of pending count, invokes {@link #onCompletion},
-     * marks this task as complete with a {@code null} return value,
-     * and further triggers {@link #tryComplete} on this task's
-     * completer, if one exists. This method may be useful when
-     * forcing completion as soon as any one (versus all) of several
-     * subtask results are obtained.
-     *
-     * @param mustBeNull the {@code null} completion value
-     */
-    public void complete(Void mustBeNull) {
-        CountedCompleter p;
+     * marks this task as complete and further triggers {@link
+     * #tryComplete} on this task's completer, if one exists.  The
+     * given rawResult is used as an argument to {@link #setRawResult}
+     * before invoking {@link #onCompletion} or marking this task as
+     * complete; its value is meaningful only for classes overriding
+     * {@code setRawResult}.
+     *
+     * 

This method may be useful when forcing completion as soon as + * any one (versus all) of several subtask results are obtained. + * However, in the common (and recommended) case in which {@code + * setRawResult} is not overridden, this effect can be obtained + * more simply using {@code getRoot().quietlyComplete();}. + * + * @param rawResult the raw result + */ + public void complete(T rawResult) { + CountedCompleter p; + setRawResult(rawResult); onCompletion(this); quietlyComplete(); if ((p = completer) != null) @@ -380,6 +513,16 @@ public abstract class CountedCompleter e } /** + * Support for FJT exception propagation + */ + void internalPropagateException(Throwable ex) { + CountedCompleter a = this, s = a; + while (a.onExceptionalCompletion(ex, s) && + (a = (s = a).completer) != null && a.status >= 0) + a.recordExceptionalCompletion(ex); + } + + /** * Implements execution conventions for CountedCompleters */ protected final boolean exec() { @@ -388,28 +531,28 @@ public abstract class CountedCompleter e } /** - * Always returns {@code null}. + * Returns the result of the computation. By default + * returns {@code null}, which is appropriate for {@code Void} + * actions, but in other cases should be overridden. * - * @return {@code null} always - */ - public final Void getRawResult() { return null; } - - /** - * Requires null completion value. + * @return the result of the computation */ - protected final void setRawResult(Void mustBeNull) { } + public T getRawResult() { return null; } /** - * Support for FJT exception propagation + * A method that result-bearing CountedCompleters may optionally + * use to help maintain result data. By default, does nothing. + * If this method is overridden to update existing objects or + * fields, then it must in general be defined to be thread-safe. */ - final ForkJoinTask internalGetCompleter() { return completer; } + protected void setRawResult(T t) { } // Unsafe mechanics private static final sun.misc.Unsafe U; private static final long PENDING; static { try { - U = getUnsafe(); + U = sun.misc.Unsafe.getUnsafe(); PENDING = U.objectFieldOffset (CountedCompleter.class.getDeclaredField("pending")); } catch (Exception e) { @@ -417,7 +560,6 @@ public abstract class CountedCompleter e } } - /** * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. * Replace with a simple call to Unsafe.getUnsafe when integrating @@ -445,5 +587,4 @@ public abstract class CountedCompleter e } } } - }