--- jsr166/src/jsr166y/ForkJoinTask.java 2012/11/14 17:20:37 1.93 +++ jsr166/src/jsr166y/ForkJoinTask.java 2015/09/03 22:54:46 1.102 @@ -33,7 +33,7 @@ import java.lang.reflect.Constructor; *

A "main" {@code ForkJoinTask} begins execution when it is * explicitly submitted to a {@link ForkJoinPool}, or, if not already * engaged in a ForkJoin computation, commenced in the {@link - * ForkJoinPool#commonPool} via {@link #fork}, {@link #invoke}, or + * ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or * related methods. Once started, it will usually in turn start other * subtasks. As indicated by the name of this class, many programs * using {@code ForkJoinTask} employ only methods {@link #fork} and @@ -55,7 +55,7 @@ import java.lang.reflect.Constructor; * minimize other blocking synchronization apart from joining other * tasks or using synchronizers such as Phasers that are advertised to * cooperate with fork/join scheduling. Subdividable tasks should also - * not perform blocking IO, and should ideally access variables that + * not perform blocking I/O, and should ideally access variables that * are completely independent of those accessed by other running * tasks. These guidelines are loosely enforced by not permitting * checked exceptions such as {@code IOExceptions} to be @@ -73,7 +73,7 @@ import java.lang.reflect.Constructor; *

It is possible to define and use ForkJoinTasks that may block, * but doing do requires three further considerations: (1) Completion * of few if any other tasks should be dependent on a task - * that blocks on external synchronization or IO. Event-style async + * that blocks on external synchronization or I/O. Event-style async * tasks that are never joined (for example, those subclassing {@link * CountedCompleter}) often fall into this category. (2) To minimize * resource impact, tasks should be small; ideally performing only the @@ -395,11 +395,13 @@ public abstract class ForkJoinTask im final Throwable ex; ExceptionNode next; final long thrower; // use id not ref to avoid weak cycles + final int hashCode; // store task hashCode before weak ref disappears ExceptionNode(ForkJoinTask task, Throwable ex, ExceptionNode next) { super(task, exceptionTableRefQueue); this.ex = ex; this.next = next; this.thrower = Thread.currentThread().getId(); + this.hashCode = System.identityHashCode(task); } } @@ -435,7 +437,7 @@ public abstract class ForkJoinTask im } /** - * Records exception and possibly propagates + * Records exception and possibly propagates. * * @return status on exit */ @@ -468,7 +470,7 @@ public abstract class ForkJoinTask im } /** - * Removes exception node and clears status + * Removes exception node and clears status. */ private void clearExceptionalCompletion() { int h = System.identityHashCode(this); @@ -561,9 +563,9 @@ public abstract class ForkJoinTask im private static void expungeStaleExceptions() { for (Object x; (x = exceptionTableRefQueue.poll()) != null;) { if (x instanceof ExceptionNode) { - ForkJoinTask key = ((ExceptionNode)x).get(); + int hashCode = ((ExceptionNode)x).hashCode; ExceptionNode[] t = exceptionTable; - int i = System.identityHashCode(key) & (t.length - 1); + int i = hashCode & (t.length - 1); ExceptionNode e = t[i]; ExceptionNode pred = null; while (e != null) { @@ -606,7 +608,7 @@ public abstract class ForkJoinTask im throw (Error)ex; if (ex instanceof RuntimeException) throw (RuntimeException)ex; - throw uncheckedThrowable(ex, RuntimeException.class); + ForkJoinTask.uncheckedThrow(ex); } } @@ -616,8 +618,9 @@ public abstract class ForkJoinTask im * unchecked exceptions */ @SuppressWarnings("unchecked") static - T uncheckedThrowable(final Throwable t, final Class c) { - return (T)t; // rely on vacuous cast + void uncheckedThrow(Throwable t) throws T { + if (t != null) + throw (T)t; // rely on vacuous cast } /** @@ -635,7 +638,7 @@ public abstract class ForkJoinTask im /** * Arranges to asynchronously execute this task in the pool the * current task is running in, if applicable, or using the {@link - * ForkJoinPool#commonPool} if not {@link #inForkJoinPool}. While + * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}. While * it is not necessarily enforced, it is a usage error to fork a * task more than once unless it has completed and been * reinitialized. Subsequent modifications to the state of this @@ -652,7 +655,7 @@ public abstract class ForkJoinTask im if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ((ForkJoinWorkerThread)t).workQueue.push(this); else - ForkJoinPool.commonPool.externalPush(this); + ForkJoinPool.common.externalPush(this); return this; } @@ -978,8 +981,9 @@ public abstract class ForkJoinTask im if (Thread.interrupted()) throw new InterruptedException(); // Messy in part because we measure in nanosecs, but wait in millisecs - int s; long ns, ms; - if ((s = status) >= 0 && (ns = unit.toNanos(timeout)) > 0L) { + int s; long ms; + long ns = unit.toNanos(timeout); + if ((s = status) >= 0 && ns > 0L) { long deadline = System.nanoTime() + ns; ForkJoinPool p = null; ForkJoinPool.WorkQueue w = null; @@ -1075,7 +1079,7 @@ public abstract class ForkJoinTask im wt.pool.helpQuiescePool(wt.workQueue); } else - ForkJoinPool.externalHelpQuiescePool(); + ForkJoinPool.quiesceCommonPool(); } /** @@ -1324,7 +1328,7 @@ public abstract class ForkJoinTask im } /** - * Adaptor for Runnables. This implements RunnableFuture + * Adapter for Runnables. This implements RunnableFuture * to be compliant with AbstractExecutorService constraints * when used in ForkJoinPool. */ @@ -1345,7 +1349,7 @@ public abstract class ForkJoinTask im } /** - * Adaptor for Runnables without results + * Adapter for Runnables without results */ static final class AdaptedRunnableAction extends ForkJoinTask implements RunnableFuture { @@ -1362,7 +1366,7 @@ public abstract class ForkJoinTask im } /** - * Adaptor for Callables + * Adapter for Callables */ static final class AdaptedCallable extends ForkJoinTask implements RunnableFuture { @@ -1483,21 +1487,23 @@ public abstract class ForkJoinTask im private static sun.misc.Unsafe getUnsafe() { try { return sun.misc.Unsafe.getUnsafe(); - } catch (SecurityException se) { - try { - return java.security.AccessController.doPrivileged - (new java.security - .PrivilegedExceptionAction() { - public sun.misc.Unsafe run() throws Exception { - java.lang.reflect.Field f = sun.misc - .Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - return (sun.misc.Unsafe) f.get(null); - }}); - } catch (java.security.PrivilegedActionException e) { - throw new RuntimeException("Could not initialize intrinsics", - e.getCause()); - } + } catch (SecurityException tryReflectionInstead) {} + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public sun.misc.Unsafe run() throws Exception { + Class k = sun.misc.Unsafe.class; + for (java.lang.reflect.Field f : k.getDeclaredFields()) { + f.setAccessible(true); + Object x = f.get(null); + if (k.isInstance(x)) + return k.cast(x); + } + throw new NoSuchFieldError("the Unsafe"); + }}); + } catch (java.security.PrivilegedActionException e) { + throw new RuntimeException("Could not initialize intrinsics", + e.getCause()); } } }