RFR 8158039 VarHandle float/double field/array access should support CAS/set/add atomics

Vladimir Ivanov vladimir.x.ivanov at oracle.com
Thu Jun 2 12:55:38 UTC 2016


Paul,

Leaving practicality aspect of float/double API aside...

> I am concerned that the CAS loops could in some cases loop without termination:

Can you elaborate? I don't see how it is possible with the proposed patch.

Unless getFloatVolatile() or weakCompareAndSwapFloatVolatile() change 
the original value, it should not happen. And both functions preserve 
float value bits intact:

+    @ForceInline
+    public final boolean weakCompareAndSwapFloatVolatile(Object o, long 
offset,
+                                                       float expected,
+                                                       float x) {
+        return weakCompareAndSwapIntVolatile(o, offset,
+ 
Float.floatToRawIntBits(expected),
+                                             Float.floatToRawIntBits(x));
+    }

Float.floatToRawIntBits() makes it safe, but with Float.intBitsToFloat() 
used infinite looping can happen for NaNs.

Best regards,
Vladimir Ivanov

>
> @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;
> }
>
>
> 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;
> }
>
> and likewise for the atomic get-and-set methods.
>
> Paul.
>
>> David
>> -----
>>
>>>  http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8158039-float-double-field-array-cas.jdk/webrev/ <http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8158039-float-double-field-array-cas.jdk/webrev/>
>>>  http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8158039-float-double-field-array-cas.hotspot/webrev/ <http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8158039-float-double-field-array-cas.hotspot/webrev/>
>>>
>>> These patches are based on those of for sub-word CAS
>>>
>>>  https://bugs.openjdk.java.net/browse/JDK-8157726 <https://bugs.openjdk.java.net/browse/JDK-8157726>
>>>  http://cr.openjdk.java.net/~shade/8157726/webrev.hs.03/ <http://cr.openjdk.java.net/~shade/8157726/webrev.hs.03/>
>>>  http://cr.openjdk.java.net/~shade/8157726/webrev.jdk.03/ <http://cr.openjdk.java.net/~shade/8157726/webrev.jdk.03/>
>>>
>>> And the two patches combined expand the support of fields/arrays.
>>>
>>>
>>> New float/double CAS methods are added to Unsafe that defer to int/long equivalents. Then the other atomic methods are built from weak CAS loops.
>>>
>>> No changes are required to HotSpot, but changes are required to the Unsafe tests in the hotspot repository.
>>>
>>> In general the changes to VHs and tests are minimal since it is triggered from changes to the generating scripts that now include float/double into the CAS category.
>>>
>>> There are some minor specification changes and a CCC has been initiated.
>>>
>>> JPRT tests results show no relevant failures for hotspot and core test sets.
>>>
>>> Thanks,
>>> Paul.
>>>
>


More information about the jdk9-dev mailing list