--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/26 05:55:34 1.20 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/27 20:57:44 1.23 @@ -493,7 +493,7 @@ public abstract class ForkJoinTask im * #inForkJoinPool}). Attempts to invoke in other contexts result * in exceptions or errors, possibly including ClassCastException. * - * @return this, to simplify usage. + * @return {@code this}, to simplify usage. */ public final ForkJoinTask fork() { ((ForkJoinWorkerThread) Thread.currentThread()) @@ -652,19 +652,19 @@ public abstract class ForkJoinTask im } /** - * Returns true if the computation performed by this task has - * completed (or has been cancelled). + * Returns {@code true} if the computation performed by this task + * has completed (or has been cancelled). * - * @return true if this computation has completed + * @return {@code true} if this computation has completed */ public final boolean isDone() { return status < 0; } /** - * Returns true if this task was cancelled. + * Returns {@code true} if this task was cancelled. * - * @return true if this task was cancelled + * @return {@code true} if this task was cancelled */ public final boolean isCancelled() { return (status & COMPLETION_MASK) == CANCELLED; @@ -695,7 +695,7 @@ public abstract class ForkJoinTask im * default implementation because tasks are not in general * cancelled via interruption * - * @return true if this task is now cancelled + * @return {@code true} if this task is now cancelled */ public boolean cancel(boolean mayInterruptIfRunning) { setCompletion(CANCELLED); @@ -703,9 +703,9 @@ public abstract class ForkJoinTask im } /** - * Returns true if this task threw an exception or was cancelled. + * Returns {@code true} if this task threw an exception or was cancelled. * - * @return true if this task threw an exception or was cancelled + * @return {@code true} if this task threw an exception or was cancelled */ public final boolean isCompletedAbnormally() { return (status & COMPLETION_MASK) < NORMAL; @@ -716,7 +716,7 @@ public abstract class ForkJoinTask im * CancellationException if cancelled, or null if none or if the * method has not yet completed. * - * @return the exception, or null if none + * @return the exception, or {@code null} if none */ public final Throwable getException() { int s = status & COMPLETION_MASK; @@ -879,7 +879,7 @@ public abstract class ForkJoinTask im * Returns the pool hosting the current task execution, or null * if this task is executing outside of any ForkJoinPool. * - * @return the pool, or null if none + * @return the pool, or {@code null} if none */ public static ForkJoinPool getPool() { Thread t = Thread.currentThread(); @@ -910,7 +910,7 @@ public abstract class ForkJoinTask im * result in exceptions or errors, possibly including * ClassCastException. * - * @return true if unforked + * @return {@code true} if unforked */ public boolean tryUnfork() { return ((ForkJoinWorkerThread) Thread.currentThread()) @@ -950,13 +950,13 @@ public abstract class ForkJoinTask im // Extension methods /** - * Returns the result that would be returned by {@code join}, - * even if this task completed abnormally, or null if this task is - * not known to have been completed. This method is designed to - * aid debugging, as well as to support extensions. Its use in any - * other context is discouraged. + * Returns the result that would be returned by {@link #join}, even + * if this task completed abnormally, or {@code null} if this task + * is not known to have been completed. This method is designed + * to aid debugging, as well as to support extensions. Its use in + * any other context is discouraged. * - * @return the result, or null if not completed + * @return the result, or {@code null} if not completed */ public abstract V getRawResult(); @@ -975,10 +975,10 @@ public abstract class ForkJoinTask im * called otherwise. The return value controls whether this task * is considered to be done normally. It may return false in * asynchronous actions that require explicit invocations of - * {@code complete} to become joinable. It may throw exceptions + * {@link #complete} to become joinable. It may throw exceptions * to indicate abnormal exit. * - * @return true if completed normally + * @return {@code true} if completed normally * @throws Error or RuntimeException if encountered during computation */ protected abstract boolean exec(); @@ -994,7 +994,7 @@ public abstract class ForkJoinTask im * #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 + * @return the next task, or {@code null} if none are available */ protected static ForkJoinTask peekNextLocalTask() { return ((ForkJoinWorkerThread) Thread.currentThread()) @@ -1011,7 +1011,7 @@ public abstract class ForkJoinTask im * contexts result in exceptions or errors, possibly including * ClassCastException. * - * @return the next task, or null if none are available + * @return the next task, or {@code null} if none are available */ protected static ForkJoinTask pollNextLocalTask() { return ((ForkJoinWorkerThread) Thread.currentThread()) @@ -1032,7 +1032,7 @@ public abstract class ForkJoinTask im * result in exceptions or errors, possibly including * ClassCastException. * - * @return a task, or null if none are available + * @return a task, or {@code null} if none are available */ protected static ForkJoinTask pollTask() { return ((ForkJoinWorkerThread) Thread.currentThread()) @@ -1042,9 +1042,9 @@ public abstract class ForkJoinTask im // adaptors /** - * Returns a new ForkJoinTask that performs the run + * Returns a new ForkJoinTask that performs the {@code run} * method of the given Runnable as its action, and returns a null - * result upon join. + * result upon {@code join}. * * @param runnable the runnable action * @return the task @@ -1054,9 +1054,9 @@ public abstract class ForkJoinTask im } /** - * Returns a new ForkJoinTask that performs the run + * Returns a new ForkJoinTask that performs the {@code run} * method of the given Runnable as its action, and returns the - * given result upon join. + * given result upon {@code join}. * * @param runnable the runnable action * @param result the result upon completion @@ -1067,10 +1067,10 @@ public abstract class ForkJoinTask im } /** - * Returns a new ForkJoinTask that performs the call + * Returns a new ForkJoinTask that performs the {@code call} * method of the given Callable as its action, and returns its - * result upon join, translating any checked - * exceptions encountered into RuntimeException. + * result upon {@code join}, translating any checked + * exceptions encountered into {@code RuntimeException}. * * @param callable the callable action * @return the task @@ -1087,7 +1087,7 @@ public abstract class ForkJoinTask im * Save the state to a stream. * * @serialData the current run status and the exception thrown - * during execution, or null if none + * during execution, or {@code null} if none * @param s the stream */ private void writeObject(java.io.ObjectOutputStream s) @@ -1111,16 +1111,43 @@ public abstract class ForkJoinTask im setDoneExceptionally((Throwable) ex); } - // Unsafe mechanics for jsr166y 3rd party package. + // Unsafe mechanics + + private static final sun.misc.Unsafe UNSAFE = getUnsafe(); + private static final long statusOffset = + objectFieldOffset("status", ForkJoinTask.class); + + private static long objectFieldOffset(String field, Class klazz) { + try { + return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); + } catch (NoSuchFieldException e) { + // Convert Exception to corresponding Error + NoSuchFieldError error = new NoSuchFieldError(field); + error.initCause(e); + throw error; + } + } + + /** + * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. + * Replace with a simple call to Unsafe.getUnsafe when integrating + * into a jdk. + * + * @return a sun.misc.Unsafe + */ 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() { + (new java.security + .PrivilegedExceptionAction() { public sun.misc.Unsafe run() throws Exception { - return getUnsafeByReflection(); + 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", @@ -1128,28 +1155,4 @@ public abstract class ForkJoinTask im } } } - - private static sun.misc.Unsafe getUnsafeByReflection() - throws NoSuchFieldException, IllegalAccessException { - java.lang.reflect.Field f = - sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - return (sun.misc.Unsafe) f.get(null); - } - - private static long fieldOffset(String fieldName, Class klazz) { - try { - return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); - } catch (NoSuchFieldException e) { - // Convert Exception to Error - NoSuchFieldError error = new NoSuchFieldError(fieldName); - error.initCause(e); - throw error; - } - } - - private static final sun.misc.Unsafe UNSAFE = getUnsafe(); - private static final long statusOffset = - fieldOffset("status", ForkJoinTask.class); - }