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; |
13 |
|
|
14 |
|
/** |
675 |
|
} |
676 |
|
|
677 |
|
/** |
678 |
< |
* Returns the parent of this phaser, or null if none. |
678 |
> |
* Returns the parent of this phaser, or {@code null} if none. |
679 |
|
* |
680 |
< |
* @return the parent of this phaser, or null if none |
680 |
> |
* @return the parent of this phaser, or {@code null} if none |
681 |
|
*/ |
682 |
|
public Phaser getParent() { |
683 |
|
return parent; |
706 |
|
* Overridable method to perform an action upon phase advance, and |
707 |
|
* to control termination. This method is invoked whenever the |
708 |
|
* barrier is tripped (and thus all other waiting parties are |
709 |
< |
* dormant). If it returns true, then, rather than advance the |
710 |
< |
* phase number, this barrier will be set to a final termination |
711 |
< |
* state, and subsequent calls to {@code isTerminated} will |
712 |
< |
* return true. |
709 |
> |
* dormant). If it returns {@code true}, then, rather than advance |
710 |
> |
* the phase number, this barrier will be set to a final |
711 |
> |
* termination state, and subsequent calls to {@link #isTerminated} |
712 |
> |
* will return true. |
713 |
|
* |
714 |
< |
* <p> The default version returns true when the number of |
714 |
> |
* <p> The default version returns {@code true} when the number of |
715 |
|
* registered parties is zero. Normally, overrides that arrange |
716 |
|
* termination for other reasons should also preserve this |
717 |
|
* property. |
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", |
978 |
|
} |
979 |
|
} |
980 |
|
} |
949 |
– |
|
950 |
– |
private static sun.misc.Unsafe getUnsafeByReflection() |
951 |
– |
throws NoSuchFieldException, IllegalAccessException { |
952 |
– |
java.lang.reflect.Field f = |
953 |
– |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
954 |
– |
f.setAccessible(true); |
955 |
– |
return (sun.misc.Unsafe) f.get(null); |
956 |
– |
} |
957 |
– |
|
958 |
– |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
959 |
– |
try { |
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 |
– |
} |
981 |
|
} |