< 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,121 **** * 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 || ((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 || !(uncontended = ! (r = function.applyAsLong(v = a.value, x)) == v ! || a.cas(v, r))) longAccumulate(x, function, uncontended); } } /** --- 101,121 ---- * Updates with the given value. * * @param x the value */ public void accumulate(long x) { ! 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 (cs == null ! || (m = cs.length - 1) < 0 ! || (c = cs[getProbe() & m]) == null || !(uncontended = ! (r = function.applyAsLong(v = c.value, x)) == v ! || c.cas(v, r))) longAccumulate(x, function, uncontended); } } /**
*** 126,141 **** * incorporated. * * @return the current value */ public long get() { ! Cell[] as = cells; long result = base; ! if (as != null) { ! for (Cell a : as) ! if (a != null) ! result = function.applyAsLong(result, a.value); } return result; } /** --- 126,141 ---- * incorporated. * * @return the current value */ public long get() { ! Cell[] cs = cells; long result = base; ! if (cs != null) { ! for (Cell c : cs) ! if (c != null) ! result = function.applyAsLong(result, c.value); } return result; } /**
*** 145,160 **** * 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; base = identity; ! if (as != null) { ! for (Cell a : as) ! if (a != null) ! a.reset(identity); } } /** * Equivalent in effect to {@link #get} followed by {@link --- 145,160 ---- * 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[] cs = cells; base = identity; ! if (cs != null) { ! for (Cell c : cs) ! if (c != null) ! c.reset(identity); } } /** * Equivalent in effect to {@link #get} followed by {@link
*** 165,182 **** * 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); result = function.applyAsLong(result, v); } } } return result; --- 165,180 ---- * the reset. * * @return the value before reset */ public long getThenReset() { ! 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 >