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.2 by jsr166, Tue Aug 9 07:44:08 2011 UTC vs.
Revision 1.3 by jsr166, Tue Aug 9 08:28:54 2011 UTC

# Line 8 | Line 8
8   package jsr166e.extra;
9   import static java.lang.Double.doubleToRawLongBits;
10   import static java.lang.Double.longBitsToDouble;
11 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 {@code
16 < * AtomicDouble} is used in applications such as atomic summation, and
17 < * cannot be used as a replacement for a {@link Double}.  However,
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   *
# Line 29 | Line 28 | import sun.misc.Unsafe;
28   *   long yBits = Double.doubleToRawLongBits(y);
29   *   return xBits == yBits;
30   * }}</pre>
31 < *
33 < * @since 1.5
31 > *
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  
42      /**
# Line 54 | Line 52 | public class AtomicDouble extends Number
52       * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
53       */
54      private static boolean VMSupportsCS8() {
55 <        try {
56 <            Class<?> klazz = java.util.concurrent.atomic.AtomicLong.class;
57 <            java.lang.reflect.Method m =
58 <                klazz.getDeclaredMethod("VMSupportsCS8", new Class<?>[0]);
59 <            m.setAccessible(true);
60 <            return (Boolean) m.invoke(new Class<?>[0]);
61 <        } catch (Throwable t) { throw new Error(t); }
55 >        final Class<?> klazz = java.util.concurrent.atomic.AtomicLong.class;
56 >        return java.security.AccessController.doPrivileged
57 >            (new java.security.PrivilegedAction<Boolean>() {
58 >                public Boolean run() {
59 >                    try {
60 >                        java.lang.reflect.Method m =
61 >                            klazz.getDeclaredMethod("VMSupportsCS8", new Class<?>[0]);
62 >                        m.setAccessible(true);
63 >                        return (Boolean) m.invoke(new Class<?>[0]);
64 >                    } catch (Throwable t) { throw new Error(t); }
65 >                }});
66      }
67  
68      static {
# Line 108 | Line 110 | public class AtomicDouble extends Number
110       * Eventually sets to the given value.
111       *
112       * @param newValue the new value
111     * @since 1.6
113       */
114      public final void lazySet(double newValue) {
115          unsafe.putOrderedLong(this, valueOffset, doubleToRawLongBits(newValue));
# Line 232 | Line 233 | public class AtomicDouble extends Number
233          return get();
234      }
235  
236 +    /**
237 +     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
238 +     * Replace with a simple call to Unsafe.getUnsafe when integrating
239 +     * into a jdk.
240 +     *
241 +     * @return a sun.misc.Unsafe
242 +     */
243 +    private static sun.misc.Unsafe getUnsafe() {
244 +        try {
245 +            return sun.misc.Unsafe.getUnsafe();
246 +        } catch (SecurityException se) {
247 +            try {
248 +                return java.security.AccessController.doPrivileged
249 +                    (new java.security
250 +                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
251 +                        public sun.misc.Unsafe run() throws Exception {
252 +                            java.lang.reflect.Field f = sun.misc
253 +                                .Unsafe.class.getDeclaredField("theUnsafe");
254 +                            f.setAccessible(true);
255 +                            return (sun.misc.Unsafe) f.get(null);
256 +                        }});
257 +            } catch (java.security.PrivilegedActionException e) {
258 +                throw new RuntimeException("Could not initialize intrinsics",
259 +                                           e.getCause());
260 +            }
261 +        }
262 +    }
263 +
264   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines