ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/extra/AtomicDouble.java
(Generate patch)

Comparing jsr166/src/jsr166e/extra/AtomicDouble.java (file contents):
Revision 1.1 by jsr166, Tue Aug 9 04:05:25 2011 UTC vs.
Revision 1.4 by jsr166, Tue Aug 9 19:10:29 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8   package jsr166e.extra;
9   import static java.lang.Double.doubleToRawLongBits;
10   import static java.lang.Double.longBitsToDouble;
10 import sun.misc.Unsafe;
11  
12   /**
13   * A {@code double} value that may be updated atomically.  See the
14   * {@link java.util.concurrent.atomic} package specification for
15 < * description of the properties of atomic variables. An
16 < * {@code AtomicDouble} is used in applications such as atomically
17 < * incremented sequence numbers, and cannot be used as a replacement
18 < * for a {@link java.lang.Double}. However, this class does extend
19 < * {@code Number} to allow uniform access by tools and utilities that
20 < * deal with numerically-based classes.
15 > * description of the properties of atomic variables.  An {@code
16 > * AtomicDouble} is used in applications such as atomic accumulation,
17 > * and cannot be used as a replacement for a {@link Double}.  However,
18 > * this class does extend {@code Number} to allow uniform access by
19 > * tools and utilities that deal with numerically-based classes.
20 > *
21 > * <p>This class differs from the primitive double {@code ==} operator
22 > * and from {@link Double#equals} in that it uses purely bitwise
23 > * equality in methods such as {@link #compareAndSet}, as if
24 > * implemented by:
25 > *  <pre> {@code
26 > * boolean bitEquals(double x, double y) {
27 > *   long xBits = Double.doubleToRawLongBits(x);
28 > *   long yBits = Double.doubleToRawLongBits(y);
29 > *   return xBits == yBits;
30 > * }}</pre>
31   *
22 * @since 1.5
32   * @author Doug Lea
33 + * @author Martin Buchholz
34   */
35   public class AtomicDouble extends Number implements java.io.Serializable {
36 <    private static final long serialVersionUID = 1927816293512124184L;
36 >    static final long serialVersionUID = -8405198993435143622L;
37  
38      // setup to use Unsafe.compareAndSwapLong for updates
39 <    private static final Unsafe unsafe = Unsafe.getUnsafe();
39 >    private static final sun.misc.Unsafe unsafe = getUnsafe();
40      private static final long valueOffset;
41  
32    /**
33     * Records whether the underlying JVM supports lockless
34     * compareAndSwap for longs. While the Unsafe.compareAndSwapLong
35     * method works in either case, some constructions should be
36     * handled at Java level to avoid locking user-visible locks.
37     */
38    static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
39
40    /**
41     * Returns whether underlying JVM supports lockless CompareAndSet
42     * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
43     */
44    private static native boolean VMSupportsCS8();
45
42      static {
43          try {
44              valueOffset = unsafe.objectFieldOffset
# Line 88 | Line 84 | public class AtomicDouble extends Number
84       * Eventually sets to the given value.
85       *
86       * @param newValue the new value
91     * @since 1.6
87       */
88      public final void lazySet(double newValue) {
89          unsafe.putOrderedLong(this, valueOffset, doubleToRawLongBits(newValue));
# Line 104 | Line 99 | public class AtomicDouble extends Number
99          long newBits = doubleToRawLongBits(newValue);
100          while (true) {
101              long currentBits = value;
102 <            if (compareAndSet(currentBits, newBits))
102 >            if (unsafe.compareAndSwapLong(this, valueOffset,
103 >                                          currentBits, newBits))
104                  return longBitsToDouble(currentBits);
105          }
106      }
# Line 150 | Line 146 | public class AtomicDouble extends Number
146          while (true) {
147              long currentBits = value;
148              long nextBits = doubleToRawLongBits(longBitsToDouble(currentBits) + delta);
149 <            if (compareAndSet(currentBits, nextBits))
149 >            if (unsafe.compareAndSwapLong(this, valueOffset,
150 >                                          currentBits, nextBits))
151                  return longBitsToDouble(currentBits);
152          }
153      }
# Line 210 | Line 207 | public class AtomicDouble extends Number
207          return get();
208      }
209  
210 +    /**
211 +     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
212 +     * Replace with a simple call to Unsafe.getUnsafe when integrating
213 +     * into a jdk.
214 +     *
215 +     * @return a sun.misc.Unsafe
216 +     */
217 +    private static sun.misc.Unsafe getUnsafe() {
218 +        try {
219 +            return sun.misc.Unsafe.getUnsafe();
220 +        } catch (SecurityException se) {
221 +            try {
222 +                return java.security.AccessController.doPrivileged
223 +                    (new java.security
224 +                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
225 +                        public sun.misc.Unsafe run() throws Exception {
226 +                            java.lang.reflect.Field f = sun.misc
227 +                                .Unsafe.class.getDeclaredField("theUnsafe");
228 +                            f.setAccessible(true);
229 +                            return (sun.misc.Unsafe) f.get(null);
230 +                        }});
231 +            } catch (java.security.PrivilegedActionException e) {
232 +                throw new RuntimeException("Could not initialize intrinsics",
233 +                                           e.getCause());
234 +            }
235 +        }
236 +    }
237 +
238   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines