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.12 by jsr166, Wed Jul 22 01:36:51 2009 UTC vs.
Revision 1.14 by jsr166, Thu Jul 23 23:07:57 2009 UTC

# Line 81 | Line 81 | import java.lang.reflect.*;
81   * class. While these methods have {@code public} access (to allow
82   * instances of different task subclasses to call each others
83   * methods), some of them may only be called from within other
84 < * ForkJoinTasks. Attempts to invoke them in other contexts result in
85 < * exceptions or errors possibly including ClassCastException.
84 > * ForkJoinTasks (as may be determined using method {@link
85 > * #inForkJoinPool}).  Attempts to invoke them in other contexts
86 > * result in exceptions or errors, possibly including
87 > * ClassCastException.
88   *
89   * <p>Most base support methods are {@code final} because their
90   * implementations are intrinsically tied to the underlying
# Line 160 | Line 162 | public abstract class ForkJoinTask<V> im
162       */
163      static ForkJoinWorkerThread getWorker() {
164          Thread t = Thread.currentThread();
165 <        return ((t instanceof ForkJoinWorkerThread)?
166 <                (ForkJoinWorkerThread)t : null);
165 >        return ((t instanceof ForkJoinWorkerThread) ?
166 >                (ForkJoinWorkerThread) t : null);
167      }
168  
169      final boolean casStatus(int cmp, int val) {
# Line 187 | Line 189 | public abstract class ForkJoinTask<V> im
189          ForkJoinPool pool = getPool();
190          if (pool != null) {
191              int s; // Clear signal bits while setting completion status
192 <            do;while ((s = status) >= 0 && !casStatus(s, completion));
192 >            do {} while ((s = status) >= 0 && !casStatus(s, completion));
193  
194              if ((s & SIGNAL_MASK) != 0) {
195                  if ((s &= INTERNAL_SIGNAL_MASK) != 0)
196                      pool.updateRunningCount(s);
197 <                synchronized(this) { notifyAll(); }
197 >                synchronized (this) { notifyAll(); }
198              }
199          }
200          else
# Line 205 | Line 207 | public abstract class ForkJoinTask<V> im
207       */
208      private void externallySetCompletion(int completion) {
209          int s;
210 <        do;while ((s = status) >= 0 &&
211 <                  !casStatus(s, (s & SIGNAL_MASK) | completion));
212 <        synchronized(this) { notifyAll(); }
210 >        do {} while ((s = status) >= 0 &&
211 >                     !casStatus(s, (s & SIGNAL_MASK) | completion));
212 >        synchronized (this) { notifyAll(); }
213      }
214  
215      /**
216 <     * Sets status to indicate normal completion
216 >     * Sets status to indicate normal completion.
217       */
218      final void setNormalCompletion() {
219          // Try typical fast case -- single CAS, no signal, not already done.
# Line 223 | Line 225 | public abstract class ForkJoinTask<V> im
225      // internal waiting and notification
226  
227      /**
228 <     * Performs the actual monitor wait for awaitDone
228 >     * Performs the actual monitor wait for awaitDone.
229       */
230      private void doAwaitDone() {
231          // Minimize lock bias and in/de-flation effects by maximizing
232          // chances of waiting inside sync
233          try {
234              while (status >= 0)
235 <                synchronized(this) { if (status >= 0) wait(); }
235 >                synchronized (this) { if (status >= 0) wait(); }
236          } catch (InterruptedException ie) {
237              onInterruptedWait();
238          }
239      }
240  
241      /**
242 <     * Performs the actual monitor wait for awaitDone
242 >     * Performs the actual timed monitor wait for awaitDone.
243       */
244      private void doAwaitDone(long startTime, long nanos) {
245 <        synchronized(this) {
245 >        synchronized (this) {
246              try {
247                  while (status >= 0) {
248                      long nt = nanos - System.nanoTime() - startTime;
249                      if (nt <= 0)
250                          break;
251 <                    wait(nt / 1000000, (int)(nt % 1000000));
251 >                    wait(nt / 1000000, (int) (nt % 1000000));
252                  }
253              } catch (InterruptedException ie) {
254                  onInterruptedWait();
# Line 262 | Line 264 | public abstract class ForkJoinTask<V> im
264       *
265       * @return status upon exit
266       */
267 <    private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) {
268 <        ForkJoinPool pool = w == null? null : w.pool;
267 >    private int awaitDone(ForkJoinWorkerThread w,
268 >                          boolean maintainParallelism) {
269 >        ForkJoinPool pool = (w == null) ? null : w.pool;
270          int s;
271          while ((s = status) >= 0) {
272 <            if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) {
272 >            if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) {
273                  if (pool == null || !pool.preJoin(this, maintainParallelism))
274                      doAwaitDone();
275                  if (((s = status) & INTERNAL_SIGNAL_MASK) != 0)
# Line 279 | Line 282 | public abstract class ForkJoinTask<V> im
282  
283      /**
284       * Timed version of awaitDone
285 +     *
286       * @return status upon exit
287       */
288      private int awaitDone(ForkJoinWorkerThread w, long nanos) {
289 <        ForkJoinPool pool = w == null? null : w.pool;
289 >        ForkJoinPool pool = (w == null) ? null : w.pool;
290          int s;
291          while ((s = status) >= 0) {
292 <            if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) {
292 >            if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) {
293                  long startTime = System.nanoTime();
294                  if (pool == null || !pool.preJoin(this, false))
295                      doAwaitDone(startTime, nanos);
# Line 307 | Line 311 | public abstract class ForkJoinTask<V> im
311       */
312      private void adjustPoolCountsOnUnblock(ForkJoinPool pool) {
313          int s;
314 <        do;while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK));
314 >        do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK));
315          if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0)
316              pool.updateRunningCount(s);
317      }
# Line 362 | Line 366 | public abstract class ForkJoinTask<V> im
366  
367      /**
368       * Returns result or throws exception using j.u.c.Future conventions.
369 <     * Only call when isDone known to be true.
369 >     * Only call when {@code isDone} known to be true.
370       */
371      private V reportFutureResult()
372          throws ExecutionException, InterruptedException {
# Line 428 | Line 432 | public abstract class ForkJoinTask<V> im
432              try {
433                  if (!exec())
434                      return;
435 <            } catch(Throwable rex) {
435 >            } catch (Throwable rex) {
436                  setDoneExceptionally(rex);
437                  return;
438              }
# Line 460 | Line 464 | public abstract class ForkJoinTask<V> im
464      final void cancelIgnoringExceptions() {
465          try {
466              cancel(false);
467 <        } catch(Throwable ignore) {
467 >        } catch (Throwable ignore) {
468          }
469      }
470  
# Line 472 | Line 476 | public abstract class ForkJoinTask<V> im
476          ForkJoinTask<?> t;
477          while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null)
478              t.quietlyExec();
479 <        return (s >= 0)? awaitDone(w, false) : s; // block if no work
479 >        return (s >= 0) ? awaitDone(w, false) : s; // block if no work
480      }
481  
482      // public methods
# Line 482 | Line 486 | public abstract class ForkJoinTask<V> im
486       * necessarily enforced, it is a usage error to fork a task more
487       * than once unless it has completed and been reinitialized.  This
488       * method may be invoked only from within ForkJoinTask
489 <     * computations. Attempts to invoke in other contexts result in
490 <     * exceptions or errors possibly including ClassCastException.
489 >     * computations (as may be determined using method {@link
490 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
491 >     * in exceptions or errors, possibly including ClassCastException.
492       */
493      public final void fork() {
494 <        ((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this);
494 >        ((ForkJoinWorkerThread) Thread.currentThread())
495 >            .pushTask(this);
496      }
497  
498      /**
# Line 522 | Line 528 | public abstract class ForkJoinTask<V> im
528      /**
529       * Forks both tasks, returning when {@code isDone} holds for
530       * both of them or an exception is encountered. This method may be
531 <     * invoked only from within ForkJoinTask computations. Attempts to
532 <     * invoke in other contexts result in exceptions or errors
531 >     * invoked only from within ForkJoinTask computations (as may be
532 >     * determined using method {@link #inForkJoinPool}). Attempts to
533 >     * invoke in other contexts result in exceptions or errors,
534       * possibly including ClassCastException.
535       *
536       * @param t1 one task
# Line 541 | Line 548 | public abstract class ForkJoinTask<V> im
548       * Forks the given tasks, returning when {@code isDone} holds
549       * for all of them. If any task encounters an exception, others
550       * may be cancelled.  This method may be invoked only from within
551 <     * ForkJoinTask computations. Attempts to invoke in other contexts
552 <     * result in exceptions or errors possibly including ClassCastException.
551 >     * ForkJoinTask computations (as may be determined using method
552 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
553 >     * result in exceptions or errors, possibly including
554 >     * ClassCastException.
555       *
556       * @param tasks the array of tasks
557       * @throws NullPointerException if tasks or any element are null
# Line 585 | Line 594 | public abstract class ForkJoinTask<V> im
594       * Forks all tasks in the collection, returning when
595       * {@code isDone} holds for all of them. If any task
596       * encounters an exception, others may be cancelled.  This method
597 <     * may be invoked only from within ForkJoinTask
598 <     * computations. Attempts to invoke in other contexts result in
599 <     * exceptions or errors possibly including ClassCastException.
597 >     * may be invoked only from within ForkJoinTask computations (as
598 >     * may be determined using method {@link
599 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
600 >     * in exceptions or errors, possibly including ClassCastException.
601       *
602       * @param tasks the collection of tasks
603       * @throws NullPointerException if tasks or any element are null
# Line 595 | Line 605 | public abstract class ForkJoinTask<V> im
605       */
606      public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) {
607          if (!(tasks instanceof List)) {
608 <            invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()]));
608 >            invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
609              return;
610          }
611          List<? extends ForkJoinTask<?>> ts =
612 <            (List<? extends ForkJoinTask<?>>)tasks;
612 >            (List<? extends ForkJoinTask<?>>) tasks;
613          Throwable ex = null;
614          int last = ts.size() - 1;
615          for (int i = last; i >= 0; --i) {
# Line 674 | Line 684 | public abstract class ForkJoinTask<V> im
684       *
685       * @param mayInterruptIfRunning this value is ignored in the
686       * default implementation because tasks are not in general
687 <     * cancelled via interruption.
687 >     * cancelled via interruption
688       *
689       * @return true if this task is now cancelled
690       */
# Line 724 | Line 734 | public abstract class ForkJoinTask<V> im
734       */
735      public void completeExceptionally(Throwable ex) {
736          setDoneExceptionally((ex instanceof RuntimeException) ||
737 <                             (ex instanceof Error)? ex :
737 >                             (ex instanceof Error) ? ex :
738                               new RuntimeException(ex));
739      }
740  
# Line 743 | Line 753 | public abstract class ForkJoinTask<V> im
753      public void complete(V value) {
754          try {
755              setRawResult(value);
756 <        } catch(Throwable rex) {
756 >        } catch (Throwable rex) {
757              setDoneExceptionally(rex);
758              return;
759          }
# Line 773 | Line 783 | public abstract class ForkJoinTask<V> im
783       * current task and that of any other task that might be executed
784       * while helping. (This usually holds for pure divide-and-conquer
785       * tasks). This method may be invoked only from within
786 <     * ForkJoinTask computations. Attempts to invoke in other contexts
787 <     * result in exceptions or errors possibly including ClassCastException.
786 >     * ForkJoinTask computations (as may be determined using method
787 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
788 >     * result in exceptions or errors, possibly including
789 >     * ClassCastException.
790       *
791       * @return the computed result
792       */
793      public final V helpJoin() {
794 <        ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread());
794 >        ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread();
795          if (status < 0 || !w.unpushTask(this) || !tryExec())
796              reportException(busyJoin(w));
797          return getRawResult();
# Line 788 | Line 800 | public abstract class ForkJoinTask<V> im
800      /**
801       * Possibly executes other tasks until this task is ready.  This
802       * method may be invoked only from within ForkJoinTask
803 <     * computations. Attempts to invoke in other contexts result in
804 <     * exceptions or errors possibly including ClassCastException.
803 >     * computations (as may be determined using method {@link
804 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
805 >     * in exceptions or errors, possibly including ClassCastException.
806       */
807      public final void quietlyHelpJoin() {
808          if (status >= 0) {
809              ForkJoinWorkerThread w =
810 <                (ForkJoinWorkerThread)(Thread.currentThread());
810 >                (ForkJoinWorkerThread) Thread.currentThread();
811              if (!w.unpushTask(this) || !tryQuietlyInvoke())
812                  busyJoin(w);
813          }
# Line 833 | Line 846 | public abstract class ForkJoinTask<V> im
846       * joined, instead executing them until all are processed.
847       */
848      public static void helpQuiesce() {
849 <        ((ForkJoinWorkerThread)(Thread.currentThread())).
850 <            helpQuiescePool();
849 >        ((ForkJoinWorkerThread) Thread.currentThread())
850 >            .helpQuiescePool();
851      }
852  
853      /**
# Line 855 | Line 868 | public abstract class ForkJoinTask<V> im
868  
869      /**
870       * Returns the pool hosting the current task execution, or null
871 <     * if this task is executing outside of any pool.
871 >     * if this task is executing outside of any ForkJoinPool.
872       *
873       * @return the pool, or null if none
874       */
875      public static ForkJoinPool getPool() {
876          Thread t = Thread.currentThread();
877 <        return ((t instanceof ForkJoinWorkerThread)?
878 <                ((ForkJoinWorkerThread)t).pool : null);
877 >        return ((t instanceof ForkJoinWorkerThread) ?
878 >                ((ForkJoinWorkerThread) t).pool : null);
879 >    }
880 >
881 >    /**
882 >     * Returns {@code true} if the current thread is executing as a
883 >     * ForkJoinPool computation.
884 >     *
885 >     * @return {@code true} if the current thread is executing as a
886 >     * ForkJoinPool computation, or false otherwise
887 >     */
888 >    public static boolean inForkJoinPool() {
889 >        return Thread.currentThread() instanceof ForkJoinWorkerThread;
890      }
891  
892      /**
# Line 872 | Line 896 | public abstract class ForkJoinTask<V> im
896       * another thread.  This method may be useful when arranging
897       * alternative local processing of tasks that could have been, but
898       * were not, stolen. This method may be invoked only from within
899 <     * ForkJoinTask computations. Attempts to invoke in other contexts
900 <     * result in exceptions or errors possibly including ClassCastException.
899 >     * ForkJoinTask computations (as may be determined using method
900 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
901 >     * result in exceptions or errors, possibly including
902 >     * ClassCastException.
903       *
904       * @return true if unforked
905       */
906      public boolean tryUnfork() {
907 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this);
907 >        return ((ForkJoinWorkerThread) Thread.currentThread())
908 >            .unpushTask(this);
909      }
910  
911      /**
# Line 890 | Line 917 | public abstract class ForkJoinTask<V> im
917       * @return the number of tasks
918       */
919      public static int getQueuedTaskCount() {
920 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
921 <            getQueueSize();
920 >        return ((ForkJoinWorkerThread) Thread.currentThread())
921 >            .getQueueSize();
922      }
923  
924      /**
# Line 907 | Line 934 | public abstract class ForkJoinTask<V> im
934       * @return the surplus number of tasks, which may be negative
935       */
936      public static int getSurplusQueuedTaskCount() {
937 <        return ((ForkJoinWorkerThread)(Thread.currentThread()))
937 >        return ((ForkJoinWorkerThread) Thread.currentThread())
938              .getEstimatedSurplusTaskCount();
939      }
940  
# Line 954 | Line 981 | public abstract class ForkJoinTask<V> im
981       * be polled or executed next.  This method is designed primarily
982       * to support extensions, and is unlikely to be useful otherwise.
983       * This method may be invoked only from within ForkJoinTask
984 <     * computations. Attempts to invoke in other contexts result in
985 <     * exceptions or errors possibly including ClassCastException.
984 >     * computations (as may be determined using method {@link
985 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
986 >     * in exceptions or errors, possibly including ClassCastException.
987       *
988       * @return the next task, or null if none are available
989       */
990      protected static ForkJoinTask<?> peekNextLocalTask() {
991 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask();
991 >        return ((ForkJoinWorkerThread) Thread.currentThread())
992 >            .peekTask();
993      }
994  
995      /**
# Line 968 | Line 997 | public abstract class ForkJoinTask<V> im
997       * queued by the current thread but not yet executed.  This method
998       * is designed primarily to support extensions, and is unlikely to
999       * be useful otherwise.  This method may be invoked only from
1000 <     * within ForkJoinTask computations. Attempts to invoke in other
1001 <     * contexts result in exceptions or errors possibly including
1000 >     * within ForkJoinTask computations (as may be determined using
1001 >     * method {@link #inForkJoinPool}). Attempts to invoke in other
1002 >     * contexts result in exceptions or errors, possibly including
1003       * ClassCastException.
1004       *
1005       * @return the next task, or null if none are available
1006       */
1007      protected static ForkJoinTask<?> pollNextLocalTask() {
1008 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask();
1008 >        return ((ForkJoinWorkerThread) Thread.currentThread())
1009 >            .pollLocalTask();
1010      }
1011  
1012      /**
# Line 987 | Line 1018 | public abstract class ForkJoinTask<V> im
1018       * of the pool this task is operating in.  This method is designed
1019       * primarily to support extensions, and is unlikely to be useful
1020       * otherwise.  This method may be invoked only from within
1021 <     * ForkJoinTask computations. Attempts to invoke in other contexts
1022 <     * result in exceptions or errors possibly including
1021 >     * ForkJoinTask computations (as may be determined using method
1022 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
1023 >     * result in exceptions or errors, possibly including
1024       * ClassCastException.
1025       *
1026       * @return a task, or null if none are available
1027       */
1028      protected static ForkJoinTask<?> pollTask() {
1029 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
1030 <            pollTask();
1029 >        return ((ForkJoinWorkerThread) Thread.currentThread())
1030 >            .pollTask();
1031      }
1032  
1033      // Serialization support
# Line 1027 | Line 1059 | public abstract class ForkJoinTask<V> im
1059          status |= EXTERNAL_SIGNAL; // conservatively set external signal
1060          Object ex = s.readObject();
1061          if (ex != null)
1062 <            setDoneExceptionally((Throwable)ex);
1062 >            setDoneExceptionally((Throwable) ex);
1063      }
1064  
1065      // Temporary Unsafe mechanics for preliminary release

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines