rt/emul/compact/src/main/java/java/util/concurrent/Phaser.java
changeset 1895 bfaf3300b7ba
parent 1890 212417b74b72
     1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/Phaser.java	Sat Mar 19 10:46:31 2016 +0100
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/Phaser.java	Sat Mar 19 12:51:03 2016 +0100
     1.3 @@ -388,7 +388,7 @@
     1.4                  if (root == this || reconcileState() == s)
     1.5                      throw new IllegalStateException(badArrive(s));
     1.6              }
     1.7 -            else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) {
     1.8 +            else if (compareAndSwapLong(s, s-=adj)) {
     1.9                  if (unarrived == 0) {
    1.10                      long n = s & PARTIES_MASK;  // base of next state
    1.11                      int nextUnarrived = (int)n >>> PARTIES_SHIFT;
    1.12 @@ -401,7 +401,7 @@
    1.13                      else
    1.14                          n |= nextUnarrived;
    1.15                      n |= (long)((phase + 1) & MAX_PHASE) << PHASE_SHIFT;
    1.16 -                    UNSAFE.compareAndSwapLong(this, stateOffset, s, n);
    1.17 +                    compareAndSwapLong(s, n);
    1.18                      releaseWaiters(phase);
    1.19                  }
    1.20                  return phase;
    1.21 @@ -433,14 +433,13 @@
    1.22                  if (parent == null || reconcileState() == s) {
    1.23                      if (unarrived == 0)             // wait out advance
    1.24                          root.internalAwaitAdvance(phase, null);
    1.25 -                    else if (UNSAFE.compareAndSwapLong(this, stateOffset,
    1.26 -                                                       s, s + adj))
    1.27 +                    else if (compareAndSwapLong(                                                       s, s + adj))
    1.28                          break;
    1.29                  }
    1.30              }
    1.31              else if (parent == null) {              // 1st root registration
    1.32                  long next = ((long)phase << PHASE_SHIFT) | adj;
    1.33 -                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
    1.34 +                if (compareAndSwapLong(s, next))
    1.35                      break;
    1.36              }
    1.37              else {
    1.38 @@ -450,8 +449,8 @@
    1.39                          do {                        // force current phase
    1.40                              phase = (int)(root.state >>> PHASE_SHIFT);
    1.41                              // assert phase < 0 || (int)state == EMPTY;
    1.42 -                        } while (!UNSAFE.compareAndSwapLong
    1.43 -                                 (this, stateOffset, state,
    1.44 +                        } while (!compareAndSwapLong
    1.45 +                                 (state,
    1.46                                    ((long)phase << PHASE_SHIFT) | adj));
    1.47                          break;
    1.48                      }
    1.49 @@ -482,8 +481,8 @@
    1.50              // CAS root phase with current parties; possibly trip unarrived
    1.51              while ((phase = (int)(root.state >>> PHASE_SHIFT)) !=
    1.52                     (int)(s >>> PHASE_SHIFT) &&
    1.53 -                   !UNSAFE.compareAndSwapLong
    1.54 -                   (this, stateOffset, s,
    1.55 +                   !compareAndSwapLong
    1.56 +                   (s,
    1.57                      s = (((long)phase << PHASE_SHIFT) |
    1.58                           (s & PARTIES_MASK) |
    1.59                           ((p = (int)s >>> PARTIES_SHIFT) == 0 ? EMPTY :
    1.60 @@ -674,7 +673,7 @@
    1.61                  if (reconcileState() == s)
    1.62                      throw new IllegalStateException(badArrive(s));
    1.63              }
    1.64 -            else if (UNSAFE.compareAndSwapLong(this, stateOffset, s,
    1.65 +            else if (compareAndSwapLong(s,
    1.66                                                 s -= ONE_ARRIVAL)) {
    1.67                  if (unarrived != 0)
    1.68                      return root.internalAwaitAdvance(phase, null);
    1.69 @@ -690,7 +689,7 @@
    1.70                      n |= nextUnarrived;
    1.71                  int nextPhase = (phase + 1) & MAX_PHASE;
    1.72                  n |= (long)nextPhase << PHASE_SHIFT;
    1.73 -                if (!UNSAFE.compareAndSwapLong(this, stateOffset, s, n))
    1.74 +                if (!compareAndSwapLong(s, n))
    1.75                      return (int)(state >>> PHASE_SHIFT); // terminated
    1.76                  releaseWaiters(phase);
    1.77                  return nextPhase;
    1.78 @@ -806,8 +805,7 @@
    1.79          final Phaser root = this.root;
    1.80          long s;
    1.81          while ((s = root.state) >= 0) {
    1.82 -            if (UNSAFE.compareAndSwapLong(root, stateOffset,
    1.83 -                                          s, s | TERMINATION_BIT)) {
    1.84 +            if (compareAndSwapLong(                                          s, s | TERMINATION_BIT)) {
    1.85                  // signal all threads
    1.86                  releaseWaiters(0);
    1.87                  releaseWaiters(1);
    1.88 @@ -999,7 +997,7 @@
    1.89      }
    1.90  
    1.91      /** The number of CPUs, for spin control */
    1.92 -    private static final int NCPU = Runtime.getRuntime().availableProcessors();
    1.93 +    private static final int NCPU = 1;
    1.94  
    1.95      /**
    1.96       * The number of times to spin before blocking while waiting for
    1.97 @@ -1072,6 +1070,14 @@
    1.98          return p;
    1.99      }
   1.100  
   1.101 +    private boolean compareAndSwapLong(long s, long l) {
   1.102 +        if (this.state == s) {
   1.103 +            this.state = l;
   1.104 +            return true;
   1.105 +        }
   1.106 +        return false;
   1.107 +    }
   1.108 +
   1.109      /**
   1.110       * Wait nodes for Treiber stack representing wait queue
   1.111       */
   1.112 @@ -1134,19 +1140,4 @@
   1.113              return isReleasable();
   1.114          }
   1.115      }
   1.116 -
   1.117 -    // Unsafe mechanics
   1.118 -
   1.119 -    private static final sun.misc.Unsafe UNSAFE;
   1.120 -    private static final long stateOffset;
   1.121 -    static {
   1.122 -        try {
   1.123 -            UNSAFE = sun.misc.Unsafe.getUnsafe();
   1.124 -            Class k = Phaser.class;
   1.125 -            stateOffset = UNSAFE.objectFieldOffset
   1.126 -                (k.getDeclaredField("state"));
   1.127 -        } catch (Exception e) {
   1.128 -            throw new Error(e);
   1.129 -        }
   1.130 -    }
   1.131  }