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