ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinTask.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinTask.java (file contents):
Revision 1.88 by dl, Sun Mar 4 19:47:08 2012 UTC vs.
Revision 1.90 by dl, Sat Apr 21 11:45:20 2012 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 +
9   import java.io.Serializable;
10   import java.util.Collection;
11   import java.util.List;
# Line 70 | Line 71 | import java.lang.reflect.Constructor;
71   * but doing do requires three further considerations: (1) Completion
72   * of few if any <em>other</em> tasks should be dependent on a task
73   * that blocks on external synchronization or IO. Event-style async
74 < * tasks that are never joined often fall into this category.  (2) To
75 < * minimize resource impact, tasks should be small; ideally performing
76 < * only the (possibly) blocking action. (3) Unless the {@link
74 > * tasks that are never joined (for example, those subclassing {@link
75 > * CountedCompleter}) often fall into this category.  (2) To minimize
76 > * resource impact, tasks should be small; ideally performing only the
77 > * (possibly) blocking action. (3) Unless the {@link
78   * ForkJoinPool.ManagedBlocker} API is used, or the number of possibly
79   * blocked tasks is known to be less than the pool's {@link
80   * ForkJoinPool#getParallelism} level, the pool cannot guarantee that
# Line 115 | Line 117 | import java.lang.reflect.Constructor;
117   * <p>The ForkJoinTask class is not usually directly subclassed.
118   * Instead, you subclass one of the abstract classes that support a
119   * particular style of fork/join processing, typically {@link
120 < * RecursiveAction} for computations that do not return results, or
121 < * {@link RecursiveTask} for those that do.  Normally, a concrete
122 < * ForkJoinTask subclass declares fields comprising its parameters,
123 < * established in a constructor, and then defines a {@code compute}
124 < * method that somehow uses the control methods supplied by this base
125 < * class. While these methods have {@code public} access (to allow
126 < * instances of different task subclasses to call each other's
127 < * methods), some of them may only be called from within other
128 < * ForkJoinTasks (as may be determined using method {@link
129 < * #inForkJoinPool}).  Attempts to invoke them in other contexts
130 < * result in exceptions or errors, possibly including
131 < * {@code ClassCastException}.
120 > * RecursiveAction} for most computations that do not return results,
121 > * {@link RecursiveTask} for those that do, and {@link
122 > * CountedCompleter} for those in which completed actions trigger
123 > * other actions.  Normally, a concrete ForkJoinTask subclass declares
124 > * fields comprising its parameters, established in a constructor, and
125 > * then defines a {@code compute} method that somehow uses the control
126 > * methods supplied by this base class. While these methods have
127 > * {@code public} access (to allow instances of different task
128 > * subclasses to call each other's methods), some of them may only be
129 > * called from within other ForkJoinTasks (as may be determined using
130 > * method {@link #inForkJoinPool}).  Attempts to invoke them in other
131 > * contexts result in exceptions or errors, possibly including {@code
132 > * ClassCastException}.
133   *
134   * <p>Method {@link #join} and its variants are appropriate for use
135   * only when completion dependencies are acyclic; that is, the
# Line 137 | Line 140 | import java.lang.reflect.Constructor;
140   * {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
141   * may be of use in constructing custom subclasses for problems that
142   * are not statically structured as DAGs. To support such usages a
143 < * ForkJoinTask may be atomically <em>tagged</em> with a {@code
144 < * short} value using {@link #setForkJoinTaskTag} or {@link
143 > * ForkJoinTask may be atomically <em>tagged</em> with a {@code short}
144 > * value using {@link #setForkJoinTaskTag} or {@link
145   * #compareAndSetForkJoinTaskTag} and checked using {@link
146 < * #getForkJoinTaskTag}. The ForkJoinTask implementation does not
147 < * use these {@code protected} methods or tags for any purpose, but
148 < * they may be of use in the construction of specialized subclasses.
149 < * For example, parallel graph traversals can use the supplied methods
150 < * to avoid revisiting nodes/tasks that have already been processed.
151 < * Also, completion based designs can use them to record that subtasks
152 < * have completed. (Method names for tagging are bulky in part to
150 < * encourage definition of methods that reflect their usage patterns.)
146 > * #getForkJoinTaskTag}. The ForkJoinTask implementation does not use
147 > * these {@code protected} methods or tags for any purpose, but they
148 > * may be of use in the construction of specialized subclasses.  For
149 > * example, parallel graph traversals can use the supplied methods to
150 > * avoid revisiting nodes/tasks that have already been processed.
151 > * (Method names for tagging are bulky in part to encourage definition
152 > * of methods that reflect their usage patterns.)
153   *
154   * <p>Most base support methods are {@code final}, to prevent
155   * overriding of implementations that are intrinsically tied to the
# Line 269 | Line 271 | public abstract class ForkJoinTask<V> im
271      }
272  
273      /**
274 <     * Tries to set SIGNAL status. Used by ForkJoinPool. Other
275 <     * variants are directly incorporated into externalAwaitDone etc.
274 >     * Tries to set SIGNAL status unless already completed. Used by
275 >     * ForkJoinPool. Other variants are directly incorporated into
276 >     * externalAwaitDone etc.
277       *
278       * @return true if successful
279       */
280      final boolean trySetSignal() {
281 <        int s;
282 <        return U.compareAndSwapInt(this, STATUS, s = status, s | SIGNAL);
281 >        int s = status;
282 >        return s >= 0 && U.compareAndSwapInt(this, STATUS, s, s | SIGNAL);
283      }
284  
285      /**
# Line 409 | Line 412 | public abstract class ForkJoinTask<V> im
412      }
413  
414      /**
415 <     * Records exception and sets exceptional completion.
415 >     * Records exception and sets status.
416       *
417       * @return status on exit
418       */
419 <    private int setExceptionalCompletion(Throwable ex) {
420 <        int h = System.identityHashCode(this);
421 <        final ReentrantLock lock = exceptionTableLock;
422 <        lock.lock();
423 <        try {
424 <            expungeStaleExceptions();
425 <            ExceptionNode[] t = exceptionTable;
426 <            int i = h & (t.length - 1);
427 <            for (ExceptionNode e = t[i]; ; e = e.next) {
428 <                if (e == null) {
429 <                    t[i] = new ExceptionNode(this, ex, t[i]);
430 <                    break;
419 >    final int recordExceptionalCompletion(Throwable ex) {
420 >        int s;
421 >        if ((s = status) >= 0) {
422 >            int h = System.identityHashCode(this);
423 >            final ReentrantLock lock = exceptionTableLock;
424 >            lock.lock();
425 >            try {
426 >                expungeStaleExceptions();
427 >                ExceptionNode[] t = exceptionTable;
428 >                int i = h & (t.length - 1);
429 >                for (ExceptionNode e = t[i]; ; e = e.next) {
430 >                    if (e == null) {
431 >                        t[i] = new ExceptionNode(this, ex, t[i]);
432 >                        break;
433 >                    }
434 >                    if (e.get() == this) // already present
435 >                        break;
436                  }
437 <                if (e.get() == this) // already present
438 <                    break;
437 >            } finally {
438 >                lock.unlock();
439              }
440 <        } finally {
433 <            lock.unlock();
440 >            s = setCompletion(EXCEPTIONAL);
441          }
442 <        return setCompletion(EXCEPTIONAL);
442 >        return s;
443 >    }
444 >
445 >    /**
446 >     * Records exception and possibly propagates
447 >     *
448 >     * @return status on exit
449 >     */
450 >    private int setExceptionalCompletion(Throwable ex) {
451 >        int s = recordExceptionalCompletion(ex);
452 >        if ((s & DONE_MASK) == EXCEPTIONAL)
453 >            internalPropagateException(ex);
454 >        return s;
455 >    }
456 >
457 >    /**
458 >     * Hook for exception propagation support for tasks with completers.
459 >     */
460 >    void internalPropagateException(Throwable ex) {
461      }
462  
463      /**
# Line 514 | Line 539 | public abstract class ForkJoinTask<V> im
539          Throwable ex;
540          if (e == null || (ex = e.ex) == null)
541              return null;
542 <        if (e.thrower != Thread.currentThread().getId()) {
542 >        if (false && e.thrower != Thread.currentThread().getId()) {
543              Class<? extends Throwable> ec = ex.getClass();
544              try {
545                  Constructor<?> noArgCtor = null;
# Line 904 | Line 929 | public abstract class ForkJoinTask<V> im
929      }
930  
931      /**
932 <     * Completes this task. The most recent value established by
933 <     * {@link #setRawResult} (or {@code null}) will be returned as the
934 <     * result of subsequent invocations of {@code join} and related
935 <     * operations. This method may be useful when processing sets of
936 <     * tasks when some do not otherwise complete normally. Its use in
937 <     * other situations is discouraged.
932 >     * Completes this task normally without setting a value. The most
933 >     * recent value established by {@link #setRawResult} (or {@code
934 >     * null} by default) will be returned as the result of subsequent
935 >     * invocations of {@code join} and related operations.
936 >     *
937 >     * @since 1.8
938       */
939      public final void quietlyComplete() {
940          setCompletion(NORMAL);
# Line 1234 | Line 1259 | public abstract class ForkJoinTask<V> im
1259      protected abstract void setRawResult(V value);
1260  
1261      /**
1262 <     * Immediately performs the base action of this task.  This method
1263 <     * is designed to support extensions, and should not in general be
1264 <     * called otherwise. The return value controls whether this task
1265 <     * is considered to be done normally. It may return false in
1262 >     * Immediately performs the base action of this task and returns
1263 >     * true if, upon return from this method, this task is guaranteed
1264 >     * to have completed normally. This method may return false
1265 >     * otherwise, to indicate that this task is not necessarily
1266 >     * complete (or is not known to be complete), for example in
1267       * asynchronous actions that require explicit invocations of
1268 <     * {@link #complete} to become joinable. It may also throw an
1269 <     * (unchecked) exception to indicate abnormal exit.
1268 >     * completion methods. This method may also throw an (unchecked)
1269 >     * exception to indicate abnormal exit. This method is designed to
1270 >     * support extensions, and should not in general be called
1271 >     * otherwise.
1272       *
1273 <     * @return {@code true} if completed normally
1273 >     * @return {@code true} if this task is known to have completed normally
1274       */
1275      protected abstract boolean exec();
1276  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines