RFR 8158039 VarHandle float/double field/array access should support CAS/set/add atomics
Doug Lea
dl at cs.oswego.edu
Thu Jun 2 12:08:34 UTC 2016
On 06/02/2016 04:34 AM, Paul Sandoz wrote:
>
> I am concerned that the CAS loops could in some cases loop without termination:
>
> @ForceInline
> public final float getAndAddFloat(Object o, long offset, float delta) {
> float v;
> do {
> v = getFloatVolatile(o, offset);
> } while (!weakCompareAndSwapFloatVolatile(o, offset, v, v + delta));
> return v;
> }
>
Yes. This is why we did not introduce Atomic{Float, Double} in
java.util.concurrent.atomic, but instead tell people to
use intBitsToFloat etc if that's what they intend (see
http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html)
People (apparently including you :-) argue that this is too pedantic
though. The standard way suggested in docs is almost always what
people want, so could be used, so long as it is carefully spelled
out so that when people do hit multiple NaN problems and the like,
at least they can find an explanation.
>
> I think this should be:
>
> @ForceInline
> public final float getAndAddFloat(Object o, long offset, float delta) {
> int expectedBits;
> float v;
> do {
> expectedBits = getIntVolatile(o, offset);
> v = Float.intBitsToFloat(bits); // May not preserve a NaN value with the same bit pattern as expectedBits
> } while (!weakCompareAndSwapIntVolatile(o, offset, expectedBits, Float.floatToRawIntBits(v + delta)));
> return v;
> }
>
More information about the jdk9-dev
mailing list