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 |
930 |
|
return p; |
931 |
|
} |
932 |
|
|
933 |
< |
// Temporary Unsafe mechanics for preliminary release |
934 |
< |
private static Unsafe getUnsafe() throws Throwable { |
933 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
934 |
> |
private static sun.misc.Unsafe getUnsafe() { |
935 |
|
try { |
936 |
< |
return Unsafe.getUnsafe(); |
936 |
> |
return sun.misc.Unsafe.getUnsafe(); |
937 |
|
} catch (SecurityException se) { |
938 |
|
try { |
939 |
|
return java.security.AccessController.doPrivileged |
940 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
941 |
< |
public Unsafe run() throws Exception { |
942 |
< |
return getUnsafePrivileged(); |
940 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
941 |
> |
public sun.misc.Unsafe run() throws Exception { |
942 |
> |
return getUnsafeByReflection(); |
943 |
|
}}); |
944 |
|
} catch (java.security.PrivilegedActionException e) { |
945 |
< |
throw e.getCause(); |
945 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
946 |
> |
e.getCause()); |
947 |
|
} |
948 |
|
} |
949 |
|
} |
950 |
|
|
951 |
< |
private static Unsafe getUnsafePrivileged() |
951 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
952 |
|
throws NoSuchFieldException, IllegalAccessException { |
953 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
953 |
> |
java.lang.reflect.Field f = |
954 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
955 |
|
f.setAccessible(true); |
956 |
< |
return (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)); |
956 |
> |
return (sun.misc.Unsafe) f.get(null); |
957 |
|
} |
958 |
|
|
959 |
< |
static final Unsafe UNSAFE; |
965 |
< |
static final long stateOffset; |
966 |
< |
|
967 |
< |
static { |
959 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
960 |
|
try { |
961 |
< |
UNSAFE = getUnsafe(); |
962 |
< |
stateOffset = fieldOffset("state"); |
963 |
< |
} catch (Throwable e) { |
964 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
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 |
+ |
static final long stateOffset = |
972 |
+ |
fieldOffset("state", Phaser.class); |
973 |
+ |
|
974 |
|
final boolean casState(long cmp, long val) { |
975 |
|
return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); |
976 |
|
} |