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.14 by jsr166, Tue Jul 21 00:15:14 2009 UTC vs.
Revision 1.23 by jsr166, Mon Jul 27 20:57:44 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 672 | Line 675 | public class Phaser {
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;
# Line 703 | Line 706 | public class Phaser {
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.
# Line 927 | Line 930 | public class Phaser {
930          return p;
931      }
932  
933 <    // Temporary Unsafe mechanics for preliminary release
931 <    private static Unsafe getUnsafe() throws Throwable {
932 <        try {
933 <            return Unsafe.getUnsafe();
934 <        } catch (SecurityException se) {
935 <            try {
936 <                return java.security.AccessController.doPrivileged
937 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
938 <                        public Unsafe run() throws Exception {
939 <                            return getUnsafePrivileged();
940 <                        }});
941 <            } catch (java.security.PrivilegedActionException e) {
942 <                throw e.getCause();
943 <            }
944 <        }
945 <    }
933 >    // Unsafe mechanics
934  
935 <    private static Unsafe getUnsafePrivileged()
936 <            throws NoSuchFieldException, IllegalAccessException {
937 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
950 <        f.setAccessible(true);
951 <        return (Unsafe) f.get(null);
952 <    }
935 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
936 >    private static final long stateOffset =
937 >        objectFieldOffset("state", Phaser.class);
938  
939 <    private static long fieldOffset(String fieldName)
940 <            throws NoSuchFieldException {
956 <        return _unsafe.objectFieldOffset
957 <            (Phaser.class.getDeclaredField(fieldName));
939 >    private final boolean casState(long cmp, long val) {
940 >        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
941      }
942  
943 <    static final Unsafe _unsafe;
961 <    static final long stateOffset;
962 <
963 <    static {
943 >    private static long objectFieldOffset(String field, Class<?> klazz) {
944          try {
945 <            _unsafe = getUnsafe();
946 <            stateOffset = fieldOffset("state");
947 <        } catch (Throwable e) {
948 <            throw new RuntimeException("Could not initialize intrinsics", e);
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 <    final boolean casState(long cmp, long val) {
955 <        return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val);
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
968 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
969 >                        public sun.misc.Unsafe run() throws Exception {
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",
977 >                                           e.getCause());
978 >            }
979 >        }
980      }
981   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines