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