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.16 by jsr166, Thu Jul 23 23:07:57 2009 UTC vs.
Revision 1.21 by jsr166, Mon Jul 27 20:57:44 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 < import java.util.*;
8 >
9   import java.util.concurrent.*;
10 < import java.util.concurrent.atomic.*;
11 < import java.util.concurrent.locks.*;
12 < import sun.misc.Unsafe;
13 < import java.lang.reflect.*;
10 >
11 > import java.util.Collection;
12  
13   /**
14   * A thread managed by a {@link ForkJoinPool}.  This class is
# Line 267 | 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 374 | 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 669 | 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 733 | Line 732 | public class ForkJoinWorkerThread extend
732          do {} while (!tryActivate()); // re-activate on exit
733      }
734  
735 <    // Temporary Unsafe mechanics for preliminary release
736 <    private static Unsafe getUnsafe() throws Throwable {
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 Unsafe.getUnsafe();
773 >            return sun.misc.Unsafe.getUnsafe();
774          } catch (SecurityException se) {
775              try {
776                  return java.security.AccessController.doPrivileged
777 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
778 <                        public Unsafe run() throws Exception {
779 <                            return getUnsafePrivileged();
777 >                    (new java.security
778 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
779 >                        public sun.misc.Unsafe run() throws Exception {
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 e.getCause();
786 >                throw new RuntimeException("Could not initialize intrinsics",
787 >                                           e.getCause());
788              }
789          }
790      }
752
753    private static Unsafe getUnsafePrivileged()
754            throws NoSuchFieldException, IllegalAccessException {
755        Field f = Unsafe.class.getDeclaredField("theUnsafe");
756        f.setAccessible(true);
757        return (Unsafe) f.get(null);
758    }
759
760    private static long fieldOffset(String fieldName)
761            throws NoSuchFieldException {
762        return UNSAFE.objectFieldOffset
763            (ForkJoinWorkerThread.class.getDeclaredField(fieldName));
764    }
765
766    static final Unsafe UNSAFE;
767    static final long baseOffset;
768    static final long spOffset;
769    static final long runStateOffset;
770    static final long qBase;
771    static final int qShift;
772    static {
773        try {
774            UNSAFE = getUnsafe();
775            baseOffset = fieldOffset("base");
776            spOffset = fieldOffset("sp");
777            runStateOffset = fieldOffset("runState");
778            qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
779            int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
780            if ((s & (s-1)) != 0)
781                throw new Error("data type scale not a power of two");
782            qShift = 31 - Integer.numberOfLeadingZeros(s);
783        } catch (Throwable e) {
784            throw new RuntimeException("Could not initialize intrinsics", e);
785        }
786    }
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines