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