rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicReferenceArray.java
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
     1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicReferenceArray.java	Thu Oct 03 15:40:35 2013 +0200
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicReferenceArray.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   * An array of object references in which elements may be updated
    1.12 @@ -49,29 +47,8 @@
    1.13  public class AtomicReferenceArray<E> implements java.io.Serializable {
    1.14      private static final long serialVersionUID = -6209656149925076980L;
    1.15  
    1.16 -    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.17 -    private static final int base = unsafe.arrayBaseOffset(Object[].class);
    1.18 -    private static final int shift;
    1.19      private final Object[] array;
    1.20  
    1.21 -    static {
    1.22 -        int scale = unsafe.arrayIndexScale(Object[].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 AtomicReferenceArray of the given length, with all
    1.41       * elements initially null.
    1.42 @@ -110,11 +87,7 @@
    1.43       * @return the current value
    1.44       */
    1.45      public final E get(int i) {
    1.46 -        return getRaw(checkedByteOffset(i));
    1.47 -    }
    1.48 -
    1.49 -    private E getRaw(long offset) {
    1.50 -        return (E) unsafe.getObjectVolatile(array, offset);
    1.51 +        return (E)array[i];
    1.52      }
    1.53  
    1.54      /**
    1.55 @@ -124,7 +97,7 @@
    1.56       * @param newValue the new value
    1.57       */
    1.58      public final void set(int i, E newValue) {
    1.59 -        unsafe.putObjectVolatile(array, checkedByteOffset(i), newValue);
    1.60 +        array[i] = newValue;
    1.61      }
    1.62  
    1.63      /**
    1.64 @@ -135,7 +108,7 @@
    1.65       * @since 1.6
    1.66       */
    1.67      public final void lazySet(int i, E newValue) {
    1.68 -        unsafe.putOrderedObject(array, checkedByteOffset(i), newValue);
    1.69 +        array[i] = newValue;
    1.70      }
    1.71  
    1.72  
    1.73 @@ -148,12 +121,9 @@
    1.74       * @return the previous value
    1.75       */
    1.76      public final E getAndSet(int i, E newValue) {
    1.77 -        long offset = checkedByteOffset(i);
    1.78 -        while (true) {
    1.79 -            E current = (E) getRaw(offset);
    1.80 -            if (compareAndSetRaw(offset, current, newValue))
    1.81 -                return current;
    1.82 -        }
    1.83 +        E v = (E)array[i];
    1.84 +        array[i] = newValue;
    1.85 +        return v;
    1.86      }
    1.87  
    1.88      /**
    1.89 @@ -167,11 +137,12 @@
    1.90       * the actual value was not equal to the expected value.
    1.91       */
    1.92      public final boolean compareAndSet(int i, E expect, E update) {
    1.93 -        return compareAndSetRaw(checkedByteOffset(i), expect, update);
    1.94 -    }
    1.95 -
    1.96 -    private boolean compareAndSetRaw(long offset, E expect, E update) {
    1.97 -        return unsafe.compareAndSwapObject(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 @@ -203,7 +174,7 @@
   1.108          StringBuilder b = new StringBuilder();
   1.109          b.append('[');
   1.110          for (int i = 0; ; i++) {
   1.111 -            b.append(getRaw(byteOffset(i)));
   1.112 +            b.append(get(i));
   1.113              if (i == iMax)
   1.114                  return b.append(']').toString();
   1.115              b.append(',').append(' ');