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.58 by dl, Mon Sep 6 11:55:39 2010 UTC vs.
Revision 1.64 by jsr166, Mon Sep 20 20:42:37 2010 UTC

# Line 7 | Line 7
7   package jsr166y;
8  
9   import java.util.concurrent.*;
10
10   import java.io.Serializable;
11   import java.util.Collection;
12   import java.util.Collections;
# Line 28 | Line 27 | import java.util.WeakHashMap;
27   * start other subtasks.  As indicated by the name of this class,
28   * many programs using {@code ForkJoinTask} employ only methods
29   * {@link #fork} and {@link #join}, or derivatives such as {@link
30 < * #invokeAll}.  However, this class also provides a number of other
31 < * methods that can come into play in advanced usages, as well as
32 < * extension mechanics that allow support of new forms of fork/join
33 < * processing.
30 > * #invokeAll(ForkJoinTask...) invokeAll}.  However, this class also
31 > * provides a number of other methods that can come into play in
32 > * advanced usages, as well as extension mechanics that allow
33 > * support of new forms of fork/join processing.
34   *
35   * <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}.
36   * The efficiency of {@code ForkJoinTask}s stems from a set of
# Line 153 | Line 152 | public abstract class ForkJoinTask<V> im
152       * single int to minimize footprint and to ensure atomicity (via
153       * CAS).  Status is initially zero, and takes on nonnegative
154       * values until completed, upon which status holds value
155 <     * NORMAL. CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking
155 >     * NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking
156       * waits by other threads have the SIGNAL bit set.  Completion of
157       * a stolen task with SIGNAL set awakens any waiters via
158       * notifyAll. Even though suboptimal for some purposes, we use
# Line 206 | Line 205 | public abstract class ForkJoinTask<V> im
205  
206      /**
207       * Records exception and sets exceptional completion.
208 <     *
208 >     *
209       * @return status on exit
210       */
211      private void setExceptionalCompletion(Throwable rex) {
# Line 223 | Line 222 | public abstract class ForkJoinTask<V> im
222          int s;         // the odd construction reduces lock bias effects
223          while ((s = status) >= 0) {
224              try {
225 <                synchronized(this) {
225 >                synchronized (this) {
226                      if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL))
227                          wait();
228                  }
# Line 243 | Line 242 | public abstract class ForkJoinTask<V> im
242          int s;
243          if ((s = status) >= 0) {
244              try {
245 <                synchronized(this) {
245 >                synchronized (this) {
246                      if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL))
247                          wait(millis, 0);
248                  }
# Line 261 | Line 260 | public abstract class ForkJoinTask<V> im
260      private void externalAwaitDone() {
261          int s;
262          while ((s = status) >= 0) {
263 <            synchronized(this) {
263 >            synchronized (this) {
264                  if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){
265                      boolean interrupted = false;
266                      while (status >= 0) {
# Line 642 | Line 641 | public abstract class ForkJoinTask<V> im
641          setCompletion(NORMAL);
642      }
643  
644 +    /**
645 +     * Waits if necessary for the computation to complete, and then
646 +     * retrieves its result.
647 +     *
648 +     * @return the computed result
649 +     * @throws CancellationException if the computation was cancelled
650 +     * @throws ExecutionException if the computation threw an
651 +     * exception
652 +     * @throws InterruptedException if the current thread is not a
653 +     * member of a ForkJoinPool and was interrupted while waiting
654 +     */
655      public final V get() throws InterruptedException, ExecutionException {
656 <        quietlyJoin();
657 <        if (Thread.interrupted())
658 <            throw new InterruptedException();
659 <        int s = status;
656 >        int s;
657 >        if (Thread.currentThread() instanceof ForkJoinWorkerThread) {
658 >            quietlyJoin();
659 >            s = status;
660 >        }
661 >        else {
662 >            while ((s = status) >= 0) {
663 >                synchronized (this) { // interruptible form of awaitDone
664 >                    if (UNSAFE.compareAndSwapInt(this, statusOffset,
665 >                                                 s, SIGNAL)) {
666 >                        while (status >= 0)
667 >                            wait();
668 >                    }
669 >                }
670 >            }
671 >        }
672          if (s < NORMAL) {
673              Throwable ex;
674              if (s == CANCELLED)
# Line 657 | Line 679 | public abstract class ForkJoinTask<V> im
679          return getRawResult();
680      }
681  
682 +    /**
683 +     * Waits if necessary for at most the given time for the computation
684 +     * to complete, and then retrieves its result, if available.
685 +     *
686 +     * @param timeout the maximum time to wait
687 +     * @param unit the time unit of the timeout argument
688 +     * @return the computed result
689 +     * @throws CancellationException if the computation was cancelled
690 +     * @throws ExecutionException if the computation threw an
691 +     * exception
692 +     * @throws InterruptedException if the current thread is not a
693 +     * member of a ForkJoinPool and was interrupted while waiting
694 +     * @throws TimeoutException if the wait timed out
695 +     */
696      public final V get(long timeout, TimeUnit unit)
697          throws InterruptedException, ExecutionException, TimeoutException {
698          Thread t = Thread.currentThread();
# Line 698 | Line 734 | public abstract class ForkJoinTask<V> im
734                          long ms = nt / 1000000;
735                          int ns = (int) (nt % 1000000);
736                          try {
737 <                            synchronized(this) {
737 >                            synchronized (this) {
738                                  if (status >= 0)
739                                      wait(ms, ns);
740                              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines