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.13 by dl, Wed Jul 22 19:04:11 2009 UTC vs.
Revision 1.14 by jsr166, Thu Jul 23 23:07:57 2009 UTC

# Line 83 | Line 83 | import java.lang.reflect.*;
83   * methods), some of them may only be called from within other
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
86 > * result in exceptions or errors, possibly including
87   * ClassCastException.
88   *
89   * <p>Most base support methods are {@code final} because their
# Line 162 | 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 189 | 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 207 | 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 225 | 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 264 | 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 281 | 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 309 | 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 364 | 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 430 | 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 462 | 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 474 | 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 486 | Line 488 | public abstract class ForkJoinTask<V> im
488       * method may be invoked only from within ForkJoinTask
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.
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 527 | Line 530 | public abstract class ForkJoinTask<V> im
530       * both of them or an exception is encountered. This method may be
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
533 >     * invoke in other contexts result in exceptions or errors,
534       * possibly including ClassCastException.
535       *
536       * @param t1 one task
# Line 547 | Line 550 | public abstract class ForkJoinTask<V> im
550       * may be cancelled.  This method may be invoked only from within
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
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
558       * @throws RuntimeException or Error if any task did so
# Line 592 | Line 596 | public abstract class ForkJoinTask<V> im
596       * encounters an exception, others may be cancelled.  This method
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 resul!t
600 <     * in exceptions or errors possibly including ClassCastException.
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 601 | 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 680 | 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 730 | 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 749 | 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 781 | Line 785 | public abstract class ForkJoinTask<V> im
785       * tasks). This method may be invoked only from within
786       * ForkJoinTask computations (as may be determined using method
787       * {@link #inForkJoinPool}). Attempts to invoke in other contexts
788 <     * resul!t in exceptions or errors possibly including
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 797 | Line 801 | public abstract class ForkJoinTask<V> im
801       * Possibly executes other tasks until this task is ready.  This
802       * method may be invoked only from within ForkJoinTask
803       * computations (as may be determined using method {@link
804 <     * #inForkJoinPool}). Attempts to invoke in other contexts resul!t
805 <     * in exceptions or errors possibly including ClassCastException.
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 842 | 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 866 | Line 870 | public abstract class ForkJoinTask<V> im
870       * Returns the pool hosting the current task execution, or null
871       * if this task is executing outside of any ForkJoinPool.
872       *
873 <     * @return the pool, or null if none.
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 true if the current thread is executing as a
882 >     * Returns {@code true} if the current thread is executing as a
883       * ForkJoinPool computation.
884 <     * @return <code>true</code> if the current thread is executing as a
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() {
# Line 893 | Line 898 | public abstract class ForkJoinTask<V> im
898       * were not, stolen. This method may be invoked only from within
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
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 911 | 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 928 | 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 977 | Line 983 | public abstract class ForkJoinTask<V> im
983       * This method may be invoked only from within ForkJoinTask
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.
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 992 | Line 999 | public abstract class ForkJoinTask<V> im
999       * be useful otherwise.  This method may be invoked only from
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
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 1012 | Line 1020 | public abstract class ForkJoinTask<V> im
1020       * otherwise.  This method may be invoked only from within
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
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 1051 | 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