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 id="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/docs/java.base/java/util/concurrent/atomic/DoubleAdder.html"> 046 * DoubleAdder</a>. 047 * 048 * @author Doug Lea 049 * @author Martin Buchholz 050 * @since 11.0 051 */ 052@ElementTypesAreNonnullByDefault 053public class AtomicDouble extends Number implements java.io.Serializable { 054 private static final long serialVersionUID = 0L; 055 056 // We would use AtomicLongFieldUpdater, but it has issues on some Android devices. 057 private transient AtomicLong value; 058 059 /** 060 * Creates a new {@code AtomicDouble} with the given initial value. 061 * 062 * @param initialValue the initial value 063 */ 064 public AtomicDouble(double initialValue) { 065 value = new AtomicLong(doubleToRawLongBits(initialValue)); 066 } 067 068 /** Creates a new {@code AtomicDouble} with initial value {@code 0.0}. */ 069 public AtomicDouble() { 070 this(0.0); 071 } 072 073 /** 074 * Gets the current value. 075 * 076 * @return the current value 077 */ 078 public final double get() { 079 return longBitsToDouble(value.get()); 080 } 081 082 /** 083 * Sets to the given value. 084 * 085 * @param newValue the new value 086 */ 087 public final void set(double newValue) { 088 long next = doubleToRawLongBits(newValue); 089 value.set(next); 090 } 091 092 /** 093 * Eventually sets to the given value. 094 * 095 * @param newValue the new value 096 */ 097 public final void lazySet(double newValue) { 098 long next = doubleToRawLongBits(newValue); 099 value.lazySet(next); 100 } 101 102 /** 103 * Atomically sets to the given value and returns the old value. 104 * 105 * @param newValue the new value 106 * @return the previous value 107 */ 108 public final double getAndSet(double newValue) { 109 long next = doubleToRawLongBits(newValue); 110 return longBitsToDouble(value.getAndSet(next)); 111 } 112 113 /** 114 * Atomically sets the value to the given updated value if the current value is <a 115 * href="#bitEquals">bitwise equal</a> to the expected value. 116 * 117 * @param expect the expected value 118 * @param update the new value 119 * @return {@code true} if successful. False return indicates that the actual value was not 120 * bitwise equal to the expected value. 121 */ 122 public final boolean compareAndSet(double expect, double update) { 123 return value.compareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 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 * <p>May <a 131 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 132 * fail spuriously</a> and does not provide ordering guarantees, so is only rarely an appropriate 133 * alternative to {@code compareAndSet}. 134 * 135 * @param expect the expected value 136 * @param update the new value 137 * @return {@code true} if successful 138 */ 139 public final boolean weakCompareAndSet(double expect, double update) { 140 return value.weakCompareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 141 } 142 143 /** 144 * Atomically adds the given value to the current value. 145 * 146 * @param delta the value to add 147 * @return the previous value 148 */ 149 @CanIgnoreReturnValue 150 public final double getAndAdd(double delta) { 151 while (true) { 152 long current = value.get(); 153 double currentVal = longBitsToDouble(current); 154 double nextVal = currentVal + delta; 155 long next = doubleToRawLongBits(nextVal); 156 if (value.compareAndSet(current, next)) { 157 return currentVal; 158 } 159 } 160 } 161 162 /** 163 * Atomically adds the given value to the current value. 164 * 165 * @param delta the value to add 166 * @return the updated value 167 * @since 31.1 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}