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.18 by jsr166, Thu Jul 23 23:07:57 2009 UTC vs.
Revision 1.22 by jsr166, Sun Jul 26 17:33:37 2009 UTC

# Line 7 | Line 7
7   package jsr166y;
8  
9   import java.util.concurrent.*;
10 < import java.util.concurrent.atomic.*;
10 >
11 > import java.util.concurrent.atomic.AtomicReference;
12   import java.util.concurrent.locks.LockSupport;
12 import sun.misc.Unsafe;
13 import java.lang.reflect.*;
13  
14   /**
15   * A reusable synchronization barrier, similar in functionality to a
# Line 931 | Line 930 | public class Phaser {
930          return p;
931      }
932  
933 <    // Temporary Unsafe mechanics for preliminary release
935 <    private static Unsafe getUnsafe() throws Throwable {
936 <        try {
937 <            return Unsafe.getUnsafe();
938 <        } catch (SecurityException se) {
939 <            try {
940 <                return java.security.AccessController.doPrivileged
941 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
942 <                        public Unsafe run() throws Exception {
943 <                            return getUnsafePrivileged();
944 <                        }});
945 <            } catch (java.security.PrivilegedActionException e) {
946 <                throw e.getCause();
947 <            }
948 <        }
949 <    }
933 >    // Unsafe mechanics
934  
935 <    private static Unsafe getUnsafePrivileged()
936 <            throws NoSuchFieldException, IllegalAccessException {
937 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
954 <        f.setAccessible(true);
955 <        return (Unsafe) f.get(null);
956 <    }
935 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
936 >    private static final long stateOffset =
937 >        objectFieldOffset("state", Phaser.class);
938  
939 <    private static long fieldOffset(String fieldName)
940 <            throws NoSuchFieldException {
960 <        return UNSAFE.objectFieldOffset
961 <            (Phaser.class.getDeclaredField(fieldName));
939 >    private final boolean casState(long cmp, long val) {
940 >        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
941      }
942  
943 <    static final Unsafe UNSAFE;
965 <    static final long stateOffset;
966 <
967 <    static {
943 >    private static long objectFieldOffset(String field, Class<?> klazz) {
944          try {
945 <            UNSAFE = getUnsafe();
946 <            stateOffset = fieldOffset("state");
947 <        } catch (Throwable e) {
948 <            throw new RuntimeException("Could not initialize intrinsics", e);
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 <    final boolean casState(long cmp, long val) {
955 <        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
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
968 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
969 >                        public sun.misc.Unsafe run() throws Exception {
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",
977 >                                           e.getCause());
978 >            }
979 >        }
980      }
981   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines