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.72 by dl, Mon May 16 11:41:14 2011 UTC vs.
Revision 1.73 by jsr166, Wed May 25 16:08:03 2011 UTC

# Line 237 | Line 237 | public class Phaser {
237       */
238  
239      /**
240 <     * Primary state representation, holding four fields:
240 >     * Primary state representation, holding four bit-fields:
241       *
242 <     * * unarrived -- the number of parties yet to hit barrier (bits  0-15)
243 <     * * parties -- the number of parties to wait              (bits 16-31)
244 <     * * phase -- the generation of the barrier                (bits 32-62)
245 <     * * terminated -- set if barrier is terminated            (bit  63 / sign)
242 >     * unarrived  -- the number of parties yet to hit barrier (bits  0-15)
243 >     * parties    -- the number of parties to wait            (bits 16-31)
244 >     * phase      -- the generation of the barrier            (bits 32-62)
245 >     * terminated -- set if barrier is terminated             (bit  63 / sign)
246       *
247       * Except that a phaser with no registered parties is
248 <     * distinguished with the otherwise illegal state of having zero
248 >     * distinguished by the otherwise illegal state of having zero
249       * parties and one unarrived parties (encoded as EMPTY below).
250       *
251       * To efficiently maintain atomicity, these values are packed into
# Line 266 | Line 266 | public class Phaser {
266      private volatile long state;
267  
268      private static final int  MAX_PARTIES     = 0xffff;
269 <    private static final int  MAX_PHASE       = 0x7fffffff;
269 >    private static final int  MAX_PHASE       = Integer.MAX_VALUE;
270      private static final int  PARTIES_SHIFT   = 16;
271      private static final int  PHASE_SHIFT     = 32;
272    private static final long PHASE_MASK      = -1L << PHASE_SHIFT;
272      private static final int  UNARRIVED_MASK  = 0xffff;      // to mask ints
273      private static final long PARTIES_MASK    = 0xffff0000L; // to mask longs
274      private static final long TERMINATION_BIT = 1L << 63;
# Line 291 | Line 290 | public class Phaser {
290      }
291  
292      private static int phaseOf(long s) {
293 <        return (int) (s >>> PHASE_SHIFT);
293 >        return (int)(s >>> PHASE_SHIFT);
294      }
295  
296      private static int arrivedOf(long s) {
# Line 363 | Line 362 | public class Phaser {
362              else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) {
363                  if (unarrived == 0) {
364                      long n = s & PARTIES_MASK;  // base of next state
365 <                    int nextUnarrived = ((int)n) >>> PARTIES_SHIFT;
365 >                    int nextUnarrived = (int)n >>> PARTIES_SHIFT;
366                      if (root != this)
367                          return parent.doArrive(nextUnarrived == 0);
368                      if (onAdvance(phase, nextUnarrived))
# Line 372 | Line 371 | public class Phaser {
371                          n |= EMPTY;
372                      else
373                          n |= nextUnarrived;
374 <                    n |= ((long)((phase + 1) & MAX_PHASE)) << PHASE_SHIFT;
374 >                    n |= (long)((phase + 1) & MAX_PHASE) << PHASE_SHIFT;
375                      UNSAFE.compareAndSwapLong(this, stateOffset, s, n);
376                      releaseWaiters(phase);
377                  }
# Line 390 | Line 389 | public class Phaser {
389      private int doRegister(int registrations) {
390          // adjustment to state
391          long adj = ((long)registrations << PARTIES_SHIFT) | registrations;
392 <        Phaser par = parent;
392 >        final Phaser parent = this.parent;
393          int phase;
394          for (;;) {
395              long s = state;
# Line 402 | Line 401 | public class Phaser {
401              else if ((phase = (int)(s >>> PHASE_SHIFT)) < 0)
402                  break;
403              else if (counts != EMPTY) {             // not 1st registration
404 <                if (par == null || reconcileState() == s) {
404 >                if (parent == null || reconcileState() == s) {
405                      if (unarrived == 0)             // wait out advance
406                          root.internalAwaitAdvance(phase, null);
407                      else if (UNSAFE.compareAndSwapLong(this, stateOffset,
# Line 410 | Line 409 | public class Phaser {
409                          break;
410                  }
411              }
412 <            else if (par == null) {                 // 1st root registration
413 <                long next = (((long) phase) << PHASE_SHIFT) | adj;
412 >            else if (parent == null) {              // 1st root registration
413 >                long next = ((long)phase << PHASE_SHIFT) | adj;
414                  if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
415                      break;
416              }
417              else {
418                  synchronized (this) {               // 1st sub registration
419                      if (state == s) {               // recheck under lock
420 <                        par.doRegister(1);
420 >                        parent.doRegister(1);
421                          do {                        // force current phase
422                              phase = (int)(root.state >>> PHASE_SHIFT);
423                              // assert phase < 0 || (int)state == EMPTY;
424                          } while (!UNSAFE.compareAndSwapLong
425                                   (this, stateOffset, state,
426 <                                  (((long) phase) << PHASE_SHIFT) | adj));
426 >                                  ((long)phase << PHASE_SHIFT) | adj));
427                          break;
428                      }
429                  }
# Line 456 | Line 455 | public class Phaser {
455                     (int)(s >>> PHASE_SHIFT) &&
456                     !UNSAFE.compareAndSwapLong
457                     (this, stateOffset, s,
458 <                    s = ((((long) phase) << PHASE_SHIFT) | (s & PARTIES_MASK) |
458 >                    s = (((long)phase << PHASE_SHIFT) |
459 >                         (s & PARTIES_MASK) |
460                           ((p = (int)s >>> PARTIES_SHIFT) == 0 ? EMPTY :
461                            (u = (int)s & UNARRIVED_MASK) == 0 ? p : u))))
462                  s = state;
# Line 525 | Line 525 | public class Phaser {
525              this.evenQ = new AtomicReference<QNode>();
526              this.oddQ = new AtomicReference<QNode>();
527          }
528 <        this.state = (parties == 0) ? (long) EMPTY :
529 <            ((((long) phase) << PHASE_SHIFT) |
530 <             (((long) parties) << PARTIES_SHIFT) |
531 <             ((long) parties));
528 >        this.state = (parties == 0) ? (long)EMPTY :
529 >            ((long)phase << PHASE_SHIFT) |
530 >            ((long)parties << PARTIES_SHIFT) |
531 >            ((long)parties);
532      }
533  
534      /**
# Line 652 | Line 652 | public class Phaser {
652                  if (root != this)
653                      return parent.arriveAndAwaitAdvance();
654                  long n = s & PARTIES_MASK;  // base of next state
655 <                int nextUnarrived = ((int)n) >>> PARTIES_SHIFT;
655 >                int nextUnarrived = (int)n >>> PARTIES_SHIFT;
656                  if (onAdvance(phase, nextUnarrived))
657                      n |= TERMINATION_BIT;
658                  else if (nextUnarrived == 0)
# Line 1108 | Line 1108 | public class Phaser {
1108  
1109      // Unsafe mechanics
1110  
1111 <    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1112 <    private static final long stateOffset =
1113 <        objectFieldOffset("state", Phaser.class);
1114 <
1115 <    private static long objectFieldOffset(String field, Class<?> klazz) {
1111 >    private static final sun.misc.Unsafe UNSAFE;
1112 >    private static final long stateOffset;
1113 >    static {
1114          try {
1115 <            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1116 <        } catch (NoSuchFieldException e) {
1117 <            // Convert Exception to corresponding Error
1118 <            NoSuchFieldError error = new NoSuchFieldError(field);
1119 <            error.initCause(e);
1120 <            throw error;
1115 >            UNSAFE = getUnsafe();
1116 >            Class k = Phaser.class;
1117 >            stateOffset = UNSAFE.objectFieldOffset
1118 >                (k.getDeclaredField("state"));
1119 >        } catch (Exception e) {
1120 >            throw new Error(e);
1121          }
1122      }
1123  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines