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.77 by jsr166, Sun Jan 4 01:06:15 2015 UTC vs.
Revision 1.78 by jsr166, Sun Jan 4 09:15:11 2015 UTC

# Line 358 | Line 358 | public class Phaser {
358              int unarrived = (counts == EMPTY) ? 0 : (counts & UNARRIVED_MASK);
359              if (unarrived <= 0)
360                  throw new IllegalStateException(badArrive(s));
361 <            if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adjust)) {
361 >            if (U.compareAndSwapLong(this, STATE, s, s-=adjust)) {
362                  if (unarrived == 1) {
363                      long n = s & PARTIES_MASK;  // base of next state
364                      int nextUnarrived = (int)n >>> PARTIES_SHIFT;
# Line 371 | Line 371 | public class Phaser {
371                              n |= nextUnarrived;
372                          int nextPhase = (phase + 1) & MAX_PHASE;
373                          n |= (long)nextPhase << PHASE_SHIFT;
374 <                        UNSAFE.compareAndSwapLong(this, stateOffset, s, n);
374 >                        U.compareAndSwapLong(this, STATE, s, n);
375                          releaseWaiters(phase);
376                      }
377                      else if (nextUnarrived == 0) { // propagate deregistration
378                          phase = parent.doArrive(ONE_DEREGISTER);
379 <                        UNSAFE.compareAndSwapLong(this, stateOffset,
380 <                                                  s, s | EMPTY);
379 >                        U.compareAndSwapLong(this, STATE, s, s | EMPTY);
380                      }
381                      else
382                          phase = parent.doArrive(ONE_ARRIVAL);
# Line 412 | Line 411 | public class Phaser {
411                  if (parent == null || reconcileState() == s) {
412                      if (unarrived == 0)             // wait out advance
413                          root.internalAwaitAdvance(phase, null);
414 <                    else if (UNSAFE.compareAndSwapLong(this, stateOffset,
416 <                                                       s, s + adjust))
414 >                    else if (U.compareAndSwapLong(this, STATE, s, s + adjust))
415                          break;
416                  }
417              }
418              else if (parent == null) {              // 1st root registration
419                  long next = ((long)phase << PHASE_SHIFT) | adjust;
420 <                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
420 >                if (U.compareAndSwapLong(this, STATE, s, next))
421                      break;
422              }
423              else {
# Line 431 | Line 429 | public class Phaser {
429                          // finish registration whenever parent registration
430                          // succeeded, even when racing with termination,
431                          // since these are part of the same "transaction".
432 <                        while (!UNSAFE.compareAndSwapLong
433 <                               (this, stateOffset, s,
432 >                        while (!U.compareAndSwapLong
433 >                               (this, STATE, s,
434                                  ((long)phase << PHASE_SHIFT) | adjust)) {
435                              s = state;
436                              phase = (int)(root.state >>> PHASE_SHIFT);
# Line 463 | Line 461 | public class Phaser {
461              // CAS to root phase with current parties, tripping unarrived
462              while ((phase = (int)(root.state >>> PHASE_SHIFT)) !=
463                     (int)(s >>> PHASE_SHIFT) &&
464 <                   !UNSAFE.compareAndSwapLong
465 <                   (this, stateOffset, s,
464 >                   !U.compareAndSwapLong
465 >                   (this, STATE, s,
466                      s = (((long)phase << PHASE_SHIFT) |
467                           ((phase < 0) ? (s & COUNTS_MASK) :
468                            (((p = (int)s >>> PARTIES_SHIFT) == 0) ? EMPTY :
# Line 653 | Line 651 | public class Phaser {
651              int unarrived = (counts == EMPTY) ? 0 : (counts & UNARRIVED_MASK);
652              if (unarrived <= 0)
653                  throw new IllegalStateException(badArrive(s));
654 <            if (UNSAFE.compareAndSwapLong(this, stateOffset, s,
657 <                                          s -= ONE_ARRIVAL)) {
654 >            if (U.compareAndSwapLong(this, STATE, s, s -= ONE_ARRIVAL)) {
655                  if (unarrived > 1)
656                      return root.internalAwaitAdvance(phase, null);
657                  if (root != this)
# Line 669 | Line 666 | public class Phaser {
666                      n |= nextUnarrived;
667                  int nextPhase = (phase + 1) & MAX_PHASE;
668                  n |= (long)nextPhase << PHASE_SHIFT;
669 <                if (!UNSAFE.compareAndSwapLong(this, stateOffset, s, n))
669 >                if (!U.compareAndSwapLong(this, STATE, s, n))
670                      return (int)(state >>> PHASE_SHIFT); // terminated
671                  releaseWaiters(phase);
672                  return nextPhase;
# Line 785 | Line 782 | public class Phaser {
782          final Phaser root = this.root;
783          long s;
784          while ((s = root.state) >= 0) {
785 <            if (UNSAFE.compareAndSwapLong(root, stateOffset,
789 <                                          s, s | TERMINATION_BIT)) {
785 >            if (U.compareAndSwapLong(root, STATE, s, s | TERMINATION_BIT)) {
786                  // signal all threads
787                  releaseWaiters(0); // Waiters on evenQ
788                  releaseWaiters(1); // Waiters on oddQ
# Line 1115 | Line 1111 | public class Phaser {
1111  
1112      // Unsafe mechanics
1113  
1114 <    private static final sun.misc.Unsafe UNSAFE;
1115 <    private static final long stateOffset;
1114 >    private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
1115 >    private static final long STATE;
1116      static {
1117          try {
1118 <            UNSAFE = sun.misc.Unsafe.getUnsafe();
1119 <            Class<?> k = Phaser.class;
1124 <            stateOffset = UNSAFE.objectFieldOffset
1125 <                (k.getDeclaredField("state"));
1118 >            STATE = U.objectFieldOffset
1119 >                (Phaser.class.getDeclaredField("state"));
1120          } catch (ReflectiveOperationException e) {
1121              throw new Error(e);
1122          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines