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.3 by jsr166, Thu Oct 20 17:47:36 2011 UTC vs.
Revision 1.4 by jsr166, Tue Oct 25 19:21:27 2011 UTC

# 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 235 | Line 235 | public class AtomicDoubleArray implement
235          }
236      }
237  
238 +    /**
239 +     * Saves the state to a stream (that is, serializes it).
240 +     *
241 +     * @serialData The length of the array is emitted (int), followed by all
242 +     *             of its elements (each a {@code double}) in the proper order.
243 +     */
244 +    private void writeObject(java.io.ObjectOutputStream s)
245 +        throws java.io.IOException{
246 +        s.defaultWriteObject();
247 +
248 +        // Write out array length
249 +        int length = length();
250 +        s.writeInt(length);
251 +
252 +        // Write out all elements in the proper order.
253 +        for (int i = 0; i < length; i++)
254 +            s.writeDouble(get(i));
255 +    }
256 +
257 +    /**
258 +     * Reconstitutes the instance from a stream (that is, deserializes it).
259 +     */
260 +    private void readObject(java.io.ObjectInputStream s)
261 +        throws java.io.IOException, ClassNotFoundException {
262 +        s.defaultReadObject();
263 +
264 +        // Read in array length and allocate array
265 +        int length = s.readInt();
266 +        unsafe.putObjectVolatile(this, arrayOffset, new long[length]);
267 +
268 +        // Read in all elements in the proper order.
269 +        for (int i = 0; i < length; i++)
270 +            set(i, s.readDouble());
271 +    }
272 +
273      // Unsafe mechanics
274      private static final sun.misc.Unsafe unsafe = getUnsafe();
275 +    private static final long arrayOffset;
276      private static final int base = unsafe.arrayBaseOffset(long[].class);
277      private static final int shift;
278  
279      static {
280 <        int scale = unsafe.arrayIndexScale(long[].class);
281 <        if ((scale & (scale - 1)) != 0)
282 <            throw new Error("data type scale not a power of two");
283 <        shift = 31 - Integer.numberOfLeadingZeros(scale);
280 >        try {
281 >            Class<?> k = AtomicDoubleArray.class;
282 >            arrayOffset = unsafe.objectFieldOffset
283 >                (k.getDeclaredField("array"));
284 >            int scale = unsafe.arrayIndexScale(long[].class);
285 >            if ((scale & (scale - 1)) != 0)
286 >                throw new Error("data type scale not a power of two");
287 >            shift = 31 - Integer.numberOfLeadingZeros(scale);
288 >        } catch (Exception e) {
289 >            throw new Error(e);
290 >        }
291      }
292  
293      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines