--- jsr166/src/jsr166y/ForkJoinTask.java 2009/01/07 16:07:37 1.2 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/01/07 19:12:36 1.3 @@ -257,7 +257,7 @@ public abstract class ForkJoinTask im * surrounded with pool notifications. * @return status upon exit */ - final int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { + private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { ForkJoinPool pool = w == null? null : w.pool; int s; while ((s = status) >= 0) { @@ -276,7 +276,7 @@ public abstract class ForkJoinTask im * Timed version of awaitDone * @return status upon exit */ - final int awaitDone(ForkJoinWorkerThread w, long nanos) { + private int awaitDone(ForkJoinWorkerThread w, long nanos) { ForkJoinPool pool = w == null? null : w.pool; int s; while ((s = status) >= 0) { @@ -330,7 +330,7 @@ public abstract class ForkJoinTask im if (w == null) Thread.currentThread().interrupt(); // re-interrupt else if (w.isTerminating()) - cancelIgnoreExceptions(); + cancelIgnoringExceptions(); // else if FJworker, ignore interrupt } @@ -449,13 +449,24 @@ public abstract class ForkJoinTask im /** * Cancel, ignoring any exceptions it throws */ - final void cancelIgnoreExceptions() { + final void cancelIgnoringExceptions() { try { cancel(false); } catch(Throwable ignore) { } } + /** + * Main implementation of helpJoin + */ + private int busyJoin(ForkJoinWorkerThread w) { + int s; + ForkJoinTask t; + while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) + t.quietlyExec(); + return (s >= 0)? awaitDone(w, false) : s; // block if no work + } + // public methods /** @@ -485,21 +496,6 @@ public abstract class ForkJoinTask im return getRawResult(); } - public final V get() throws InterruptedException, ExecutionException { - ForkJoinWorkerThread w = getWorker(); - if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) - awaitDone(w, true); - return reportFutureResult(); - } - - public final V get(long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - ForkJoinWorkerThread w = getWorker(); - if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) - awaitDone(w, unit.toNanos(timeout)); - return reportTimedFutureResult(); - } - /** * Commences performing this task, awaits its completion if * necessary, and return its result. @@ -642,29 +638,6 @@ public abstract class ForkJoinTask im } /** - * Returns true if this task threw an exception or was cancelled - * @return true if this task threw an exception or was cancelled - */ - public final boolean isCompletedAbnormally() { - return (status & COMPLETION_MASK) < NORMAL; - } - - /** - * Returns the exception thrown by the base computation, or a - * CancellationException if cancelled, or null if none or if the - * method has not yet completed. - * @return the exception, or null if none - */ - public final Throwable getException() { - int s = status & COMPLETION_MASK; - if (s >= NORMAL) - return null; - if (s == CANCELLED) - return new CancellationException(); - return exceptionMap.get(this); - } - - /** * Asserts that the results of this task's computation will not be * used. If a cancellation occurs before atempting to execute this * task, then execution will be suppressed, isCancelled @@ -697,6 +670,29 @@ public abstract class ForkJoinTask im } /** + * Returns true if this task threw an exception or was cancelled + * @return true if this task threw an exception or was cancelled + */ + public final boolean isCompletedAbnormally() { + return (status & COMPLETION_MASK) < NORMAL; + } + + /** + * Returns the exception thrown by the base computation, or a + * CancellationException if cancelled, or null if none or if the + * method has not yet completed. + * @return the exception, or null if none + */ + public final Throwable getException() { + int s = status & COMPLETION_MASK; + if (s >= NORMAL) + return null; + if (s == CANCELLED) + return new CancellationException(); + return exceptionMap.get(this); + } + + /** * Completes this task abnormally, and if not already aborted or * cancelled, causes it to throw the given exception upon * join and related operations. This method may be used @@ -738,6 +734,21 @@ public abstract class ForkJoinTask im setNormalCompletion(); } + public final V get() throws InterruptedException, ExecutionException { + ForkJoinWorkerThread w = getWorker(); + if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) + awaitDone(w, true); + return reportFutureResult(); + } + + public final V get(long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException, TimeoutException { + ForkJoinWorkerThread w = getWorker(); + if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) + awaitDone(w, unit.toNanos(timeout)); + return reportTimedFutureResult(); + } + /** * Possibly executes other tasks until this task is ready, then * returns the result of the computation. This method may be more @@ -753,7 +764,7 @@ public abstract class ForkJoinTask im public final V helpJoin() { ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); if (status < 0 || !w.unpushTask(this) || !tryExec()) - reportException(w.helpJoinTask(this)); + reportException(busyJoin(w)); return getRawResult(); } @@ -768,7 +779,7 @@ public abstract class ForkJoinTask im ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); if (!w.unpushTask(this) || !tryQuietlyInvoke()) - w.helpJoinTask(this); + busyJoin(w); } } @@ -799,6 +810,17 @@ public abstract class ForkJoinTask im } /** + * Possibly executes tasks until the pool hosting the current task + * {@link ForkJoinPool#isQuiescent}. This method may be of use in + * designs in which many tasks are forked, but none are explicitly + * joined, instead executing them until all are processed. + */ + public static void helpQuiesce() { + ((ForkJoinWorkerThread)(Thread.currentThread())). + helpQuiescePool(); + } + + /** * Resets the internal bookkeeping state of this task, allowing a * subsequent fork. This method allows repeated reuse of * this task, but only if reuse occurs when this task has either @@ -841,17 +863,6 @@ public abstract class ForkJoinTask im } /** - * Possibly executes tasks until the pool hosting the current task - * {@link ForkJoinPool#isQuiescent}. This method may be of use in - * designs in which many tasks are forked, but none are explicitly - * joined, instead executing them until all are processed. - */ - public static void helpQuiesce() { - ((ForkJoinWorkerThread)(Thread.currentThread())). - helpQuiescePool(); - } - - /** * Returns an estimate of the number of tasks that have been * forked by the current worker thread but not yet executed. This * value may be useful for heuristic decisions about whether to @@ -954,7 +965,7 @@ public abstract class ForkJoinTask im */ protected static ForkJoinTask pollTask() { return ((ForkJoinWorkerThread)(Thread.currentThread())). - getLocalOrStolenTask(); + pollLocalOrStolenTask(); } // Serialization support