[jmm-dev] RFR 8154755: Add a VarHandle weakCompareAndSet with volatile semantics

Paul Sandoz paul.sandoz at oracle.com
Thu Apr 21 16:21:51 UTC 2016


Please review:


This webrev adds a new VarHandle access mode method, weakCompareAndSetVolatile [*]. (I also fixed some doc errors regarding AccessMode enum names that i forgot to update in a previous issue).

Aleksey noticed a discrepancy with the current set of weak CAS methods when comparing with C++ atomics.

On hardware that supports LL/SC (e.g. ARM) the weak volatile variant may be more performant than the strong volatile variant (compareAndSet) when the CAS is performed in a loop.

See the thread on the jmm-dev list for more details:


Implementations currently defer to the strong volatile Unsafe CAS. We will follow up with changes to Unsafe, hotspot, and updating the VH implementations as separate issues.


 * Possibly atomically sets the value of a variable to the {@code newValue}
 * with the memory semantics of {@link #setVolatile} if the variable's
 * current value, referred to as the <em>witness value</em>, {@code ==} the
 * {@code expectedValue}, as accessed with the memory semantics of
 * {@link #getVolatile}.
 * <p>This operation may fail spuriously (typically, due to memory
 * contention) even if the witness value does match the expected value.
public final native
boolean weakCompareAndSetVolatile(Object... args);


More information about the jmm-dev mailing list