--- jsr166/src/jsr166y/ForkJoinTask.java 2010/08/11 18:45:12 1.53 +++ jsr166/src/jsr166y/ForkJoinTask.java 2010/09/04 00:21:31 1.56 @@ -205,7 +205,8 @@ public abstract class ForkJoinTask im } /** - * Record exception and set exceptional completion + * Records exception and sets exceptional completion. + * * @return status on exit */ private void setExceptionalCompletion(Throwable rex) { @@ -214,7 +215,9 @@ public abstract class ForkJoinTask im } /** - * Blocks a worker thread until completion. Called only by pool. + * Blocks a worker thread until completion. Called only by + * pool. Currently unused -- pool-based waits use timeout + * version below. */ final void internalAwaitDone() { int s; // the odd construction reduces lock bias effects @@ -231,6 +234,28 @@ public abstract class ForkJoinTask im } /** + * Blocks a worker thread until completed or timed out. Called + * only by pool. + * + * @return status on exit + */ + final int internalAwaitDone(long millis) { + int s; + if ((s = status) >= 0) { + try { + synchronized(this) { + if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) + wait(millis, 0); + } + } catch (InterruptedException ie) { + cancelIfTerminating(); + } + s = status; + } + return s; + } + + /** * Blocks a non-worker-thread until completion. */ private void externalAwaitDone() { @@ -314,7 +339,7 @@ public abstract class ForkJoinTask im /** * Commences performing this task, awaits its completion if - * necessary, and return its result, or throws an (unchecked) + * necessary, and returns its result, or throws an (unchecked) * exception if the underlying computation did so. * * @return the computed result @@ -510,7 +535,8 @@ public abstract class ForkJoinTask im } /** - * Cancels ignoring exceptions if worker is terminating + * Cancels if current thread is a terminating worker thread, + * ignoring any exceptions thrown by cancel. */ final void cancelIfTerminating() { Thread t = Thread.currentThread(); @@ -643,6 +669,7 @@ public abstract class ForkJoinTask im */ boolean interrupted = false; boolean dec = false; // true if pool count decremented + long nanos = unit.toNanos(timeout); for (;;) { if (Thread.interrupted() && pool == null) { interrupted = true; @@ -653,7 +680,6 @@ public abstract class ForkJoinTask im break; if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { long startTime = System.nanoTime(); - long nanos = unit.toNanos(timeout); long nt; // wait time while (status >= 0 && (nt = nanos - (System.nanoTime() - startTime)) > 0) { @@ -1069,7 +1095,7 @@ public abstract class ForkJoinTask im private static final long serialVersionUID = -7721805057305804111L; /** - * Saves the state to a stream. + * Saves the state to a stream (that is, serializes it). * * @serialData the current run status and the exception thrown * during execution, or {@code null} if none @@ -1082,7 +1108,7 @@ public abstract class ForkJoinTask im } /** - * Reconstitutes the instance from a stream. + * Reconstitutes the instance from a stream (that is, deserializes it). * * @param s the stream */