src/java.base/share/classes/java/util/concurrent/atomic/AtomicLongArray.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.util.function.LongBinaryOperator;
  39 import java.util.function.LongUnaryOperator;
  40 
  41 /**
  42  * A {@code long} array in which elements may be updated atomically.
  43  * See the {@link java.util.concurrent.atomic} package specification
  44  * for description of the properties of atomic variables.
  45  * @since 1.5
  46  * @author Doug Lea
  47  */
  48 public class AtomicLongArray implements java.io.Serializable {
  49     private static final long serialVersionUID = -2308431214976778248L;
  50 
  51     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
  52     private static final int ABASE;
  53     private static final int ASHIFT;
  54     private final long[] array;
  55 
  56     static {
  57         ABASE = U.arrayBaseOffset(long[].class);
  58         int scale = U.arrayIndexScale(long[].class);
  59         if ((scale & (scale - 1)) != 0)
  60             throw new Error("array index scale not a power of two");
  61         ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
  62     }
  63 
  64     private long checkedByteOffset(int i) {
  65         if (i < 0 || i >= array.length)
  66             throw new IndexOutOfBoundsException("index " + i);
  67 
  68         return byteOffset(i);
  69     }
  70 
  71     private static long byteOffset(int i) {




  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.util.function.LongBinaryOperator;
  39 import java.util.function.LongUnaryOperator;
  40 
  41 /**
  42  * A {@code long} array in which elements may be updated atomically.
  43  * See the {@link java.util.concurrent.atomic} package specification
  44  * for description of the properties of atomic variables.
  45  * @since 1.5
  46  * @author Doug Lea
  47  */
  48 public class AtomicLongArray implements java.io.Serializable {
  49     private static final long serialVersionUID = -2308431214976778248L;
  50 
  51     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
  52     private static final int ABASE;
  53     private static final int ASHIFT;
  54     private final long[] array;
  55 
  56     static {
  57         ABASE = U.arrayBaseOffset(long[].class);
  58         int scale = U.arrayIndexScale(long[].class);
  59         if ((scale & (scale - 1)) != 0)
  60             throw new Error("array index scale not a power of two");
  61         ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
  62     }
  63 
  64     private long checkedByteOffset(int i) {
  65         if (i < 0 || i >= array.length)
  66             throw new IndexOutOfBoundsException("index " + i);
  67 
  68         return byteOffset(i);
  69     }
  70 
  71     private static long byteOffset(int i) {