< prev index next >
src/java.base/share/classes/java/util/concurrent/atomic/LongAccumulator.java
Print this page
8192943: Optimize atomic accumulators using VarHandle getAndSet
Reviewed-by: martin, psandoz, chegar
@@ -101,21 +101,21 @@
* Updates with the given value.
*
* @param x the value
*/
public void accumulate(long x) {
- Cell[] as; long b, v, r; int m; Cell a;
- if ((as = cells) != null
+ Cell[] cs; long b, v, r; int m; Cell c;
+ if ((cs = cells) != null
|| ((r = function.applyAsLong(b = base, x)) != b
&& !casBase(b, r))) {
boolean uncontended = true;
- if (as == null
- || (m = as.length - 1) < 0
- || (a = as[getProbe() & m]) == null
+ if (cs == null
+ || (m = cs.length - 1) < 0
+ || (c = cs[getProbe() & m]) == null
|| !(uncontended =
- (r = function.applyAsLong(v = a.value, x)) == v
- || a.cas(v, r)))
+ (r = function.applyAsLong(v = c.value, x)) == v
+ || c.cas(v, r)))
longAccumulate(x, function, uncontended);
}
}
/**
@@ -126,16 +126,16 @@
* incorporated.
*
* @return the current value
*/
public long get() {
- Cell[] as = cells;
+ Cell[] cs = cells;
long result = base;
- if (as != null) {
- for (Cell a : as)
- if (a != null)
- result = function.applyAsLong(result, a.value);
+ if (cs != null) {
+ for (Cell c : cs)
+ if (c != null)
+ result = function.applyAsLong(result, c.value);
}
return result;
}
/**
@@ -145,16 +145,16 @@
* updates. Because this method is intrinsically racy, it should
* only be used when it is known that no threads are concurrently
* updating.
*/
public void reset() {
- Cell[] as = cells;
+ Cell[] cs = cells;
base = identity;
- if (as != null) {
- for (Cell a : as)
- if (a != null)
- a.reset(identity);
+ if (cs != null) {
+ for (Cell c : cs)
+ if (c != null)
+ c.reset(identity);
}
}
/**
* Equivalent in effect to {@link #get} followed by {@link
@@ -165,18 +165,16 @@
* the reset.
*
* @return the value before reset
*/
public long getThenReset() {
- Cell[] as = cells;
- long result = base;
- base = identity;
- if (as != null) {
- for (Cell a : as) {
- if (a != null) {
- long v = a.value;
- a.reset(identity);
+ Cell[] cs = cells;
+ long result = getAndSetBase(identity);
+ if (cs != null) {
+ for (Cell c : cs) {
+ if (c != null) {
+ long v = c.getAndSet(identity);
result = function.applyAsLong(result, v);
}
}
}
return result;
< prev index next >