001/*
002 * Written by Doug Lea and Martin Buchholz with assistance from
003 * members of JCP JSR-166 Expert Group and released to the public
004 * domain, as explained at
005 * http://creativecommons.org/publicdomain/zero/1.0/
006 */
007
008/*
009 * Source:
010 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/extra/AtomicDouble.java?revision=1.13
011 * (Modified to adapt to guava coding conventions and
012 * to use AtomicLongFieldUpdater instead of sun.misc.Unsafe)
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.lang.Double.doubleToRawLongBits;
019import static java.lang.Double.longBitsToDouble;
020
021import com.google.common.annotations.GwtIncompatible;
022import com.google.common.annotations.J2ktIncompatible;
023import com.google.errorprone.annotations.CanIgnoreReturnValue;
024import com.google.j2objc.annotations.ReflectionSupport;
025import java.io.IOException;
026import java.io.ObjectInputStream;
027import java.io.ObjectOutputStream;
028import java.util.concurrent.atomic.AtomicLongFieldUpdater;
029import java.util.function.DoubleBinaryOperator;
030import java.util.function.DoubleUnaryOperator;
031
032/**
033 * A {@code double} value that may be updated atomically. See the {@link
034 * java.util.concurrent.atomic} package specification for description of the properties of atomic
035 * variables. An {@code AtomicDouble} is used in applications such as atomic accumulation, and
036 * cannot be used as a replacement for a {@link Double}. However, this class does extend {@code
037 * Number} to allow uniform access by tools and utilities that deal with numerically-based classes.
038 *
039 * <p><a id="bitEquals"></a>This class compares primitive {@code double} values in methods such as
040 * {@link #compareAndSet} by comparing their bitwise representation using {@link
041 * Double#doubleToRawLongBits}, which differs from both the primitive double {@code ==} operator and
042 * from {@link Double#equals}, as if implemented by:
043 *
044 * <pre>{@code
045 * static boolean bitEquals(double x, double y) {
046 *   long xBits = Double.doubleToRawLongBits(x);
047 *   long yBits = Double.doubleToRawLongBits(y);
048 *   return xBits == yBits;
049 * }
050 * }</pre>
051 *
052 * <p>It is possible to write a more scalable updater, at the cost of giving up strict atomicity.
053 * See for example <a
054 * href="http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java.base/java/util/concurrent/atomic/DoubleAdder.html">
055 * DoubleAdder</a>.
056 *
057 * @author Doug Lea
058 * @author Martin Buchholz
059 * @since 11.0
060 */
061@GwtIncompatible
062@J2ktIncompatible
063@ReflectionSupport(value = ReflectionSupport.Level.FULL)
064public class AtomicDouble extends Number {
065  private static final long serialVersionUID = 0L;
066
067  private transient volatile long value;
068
069  private static final AtomicLongFieldUpdater<AtomicDouble> updater =
070      AtomicLongFieldUpdater.newUpdater(AtomicDouble.class, "value");
071
072  /**
073   * Creates a new {@code AtomicDouble} with the given initial value.
074   *
075   * @param initialValue the initial value
076   */
077  public AtomicDouble(double initialValue) {
078    value = doubleToRawLongBits(initialValue);
079  }
080
081  /** Creates a new {@code AtomicDouble} with initial value {@code 0.0}. */
082  public AtomicDouble() {
083    // assert doubleToRawLongBits(0.0) == 0L;
084  }
085
086  /**
087   * Gets the current value.
088   *
089   * @return the current value
090   */
091  public final double get() {
092    return longBitsToDouble(value);
093  }
094
095  /**
096   * Sets to the given value.
097   *
098   * @param newValue the new value
099   */
100  public final void set(double newValue) {
101    long next = doubleToRawLongBits(newValue);
102    value = next;
103  }
104
105  /**
106   * Eventually sets to the given value.
107   *
108   * @param newValue the new value
109   */
110  public final void lazySet(double newValue) {
111    long next = doubleToRawLongBits(newValue);
112    updater.lazySet(this, next);
113  }
114
115  /**
116   * Atomically sets to the given value and returns the old value.
117   *
118   * @param newValue the new value
119   * @return the previous value
120   */
121  public final double getAndSet(double newValue) {
122    long next = doubleToRawLongBits(newValue);
123    return longBitsToDouble(updater.getAndSet(this, next));
124  }
125
126  /**
127   * Atomically sets the value to the given updated value if the current value is <a
128   * href="#bitEquals">bitwise equal</a> to the expected value.
129   *
130   * @param expect the expected value
131   * @param update the new value
132   * @return {@code true} if successful. False return indicates that the actual value was not
133   *     bitwise equal to the expected value.
134   */
135  public final boolean compareAndSet(double expect, double update) {
136    return updater.compareAndSet(this, doubleToRawLongBits(expect), doubleToRawLongBits(update));
137  }
138
139  /**
140   * Atomically sets the value to the given updated value if the current value is <a
141   * href="#bitEquals">bitwise equal</a> to the expected value.
142   *
143   * <p>May <a
144   * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious">
145   * fail spuriously</a> and does not provide ordering guarantees, so is only rarely an appropriate
146   * alternative to {@code compareAndSet}.
147   *
148   * @param expect the expected value
149   * @param update the new value
150   * @return {@code true} if successful
151   */
152  public final boolean weakCompareAndSet(double expect, double update) {
153    return updater.weakCompareAndSet(
154        this, doubleToRawLongBits(expect), doubleToRawLongBits(update));
155  }
156
157  /**
158   * Atomically adds the given value to the current value.
159   *
160   * @param delta the value to add
161   * @return the previous value
162   */
163  @CanIgnoreReturnValue
164  public final double getAndAdd(double delta) {
165    return getAndAccumulate(delta, Double::sum);
166  }
167
168  /**
169   * Atomically adds the given value to the current value.
170   *
171   * @param delta the value to add
172   * @return the updated value
173   */
174  @CanIgnoreReturnValue
175  public final double addAndGet(double delta) {
176    return accumulateAndGet(delta, Double::sum);
177  }
178
179  /**
180   * Atomically updates the current value with the results of applying the given function to the
181   * current and given values.
182   *
183   * @param x the update value
184   * @param accumulatorFunction the accumulator function
185   * @return the previous value
186   * @since 31.1
187   */
188  @CanIgnoreReturnValue
189  public final double getAndAccumulate(double x, DoubleBinaryOperator accumulatorFunction) {
190    checkNotNull(accumulatorFunction);
191    return getAndUpdate(oldValue -> accumulatorFunction.applyAsDouble(oldValue, x));
192  }
193
194  /**
195   * Atomically updates the current value with the results of applying the given function to the
196   * current and given values.
197   *
198   * @param x the update value
199   * @param accumulatorFunction the accumulator function
200   * @return the updated value
201   * @since 31.1
202   */
203  @CanIgnoreReturnValue
204  public final double accumulateAndGet(double x, DoubleBinaryOperator accumulatorFunction) {
205    checkNotNull(accumulatorFunction);
206    return updateAndGet(oldValue -> accumulatorFunction.applyAsDouble(oldValue, x));
207  }
208
209  /**
210   * Atomically updates the current value with the results of applying the given function.
211   *
212   * @param updateFunction the update function
213   * @return the previous value
214   * @since 31.1
215   */
216  @CanIgnoreReturnValue
217  public final double getAndUpdate(DoubleUnaryOperator updateFunction) {
218    while (true) {
219      long current = value;
220      double currentVal = longBitsToDouble(current);
221      double nextVal = updateFunction.applyAsDouble(currentVal);
222      long next = doubleToRawLongBits(nextVal);
223      if (updater.compareAndSet(this, current, next)) {
224        return currentVal;
225      }
226    }
227  }
228
229  /**
230   * Atomically updates the current value with the results of applying the given function.
231   *
232   * @param updateFunction the update function
233   * @return the updated value
234   * @since 31.1
235   */
236  @CanIgnoreReturnValue
237  public final double updateAndGet(DoubleUnaryOperator updateFunction) {
238    while (true) {
239      long current = value;
240      double currentVal = longBitsToDouble(current);
241      double nextVal = updateFunction.applyAsDouble(currentVal);
242      long next = doubleToRawLongBits(nextVal);
243      if (updater.compareAndSet(this, current, next)) {
244        return nextVal;
245      }
246    }
247  }
248
249  /**
250   * Returns the String representation of the current value.
251   *
252   * @return the String representation of the current value
253   */
254  @Override
255  public String toString() {
256    return Double.toString(get());
257  }
258
259  /**
260   * Returns the value of this {@code AtomicDouble} as an {@code int} after a narrowing primitive
261   * conversion.
262   */
263  @Override
264  public int intValue() {
265    return (int) get();
266  }
267
268  /**
269   * Returns the value of this {@code AtomicDouble} as a {@code long} after a narrowing primitive
270   * conversion.
271   */
272  @Override
273  public long longValue() {
274    return (long) get();
275  }
276
277  /**
278   * Returns the value of this {@code AtomicDouble} as a {@code float} after a narrowing primitive
279   * conversion.
280   */
281  @Override
282  public float floatValue() {
283    return (float) get();
284  }
285
286  /** Returns the value of this {@code AtomicDouble} as a {@code double}. */
287  @Override
288  public double doubleValue() {
289    return get();
290  }
291
292  /**
293   * Saves the state to a stream (that is, serializes it).
294   *
295   * @serialData The current value is emitted (a {@code double}).
296   */
297  private void writeObject(ObjectOutputStream s) throws IOException {
298    s.defaultWriteObject();
299
300    s.writeDouble(get());
301  }
302
303  /** Reconstitutes the instance from a stream (that is, deserializes it). */
304  private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
305    s.defaultReadObject();
306
307    set(s.readDouble());
308  }
309}