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.69 by dl, Mon Nov 22 12:24:34 2010 UTC vs.
Revision 1.70 by dl, Tue Nov 23 00:10:39 2010 UTC

# Line 134 | Line 134 | import java.util.concurrent.TimeoutExcep
134   * computation. Large tasks should be split into smaller subtasks,
135   * usually via recursive decomposition. As a very rough rule of thumb,
136   * a task should perform more than 100 and less than 10000 basic
137 < * computational steps. If tasks are too big, then parallelism cannot
138 < * improve throughput. If too small, then memory and internal task
139 < * maintenance overhead may overwhelm processing.
137 > * computational steps, and should avoid indefinite looping. If tasks
138 > * are too big, then parallelism cannot improve throughput. If too
139 > * small, then memory and internal task maintenance overhead may
140 > * overwhelm processing.
141   *
142   * <p>This class provides {@code adapt} methods for {@link Runnable}
143   * and {@link Callable}, that may be of use when mixing execution of
# Line 233 | Line 234 | public abstract class ForkJoinTask<V> im
234      }
235  
236      /**
237 <     * Blocks a worker thread until completion. Called only by
238 <     * pool. Currently unused -- pool-based waits use timeout
238 <     * version below.
237 >     * Blocks a worker thread until completed or timed out.  Called
238 >     * only by pool.
239       */
240 <    final void internalAwaitDone() {
241 <        int s;         // the odd construction reduces lock bias effects
242 <        while ((s = status) >= 0) {
243 <            try {
240 >    final void internalAwaitDone(long millis, int nanos) {
241 >        if (status >= 0) {
242 >            try {     // the odd construction reduces lock bias effects
243                  synchronized (this) {
244 <                    if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL))
245 <                        wait();
244 >                    if (status > 0 ||
245 >                        UNSAFE.compareAndSwapInt(this, statusOffset,
246 >                                                 0, SIGNAL))
247 >                        wait(millis, nanos);
248                  }
249              } catch (InterruptedException ie) {
250                  cancelIfTerminating();
# Line 252 | Line 253 | public abstract class ForkJoinTask<V> im
253      }
254  
255      /**
256 <     * Blocks a worker thread until completed or timed out.  Called
256 <     * only by pool.
257 <     *
258 <     * @return status on exit
256 >     * Blocks a non-worker-thread until completion.
257       */
258 <    final int internalAwaitDone(long millis, int nanos) {
259 <        int s;
260 <        if ((s = status) >= 0) {
261 <            try {
262 <                synchronized (this) {
263 <                    if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL))
264 <                        wait(millis, nanos);
258 >    private void externalAwaitDone() {
259 >        if (status >= 0) {
260 >            boolean interrupted = false;
261 >            synchronized(this) {
262 >                int s;
263 >                while ((s = status) >= 0) {
264 >                    if (s == 0 &&
265 >                        !UNSAFE.compareAndSwapInt(this, statusOffset,
266 >                                                  0, SIGNAL))
267 >                        continue;
268 >                    try {
269 >                        wait();
270 >                    } catch (InterruptedException ie) {
271 >                        interrupted = true;
272 >                    }
273                  }
268            } catch (InterruptedException ie) {
269                cancelIfTerminating();
274              }
275 <            s = status;
275 >            if (interrupted)
276 >                Thread.currentThread().interrupt();
277          }
273        return s;
278      }
279  
280      /**
281 <     * Blocks a non-worker-thread until completion.
281 >     * Blocks a non-worker-thread until completion or interruption or timeout
282       */
283 <    private void externalAwaitDone() {
284 <        int s;
285 <        while ((s = status) >= 0) {
286 <            synchronized (this) {
287 <                if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) {
288 <                    boolean interrupted = false;
289 <                    while (status >= 0) {
290 <                        try {
291 <                            wait();
292 <                        } catch (InterruptedException ie) {
293 <                            interrupted = true;
294 <                        }
295 <                    }
296 <                    if (interrupted)
297 <                        Thread.currentThread().interrupt();
298 <                    break;
283 >    private void externalInterruptibleAwaitDone(boolean timed, long nanos)
284 >        throws InterruptedException {
285 >        if (Thread.interrupted())
286 >            throw new InterruptedException();
287 >        if (status >= 0) {
288 >            long startTime = timed ? System.nanoTime() : 0L;
289 >            synchronized(this) {
290 >                int s;
291 >                while ((s = status) >= 0) {
292 >                    long nt;
293 >                    if (s == 0 &&
294 >                        !UNSAFE.compareAndSwapInt(this, statusOffset,
295 >                                                  0, SIGNAL))
296 >                        continue;
297 >                    else if (!timed)
298 >                        wait();
299 >                    else if ((nt = nanos - (System.nanoTime()-startTime)) > 0L)
300 >                        wait(nt / 1000000, (int)(nt % 1000000));
301 >                    else
302 >                        break;
303                  }
304              }
305          }
# Line 326 | Line 334 | public abstract class ForkJoinTask<V> im
334       * #isDone} returning {@code true}.
335       *
336       * <p>This method may be invoked only from within {@code
337 <     * ForkJoinTask} computations (as may be determined using method
337 >     * ForkJoinPool} computations (as may be determined using method
338       * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
339       * result in exceptions or errors, possibly including {@code
340       * ClassCastException}.
# Line 678 | Line 686 | public abstract class ForkJoinTask<V> im
686       * member of a ForkJoinPool and was interrupted while waiting
687       */
688      public final V get() throws InterruptedException, ExecutionException {
689 <        int s;
690 <        if (Thread.currentThread() instanceof ForkJoinWorkerThread) {
689 >        Thread t = Thread.currentThread();
690 >        if (t instanceof ForkJoinWorkerThread)
691              quietlyJoin();
692 <            s = status;
693 <        }
694 <        else {
695 <            while ((s = status) >= 0) {
688 <                synchronized (this) { // interruptible form of awaitDone
689 <                    if (UNSAFE.compareAndSwapInt(this, statusOffset,
690 <                                                 s, SIGNAL)) {
691 <                        while (status >= 0)
692 <                            wait();
693 <                    }
694 <                }
695 <            }
696 <        }
697 <        if (s < NORMAL) {
692 >        else
693 >            externalInterruptibleAwaitDone(false, 0L);
694 >        int s = status;
695 >        if (s != NORMAL) {
696              Throwable ex;
697              if (s == CANCELLED)
698                  throw new CancellationException();
# Line 721 | Line 719 | public abstract class ForkJoinTask<V> im
719      public final V get(long timeout, TimeUnit unit)
720          throws InterruptedException, ExecutionException, TimeoutException {
721          long nanos = unit.toNanos(timeout);
722 <        if (status >= 0) {
723 <            Thread t = Thread.currentThread();
724 <            if (t instanceof ForkJoinWorkerThread) {
725 <                ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
726 <                boolean completed = false; // timed variant of quietlyJoin
727 <                if (w.unpushTask(this)) {
728 <                    try {
731 <                        completed = exec();
732 <                    } catch (Throwable rex) {
733 <                        setExceptionalCompletion(rex);
734 <                    }
735 <                }
736 <                if (completed)
737 <                    setCompletion(NORMAL);
738 <                else if (status >= 0)
739 <                    w.joinTask(this, true, nanos);
740 <            }
741 <            else if (Thread.interrupted())
742 <                throw new InterruptedException();
743 <            else {
744 <                long startTime = System.nanoTime();
745 <                int s; long nt;
746 <                while ((s = status) >= 0 &&
747 <                       (nt = nanos - (System.nanoTime() - startTime)) > 0) {
748 <                    if (UNSAFE.compareAndSwapInt(this, statusOffset, s,
749 <                                                 SIGNAL)) {
750 <                        long ms = nt / 1000000;
751 <                        int ns = (int) (nt % 1000000);
752 <                        synchronized (this) {
753 <                            if (status >= 0)
754 <                                wait(ms, ns); // exit on IE throw
755 <                        }
756 <                    }
757 <                }
758 <            }
759 <        }
760 <        int es = status;
761 <        if (es != NORMAL) {
722 >        Thread t = Thread.currentThread();
723 >        if (t instanceof ForkJoinWorkerThread)
724 >            ((ForkJoinWorkerThread)t).joinTask(this, true, nanos);
725 >        else
726 >            externalInterruptibleAwaitDone(true, nanos);
727 >        int s = status;
728 >        if (s != NORMAL) {
729              Throwable ex;
730 <            if (es == CANCELLED)
730 >            if (s == CANCELLED)
731                  throw new CancellationException();
732 <            if (es == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null)
732 >            if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null)
733                  throw new ExecutionException(ex);
734              throw new TimeoutException();
735          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines