< prev index next >

src/java.base/share/classes/java/util/concurrent/atomic/DoubleAdder.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


  75      * they just re-interpret bits.
  76      */
  77 
  78     /**
  79      * Creates a new adder with initial sum of zero.
  80      */
  81     public DoubleAdder() {
  82     }
  83 
  84     /**
  85      * Adds the given value.
  86      *
  87      * @param x the value to add
  88      */
  89     public void add(double x) {
  90         Cell[] cs; long b, v; int m; Cell c;
  91         if ((cs = cells) != null ||
  92             !casBase(b = base,
  93                      Double.doubleToRawLongBits
  94                      (Double.longBitsToDouble(b) + x))) {

  95             boolean uncontended = true;
  96             if (cs == null || (m = cs.length - 1) < 0 ||
  97                 (c = cs[getProbe() & m]) == null ||
  98                 !(uncontended = c.cas(v = c.value,
  99                                       Double.doubleToRawLongBits
 100                                       (Double.longBitsToDouble(v) + x))))
 101                 doubleAccumulate(x, null, uncontended);
 102         }
 103     }
 104 
 105     /**
 106      * Returns the current sum.  The returned value is <em>NOT</em> an
 107      * atomic snapshot; invocation in the absence of concurrent
 108      * updates returns an accurate result, but concurrent updates that
 109      * occur while the sum is being calculated might not be
 110      * incorporated.  Also, because floating-point arithmetic is not
 111      * strictly associative, the returned result need not be identical
 112      * to the value that would be obtained in a sequential series of
 113      * updates to a single variable.
 114      *
 115      * @return the sum
 116      */
 117     public double sum() {
 118         Cell[] cs = cells;
 119         double sum = Double.longBitsToDouble(base);
 120         if (cs != null) {
 121             for (Cell c : cs)




  75      * they just re-interpret bits.
  76      */
  77 
  78     /**
  79      * Creates a new adder with initial sum of zero.
  80      */
  81     public DoubleAdder() {
  82     }
  83 
  84     /**
  85      * Adds the given value.
  86      *
  87      * @param x the value to add
  88      */
  89     public void add(double x) {
  90         Cell[] cs; long b, v; int m; Cell c;
  91         if ((cs = cells) != null ||
  92             !casBase(b = base,
  93                      Double.doubleToRawLongBits
  94                      (Double.longBitsToDouble(b) + x))) {
  95             int index = getProbe();
  96             boolean uncontended = true;
  97             if (cs == null || (m = cs.length - 1) < 0 ||
  98                 (c = cs[index & m]) == null ||
  99                 !(uncontended = c.cas(v = c.value,
 100                                       Double.doubleToRawLongBits
 101                                       (Double.longBitsToDouble(v) + x))))
 102                 doubleAccumulate(x, null, uncontended, index);
 103         }
 104     }
 105 
 106     /**
 107      * Returns the current sum.  The returned value is <em>NOT</em> an
 108      * atomic snapshot; invocation in the absence of concurrent
 109      * updates returns an accurate result, but concurrent updates that
 110      * occur while the sum is being calculated might not be
 111      * incorporated.  Also, because floating-point arithmetic is not
 112      * strictly associative, the returned result need not be identical
 113      * to the value that would be obtained in a sequential series of
 114      * updates to a single variable.
 115      *
 116      * @return the sum
 117      */
 118     public double sum() {
 119         Cell[] cs = cells;
 120         double sum = Double.longBitsToDouble(base);
 121         if (cs != null) {
 122             for (Cell c : cs)


< prev index next >