--- jsr166/src/jsr166y/ForkJoinTask.java 2009/01/12 17:16:18 1.4 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/16 15:32:34 1.6 @@ -926,15 +926,14 @@ public abstract class ForkJoinTask im protected abstract boolean exec(); /** - * Returns, but does not unschedule or execute, the task most - * recently forked by the current thread but not yet executed, if - * one is available. There is no guarantee that this task will - * actually 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. + * Returns, but does not unschedule or execute, the task queued by + * the current thread but not yet executed, if one is + * available. There is no guarantee that this task will actually + * 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. * * @return the next task, or null if none are available */ @@ -943,32 +942,32 @@ public abstract class ForkJoinTask im } /** - * Unschedules and returns, without executing, the task most - * recently forked 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 contexts - * result in exceptions or errors possibly including ClassCastException. + * Unschedules and returns, without executing, the next task + * 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 + * contexts result in exceptions or errors possibly including + * ClassCastException. * * @return the next task, or null if none are available */ protected static ForkJoinTask pollNextLocalTask() { - return ((ForkJoinWorkerThread)(Thread.currentThread())).popTask(); + return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask(); } - + /** - * Unschedules and returns, without executing, the task most - * recently forked by the current thread but not yet executed, if - * one is available, or if not available, a task that was forked - * by some other thread, if available. Availability may be - * transient, so a null result does not necessarily - * imply quiecence 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 + * Unschedules and returns, without executing, the next task + * queued by the current thread but not yet executed, if one is + * available, or if not available, a task that was forked by some + * other thread, if available. Availability may be transient, so a + * null result does not necessarily imply quiecence + * 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 - * result in exceptions or errors possibly including ClassCastException. + * result in exceptions or errors possibly including + * ClassCastException. * * @return a task, or null if none are available */ @@ -1009,22 +1008,45 @@ public abstract class ForkJoinTask im } // Temporary Unsafe mechanics for preliminary release + private static Unsafe getUnsafe() throws Throwable { + try { + return Unsafe.getUnsafe(); + } catch (SecurityException se) { + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public Unsafe run() throws Exception { + return getUnsafePrivileged(); + }}); + } catch (java.security.PrivilegedActionException e) { + throw e.getCause(); + } + } + } + + private static Unsafe getUnsafePrivileged() + throws NoSuchFieldException, IllegalAccessException { + Field f = Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (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 { try { - if (ForkJoinTask.class.getClassLoader() != null) { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - _unsafe = (Unsafe)f.get(null); - } - else - _unsafe = Unsafe.getUnsafe(); - statusOffset = _unsafe.objectFieldOffset - (ForkJoinTask.class.getDeclaredField("status")); - } catch (Exception ex) { throw new Error(ex); } + _unsafe = getUnsafe(); + statusOffset = fieldOffset("status"); + } catch (Throwable e) { + throw new RuntimeException("Could not initialize intrinsics", e); + } } }