mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8192943: Optimize atomic accumulators using VarHandle getAndSet
Reviewed-by: martin, psandoz, chegar
This commit is contained in:
parent
0b3b384a27
commit
00d1900dc9
5 changed files with 113 additions and 117 deletions
|
@ -87,15 +87,15 @@ public class DoubleAdder extends Striped64 implements Serializable {
|
|||
* @param x the value to add
|
||||
*/
|
||||
public void add(double x) {
|
||||
Cell[] as; long b, v; int m; Cell a;
|
||||
if ((as = cells) != null ||
|
||||
Cell[] cs; long b, v; int m; Cell c;
|
||||
if ((cs = cells) != null ||
|
||||
!casBase(b = base,
|
||||
Double.doubleToRawLongBits
|
||||
(Double.longBitsToDouble(b) + x))) {
|
||||
boolean uncontended = true;
|
||||
if (as == null || (m = as.length - 1) < 0 ||
|
||||
(a = as[getProbe() & m]) == null ||
|
||||
!(uncontended = a.cas(v = a.value,
|
||||
if (cs == null || (m = cs.length - 1) < 0 ||
|
||||
(c = cs[getProbe() & m]) == null ||
|
||||
!(uncontended = c.cas(v = c.value,
|
||||
Double.doubleToRawLongBits
|
||||
(Double.longBitsToDouble(v) + x))))
|
||||
doubleAccumulate(x, null, uncontended);
|
||||
|
@ -115,12 +115,12 @@ public class DoubleAdder extends Striped64 implements Serializable {
|
|||
* @return the sum
|
||||
*/
|
||||
public double sum() {
|
||||
Cell[] as = cells;
|
||||
Cell[] cs = cells;
|
||||
double sum = Double.longBitsToDouble(base);
|
||||
if (as != null) {
|
||||
for (Cell a : as)
|
||||
if (a != null)
|
||||
sum += Double.longBitsToDouble(a.value);
|
||||
if (cs != null) {
|
||||
for (Cell c : cs)
|
||||
if (c != null)
|
||||
sum += Double.longBitsToDouble(c.value);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
@ -133,12 +133,12 @@ public class DoubleAdder extends Striped64 implements Serializable {
|
|||
* known that no threads are concurrently updating.
|
||||
*/
|
||||
public void reset() {
|
||||
Cell[] as = cells;
|
||||
Cell[] cs = cells;
|
||||
base = 0L; // relies on fact that double 0 must have same rep as long
|
||||
if (as != null) {
|
||||
for (Cell a : as)
|
||||
if (a != null)
|
||||
a.reset();
|
||||
if (cs != null) {
|
||||
for (Cell c : cs)
|
||||
if (c != null)
|
||||
c.reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,16 +153,12 @@ public class DoubleAdder extends Striped64 implements Serializable {
|
|||
* @return the sum
|
||||
*/
|
||||
public double sumThenReset() {
|
||||
Cell[] as = cells;
|
||||
double sum = Double.longBitsToDouble(base);
|
||||
base = 0L;
|
||||
if (as != null) {
|
||||
for (Cell a : as) {
|
||||
if (a != null) {
|
||||
long v = a.value;
|
||||
a.reset();
|
||||
sum += Double.longBitsToDouble(v);
|
||||
}
|
||||
Cell[] cs = cells;
|
||||
double sum = Double.longBitsToDouble(getAndSetBase(0L));
|
||||
if (cs != null) {
|
||||
for (Cell c : cs) {
|
||||
if (c != null)
|
||||
sum += Double.longBitsToDouble(c.getAndSet(0L));
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue