--- jsr166/src/jsr166y/Phaser.java 2009/07/21 00:15:14 1.14 +++ jsr166/src/jsr166y/Phaser.java 2009/07/25 00:34:00 1.20 @@ -7,10 +7,9 @@ package jsr166y; import java.util.concurrent.*; -import java.util.concurrent.atomic.*; + +import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.LockSupport; -import sun.misc.Unsafe; -import java.lang.reflect.*; /** * A reusable synchronization barrier, similar in functionality to a @@ -175,6 +174,9 @@ import java.lang.reflect.*; * parties result in IllegalStateExceptions. However, you can and * should create tiered phasers to accommodate arbitrarily large sets * of participants. + * + * @since 1.7 + * @author Doug Lea */ public class Phaser { /* @@ -208,15 +210,15 @@ public class Phaser { private static final int phaseMask = 0x7fffffff; private static int unarrivedOf(long s) { - return (int)(s & ushortMask); + return (int) (s & ushortMask); } private static int partiesOf(long s) { - return ((int)s) >>> 16; + return ((int) s) >>> 16; } private static int phaseOf(long s) { - return (int)(s >>> 32); + return (int) (s >>> 32); } private static int arrivedOf(long s) { @@ -224,13 +226,13 @@ public class Phaser { } private static long stateFor(int phase, int parties, int unarrived) { - return ((((long)phase) << 32) | (((long)parties) << 16) | - (long)unarrived); + return ((((long) phase) << 32) | (((long) parties) << 16) | + (long) unarrived); } private static long trippedStateFor(int phase, int parties) { - long lp = (long)parties; - return (((long)phase) << 32) | (lp << 16) | lp; + long lp = (long) parties; + return (((long) phase) << 32) | (lp << 16) | lp; } /** @@ -263,7 +265,7 @@ public class Phaser { private final AtomicReference oddQ = new AtomicReference(); private AtomicReference queueFor(int phase) { - return (phase & 1) == 0? evenQ : oddQ; + return ((phase & 1) == 0) ? evenQ : oddQ; } /** @@ -271,7 +273,7 @@ public class Phaser { * root if necessary. */ private long getReconciledState() { - return parent == null? state : reconcileState(); + return (parent == null) ? state : reconcileState(); } /** @@ -438,7 +440,7 @@ public class Phaser { if (par == null) { // directly trip if (casState (s, - trippedStateFor(onAdvance(phase, parties)? -1 : + trippedStateFor(onAdvance(phase, parties) ? -1 : ((phase + 1) & phaseMask), parties))) { releaseWaiters(phase); break; @@ -499,7 +501,7 @@ public class Phaser { if (unarrived == 0) { if (casState (s, - trippedStateFor(onAdvance(phase, parties)? -1 : + trippedStateFor(onAdvance(phase, parties) ? -1 : ((phase + 1) & phaseMask), parties))) { releaseWaiters(phase); break; @@ -584,7 +586,8 @@ public class Phaser { * @throws InterruptedException if thread interrupted while waiting * @throws TimeoutException if timed out while waiting */ - public int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit) + public int awaitAdvanceInterruptibly(int phase, + long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { if (phase < 0) return phase; @@ -927,49 +930,48 @@ public class Phaser { return p; } - // Temporary Unsafe mechanics for preliminary release - private static Unsafe getUnsafe() throws Throwable { + // Unsafe mechanics for jsr166y 3rd party package. + private static sun.misc.Unsafe getUnsafe() { try { - return Unsafe.getUnsafe(); + return sun.misc.Unsafe.getUnsafe(); } catch (SecurityException se) { try { return java.security.AccessController.doPrivileged - (new java.security.PrivilegedExceptionAction() { - public Unsafe run() throws Exception { - return getUnsafePrivileged(); + (new java.security.PrivilegedExceptionAction() { + public sun.misc.Unsafe run() throws Exception { + return getUnsafeByReflection(); }}); } catch (java.security.PrivilegedActionException e) { - throw e.getCause(); + throw new RuntimeException("Could not initialize intrinsics", + e.getCause()); } } } - private static Unsafe getUnsafePrivileged() + private static sun.misc.Unsafe getUnsafeByReflection() throws NoSuchFieldException, IllegalAccessException { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); + java.lang.reflect.Field f = + sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); - return (Unsafe) f.get(null); - } - - private static long fieldOffset(String fieldName) - throws NoSuchFieldException { - return _unsafe.objectFieldOffset - (Phaser.class.getDeclaredField(fieldName)); + return (sun.misc.Unsafe) f.get(null); } - static final Unsafe _unsafe; - static final long stateOffset; - - static { + private static long fieldOffset(String fieldName, Class klazz) { try { - _unsafe = getUnsafe(); - stateOffset = fieldOffset("state"); - } catch (Throwable e) { - throw new RuntimeException("Could not initialize intrinsics", e); + return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); + } catch (NoSuchFieldException e) { + // Convert Exception to Error + NoSuchFieldError error = new NoSuchFieldError(fieldName); + error.initCause(e); + throw error; } } + private static final sun.misc.Unsafe UNSAFE = getUnsafe(); + static final long stateOffset = + fieldOffset("state", Phaser.class); + final boolean casState(long cmp, long val) { - return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val); + return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); } }