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 |
209 |
|
private static final int phaseMask = 0x7fffffff; |
210 |
|
|
211 |
|
private static int unarrivedOf(long s) { |
212 |
< |
return (int)(s & ushortMask); |
212 |
> |
return (int) (s & ushortMask); |
213 |
|
} |
214 |
|
|
215 |
|
private static int partiesOf(long s) { |
216 |
< |
return ((int)s) >>> 16; |
216 |
> |
return ((int) s) >>> 16; |
217 |
|
} |
218 |
|
|
219 |
|
private static int phaseOf(long s) { |
220 |
< |
return (int)(s >>> 32); |
220 |
> |
return (int) (s >>> 32); |
221 |
|
} |
222 |
|
|
223 |
|
private static int arrivedOf(long s) { |
225 |
|
} |
226 |
|
|
227 |
|
private static long stateFor(int phase, int parties, int unarrived) { |
228 |
< |
return ((((long)phase) << 32) | (((long)parties) << 16) | |
229 |
< |
(long)unarrived); |
228 |
> |
return ((((long) phase) << 32) | (((long) parties) << 16) | |
229 |
> |
(long) unarrived); |
230 |
|
} |
231 |
|
|
232 |
|
private static long trippedStateFor(int phase, int parties) { |
233 |
< |
long lp = (long)parties; |
234 |
< |
return (((long)phase) << 32) | (lp << 16) | lp; |
233 |
> |
long lp = (long) parties; |
234 |
> |
return (((long) phase) << 32) | (lp << 16) | lp; |
235 |
|
} |
236 |
|
|
237 |
|
/** |
264 |
|
private final AtomicReference<QNode> oddQ = new AtomicReference<QNode>(); |
265 |
|
|
266 |
|
private AtomicReference<QNode> queueFor(int phase) { |
267 |
< |
return (phase & 1) == 0? evenQ : oddQ; |
267 |
> |
return ((phase & 1) == 0) ? evenQ : oddQ; |
268 |
|
} |
269 |
|
|
270 |
|
/** |
272 |
|
* root if necessary. |
273 |
|
*/ |
274 |
|
private long getReconciledState() { |
275 |
< |
return parent == null? state : reconcileState(); |
275 |
> |
return (parent == null) ? state : reconcileState(); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
439 |
|
if (par == null) { // directly trip |
440 |
|
if (casState |
441 |
|
(s, |
442 |
< |
trippedStateFor(onAdvance(phase, parties)? -1 : |
442 |
> |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
443 |
|
((phase + 1) & phaseMask), parties))) { |
444 |
|
releaseWaiters(phase); |
445 |
|
break; |
500 |
|
if (unarrived == 0) { |
501 |
|
if (casState |
502 |
|
(s, |
503 |
< |
trippedStateFor(onAdvance(phase, parties)? -1 : |
503 |
> |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
504 |
|
((phase + 1) & phaseMask), parties))) { |
505 |
|
releaseWaiters(phase); |
506 |
|
break; |
585 |
|
* @throws InterruptedException if thread interrupted while waiting |
586 |
|
* @throws TimeoutException if timed out while waiting |
587 |
|
*/ |
588 |
< |
public int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit) |
588 |
> |
public int awaitAdvanceInterruptibly(int phase, |
589 |
> |
long timeout, TimeUnit unit) |
590 |
|
throws InterruptedException, TimeoutException { |
591 |
|
if (phase < 0) |
592 |
|
return phase; |
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) |
958 |
< |
throws NoSuchFieldException { |
959 |
< |
return UNSAFE.objectFieldOffset |
960 |
< |
(Phaser.class.getDeclaredField(fieldName)); |
961 |
< |
} |
962 |
< |
|
963 |
< |
static final Unsafe UNSAFE; |
964 |
< |
static final long stateOffset; |
965 |
< |
|
966 |
< |
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 |
|
} |