ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/Phaser.java
(Generate patch)

Comparing jsr166/src/jsr166y/Phaser.java (file contents):
Revision 1.15 by jsr166, Tue Jul 21 18:11:44 2009 UTC vs.
Revision 1.20 by jsr166, Sat Jul 25 00:34:00 2009 UTC

# Line 7 | Line 7
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
# Line 175 | Line 174 | import java.lang.reflect.*;
174   * parties result in IllegalStateExceptions. However, you can and
175   * should create tiered phasers to accommodate arbitrarily large sets
176   * of participants.
177 + *
178 + * @since 1.7
179 + * @author Doug Lea
180   */
181   public class Phaser {
182      /*
# Line 208 | Line 210 | public class Phaser {
210      private static final int phaseMask  = 0x7fffffff;
211  
212      private static int unarrivedOf(long s) {
213 <        return (int)(s & ushortMask);
213 >        return (int) (s & ushortMask);
214      }
215  
216      private static int partiesOf(long s) {
217 <        return ((int)s) >>> 16;
217 >        return ((int) s) >>> 16;
218      }
219  
220      private static int phaseOf(long s) {
221 <        return (int)(s >>> 32);
221 >        return (int) (s >>> 32);
222      }
223  
224      private static int arrivedOf(long s) {
# Line 224 | Line 226 | public class Phaser {
226      }
227  
228      private static long stateFor(int phase, int parties, int unarrived) {
229 <        return ((((long)phase) << 32) | (((long)parties) << 16) |
230 <                (long)unarrived);
229 >        return ((((long) phase) << 32) | (((long) parties) << 16) |
230 >                (long) unarrived);
231      }
232  
233      private static long trippedStateFor(int phase, int parties) {
234 <        long lp = (long)parties;
235 <        return (((long)phase) << 32) | (lp << 16) | lp;
234 >        long lp = (long) parties;
235 >        return (((long) phase) << 32) | (lp << 16) | lp;
236      }
237  
238      /**
# Line 263 | Line 265 | public class Phaser {
265      private final AtomicReference<QNode> oddQ  = new AtomicReference<QNode>();
266  
267      private AtomicReference<QNode> queueFor(int phase) {
268 <        return (phase & 1) == 0? evenQ : oddQ;
268 >        return ((phase & 1) == 0) ? evenQ : oddQ;
269      }
270  
271      /**
# Line 271 | Line 273 | public class Phaser {
273       * root if necessary.
274       */
275      private long getReconciledState() {
276 <        return parent == null? state : reconcileState();
276 >        return (parent == null) ? state : reconcileState();
277      }
278  
279      /**
# Line 438 | Line 440 | public class Phaser {
440                  if (par == null) {      // directly trip
441                      if (casState
442                          (s,
443 <                         trippedStateFor(onAdvance(phase, parties)? -1 :
443 >                         trippedStateFor(onAdvance(phase, parties) ? -1 :
444                                           ((phase + 1) & phaseMask), parties))) {
445                          releaseWaiters(phase);
446                          break;
# Line 499 | Line 501 | public class Phaser {
501                  if (unarrived == 0) {
502                      if (casState
503                          (s,
504 <                         trippedStateFor(onAdvance(phase, parties)? -1 :
504 >                         trippedStateFor(onAdvance(phase, parties) ? -1 :
505                                           ((phase + 1) & phaseMask), parties))) {
506                          releaseWaiters(phase);
507                          break;
# Line 584 | Line 586 | public class Phaser {
586       * @throws InterruptedException if thread interrupted while waiting
587       * @throws TimeoutException if timed out while waiting
588       */
589 <    public int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit)
589 >    public int awaitAdvanceInterruptibly(int phase,
590 >                                         long timeout, TimeUnit unit)
591          throws InterruptedException, TimeoutException {
592          if (phase < 0)
593              return phase;
# Line 927 | Line 930 | public class Phaser {
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);
952 <    }
953 <
954 <    private static long fieldOffset(String fieldName)
955 <            throws NoSuchFieldException {
956 <        return UNSAFE.objectFieldOffset
957 <            (Phaser.class.getDeclaredField(fieldName));
956 >        return (sun.misc.Unsafe) f.get(null);
957      }
958  
959 <    static final Unsafe UNSAFE;
961 <    static final long stateOffset;
962 <
963 <    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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines