9 |
|
import java.util.concurrent.*; |
10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.util.concurrent.locks.LockSupport; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
12 |
|
|
13 |
|
/** |
14 |
|
* A reusable synchronization barrier, similar in functionality to a |
929 |
|
return p; |
930 |
|
} |
931 |
|
|
932 |
< |
// Temporary Unsafe mechanics for preliminary release |
933 |
< |
private static Unsafe getUnsafe() throws Throwable { |
932 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
933 |
> |
private static sun.misc.Unsafe getUnsafe() { |
934 |
|
try { |
935 |
< |
return Unsafe.getUnsafe(); |
935 |
> |
return sun.misc.Unsafe.getUnsafe(); |
936 |
|
} catch (SecurityException se) { |
937 |
|
try { |
938 |
|
return java.security.AccessController.doPrivileged |
939 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
940 |
< |
public Unsafe run() throws Exception { |
941 |
< |
return getUnsafePrivileged(); |
939 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
940 |
> |
public sun.misc.Unsafe run() throws Exception { |
941 |
> |
return getUnsafeByReflection(); |
942 |
|
}}); |
943 |
|
} catch (java.security.PrivilegedActionException e) { |
944 |
< |
throw e.getCause(); |
944 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
945 |
> |
e.getCause()); |
946 |
|
} |
947 |
|
} |
948 |
|
} |
949 |
|
|
950 |
< |
private static Unsafe getUnsafePrivileged() |
950 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
951 |
|
throws NoSuchFieldException, IllegalAccessException { |
952 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
952 |
> |
java.lang.reflect.Field f = |
953 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
954 |
|
f.setAccessible(true); |
955 |
< |
return (Unsafe) f.get(null); |
955 |
> |
return (sun.misc.Unsafe) f.get(null); |
956 |
|
} |
957 |
|
|
958 |
< |
private static long fieldOffset(String fieldName) |
959 |
< |
throws NoSuchFieldException { |
960 |
< |
return UNSAFE.objectFieldOffset |
961 |
< |
(Phaser.class.getDeclaredField(fieldName)); |
962 |
< |
} |
963 |
< |
|
964 |
< |
static final Unsafe UNSAFE; |
965 |
< |
static final long stateOffset; |
966 |
< |
|
967 |
< |
static { |
958 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
959 |
|
try { |
960 |
< |
UNSAFE = getUnsafe(); |
961 |
< |
stateOffset = fieldOffset("state"); |
962 |
< |
} catch (Throwable e) { |
963 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
960 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
961 |
> |
} catch (NoSuchFieldException e) { |
962 |
> |
// Convert Exception to Error |
963 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
964 |
> |
error.initCause(e); |
965 |
> |
throw error; |
966 |
|
} |
967 |
|
} |
968 |
|
|
969 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
970 |
+ |
static final long stateOffset = |
971 |
+ |
fieldOffset("state", Phaser.class); |
972 |
+ |
|
973 |
|
final boolean casState(long cmp, long val) { |
974 |
|
return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); |
975 |
|
} |