rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicLongArray.java
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
     1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicLongArray.java	Thu Oct 03 15:40:35 2013 +0200
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicLongArray.java	Fri Oct 04 10:52:01 2013 +0200
     1.3 @@ -34,8 +34,6 @@
     1.4   */
     1.5  
     1.6  package java.util.concurrent.atomic;
     1.7 -import sun.misc.Unsafe;
     1.8 -import java.util.*;
     1.9  
    1.10  /**
    1.11   * A {@code long} array in which elements may be updated atomically.
    1.12 @@ -47,29 +45,8 @@
    1.13  public class AtomicLongArray implements java.io.Serializable {
    1.14      private static final long serialVersionUID = -2308431214976778248L;
    1.15  
    1.16 -    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.17 -    private static final int base = unsafe.arrayBaseOffset(long[].class);
    1.18 -    private static final int shift;
    1.19      private final long[] array;
    1.20  
    1.21 -    static {
    1.22 -        int scale = unsafe.arrayIndexScale(long[].class);
    1.23 -        if ((scale & (scale - 1)) != 0)
    1.24 -            throw new Error("data type scale not a power of two");
    1.25 -        shift = 31 - Integer.numberOfLeadingZeros(scale);
    1.26 -    }
    1.27 -
    1.28 -    private long checkedByteOffset(int i) {
    1.29 -        if (i < 0 || i >= array.length)
    1.30 -            throw new IndexOutOfBoundsException("index " + i);
    1.31 -
    1.32 -        return byteOffset(i);
    1.33 -    }
    1.34 -
    1.35 -    private static long byteOffset(int i) {
    1.36 -        return ((long) i << shift) + base;
    1.37 -    }
    1.38 -
    1.39      /**
    1.40       * Creates a new AtomicLongArray of the given length, with all
    1.41       * elements initially zero.
    1.42 @@ -108,11 +85,7 @@
    1.43       * @return the current value
    1.44       */
    1.45      public final long get(int i) {
    1.46 -        return getRaw(checkedByteOffset(i));
    1.47 -    }
    1.48 -
    1.49 -    private long getRaw(long offset) {
    1.50 -        return unsafe.getLongVolatile(array, offset);
    1.51 +        return array[i];
    1.52      }
    1.53  
    1.54      /**
    1.55 @@ -122,7 +95,7 @@
    1.56       * @param newValue the new value
    1.57       */
    1.58      public final void set(int i, long newValue) {
    1.59 -        unsafe.putLongVolatile(array, checkedByteOffset(i), newValue);
    1.60 +        array[i] = newValue;
    1.61      }
    1.62  
    1.63      /**
    1.64 @@ -133,7 +106,7 @@
    1.65       * @since 1.6
    1.66       */
    1.67      public final void lazySet(int i, long newValue) {
    1.68 -        unsafe.putOrderedLong(array, checkedByteOffset(i), newValue);
    1.69 +        array[i] = newValue;
    1.70      }
    1.71  
    1.72  
    1.73 @@ -146,12 +119,9 @@
    1.74       * @return the previous value
    1.75       */
    1.76      public final long getAndSet(int i, long newValue) {
    1.77 -        long offset = checkedByteOffset(i);
    1.78 -        while (true) {
    1.79 -            long current = getRaw(offset);
    1.80 -            if (compareAndSetRaw(offset, current, newValue))
    1.81 -                return current;
    1.82 -        }
    1.83 +        long v = array[i];
    1.84 +        array[i] = newValue;
    1.85 +        return v;
    1.86      }
    1.87  
    1.88      /**
    1.89 @@ -165,11 +135,12 @@
    1.90       * the actual value was not equal to the expected value.
    1.91       */
    1.92      public final boolean compareAndSet(int i, long expect, long update) {
    1.93 -        return compareAndSetRaw(checkedByteOffset(i), expect, update);
    1.94 -    }
    1.95 -
    1.96 -    private boolean compareAndSetRaw(long offset, long expect, long update) {
    1.97 -        return unsafe.compareAndSwapLong(array, offset, expect, update);
    1.98 +        if (array[i] == expect) {
    1.99 +            array[i] = update;
   1.100 +            return true;
   1.101 +        } else {
   1.102 +            return false;
   1.103 +        }
   1.104      }
   1.105  
   1.106      /**
   1.107 @@ -217,12 +188,9 @@
   1.108       * @return the previous value
   1.109       */
   1.110      public final long getAndAdd(int i, long delta) {
   1.111 -        long offset = checkedByteOffset(i);
   1.112 -        while (true) {
   1.113 -            long current = getRaw(offset);
   1.114 -            if (compareAndSetRaw(offset, current, current + delta))
   1.115 -                return current;
   1.116 -        }
   1.117 +        long v = array[i];
   1.118 +        array[i] += delta;
   1.119 +        return v;
   1.120      }
   1.121  
   1.122      /**
   1.123 @@ -253,13 +221,8 @@
   1.124       * @return the updated value
   1.125       */
   1.126      public long addAndGet(int i, long delta) {
   1.127 -        long offset = checkedByteOffset(i);
   1.128 -        while (true) {
   1.129 -            long current = getRaw(offset);
   1.130 -            long next = current + delta;
   1.131 -            if (compareAndSetRaw(offset, current, next))
   1.132 -                return next;
   1.133 -        }
   1.134 +        array[i] += delta;
   1.135 +        return array[i];
   1.136      }
   1.137  
   1.138      /**
   1.139 @@ -274,7 +237,7 @@
   1.140          StringBuilder b = new StringBuilder();
   1.141          b.append('[');
   1.142          for (int i = 0; ; i++) {
   1.143 -            b.append(getRaw(byteOffset(i)));
   1.144 +            b.append(get(i));
   1.145              if (i == iMax)
   1.146                  return b.append(']').toString();
   1.147              b.append(',').append(' ');