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

Comparing jsr166/src/jsr166y/Phaser.java (file contents):
Revision 1.21 by jsr166, Sun Jul 26 05:55:34 2009 UTC vs.
Revision 1.22 by jsr166, Sun Jul 26 17:33:37 2009 UTC

# Line 930 | Line 930 | public class Phaser {
930          return p;
931      }
932  
933 <    // Unsafe mechanics for jsr166y 3rd party package.
933 >    // Unsafe mechanics
934 >
935 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
936 >    private static final long stateOffset =
937 >        objectFieldOffset("state", Phaser.class);
938 >
939 >    private final boolean casState(long cmp, long val) {
940 >        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
941 >    }
942 >
943 >    private static long objectFieldOffset(String field, Class<?> klazz) {
944 >        try {
945 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
946 >        } catch (NoSuchFieldException e) {
947 >            // Convert Exception to corresponding Error
948 >            NoSuchFieldError error = new NoSuchFieldError(field);
949 >            error.initCause(e);
950 >            throw error;
951 >        }
952 >    }
953 >
954 >    /**
955 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
956 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
957 >     * into a jdk.
958 >     *
959 >     * @return a sun.misc.Unsafe
960 >     */
961      private static sun.misc.Unsafe getUnsafe() {
962          try {
963              return sun.misc.Unsafe.getUnsafe();
964          } catch (SecurityException se) {
965              try {
966                  return java.security.AccessController.doPrivileged
967 <                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
967 >                    (new java.security
968 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
969                          public sun.misc.Unsafe run() throws Exception {
970 <                            return getUnsafeByReflection();
970 >                            java.lang.reflect.Field f = sun.misc
971 >                                .Unsafe.class.getDeclaredField("theUnsafe");
972 >                            f.setAccessible(true);
973 >                            return (sun.misc.Unsafe) f.get(null);
974                          }});
975              } catch (java.security.PrivilegedActionException e) {
976                  throw new RuntimeException("Could not initialize intrinsics",
# Line 947 | Line 978 | public class Phaser {
978              }
979          }
980      }
950
951    private static sun.misc.Unsafe getUnsafeByReflection()
952            throws NoSuchFieldException, IllegalAccessException {
953        java.lang.reflect.Field f =
954            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
955        f.setAccessible(true);
956        return (sun.misc.Unsafe) f.get(null);
957    }
958
959    private static long fieldOffset(String fieldName, Class<?> klazz) {
960        try {
961            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
962        } catch (NoSuchFieldException e) {
963            // Convert Exception to Error
964            NoSuchFieldError error = new NoSuchFieldError(fieldName);
965            error.initCause(e);
966            throw error;
967        }
968    }
969
970    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
971    private static final long stateOffset =
972        fieldOffset("state", Phaser.class);
973
974    private final boolean casState(long cmp, long val) {
975        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
976    }
981   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines