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 AtomicLong instead of sun.misc.Unsafe)
013 */
014
015package com.google.common.util.concurrent;
016
017import static java.lang.Double.doubleToRawLongBits;
018import static java.lang.Double.longBitsToDouble;
019
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024import java.io.Serializable;
025import java.util.concurrent.atomic.AtomicLong;
026
027/**
028 * A {@code double} value that may be updated atomically. See the {@link
029 * java.util.concurrent.atomic} package specification for description of the properties of atomic
030 * variables. An {@code AtomicDouble} is used in applications such as atomic accumulation, and
031 * cannot be used as a replacement for a {@link Double}. However, this class does extend {@code
032 * Number} to allow uniform access by tools and utilities that deal with numerically-based classes.
033 *
034 * <p><a id="bitEquals"></a>This class compares primitive {@code double} values in methods such as
035 * {@link #compareAndSet} by comparing their bitwise representation using {@link
036 * Double#doubleToRawLongBits}, which differs from both the primitive double {@code ==} operator and
037 * from {@link Double#equals}, as if implemented by:
038 *
039 * <pre>{@code
040 * static boolean bitEquals(double x, double y) {
041 *   long xBits = Double.doubleToRawLongBits(x);
042 *   long yBits = Double.doubleToRawLongBits(y);
043 *   return xBits == yBits;
044 * }
045 * }</pre>
046 *
047 * <p>It is possible to write a more scalable updater, at the cost of giving up strict atomicity.
048 * See for example <a
049 * href="http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java.base/java/util/concurrent/atomic/DoubleAdder.html">
050 * DoubleAdder</a>.
051 *
052 * @author Doug Lea
053 * @author Martin Buchholz
054 * @since 11.0
055 */
056public class AtomicDouble extends Number implements Serializable {
057  private static final long serialVersionUID = 0L;
058
059  // We would use AtomicLongFieldUpdater, but it has issues on some Android devices.
060  private transient AtomicLong value;
061
062  /**
063   * Creates a new {@code AtomicDouble} with the given initial value.
064   *
065   * @param initialValue the initial value
066   */
067  public AtomicDouble(double initialValue) {
068    value = new AtomicLong(doubleToRawLongBits(initialValue));
069  }
070
071  /** Creates a new {@code AtomicDouble} with initial value {@code 0.0}. */
072  public AtomicDouble() {
073    this(0.0);
074  }
075
076  /**
077   * Gets the current value.
078   *
079   * @return the current value
080   */
081  public final double get() {
082    return longBitsToDouble(value.get());
083  }
084
085  /**
086   * Sets to the given value.
087   *
088   * @param newValue the new value
089   */
090  public final void set(double newValue) {
091    long next = doubleToRawLongBits(newValue);
092    value.set(next);
093  }
094
095  /**
096   * Eventually sets to the given value.
097   *
098   * @param newValue the new value
099   */
100  public final void lazySet(double newValue) {
101    long next = doubleToRawLongBits(newValue);
102    value.lazySet(next);
103  }
104
105  /**
106   * Atomically sets to the given value and returns the old value.
107   *
108   * @param newValue the new value
109   * @return the previous value
110   */
111  public final double getAndSet(double newValue) {
112    long next = doubleToRawLongBits(newValue);
113    return longBitsToDouble(value.getAndSet(next));
114  }
115
116  /**
117   * Atomically sets the value to the given updated value if the current value is <a
118   * href="#bitEquals">bitwise equal</a> to the expected value.
119   *
120   * @param expect the expected value
121   * @param update the new value
122   * @return {@code true} if successful. False return indicates that the actual value was not
123   *     bitwise equal to the expected value.
124   */
125  public final boolean compareAndSet(double expect, double update) {
126    return value.compareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update));
127  }
128
129  /**
130   * Atomically sets the value to the given updated value if the current value is <a
131   * href="#bitEquals">bitwise equal</a> to the expected value.
132   *
133   * <p>May <a
134   * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious">
135   * fail spuriously</a> and does not provide ordering guarantees, so is only rarely an appropriate
136   * alternative to {@code compareAndSet}.
137   *
138   * @param expect the expected value
139   * @param update the new value
140   * @return {@code true} if successful
141   */
142  public final boolean weakCompareAndSet(double expect, double update) {
143    return value.weakCompareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update));
144  }
145
146  /**
147   * Atomically adds the given value to the current value.
148   *
149   * @param delta the value to add
150   * @return the previous value
151   */
152  @CanIgnoreReturnValue
153  public final double getAndAdd(double delta) {
154    while (true) {
155      long current = value.get();
156      double currentVal = longBitsToDouble(current);
157      double nextVal = currentVal + delta;
158      long next = doubleToRawLongBits(nextVal);
159      if (value.compareAndSet(current, next)) {
160        return currentVal;
161      }
162    }
163  }
164
165  /**
166   * Atomically adds the given value to the current value.
167   *
168   * @param delta the value to add
169   * @return the updated value
170   * @since 31.1
171   */
172  @CanIgnoreReturnValue
173  public final double addAndGet(double delta) {
174    while (true) {
175      long current = value.get();
176      double currentVal = longBitsToDouble(current);
177      double nextVal = currentVal + delta;
178      long next = doubleToRawLongBits(nextVal);
179      if (value.compareAndSet(current, next)) {
180        return nextVal;
181      }
182    }
183  }
184
185  /**
186   * Returns the String representation of the current value.
187   *
188   * @return the String representation of the current value
189   */
190  @Override
191  public String toString() {
192    return Double.toString(get());
193  }
194
195  /**
196   * Returns the value of this {@code AtomicDouble} as an {@code int} after a narrowing primitive
197   * conversion.
198   */
199  @Override
200  public int intValue() {
201    return (int) get();
202  }
203
204  /**
205   * Returns the value of this {@code AtomicDouble} as a {@code long} after a narrowing primitive
206   * conversion.
207   */
208  @Override
209  public long longValue() {
210    return (long) get();
211  }
212
213  /**
214   * Returns the value of this {@code AtomicDouble} as a {@code float} after a narrowing primitive
215   * conversion.
216   */
217  @Override
218  public float floatValue() {
219    return (float) get();
220  }
221
222  /** Returns the value of this {@code AtomicDouble} as a {@code double}. */
223  @Override
224  public double doubleValue() {
225    return get();
226  }
227
228  /**
229   * Saves the state to a stream (that is, serializes it).
230   *
231   * @serialData The current value is emitted (a {@code double}).
232   */
233  private void writeObject(ObjectOutputStream s) throws IOException {
234    s.defaultWriteObject();
235
236    s.writeDouble(get());
237  }
238
239  /** Reconstitutes the instance from a stream (that is, deserializes it). */
240  private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
241    s.defaultReadObject();
242    value = new AtomicLong();
243    set(s.readDouble());
244  }
245}