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

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


  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.lang.reflect.Array;
  39 import java.util.Arrays;
  40 import java.util.function.BinaryOperator;
  41 import java.util.function.UnaryOperator;
  42 
  43 /**
  44  * An array of object references in which elements may be updated
  45  * atomically.  See the {@link java.util.concurrent.atomic} package
  46  * specification for description of the properties of atomic
  47  * variables.
  48  * @since 1.5
  49  * @author Doug Lea
  50  * @param <E> The base class of elements held in this array
  51  */
  52 public class AtomicReferenceArray<E> implements java.io.Serializable {
  53     private static final long serialVersionUID = -6209656149925076980L;
  54 
  55     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
  56     private static final long ARRAY;
  57     private static final int ABASE;
  58     private static final int ASHIFT;
  59     private final Object[] array; // must have exact type Object[]
  60 
  61     static {
  62         try {
  63             ARRAY = U.objectFieldOffset
  64                 (AtomicReferenceArray.class.getDeclaredField("array"));
  65             ABASE = U.arrayBaseOffset(Object[].class);
  66             int scale = U.arrayIndexScale(Object[].class);
  67             if ((scale & (scale - 1)) != 0)
  68                 throw new Error("array index scale not a power of two");
  69             ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
  70         } catch (ReflectiveOperationException e) {
  71             throw new Error(e);
  72         }
  73     }
  74 
  75     private long checkedByteOffset(int i) {




  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.lang.reflect.Array;
  39 import java.util.Arrays;
  40 import java.util.function.BinaryOperator;
  41 import java.util.function.UnaryOperator;
  42 
  43 /**
  44  * An array of object references in which elements may be updated
  45  * atomically.  See the {@link java.util.concurrent.atomic} package
  46  * specification for description of the properties of atomic
  47  * variables.
  48  * @since 1.5
  49  * @author Doug Lea
  50  * @param <E> The base class of elements held in this array
  51  */
  52 public class AtomicReferenceArray<E> implements java.io.Serializable {
  53     private static final long serialVersionUID = -6209656149925076980L;
  54 
  55     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
  56     private static final long ARRAY;
  57     private static final int ABASE;
  58     private static final int ASHIFT;
  59     private final Object[] array; // must have exact type Object[]
  60 
  61     static {
  62         try {
  63             ARRAY = U.objectFieldOffset
  64                 (AtomicReferenceArray.class.getDeclaredField("array"));
  65             ABASE = U.arrayBaseOffset(Object[].class);
  66             int scale = U.arrayIndexScale(Object[].class);
  67             if ((scale & (scale - 1)) != 0)
  68                 throw new Error("array index scale not a power of two");
  69             ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
  70         } catch (ReflectiveOperationException e) {
  71             throw new Error(e);
  72         }
  73     }
  74 
  75     private long checkedByteOffset(int i) {