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

Comparing jsr166/src/jsr166e/extra/AtomicDoubleArray.java (file contents):
Revision 1.2 by jsr166, Thu Oct 20 16:33:25 2011 UTC vs.
Revision 1.10 by jsr166, Mon Mar 4 16:12:26 2013 UTC

# Line 14 | Line 14 | import static java.lang.Double.longBitsT
14   * See the {@link java.util.concurrent.atomic} package specification
15   * for description of the properties of atomic variables.
16   *
17 < * <p><a name="bitEquals">This class compares primitive {@code double}
17 > * <p id="bitEquals">This class compares primitive {@code double}
18   * values in methods such as {@link #compareAndSet} by comparing their
19   * bitwise representation using {@link Double#doubleToRawLongBits},
20   * which differs from both the primitive double {@code ==} operator
21   * and from {@link Double#equals}, as if implemented by:
22   *  <pre> {@code
23 < * boolean bitEquals(double x, double y) {
23 > * static boolean bitEquals(double x, double y) {
24   *   long xBits = Double.doubleToRawLongBits(x);
25   *   long yBits = Double.doubleToRawLongBits(y);
26   *   return xBits == yBits;
# Line 32 | Line 32 | import static java.lang.Double.longBitsT
32   public class AtomicDoubleArray implements java.io.Serializable {
33      private static final long serialVersionUID = -2308431214976778248L;
34  
35 <    private final long[] array;
35 >    private final transient long[] array;
36  
37      private long checkedByteOffset(int i) {
38          if (i < 0 || i >= array.length)
# Line 162 | Line 162 | public class AtomicDoubleArray implement
162       * if the current value is <a href="#bitEquals">bitwise equal</a>
163       * to the expected value.
164       *
165 <     * <p>May <a
165 >     * <p><a
166       * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious">
167 <     * fail spuriously</a>
168 <     * and does not provide ordering guarantees, so is only rarely an
169 <     * appropriate alternative to {@code compareAndSet}.
167 >     * May fail spuriously and does not provide ordering guarantees</a>,
168 >     * so is only rarely an appropriate alternative to {@code compareAndSet}.
169       *
170       * @param i the index
171       * @param expect the expected value
# Line 235 | Line 234 | public class AtomicDoubleArray implement
234          }
235      }
236  
237 +    /**
238 +     * Saves the state to a stream (that is, serializes it).
239 +     *
240 +     * @serialData The length of the array is emitted (int), followed by all
241 +     *             of its elements (each a {@code double}) in the proper order.
242 +     */
243 +    private void writeObject(java.io.ObjectOutputStream s)
244 +        throws java.io.IOException {
245 +        s.defaultWriteObject();
246 +
247 +        // Write out array length
248 +        int length = length();
249 +        s.writeInt(length);
250 +
251 +        // Write out all elements in the proper order.
252 +        for (int i = 0; i < length; i++)
253 +            s.writeDouble(get(i));
254 +    }
255 +
256 +    /**
257 +     * Reconstitutes the instance from a stream (that is, deserializes it).
258 +     */
259 +    private void readObject(java.io.ObjectInputStream s)
260 +        throws java.io.IOException, ClassNotFoundException {
261 +        s.defaultReadObject();
262 +
263 +        // Read in array length and allocate array
264 +        int length = s.readInt();
265 +        unsafe.putObjectVolatile(this, arrayOffset, new long[length]);
266 +
267 +        // Read in all elements in the proper order.
268 +        for (int i = 0; i < length; i++)
269 +            set(i, s.readDouble());
270 +    }
271 +
272      // Unsafe mechanics
273      private static final sun.misc.Unsafe unsafe = getUnsafe();
274 +    private static final long arrayOffset;
275      private static final int base = unsafe.arrayBaseOffset(long[].class);
276      private static final int shift;
277  
278      static {
279 <        int scale = unsafe.arrayIndexScale(long[].class);
280 <        if ((scale & (scale - 1)) != 0)
281 <            throw new Error("data type scale not a power of two");
282 <        shift = 31 - Integer.numberOfLeadingZeros(scale);
279 >        try {
280 >            Class<?> k = AtomicDoubleArray.class;
281 >            arrayOffset = unsafe.objectFieldOffset
282 >                (k.getDeclaredField("array"));
283 >            int scale = unsafe.arrayIndexScale(long[].class);
284 >            if ((scale & (scale - 1)) != 0)
285 >                throw new Error("data type scale not a power of two");
286 >            shift = 31 - Integer.numberOfLeadingZeros(scale);
287 >        } catch (Exception e) {
288 >            throw new Error(e);
289 >        }
290      }
291  
292      /**
# Line 257 | Line 299 | public class AtomicDoubleArray implement
299      private static sun.misc.Unsafe getUnsafe() {
300          try {
301              return sun.misc.Unsafe.getUnsafe();
302 <        } catch (SecurityException se) {
303 <            try {
304 <                return java.security.AccessController.doPrivileged
305 <                    (new java.security
306 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
307 <                        public sun.misc.Unsafe run() throws Exception {
308 <                            java.lang.reflect.Field f = sun.misc
309 <                                .Unsafe.class.getDeclaredField("theUnsafe");
310 <                            f.setAccessible(true);
311 <                            return (sun.misc.Unsafe) f.get(null);
312 <                        }});
313 <            } catch (java.security.PrivilegedActionException e) {
314 <                throw new RuntimeException("Could not initialize intrinsics",
315 <                                           e.getCause());
316 <            }
302 >        } catch (SecurityException tryReflectionInstead) {}
303 >        try {
304 >            return java.security.AccessController.doPrivileged
305 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
306 >                public sun.misc.Unsafe run() throws Exception {
307 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
308 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
309 >                        f.setAccessible(true);
310 >                        Object x = f.get(null);
311 >                        if (k.isInstance(x))
312 >                            return k.cast(x);
313 >                    }
314 >                    throw new NoSuchFieldError("the Unsafe");
315 >                }});
316 >        } catch (java.security.PrivilegedActionException e) {
317 >            throw new RuntimeException("Could not initialize intrinsics",
318 >                                       e.getCause());
319          }
320      }
321   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines