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