--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/21 00:15:13 1.10 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/22 19:04:11 1.13 @@ -81,8 +81,10 @@ import java.lang.reflect.*; * class. While these methods have {@code public} access (to allow * instances of different task subclasses to call each others * methods), some of them may only be called from within other - * ForkJoinTasks. Attempts to invoke them in other contexts result in - * exceptions or errors possibly including ClassCastException. + * ForkJoinTasks (as may be determined using method {@link + * #inForkJoinPool}). Attempts to invoke them in other contexts + * result in exceptions or errors possibly including + * ClassCastException. * *

Most base support methods are {@code final} because their * implementations are intrinsically tied to the underlying @@ -107,6 +109,9 @@ import java.lang.reflect.*; * in general sensible to serialize tasks only before or after, but * not during execution. Serialization is not relied on during * execution itself. + * + * @since 1.7 + * @author Doug Lea */ public abstract class ForkJoinTask implements Future, Serializable { @@ -162,7 +167,7 @@ public abstract class ForkJoinTask im } final boolean casStatus(int cmp, int val) { - return _unsafe.compareAndSwapInt(this, statusOffset, cmp, val); + return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); } /** @@ -170,7 +175,7 @@ public abstract class ForkJoinTask im */ static void rethrowException(Throwable ex) { if (ex != null) - _unsafe.throwException(ex); + UNSAFE.throwException(ex); } // Setting completion status @@ -213,7 +218,7 @@ public abstract class ForkJoinTask im final void setNormalCompletion() { // Try typical fast case -- single CAS, no signal, not already done. // Manually expand casStatus to improve chances of inlining it - if (!_unsafe.compareAndSwapInt(this, statusOffset, 0, NORMAL)) + if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) setCompletion(NORMAL); } @@ -479,8 +484,9 @@ public abstract class ForkJoinTask im * necessarily enforced, it is a usage error to fork a task more * than once unless it has completed and been reinitialized. This * method may be invoked only from within ForkJoinTask - * computations. Attempts to invoke in other contexts result in - * exceptions or errors possibly including ClassCastException. + * computations (as may be determined using method {@link + * #inForkJoinPool}). Attempts to invoke in other contexts result + * in exceptions or errors possibly including ClassCastException. */ public final void fork() { ((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this); @@ -519,7 +525,8 @@ public abstract class ForkJoinTask im /** * Forks both tasks, returning when {@code isDone} holds for * both of them or an exception is encountered. This method may be - * invoked only from within ForkJoinTask computations. Attempts to + * invoked only from within ForkJoinTask computations (as may be + * determined using method {@link #inForkJoinPool}). Attempts to * invoke in other contexts result in exceptions or errors * possibly including ClassCastException. * @@ -538,9 +545,10 @@ public abstract class ForkJoinTask im * Forks the given tasks, returning when {@code isDone} holds * for all of them. If any task encounters an exception, others * may be cancelled. This method may be invoked only from within - * ForkJoinTask computations. Attempts to invoke in other contexts - * result in exceptions or errors possibly including ClassCastException. - * + * ForkJoinTask computations (as may be determined using method + * {@link #inForkJoinPool}). Attempts to invoke in other contexts + * result in exceptions or errors possibly including + * ClassCastException. * @param tasks the array of tasks * @throws NullPointerException if tasks or any element are null * @throws RuntimeException or Error if any task did so @@ -582,9 +590,10 @@ public abstract class ForkJoinTask im * Forks all tasks in the collection, returning when * {@code isDone} holds for all of them. If any task * encounters an exception, others may be cancelled. This method - * may be invoked only from within ForkJoinTask - * computations. Attempts to invoke in other contexts result in - * exceptions or errors possibly including ClassCastException. + * may be invoked only from within ForkJoinTask computations (as + * may be determined using method {@link + * #inForkJoinPool}). Attempts to invoke in other contexts resul!t + * in exceptions or errors possibly including ClassCastException. * * @param tasks the collection of tasks * @throws NullPointerException if tasks or any element are null @@ -770,8 +779,10 @@ public abstract class ForkJoinTask im * current task and that of any other task that might be executed * while helping. (This usually holds for pure divide-and-conquer * tasks). This method may be invoked only from within - * ForkJoinTask computations. Attempts to invoke in other contexts - * result in exceptions or errors possibly including ClassCastException. + * ForkJoinTask computations (as may be determined using method + * {@link #inForkJoinPool}). Attempts to invoke in other contexts + * resul!t in exceptions or errors possibly including + * ClassCastException. * * @return the computed result */ @@ -785,8 +796,9 @@ public abstract class ForkJoinTask im /** * Possibly executes other tasks until this task is ready. This * method may be invoked only from within ForkJoinTask - * computations. Attempts to invoke in other contexts result in - * exceptions or errors possibly including ClassCastException. + * computations (as may be determined using method {@link + * #inForkJoinPool}). Attempts to invoke in other contexts resul!t + * in exceptions or errors possibly including ClassCastException. */ public final void quietlyHelpJoin() { if (status >= 0) { @@ -852,9 +864,9 @@ public abstract class ForkJoinTask im /** * Returns the pool hosting the current task execution, or null - * if this task is executing outside of any pool. + * if this task is executing outside of any ForkJoinPool. * - * @return the pool, or null if none + * @return the pool, or null if none. */ public static ForkJoinPool getPool() { Thread t = Thread.currentThread(); @@ -863,14 +875,26 @@ public abstract class ForkJoinTask im } /** + * Returns true if the current thread is executing as a + * ForkJoinPool computation. + * @return true if the current thread is executing as a + * ForkJoinPool computation, or false otherwise + */ + public static boolean inForkJoinPool() { + return Thread.currentThread() instanceof ForkJoinWorkerThread; + } + + /** * Tries to unschedule this task for execution. This method will * typically succeed if this task is the most recently forked task * by the current thread, and has not commenced executing in * another thread. This method may be useful when arranging * alternative local processing of tasks that could have been, but * were not, stolen. This method may be invoked only from within - * ForkJoinTask computations. Attempts to invoke in other contexts - * result in exceptions or errors possibly including ClassCastException. + * ForkJoinTask computations (as may be determined using method + * {@link #inForkJoinPool}). Attempts to invoke in other contexts + * result in exceptions or errors possibly including + * ClassCastException. * * @return true if unforked */ @@ -951,8 +975,9 @@ public abstract class ForkJoinTask im * be polled or executed next. This method is designed primarily * to support extensions, and is unlikely to be useful otherwise. * This method may be invoked only from within ForkJoinTask - * computations. Attempts to invoke in other contexts result in - * exceptions or errors possibly including ClassCastException. + * computations (as may be determined using method {@link + * #inForkJoinPool}). Attempts to invoke in other contexts result + * in exceptions or errors possibly including ClassCastException. * * @return the next task, or null if none are available */ @@ -965,7 +990,8 @@ public abstract class ForkJoinTask im * queued by the current thread but not yet executed. This method * is designed primarily to support extensions, and is unlikely to * be useful otherwise. This method may be invoked only from - * within ForkJoinTask computations. Attempts to invoke in other + * within ForkJoinTask computations (as may be determined using + * method {@link #inForkJoinPool}). Attempts to invoke in other * contexts result in exceptions or errors possibly including * ClassCastException. * @@ -984,7 +1010,8 @@ public abstract class ForkJoinTask im * of the pool this task is operating in. This method is designed * primarily to support extensions, and is unlikely to be useful * otherwise. This method may be invoked only from within - * ForkJoinTask computations. Attempts to invoke in other contexts + * ForkJoinTask computations (as may be determined using method + * {@link #inForkJoinPool}). Attempts to invoke in other contexts * result in exceptions or errors possibly including * ClassCastException. * @@ -1053,16 +1080,16 @@ public abstract class ForkJoinTask im private static long fieldOffset(String fieldName) throws NoSuchFieldException { - return _unsafe.objectFieldOffset + return UNSAFE.objectFieldOffset (ForkJoinTask.class.getDeclaredField(fieldName)); } - static final Unsafe _unsafe; + static final Unsafe UNSAFE; static final long statusOffset; static { try { - _unsafe = getUnsafe(); + UNSAFE = getUnsafe(); statusOffset = fieldOffset("status"); } catch (Throwable e) { throw new RuntimeException("Could not initialize intrinsics", e);