ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinWorkerThread.java (file contents):
Revision 1.18 by jsr166, Sat Jul 25 00:34:00 2009 UTC vs.
Revision 1.21 by jsr166, Mon Jul 27 20:57:44 2009 UTC

# Line 265 | Line 265 | public class ForkJoinWorkerThread extend
265      final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
266  
267      /**
268 <     * Transitions to at least the given state.  Returns true if not
269 <     * already at least at given state.
268 >     * Transitions to at least the given state.
269 >     *
270 >     * @return {@code true} if not already at least at given state
271       */
272      private boolean transitionRunStateTo(int state) {
273          for (;;) {
# Line 372 | Line 373 | public class ForkJoinWorkerThread extend
373       * {@code super.onTermination} at the end of the overridden method.
374       *
375       * @param exception the exception causing this thread to abort due
376 <     * to an unrecoverable error, or null if completed normally
376 >     * to an unrecoverable error, or {@code null} if completed normally
377       */
378      protected void onTermination(Throwable exception) {
379          // Execute remaining local tasks unless aborting or terminating
# Line 667 | Line 668 | public class ForkJoinWorkerThread extend
668      }
669  
670      /**
671 <     * Returns true if at least one worker in the given array appears
672 <     * to have at least one queued task.
671 >     * Returns {@code true} if at least one worker in the given array
672 >     * appears to have at least one queued task.
673       *
674       * @param ws array of workers
675       */
# Line 731 | Line 732 | public class ForkJoinWorkerThread extend
732          do {} while (!tryActivate()); // re-activate on exit
733      }
734  
735 <    // Unsafe mechanics for jsr166y 3rd party package.
735 >    // Unsafe mechanics
736 >
737 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
738 >    private static final long spOffset =
739 >        objectFieldOffset("sp", ForkJoinWorkerThread.class);
740 >    private static final long runStateOffset =
741 >        objectFieldOffset("runState", ForkJoinWorkerThread.class);
742 >    private static final long qBase;
743 >    private static final int qShift;
744 >
745 >    static {
746 >        qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
747 >        int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
748 >        if ((s & (s-1)) != 0)
749 >            throw new Error("data type scale not a power of two");
750 >        qShift = 31 - Integer.numberOfLeadingZeros(s);
751 >    }
752 >
753 >    private static long objectFieldOffset(String field, Class<?> klazz) {
754 >        try {
755 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
756 >        } catch (NoSuchFieldException e) {
757 >            // Convert Exception to corresponding Error
758 >            NoSuchFieldError error = new NoSuchFieldError(field);
759 >            error.initCause(e);
760 >            throw error;
761 >        }
762 >    }
763 >
764 >    /**
765 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
766 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
767 >     * into a jdk.
768 >     *
769 >     * @return a sun.misc.Unsafe
770 >     */
771      private static sun.misc.Unsafe getUnsafe() {
772          try {
773              return sun.misc.Unsafe.getUnsafe();
774          } catch (SecurityException se) {
775              try {
776                  return java.security.AccessController.doPrivileged
777 <                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
777 >                    (new java.security
778 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
779                          public sun.misc.Unsafe run() throws Exception {
780 <                            return getUnsafeByReflection();
780 >                            java.lang.reflect.Field f = sun.misc
781 >                                .Unsafe.class.getDeclaredField("theUnsafe");
782 >                            f.setAccessible(true);
783 >                            return (sun.misc.Unsafe) f.get(null);
784                          }});
785              } catch (java.security.PrivilegedActionException e) {
786                  throw new RuntimeException("Could not initialize intrinsics",
# Line 748 | Line 788 | public class ForkJoinWorkerThread extend
788              }
789          }
790      }
751
752    private static sun.misc.Unsafe getUnsafeByReflection()
753            throws NoSuchFieldException, IllegalAccessException {
754        java.lang.reflect.Field f =
755            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
756        f.setAccessible(true);
757        return (sun.misc.Unsafe) f.get(null);
758    }
759
760    private static long fieldOffset(String fieldName, Class<?> klazz) {
761        try {
762            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
763        } catch (NoSuchFieldException e) {
764            // Convert Exception to Error
765            NoSuchFieldError error = new NoSuchFieldError(fieldName);
766            error.initCause(e);
767            throw error;
768        }
769    }
770
771    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
772    static final long baseOffset =
773        fieldOffset("base", ForkJoinWorkerThread.class);
774    static final long spOffset =
775        fieldOffset("sp", ForkJoinWorkerThread.class);
776    static final long runStateOffset =
777        fieldOffset("runState", ForkJoinWorkerThread.class);
778    static final long qBase;
779    static final int qShift;
780
781    static {
782        qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
783        int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
784        if ((s & (s-1)) != 0)
785            throw new Error("data type scale not a power of two");
786        qShift = 31 - Integer.numberOfLeadingZeros(s);
787    }
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines