OpenJDK 11: Merging in java.util.concurrent.atomic.AtomicInteger
This is part of merging upstream changes from OpenJDK 11.28. It
updates the AtomicInteger class. Its implementation makes use of
VarHandle.
Test: m
Test: atest CtsLibcoreOjTestCases
Bug: 188889082
Change-Id: Ic7f9a2dee273bf7ce22c7b4cc19b30a61d492a99
diff --git a/api/current.txt b/api/current.txt
index 5276996..cc70486 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -15148,24 +15148,37 @@
ctor public AtomicInteger();
method public final int accumulateAndGet(int, java.util.function.IntBinaryOperator);
method public final int addAndGet(int);
+ method public final int compareAndExchange(int, int);
+ method public final int compareAndExchangeAcquire(int, int);
+ method public final int compareAndExchangeRelease(int, int);
method public final boolean compareAndSet(int, int);
method public final int decrementAndGet();
method public double doubleValue();
method public float floatValue();
method public final int get();
+ method public final int getAcquire();
method public final int getAndAccumulate(int, java.util.function.IntBinaryOperator);
method public final int getAndAdd(int);
method public final int getAndDecrement();
method public final int getAndIncrement();
method public final int getAndSet(int);
method public final int getAndUpdate(java.util.function.IntUnaryOperator);
+ method public final int getOpaque();
+ method public final int getPlain();
method public final int incrementAndGet();
method public int intValue();
method public final void lazySet(int);
method public long longValue();
method public final void set(int);
+ method public final void setOpaque(int);
+ method public final void setPlain(int);
+ method public final void setRelease(int);
method public final int updateAndGet(java.util.function.IntUnaryOperator);
- method public final boolean weakCompareAndSet(int, int);
+ method @Deprecated public final boolean weakCompareAndSet(int, int);
+ method public final boolean weakCompareAndSetAcquire(int, int);
+ method public final boolean weakCompareAndSetPlain(int, int);
+ method public final boolean weakCompareAndSetRelease(int, int);
+ method public final boolean weakCompareAndSetVolatile(int, int);
}
public class AtomicIntegerArray implements java.io.Serializable {
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandles.java b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
index 2475f3a..fc347f4 100644
--- a/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
@@ -680,7 +680,8 @@
// Android-changed: The bootstrap classloader isn't null.
if (allowedModes == ALL_MODES &&
lookupClass.getClassLoader() == Object.class.getClassLoader()) {
- if (name.startsWith("java.") ||
+ if ((name.startsWith("java.")
+ && !name.startsWith("java.util.concurrent.atomic.Atomic")) ||
(name.startsWith("sun.")
&& !name.startsWith("sun.invoke.")
&& !name.equals("sun.reflect.ReflectionFactory"))) {
diff --git a/ojluni/src/main/java/java/util/concurrent/atomic/AtomicInteger.java b/ojluni/src/main/java/java/util/concurrent/atomic/AtomicInteger.java
index 19b3418..090b419 100644
--- a/ojluni/src/main/java/java/util/concurrent/atomic/AtomicInteger.java
+++ b/ojluni/src/main/java/java/util/concurrent/atomic/AtomicInteger.java
@@ -35,18 +35,19 @@
package java.util.concurrent.atomic;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;
/**
* An {@code int} value that may be updated atomically. See the
- * {@link java.util.concurrent.atomic} package specification for
- * description of the properties of atomic variables. An
- * {@code AtomicInteger} is used in applications such as atomically
- * incremented counters, and cannot be used as a replacement for an
- * {@link java.lang.Integer}. However, this class does extend
- * {@code Number} to allow uniform access by tools and utilities that
- * deal with numerically-based classes.
+ * {@link VarHandle} specification for descriptions of the properties
+ * of atomic accesses. An {@code AtomicInteger} is used in
+ * applications such as atomically incremented counters, and cannot be
+ * used as a replacement for an {@link java.lang.Integer}. However,
+ * this class does extend {@code Number} to allow uniform access by
+ * tools and utilities that deal with numerically-based classes.
*
* @since 1.5
* @author Doug Lea
@@ -54,17 +55,23 @@
public class AtomicInteger extends Number implements java.io.Serializable {
private static final long serialVersionUID = 6214790243416807050L;
- private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
- private static final long VALUE;
-
+ /*
+ * This class intended to be implemented using VarHandles, but there
+ * are unresolved cyclic startup dependencies.
+ */
+ // BEGIN Android-changed: Using VarHandle instead of Unsafe
+ // private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
+ // private static final long VALUE = U.objectFieldOffset(AtomicInteger.class, "value");
+ private static final VarHandle VALUE;
static {
try {
- VALUE = U.objectFieldOffset
- (AtomicInteger.class.getDeclaredField("value"));
+ MethodHandles.Lookup l = MethodHandles.lookup();
+ VALUE = l.findVarHandle(AtomicInteger.class, "value", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
+ // END Android-changed: Using VarHandle instead of Unsafe
private volatile int value;
@@ -84,7 +91,8 @@
}
/**
- * Gets the current value.
+ * Returns the current value,
+ * with memory effects as specified by {@link VarHandle#getVolatile}.
*
* @return the current value
*/
@@ -93,7 +101,8 @@
}
/**
- * Sets to the given value.
+ * Sets the value to {@code newValue},
+ * with memory effects as specified by {@link VarHandle#setVolatile}.
*
* @param newValue the new value
*/
@@ -102,112 +111,171 @@
}
/**
- * Eventually sets to the given value.
+ * Sets the value to {@code newValue},
+ * with memory effects as specified by {@link VarHandle#setRelease}.
*
* @param newValue the new value
* @since 1.6
*/
public final void lazySet(int newValue) {
- U.putOrderedInt(this, VALUE, newValue);
+ // Android-changed: Using VarHandle instead of Unsafe
+ // U.putIntRelease(this, VALUE, newValue);
+ VALUE.setRelease(this, newValue);
}
/**
- * Atomically sets to the given value and returns the old value.
+ * Atomically sets the value to {@code newValue} and returns the old value,
+ * with memory effects as specified by {@link VarHandle#getAndSet}.
*
* @param newValue the new value
* @return the previous value
*/
public final int getAndSet(int newValue) {
- return U.getAndSetInt(this, VALUE, newValue);
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndSetInt(this, VALUE, newValue);
+ return (int)VALUE.getAndSet(this, newValue);
}
/**
- * Atomically sets the value to the given updated value
- * if the current value {@code ==} the expected value.
+ * Atomically sets the value to {@code newValue}
+ * if the current value {@code == expectedValue},
+ * with memory effects as specified by {@link VarHandle#compareAndSet}.
*
- * @param expect the expected value
- * @param update the new value
+ * @param expectedValue the expected value
+ * @param newValue the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
- public final boolean compareAndSet(int expect, int update) {
- return U.compareAndSwapInt(this, VALUE, expect, update);
+ public final boolean compareAndSet(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.compareAndSetInt(this, VALUE, expectedValue, newValue);
+ return VALUE.compareAndSet(this, expectedValue, newValue);
}
/**
- * Atomically sets the value to the given updated value
- * if the current value {@code ==} the expected value.
+ * Possibly atomically sets the value to {@code newValue}
+ * if the current value {@code == expectedValue},
+ * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}.
*
- * <p><a href="package-summary.html#weakCompareAndSet">May fail
- * spuriously and does not provide ordering guarantees</a>, so is
- * only rarely an appropriate alternative to {@code compareAndSet}.
+ * @deprecated This method has plain memory effects but the method
+ * name implies volatile memory effects (see methods such as
+ * {@link #compareAndExchange} and {@link #compareAndSet}). To avoid
+ * confusion over plain or volatile memory effects it is recommended that
+ * the method {@link #weakCompareAndSetPlain} be used instead.
*
- * @param expect the expected value
- * @param update the new value
+ * @param expectedValue the expected value
+ * @param newValue the new value
* @return {@code true} if successful
+ * @see #weakCompareAndSetPlain
*/
- public final boolean weakCompareAndSet(int expect, int update) {
- return U.compareAndSwapInt(this, VALUE, expect, update);
+ @Deprecated(since="9")
+ public final boolean weakCompareAndSet(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.weakCompareAndSetIntPlain(this, VALUE, expectedValue, newValue);
+ return VALUE.weakCompareAndSetPlain(this, expectedValue, newValue);
}
/**
- * Atomically increments by one the current value.
+ * Possibly atomically sets the value to {@code newValue}
+ * if the current value {@code == expectedValue},
+ * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return {@code true} if successful
+ * @since 9
+ */
+ public final boolean weakCompareAndSetPlain(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.weakCompareAndSetIntPlain(this, VALUE, expectedValue, newValue);
+ return VALUE.weakCompareAndSetPlain(this, expectedValue, newValue);
+ }
+
+ /**
+ * Atomically increments the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
+ *
+ * <p>Equivalent to {@code getAndAdd(1)}.
*
* @return the previous value
*/
public final int getAndIncrement() {
- return U.getAndAddInt(this, VALUE, 1);
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, 1);
+ return (int)VALUE.getAndAdd(this, 1);
}
/**
- * Atomically decrements by one the current value.
+ * Atomically decrements the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
+ *
+ * <p>Equivalent to {@code getAndAdd(-1)}.
*
* @return the previous value
*/
public final int getAndDecrement() {
- return U.getAndAddInt(this, VALUE, -1);
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, -1);
+ return (int)VALUE.getAndAdd(this, -1);
}
/**
- * Atomically adds the given value to the current value.
+ * Atomically adds the given value to the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
*
* @param delta the value to add
* @return the previous value
*/
public final int getAndAdd(int delta) {
- return U.getAndAddInt(this, VALUE, delta);
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, delta);
+ return (int)VALUE.getAndAdd(this, delta);
}
/**
- * Atomically increments by one the current value.
+ * Atomically increments the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
+ *
+ * <p>Equivalent to {@code addAndGet(1)}.
*
* @return the updated value
*/
public final int incrementAndGet() {
- return U.getAndAddInt(this, VALUE, 1) + 1;
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, 1) + 1;
+ return (int)VALUE.getAndAdd(this, 1) + 1;
}
/**
- * Atomically decrements by one the current value.
+ * Atomically decrements the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
+ *
+ * <p>Equivalent to {@code addAndGet(-1)}.
*
* @return the updated value
*/
public final int decrementAndGet() {
- return U.getAndAddInt(this, VALUE, -1) - 1;
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, -1) - 1;
+ return (int)VALUE.getAndAdd(this, -1) - 1;
}
/**
- * Atomically adds the given value to the current value.
+ * Atomically adds the given value to the current value,
+ * with memory effects as specified by {@link VarHandle#getAndAdd}.
*
* @param delta the value to add
* @return the updated value
*/
public final int addAndGet(int delta) {
- return U.getAndAddInt(this, VALUE, delta) + delta;
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getAndAddInt(this, VALUE, delta) + delta;
+ return (int)VALUE.getAndAdd(this, delta) + delta;
}
/**
- * Atomically updates the current value with the results of
+ * Atomically updates (with memory effects as specified by {@link
+ * VarHandle#compareAndSet}) the current value with the results of
* applying the given function, returning the previous value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads.
@@ -217,16 +285,19 @@
* @since 1.8
*/
public final int getAndUpdate(IntUnaryOperator updateFunction) {
- int prev, next;
- do {
- prev = get();
- next = updateFunction.applyAsInt(prev);
- } while (!compareAndSet(prev, next));
- return prev;
+ int prev = get(), next = 0;
+ for (boolean haveNext = false;;) {
+ if (!haveNext)
+ next = updateFunction.applyAsInt(prev);
+ if (weakCompareAndSetVolatile(prev, next))
+ return prev;
+ haveNext = (prev == (prev = get()));
+ }
}
/**
- * Atomically updates the current value with the results of
+ * Atomically updates (with memory effects as specified by {@link
+ * VarHandle#compareAndSet}) the current value with the results of
* applying the given function, returning the updated value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads.
@@ -236,22 +307,25 @@
* @since 1.8
*/
public final int updateAndGet(IntUnaryOperator updateFunction) {
- int prev, next;
- do {
- prev = get();
- next = updateFunction.applyAsInt(prev);
- } while (!compareAndSet(prev, next));
- return next;
+ int prev = get(), next = 0;
+ for (boolean haveNext = false;;) {
+ if (!haveNext)
+ next = updateFunction.applyAsInt(prev);
+ if (weakCompareAndSetVolatile(prev, next))
+ return next;
+ haveNext = (prev == (prev = get()));
+ }
}
/**
- * Atomically updates the current value with the results of
+ * Atomically updates (with memory effects as specified by {@link
+ * VarHandle#compareAndSet}) the current value with the results of
* applying the given function to the current and given values,
* returning the previous value. The function should be
* side-effect-free, since it may be re-applied when attempted
- * updates fail due to contention among threads. The function
- * is applied with the current value as its first argument,
- * and the given update as the second argument.
+ * updates fail due to contention among threads. The function is
+ * applied with the current value as its first argument, and the
+ * given update as the second argument.
*
* @param x the update value
* @param accumulatorFunction a side-effect-free function of two arguments
@@ -260,22 +334,25 @@
*/
public final int getAndAccumulate(int x,
IntBinaryOperator accumulatorFunction) {
- int prev, next;
- do {
- prev = get();
- next = accumulatorFunction.applyAsInt(prev, x);
- } while (!compareAndSet(prev, next));
- return prev;
+ int prev = get(), next = 0;
+ for (boolean haveNext = false;;) {
+ if (!haveNext)
+ next = accumulatorFunction.applyAsInt(prev, x);
+ if (weakCompareAndSetVolatile(prev, next))
+ return prev;
+ haveNext = (prev == (prev = get()));
+ }
}
/**
- * Atomically updates the current value with the results of
+ * Atomically updates (with memory effects as specified by {@link
+ * VarHandle#compareAndSet}) the current value with the results of
* applying the given function to the current and given values,
* returning the updated value. The function should be
* side-effect-free, since it may be re-applied when attempted
- * updates fail due to contention among threads. The function
- * is applied with the current value as its first argument,
- * and the given update as the second argument.
+ * updates fail due to contention among threads. The function is
+ * applied with the current value as its first argument, and the
+ * given update as the second argument.
*
* @param x the update value
* @param accumulatorFunction a side-effect-free function of two arguments
@@ -284,12 +361,14 @@
*/
public final int accumulateAndGet(int x,
IntBinaryOperator accumulatorFunction) {
- int prev, next;
- do {
- prev = get();
- next = accumulatorFunction.applyAsInt(prev, x);
- } while (!compareAndSet(prev, next));
- return next;
+ int prev = get(), next = 0;
+ for (boolean haveNext = false;;) {
+ if (!haveNext)
+ next = accumulatorFunction.applyAsInt(prev, x);
+ if (weakCompareAndSetVolatile(prev, next))
+ return next;
+ haveNext = (prev == (prev = get()));
+ }
}
/**
@@ -301,7 +380,10 @@
}
/**
- * Returns the value of this {@code AtomicInteger} as an {@code int}.
+ * Returns the current value of this {@code AtomicInteger} as an
+ * {@code int},
+ * with memory effects as specified by {@link VarHandle#getVolatile}.
+ *
* Equivalent to {@link #get()}.
*/
public int intValue() {
@@ -309,8 +391,9 @@
}
/**
- * Returns the value of this {@code AtomicInteger} as a {@code long}
- * after a widening primitive conversion.
+ * Returns the current value of this {@code AtomicInteger} as a
+ * {@code long} after a widening primitive conversion,
+ * with memory effects as specified by {@link VarHandle#getVolatile}.
* @jls 5.1.2 Widening Primitive Conversions
*/
public long longValue() {
@@ -318,8 +401,9 @@
}
/**
- * Returns the value of this {@code AtomicInteger} as a {@code float}
- * after a widening primitive conversion.
+ * Returns the current value of this {@code AtomicInteger} as a
+ * {@code float} after a widening primitive conversion,
+ * with memory effects as specified by {@link VarHandle#getVolatile}.
* @jls 5.1.2 Widening Primitive Conversions
*/
public float floatValue() {
@@ -327,12 +411,199 @@
}
/**
- * Returns the value of this {@code AtomicInteger} as a {@code double}
- * after a widening primitive conversion.
+ * Returns the current value of this {@code AtomicInteger} as a
+ * {@code double} after a widening primitive conversion,
+ * with memory effects as specified by {@link VarHandle#getVolatile}.
* @jls 5.1.2 Widening Primitive Conversions
*/
public double doubleValue() {
return (double)get();
}
+ // jdk9
+
+ /**
+ * Returns the current value, with memory semantics of reading as
+ * if the variable was declared non-{@code volatile}.
+ *
+ * @return the value
+ * @since 9
+ */
+ public final int getPlain() {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getInt(this, VALUE);
+ return (int)VALUE.get(this);
+ }
+
+ /**
+ * Sets the value to {@code newValue}, with memory semantics
+ * of setting as if the variable was declared non-{@code volatile}
+ * and non-{@code final}.
+ *
+ * @param newValue the new value
+ * @since 9
+ */
+ public final void setPlain(int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // U.putInt(this, VALUE, newValue);
+ VALUE.set(this, newValue);
+ }
+
+ /**
+ * Returns the current value,
+ * with memory effects as specified by {@link VarHandle#getOpaque}.
+ *
+ * @return the value
+ * @since 9
+ */
+ public final int getOpaque() {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getIntOpaque(this, VALUE);
+ return (int)VALUE.getOpaque(this);
+ }
+
+ /**
+ * Sets the value to {@code newValue},
+ * with memory effects as specified by {@link VarHandle#setOpaque}.
+ *
+ * @param newValue the new value
+ * @since 9
+ */
+ public final void setOpaque(int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // U.putIntOpaque(this, VALUE, newValue);
+ VALUE.setOpaque(this, newValue);
+ }
+
+ /**
+ * Returns the current value,
+ * with memory effects as specified by {@link VarHandle#getAcquire}.
+ *
+ * @return the value
+ * @since 9
+ */
+ public final int getAcquire() {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.getIntAcquire(this, VALUE);
+ return (int)VALUE.getAcquire(this);
+ }
+
+ /**
+ * Sets the value to {@code newValue},
+ * with memory effects as specified by {@link VarHandle#setRelease}.
+ *
+ * @param newValue the new value
+ * @since 9
+ */
+ public final void setRelease(int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // U.putIntRelease(this, VALUE, newValue);
+ VALUE.setRelease(this, newValue);
+ }
+
+ /**
+ * Atomically sets the value to {@code newValue} if the current value,
+ * referred to as the <em>witness value</em>, {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#compareAndExchange}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return the witness value, which will be the same as the
+ * expected value if successful
+ * @since 9
+ */
+ public final int compareAndExchange(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.compareAndExchangeInt(this, VALUE, expectedValue, newValue);
+ return (int)VALUE.compareAndExchange(this, expectedValue, newValue);
+ }
+
+ /**
+ * Atomically sets the value to {@code newValue} if the current value,
+ * referred to as the <em>witness value</em>, {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#compareAndExchangeAcquire}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return the witness value, which will be the same as the
+ * expected value if successful
+ * @since 9
+ */
+ public final int compareAndExchangeAcquire(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.compareAndExchangeIntAcquire(this, VALUE, expectedValue, newValue);
+ return (int)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue);
+ }
+
+ /**
+ * Atomically sets the value to {@code newValue} if the current value,
+ * referred to as the <em>witness value</em>, {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#compareAndExchangeRelease}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return the witness value, which will be the same as the
+ * expected value if successful
+ * @since 9
+ */
+ public final int compareAndExchangeRelease(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.compareAndExchangeIntRelease(this, VALUE, expectedValue, newValue);
+ return (int)VALUE.compareAndExchangeRelease(this, expectedValue, newValue);
+ }
+
+ /**
+ * Possibly atomically sets the value to {@code newValue} if
+ * the current value {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#weakCompareAndSet}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return {@code true} if successful
+ * @since 9
+ */
+ public final boolean weakCompareAndSetVolatile(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ //return U.weakCompareAndSetInt(this, VALUE, expectedValue, newValue);
+ return VALUE.weakCompareAndSet(this, expectedValue, newValue);
+ }
+
+ /**
+ * Possibly atomically sets the value to {@code newValue} if
+ * the current value {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#weakCompareAndSetAcquire}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return {@code true} if successful
+ * @since 9
+ */
+ public final boolean weakCompareAndSetAcquire(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.weakCompareAndSetIntAcquire(this, VALUE, expectedValue, newValue);
+ return VALUE.weakCompareAndSetAcquire(this, expectedValue, newValue);
+ }
+
+ /**
+ * Possibly atomically sets the value to {@code newValue} if
+ * the current value {@code == expectedValue},
+ * with memory effects as specified by
+ * {@link VarHandle#weakCompareAndSetRelease}.
+ *
+ * @param expectedValue the expected value
+ * @param newValue the new value
+ * @return {@code true} if successful
+ * @since 9
+ */
+ public final boolean weakCompareAndSetRelease(int expectedValue, int newValue) {
+ // Android-changed: Using VarHandle instead of Unsafe
+ // return U.weakCompareAndSetIntRelease(this, VALUE, expectedValue, newValue);
+ return VALUE.weakCompareAndSetRelease(this, expectedValue, newValue);
+ }
+
}
diff --git a/ojluni/src/main/java/java/util/concurrent/atomic/package-info.java b/ojluni/src/main/java/java/util/concurrent/atomic/package-info.java
index a8e1ff3..8390ff3 100644
--- a/ojluni/src/main/java/java/util/concurrent/atomic/package-info.java
+++ b/ojluni/src/main/java/java/util/concurrent/atomic/package-info.java
@@ -35,26 +35,10 @@
/**
* A small toolkit of classes that support lock-free thread-safe
- * programming on single variables. In essence, the classes in this
- * package extend the notion of {@code volatile} values, fields, and
- * array elements to those that also provide an atomic conditional update
- * operation of the form:
- *
- * <pre> {@code boolean compareAndSet(expectedValue, updateValue);}</pre>
- *
- * <p>This method (which varies in argument types across different
- * classes) atomically sets a variable to the {@code updateValue} if it
- * currently holds the {@code expectedValue}, reporting {@code true} on
- * success. The classes in this package also contain methods to get and
- * unconditionally set values, as well as a weaker conditional atomic
- * update operation {@code weakCompareAndSet} described below.
- *
- * <p>The specifications of these methods enable implementations to
- * employ efficient machine-level atomic instructions that are available
- * on contemporary processors. However on some platforms, support may
- * entail some form of internal locking. Thus the methods are not
- * strictly guaranteed to be non-blocking --
- * a thread may block transiently before performing the operation.
+ * programming on single variables. Instances of Atomic classes
+ * maintain values that are accessed and updated using methods
+ * otherwise available for fields using associated atomic {@link
+ * java.lang.invoke.VarHandle} operations.
*
* <p>Instances of classes
* {@link java.util.concurrent.atomic.AtomicBoolean},
@@ -76,61 +60,30 @@
* }
* }}</pre>
*
- * <p>It is straightforward to define new utility functions that, like
- * {@code getAndIncrement}, apply a function to a value atomically.
- * For example, given some transformation
- * <pre> {@code long transform(long input)}</pre>
+ * <p>Arbitrary transformations of the contained value are provided both
+ * by low-level read-modify-write operations such as {@code compareAndSet}
+ * and by higher-level methods such as {@code getAndUpdate}.
*
- * write your utility method as follows:
- * <pre> {@code
- * long getAndTransform(AtomicLong var) {
- * long prev, next;
- * do {
- * prev = var.get();
- * next = transform(prev);
- * } while (!var.compareAndSet(prev, next));
- * return prev; // return next; for transformAndGet
- * }}</pre>
+ * <p>These classes are not general purpose replacements for {@code
+ * java.lang.Integer} and related classes. They do <em>not</em>
+ * define methods such as {@code equals}, {@code hashCode} and {@code
+ * compareTo}. Because atomic variables are expected to be mutated,
+ * they are poor choices for hash table keys.
*
- * <p>The memory effects for accesses and updates of atomics generally
- * follow the rules for volatiles, as stated in
- * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4">
- * Chapter 17 of
- * <cite>The Java™ Language Specification</cite></a>:
+ * <p>The
+ * {@link java.util.concurrent.atomic.AtomicIntegerArray},
+ * {@link java.util.concurrent.atomic.AtomicLongArray}, and
+ * {@link java.util.concurrent.atomic.AtomicReferenceArray} classes
+ * further extend atomic operation support to arrays of these types.
+ * These classes are also notable in providing {@code volatile} access
+ * semantics for their array elements.
*
- * <ul>
- *
- * <li>{@code get} has the memory effects of reading a
- * {@code volatile} variable.
- *
- * <li>{@code set} has the memory effects of writing (assigning) a
- * {@code volatile} variable.
- *
- * <li>{@code lazySet} has the memory effects of writing (assigning)
- * a {@code volatile} variable except that it permits reorderings with
- * subsequent (but not previous) memory actions that do not themselves
- * impose reordering constraints with ordinary non-{@code volatile}
- * writes. Among other usage contexts, {@code lazySet} may apply when
- * nulling out, for the sake of garbage collection, a reference that is
- * never accessed again.
- *
- * <li>{@code weakCompareAndSet} atomically reads and conditionally
- * writes a variable but does <em>not</em>
- * create any happens-before orderings, so provides no guarantees
- * with respect to previous or subsequent reads and writes of any
- * variables other than the target of the {@code weakCompareAndSet}.
- *
- * <li>{@code compareAndSet}
- * and all other read-and-update operations such as {@code getAndIncrement}
- * have the memory effects of both reading and
- * writing {@code volatile} variables.
- * </ul>
- *
- * <p>In addition to classes representing single values, this package
- * contains <em>Updater</em> classes that can be used to obtain
- * {@code compareAndSet} operations on any selected {@code volatile}
- * field of any selected class.
- *
+ * <p>In addition to classes representing single values and arrays,
+ * this package contains <em>Updater</em> classes that can be used to
+ * obtain {@code compareAndSet} and related operations on any selected
+ * {@code volatile} field of any selected class. These classes
+ * predate the introduction of {@link
+ * java.lang.invoke.VarHandle}, and are of more limited use.
* {@link java.util.concurrent.atomic.AtomicReferenceFieldUpdater},
* {@link java.util.concurrent.atomic.AtomicIntegerFieldUpdater}, and
* {@link java.util.concurrent.atomic.AtomicLongFieldUpdater} are
@@ -143,38 +96,6 @@
* reflection-based setup, less convenient usage, and weaker
* guarantees.
*
- * <p>The
- * {@link java.util.concurrent.atomic.AtomicIntegerArray},
- * {@link java.util.concurrent.atomic.AtomicLongArray}, and
- * {@link java.util.concurrent.atomic.AtomicReferenceArray} classes
- * further extend atomic operation support to arrays of these types.
- * These classes are also notable in providing {@code volatile} access
- * semantics for their array elements, which is not supported for
- * ordinary arrays.
- *
- * <p id="weakCompareAndSet">The atomic classes also support method
- * {@code weakCompareAndSet}, which has limited applicability. On some
- * platforms, the weak version may be more efficient than {@code
- * compareAndSet} in the normal case, but differs in that any given
- * invocation of the {@code weakCompareAndSet} method may return {@code
- * false} <em>spuriously</em> (that is, for no apparent reason). A
- * {@code false} return means only that the operation may be retried if
- * desired, relying on the guarantee that repeated invocation when the
- * variable holds {@code expectedValue} and no other thread is also
- * attempting to set the variable will eventually succeed. (Such
- * spurious failures may for example be due to memory contention effects
- * that are unrelated to whether the expected and current values are
- * equal.) Additionally {@code weakCompareAndSet} does not provide
- * ordering guarantees that are usually needed for synchronization
- * control. However, the method may be useful for updating counters and
- * statistics when such updates are unrelated to the other
- * happens-before orderings of a program. When a thread sees an update
- * to an atomic variable caused by a {@code weakCompareAndSet}, it does
- * not necessarily see updates to any <em>other</em> variables that
- * occurred before the {@code weakCompareAndSet}. This may be
- * acceptable when, for example, updating performance statistics, but
- * rarely otherwise.
- *
* <p>The {@link java.util.concurrent.atomic.AtomicMarkableReference}
* class associates a single boolean with a reference. For example, this
* bit might be used inside a data structure to mean that the object
@@ -185,29 +106,6 @@
* used for example, to represent version numbers corresponding to
* series of updates.
*
- * <p>Atomic classes are designed primarily as building blocks for
- * implementing non-blocking data structures and related infrastructure
- * classes. The {@code compareAndSet} method is not a general
- * replacement for locking. It applies only when critical updates for an
- * object are confined to a <em>single</em> variable.
- *
- * <p>Atomic classes are not general purpose replacements for
- * {@code java.lang.Integer} and related classes. They do <em>not</em>
- * define methods such as {@code equals}, {@code hashCode} and
- * {@code compareTo}. (Because atomic variables are expected to be
- * mutated, they are poor choices for hash table keys.) Additionally,
- * classes are provided only for those types that are commonly useful in
- * intended applications. For example, there is no atomic class for
- * representing {@code byte}. In those infrequent cases where you would
- * like to do so, you can use an {@code AtomicInteger} to hold
- * {@code byte} values, and cast appropriately.
- *
- * You can also hold floats using
- * {@link java.lang.Float#floatToRawIntBits} and
- * {@link java.lang.Float#intBitsToFloat} conversions, and doubles using
- * {@link java.lang.Double#doubleToRawLongBits} and
- * {@link java.lang.Double#longBitsToDouble} conversions.
- *
* @since 1.5
*/
package java.util.concurrent.atomic;