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 */ 056@ElementTypesAreNonnullByDefault 057public class AtomicDouble extends Number implements Serializable { 058 private static final long serialVersionUID = 0L; 059 060 // We would use AtomicLongFieldUpdater, but it has issues on some Android devices. 061 private transient AtomicLong value; 062 063 /** 064 * Creates a new {@code AtomicDouble} with the given initial value. 065 * 066 * @param initialValue the initial value 067 */ 068 public AtomicDouble(double initialValue) { 069 value = new AtomicLong(doubleToRawLongBits(initialValue)); 070 } 071 072 /** Creates a new {@code AtomicDouble} with initial value {@code 0.0}. */ 073 public AtomicDouble() { 074 this(0.0); 075 } 076 077 /** 078 * Gets the current value. 079 * 080 * @return the current value 081 */ 082 public final double get() { 083 return longBitsToDouble(value.get()); 084 } 085 086 /** 087 * Sets to the given value. 088 * 089 * @param newValue the new value 090 */ 091 public final void set(double newValue) { 092 long next = doubleToRawLongBits(newValue); 093 value.set(next); 094 } 095 096 /** 097 * Eventually sets to the given value. 098 * 099 * @param newValue the new value 100 */ 101 public final void lazySet(double newValue) { 102 long next = doubleToRawLongBits(newValue); 103 value.lazySet(next); 104 } 105 106 /** 107 * Atomically sets to the given value and returns the old value. 108 * 109 * @param newValue the new value 110 * @return the previous value 111 */ 112 public final double getAndSet(double newValue) { 113 long next = doubleToRawLongBits(newValue); 114 return longBitsToDouble(value.getAndSet(next)); 115 } 116 117 /** 118 * Atomically sets the value to the given updated value if the current value is <a 119 * href="#bitEquals">bitwise equal</a> to the expected value. 120 * 121 * @param expect the expected value 122 * @param update the new value 123 * @return {@code true} if successful. False return indicates that the actual value was not 124 * bitwise equal to the expected value. 125 */ 126 public final boolean compareAndSet(double expect, double update) { 127 return value.compareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 128 } 129 130 /** 131 * Atomically sets the value to the given updated value if the current value is <a 132 * href="#bitEquals">bitwise equal</a> to the expected value. 133 * 134 * <p>May <a 135 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 136 * fail spuriously</a> and does not provide ordering guarantees, so is only rarely an appropriate 137 * alternative to {@code compareAndSet}. 138 * 139 * @param expect the expected value 140 * @param update the new value 141 * @return {@code true} if successful 142 */ 143 public final boolean weakCompareAndSet(double expect, double update) { 144 return value.weakCompareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 145 } 146 147 /** 148 * Atomically adds the given value to the current value. 149 * 150 * @param delta the value to add 151 * @return the previous value 152 */ 153 @CanIgnoreReturnValue 154 public final double getAndAdd(double delta) { 155 while (true) { 156 long current = value.get(); 157 double currentVal = longBitsToDouble(current); 158 double nextVal = currentVal + delta; 159 long next = doubleToRawLongBits(nextVal); 160 if (value.compareAndSet(current, next)) { 161 return currentVal; 162 } 163 } 164 } 165 166 /** 167 * Atomically adds the given value to the current value. 168 * 169 * @param delta the value to add 170 * @return the updated value 171 * @since 31.1 172 */ 173 @CanIgnoreReturnValue 174 public final double addAndGet(double delta) { 175 while (true) { 176 long current = value.get(); 177 double currentVal = longBitsToDouble(current); 178 double nextVal = currentVal + delta; 179 long next = doubleToRawLongBits(nextVal); 180 if (value.compareAndSet(current, next)) { 181 return nextVal; 182 } 183 } 184 } 185 186 /** 187 * Returns the String representation of the current value. 188 * 189 * @return the String representation of the current value 190 */ 191 @Override 192 public String toString() { 193 return Double.toString(get()); 194 } 195 196 /** 197 * Returns the value of this {@code AtomicDouble} as an {@code int} after a narrowing primitive 198 * conversion. 199 */ 200 @Override 201 public int intValue() { 202 return (int) get(); 203 } 204 205 /** 206 * Returns the value of this {@code AtomicDouble} as a {@code long} after a narrowing primitive 207 * conversion. 208 */ 209 @Override 210 public long longValue() { 211 return (long) get(); 212 } 213 214 /** 215 * Returns the value of this {@code AtomicDouble} as a {@code float} after a narrowing primitive 216 * conversion. 217 */ 218 @Override 219 public float floatValue() { 220 return (float) get(); 221 } 222 223 /** Returns the value of this {@code AtomicDouble} as a {@code double}. */ 224 @Override 225 public double doubleValue() { 226 return get(); 227 } 228 229 /** 230 * Saves the state to a stream (that is, serializes it). 231 * 232 * @serialData The current value is emitted (a {@code double}). 233 */ 234 private void writeObject(ObjectOutputStream s) throws IOException { 235 s.defaultWriteObject(); 236 237 s.writeDouble(get()); 238 } 239 240 /** Reconstitutes the instance from a stream (that is, deserializes it). */ 241 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { 242 s.defaultReadObject(); 243 value = new AtomicLong(); 244 set(s.readDouble()); 245 } 246}