--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/24 22:05:22 1.15 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/24 23:47:01 1.16 @@ -9,8 +9,6 @@ import java.io.Serializable; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; -import sun.misc.Unsafe; -import java.lang.reflect.*; /** * Abstract base class for tasks that run within a {@link @@ -1063,46 +1061,45 @@ public abstract class ForkJoinTask im setDoneExceptionally((Throwable) ex); } - // Temporary Unsafe mechanics for preliminary release - private static Unsafe getUnsafe() throws Throwable { + // Unsafe mechanics for jsr166y 3rd party package. + private static sun.misc.Unsafe getUnsafe() { try { - return Unsafe.getUnsafe(); + return sun.misc.Unsafe.getUnsafe(); } catch (SecurityException se) { try { return java.security.AccessController.doPrivileged - (new java.security.PrivilegedExceptionAction() { - public Unsafe run() throws Exception { - return getUnsafePrivileged(); + (new java.security.PrivilegedExceptionAction() { + public sun.misc.Unsafe run() throws Exception { + return getUnsafeByReflection(); }}); } catch (java.security.PrivilegedActionException e) { - throw e.getCause(); + throw new RuntimeException("Could not initialize intrinsics", + e.getCause()); } } } - private static Unsafe getUnsafePrivileged() + private static sun.misc.Unsafe getUnsafeByReflection() throws NoSuchFieldException, IllegalAccessException { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); + java.lang.reflect.Field f = + sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); - return (Unsafe) f.get(null); + return (sun.misc.Unsafe) f.get(null); } - private static long fieldOffset(String fieldName) - throws NoSuchFieldException { - return UNSAFE.objectFieldOffset - (ForkJoinTask.class.getDeclaredField(fieldName)); - } - - static final Unsafe UNSAFE; - static final long statusOffset; - - static { + private static long fieldOffset(String fieldName, Class klazz) { try { - UNSAFE = getUnsafe(); - statusOffset = fieldOffset("status"); - } catch (Throwable e) { - throw new RuntimeException("Could not initialize intrinsics", e); + 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(); + static final long statusOffset = + fieldOffset("status", ForkJoinTask.class); + }