RFR 8158039 VarHandle float/double field/array access should support CAS/set/add atomics
Martin Buchholz
martinrb at google.com
Thu Jun 2 12:41:35 UTC 2016
You can find an AtomicDouble in jsr166 CVS
http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/extra/AtomicDouble.java?view=markup
and in guava
https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/AtomicDouble.java
I'm still in favor of putting it in openjdk.
On Thu, Jun 2, 2016 at 5:08 AM, Doug Lea <dl at cs.oswego.edu> wrote:
> 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