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

Comparing jsr166/src/main/java/util/concurrent/Phaser.java (file contents):
Revision 1.52 by jsr166, Wed Dec 8 08:11:26 2010 UTC vs.
Revision 1.53 by dl, Wed Dec 8 15:27:20 2010 UTC

# Line 260 | Line 260 | public class Phaser {
260       * parent.
261       *
262       * The phase of a subphaser is allowed to lag that of its
263 <     * ancestors until it is actually accessed.  Method reconcileState
264 <     * is usually attempted only only when the number of unarrived
265 <     * parties appears to be zero, which indicates a potential lag in
266 <     * updating phase after the root advanced.
263 >     * ancestors until it is actually accessed -- see method
264 >     * reconcileState.
265       */
266      private volatile long state;
267  
# Line 437 | Line 435 | public class Phaser {
435  
436      /**
437       * Resolves lagged phase propagation from root if necessary.
438 +     * Reconciliation normally occurs when root has advanced but
439 +     * subphasers have not yet done so, in which case they must finish
440 +     * their own advance by setting unarrived to parties (or if
441 +     * parties is zero, resetting to unregistered EMPTY state).
442 +     * However, this method may also be called when "floating"
443 +     * subphasers with possibly some unarrived parties are merely
444 +     * catching up to current phase, in which case counts are
445 +     * unaffected.
446 +     *
447 +     * @return reconciled state
448       */
449      private long reconcileState() {
450          final Phaser root = this.root;
451          long s = state;
452          if (root != this) {
453 <            for (long rs; ((rs = root.state) ^ s) >>> PHASE_SHIFT != 0;) {
454 <                // assert rs < 0 || (s != state) || unarrivedOf(s) == 0;
455 <                long lp = s & PARTIES_MASK;
456 <                long n = (rs & PHASE_MASK) | lp;
457 <                if (rs >= 0)
458 <                    n |= (lp == 0L) ? EMPTY : (lp >>> PARTIES_SHIFT);
459 <                if (s == (s = state) &&
460 <                    UNSAFE.compareAndSwapLong(this, stateOffset, s, n))
461 <                    return n;
462 <            }
453 >            int phase, u, p;
454 >            // CAS root phase with current parties; possibly trip unarrived
455 >            while ((phase = (int)(root.state >>> PHASE_SHIFT)) !=
456 >                   (int)(s >>> PHASE_SHIFT) &&
457 >                   !UNSAFE.compareAndSwapLong
458 >                   (this, stateOffset, s,
459 >                    s = ((((long) phase) << PHASE_SHIFT) | (s & PARTIES_MASK) |
460 >                         ((p = (int)s >>> PARTIES_SHIFT) == 0 ? EMPTY :
461 >                          (u = (int)s & UNARRIVED_MASK) == 0 ? p : u))))
462 >                s = state;
463          }
464          return s;
465      }
# Line 769 | Line 777 | public class Phaser {
777          final Phaser root = this.root;
778          long s;
779          while ((s = root.state) >= 0) {
780 <            long next = (s & ~((long)UNARRIVED_MASK)) | TERMINATION_BIT;
781 <            if (UNSAFE.compareAndSwapLong(root, stateOffset, s, next)) {
780 >            if (UNSAFE.compareAndSwapLong(root, stateOffset,
781 >                                          s, s | TERMINATION_BIT)) {
782                  // signal all threads
783                  releaseWaiters(0);
784                  releaseWaiters(1);
# Line 803 | Line 811 | public class Phaser {
811  
812      /**
813       * Returns the number of registered parties that have arrived at
814 <     * the current phase of this phaser.
814 >     * the current phase of this phaser. If this phaser has terminated,
815 >     * the returned value is meaningless and arbitrary.
816       *
817       * @return the number of arrived parties
818       */
# Line 813 | Line 822 | public class Phaser {
822  
823      /**
824       * Returns the number of registered parties that have not yet
825 <     * arrived at the current phase of this phaser.
825 >     * arrived at the current phase of this phaser. If this phaser has
826 >     * terminated, the returned value is meaningless and arbitrary.
827       *
828       * @return the number of unarrived parties
829       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines