RFR: 8236778: Add Atomic::fetch_and_add

Stefan Karlsson stefan.karlsson at oracle.com
Wed Jan 8 15:34:05 UTC 2020


Hi all,

Please review this patch to introduce Atomic::fetch_and_add.

https://cr.openjdk.java.net/~stefank/8236778/webrev.01
https://bugs.openjdk.java.net/browse/JDK-8236778

There are a number of places where we have this pattern:
int result = Atomic::add(_index, amount) - amount;

I'd like to introduce Atomic::fetch_and_add so that we can write:
int result = Atomic::fetch_and_add(_index, amount);

The current implementation already has support for both "add and fetch" 
and "fetch and add" but it's not exposed to the upper layers.

Previously, the platform-specific code either implemented "add and 
fetch" or "fetch and add", and then exposed it as an "add and fetch" 
implementation by using CRTP and inheriting from either AddAndFetch or 
FetchAndAdd.

My first implementation of this continued in this track, but got 
push-back because the code was non-intuitive and/or used non-intuitive 
names. Therefore, I've removed FetchAndAdd/AddAndFetch and opted to 
duplicate the trivial functionality in the platform files instead. For 
example:

+  template<typename D, typename I>
+  D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order 
order) const {
+    return fetch_and_add(dest, add_value, order) + add_value;
+  }

There has been some thoughts that maybe we should have:

void Atomic::add(...)
D    Atomic::add_and_fetch(...)
D    Atomic::fetch_and_add(...)

Not sure if it's worth changing to this, but if others think this is 
good, I can do that change.

Tested with tier123, but only compiled on platforms I have access to.

Thanks,
StefanK


More information about the hotspot-dev mailing list