rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java
1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java Thu Oct 03 15:40:35 2013 +0200
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.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 {@code int} array in which elements may be updated atomically.
1.12 @@ -48,29 +46,8 @@
1.13 public class AtomicIntegerArray implements java.io.Serializable {
1.14 private static final long serialVersionUID = 2862133569453604235L;
1.15
1.16 - private static final Unsafe unsafe = Unsafe.getUnsafe();
1.17 - private static final int base = unsafe.arrayBaseOffset(int[].class);
1.18 - private static final int shift;
1.19 private final int[] array;
1.20
1.21 - static {
1.22 - int scale = unsafe.arrayIndexScale(int[].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 AtomicIntegerArray of the given length, with all
1.41 * elements initially zero.
1.42 @@ -109,11 +86,7 @@
1.43 * @return the current value
1.44 */
1.45 public final int get(int i) {
1.46 - return getRaw(checkedByteOffset(i));
1.47 - }
1.48 -
1.49 - private int getRaw(long offset) {
1.50 - return unsafe.getIntVolatile(array, offset);
1.51 + return array[i];
1.52 }
1.53
1.54 /**
1.55 @@ -123,7 +96,7 @@
1.56 * @param newValue the new value
1.57 */
1.58 public final void set(int i, int newValue) {
1.59 - unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);
1.60 + array[i] = newValue;
1.61 }
1.62
1.63 /**
1.64 @@ -134,7 +107,7 @@
1.65 * @since 1.6
1.66 */
1.67 public final void lazySet(int i, int newValue) {
1.68 - unsafe.putOrderedInt(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 int getAndSet(int i, int newValue) {
1.77 - long offset = checkedByteOffset(i);
1.78 - while (true) {
1.79 - int current = getRaw(offset);
1.80 - if (compareAndSetRaw(offset, current, newValue))
1.81 - return current;
1.82 - }
1.83 + int current = array[i];
1.84 + array[i] = newValue;
1.85 + return current;
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, int expect, int update) {
1.93 - return compareAndSetRaw(checkedByteOffset(i), expect, update);
1.94 - }
1.95 -
1.96 - private boolean compareAndSetRaw(long offset, int expect, int update) {
1.97 - return unsafe.compareAndSwapInt(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 int getAndAdd(int i, int delta) {
1.111 - long offset = checkedByteOffset(i);
1.112 - while (true) {
1.113 - int current = getRaw(offset);
1.114 - if (compareAndSetRaw(offset, current, current + delta))
1.115 - return current;
1.116 - }
1.117 + int 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 final int addAndGet(int i, int delta) {
1.127 - long offset = checkedByteOffset(i);
1.128 - while (true) {
1.129 - int current = getRaw(offset);
1.130 - int 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(' ');