0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent.atomic;
4740N/A
4740N/Aimport java.lang.reflect.Array;
4740N/Aimport java.util.Arrays;
0N/Aimport sun.misc.Unsafe;
0N/A
0N/A/**
0N/A * An array of object references in which elements may be updated
0N/A * atomically. See the {@link java.util.concurrent.atomic} package
0N/A * specification for description of the properties of atomic
0N/A * variables.
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A * @param <E> The base class of elements held in this array
0N/A */
0N/Apublic class AtomicReferenceArray<E> implements java.io.Serializable {
0N/A private static final long serialVersionUID = -6209656149925076980L;
0N/A
4740N/A private static final Unsafe unsafe;
4740N/A private static final int base;
3387N/A private static final int shift;
4740N/A private static final long arrayFieldOffset;
4740N/A private final Object[] array; // must have exact type Object[]
0N/A
3387N/A static {
4740N/A int scale;
4740N/A try {
4740N/A unsafe = Unsafe.getUnsafe();
4740N/A arrayFieldOffset = unsafe.objectFieldOffset
4740N/A (AtomicReferenceArray.class.getDeclaredField("array"));
4740N/A base = unsafe.arrayBaseOffset(Object[].class);
4740N/A scale = unsafe.arrayIndexScale(Object[].class);
4740N/A } catch (Exception e) {
4740N/A throw new Error(e);
4740N/A }
3387N/A if ((scale & (scale - 1)) != 0)
3387N/A throw new Error("data type scale not a power of two");
3387N/A shift = 31 - Integer.numberOfLeadingZeros(scale);
3387N/A }
3387N/A
3387N/A private long checkedByteOffset(int i) {
0N/A if (i < 0 || i >= array.length)
0N/A throw new IndexOutOfBoundsException("index " + i);
3387N/A
3387N/A return byteOffset(i);
3387N/A }
3387N/A
3387N/A private static long byteOffset(int i) {
3387N/A return ((long) i << shift) + base;
0N/A }
0N/A
0N/A /**
3387N/A * Creates a new AtomicReferenceArray of the given length, with all
3387N/A * elements initially null.
3387N/A *
0N/A * @param length the length of the array
0N/A */
0N/A public AtomicReferenceArray(int length) {
0N/A array = new Object[length];
0N/A }
0N/A
0N/A /**
0N/A * Creates a new AtomicReferenceArray with the same length as, and
0N/A * all elements copied from, the given array.
0N/A *
0N/A * @param array the array to copy elements from
0N/A * @throws NullPointerException if array is null
0N/A */
0N/A public AtomicReferenceArray(E[] array) {
3387N/A // Visibility guaranteed by final field guarantees
4740N/A this.array = Arrays.copyOf(array, array.length, Object[].class);
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of the array.
0N/A *
0N/A * @return the length of the array
0N/A */
0N/A public final int length() {
0N/A return array.length;
0N/A }
0N/A
0N/A /**
0N/A * Gets the current value at position {@code i}.
0N/A *
0N/A * @param i the index
0N/A * @return the current value
0N/A */
0N/A public final E get(int i) {
3387N/A return getRaw(checkedByteOffset(i));
3387N/A }
3387N/A
3387N/A private E getRaw(long offset) {
3387N/A return (E) unsafe.getObjectVolatile(array, offset);
0N/A }
0N/A
0N/A /**
0N/A * Sets the element at position {@code i} to the given value.
0N/A *
0N/A * @param i the index
0N/A * @param newValue the new value
0N/A */
0N/A public final void set(int i, E newValue) {
3387N/A unsafe.putObjectVolatile(array, checkedByteOffset(i), newValue);
0N/A }
0N/A
0N/A /**
0N/A * Eventually sets the element at position {@code i} to the given value.
0N/A *
0N/A * @param i the index
0N/A * @param newValue the new value
0N/A * @since 1.6
0N/A */
0N/A public final void lazySet(int i, E newValue) {
3387N/A unsafe.putOrderedObject(array, checkedByteOffset(i), newValue);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Atomically sets the element at position {@code i} to the given
0N/A * value and returns the old value.
0N/A *
0N/A * @param i the index
0N/A * @param newValue the new value
0N/A * @return the previous value
0N/A */
0N/A public final E getAndSet(int i, E newValue) {
3387N/A long offset = checkedByteOffset(i);
0N/A while (true) {
4740N/A E current = getRaw(offset);
3387N/A if (compareAndSetRaw(offset, current, newValue))
0N/A return current;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the element at position {@code i} to the given
0N/A * updated value if the current value {@code ==} the expected value.
3387N/A *
0N/A * @param i the index
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful. False return indicates that
0N/A * the actual value was not equal to the expected value.
0N/A */
0N/A public final boolean compareAndSet(int i, E expect, E update) {
3387N/A return compareAndSetRaw(checkedByteOffset(i), expect, update);
3387N/A }
3387N/A
3387N/A private boolean compareAndSetRaw(long offset, E expect, E update) {
3387N/A return unsafe.compareAndSwapObject(array, offset, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the element at position {@code i} to the given
0N/A * updated value if the current value {@code ==} the expected value.
0N/A *
0N/A * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
0N/A * and does not provide ordering guarantees, so is only rarely an
0N/A * appropriate alternative to {@code compareAndSet}.
0N/A *
0N/A * @param i the index
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful.
0N/A */
0N/A public final boolean weakCompareAndSet(int i, E expect, E update) {
0N/A return compareAndSet(i, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Returns the String representation of the current values of array.
3387N/A * @return the String representation of the current values of array
0N/A */
0N/A public String toString() {
4740N/A int iMax = array.length - 1;
3387N/A if (iMax == -1)
3387N/A return "[]";
3387N/A
3387N/A StringBuilder b = new StringBuilder();
3387N/A b.append('[');
3387N/A for (int i = 0; ; i++) {
3387N/A b.append(getRaw(byteOffset(i)));
3387N/A if (i == iMax)
3387N/A return b.append(']').toString();
3387N/A b.append(',').append(' ');
3387N/A }
0N/A }
0N/A
4740N/A /**
4740N/A * Reconstitutes the instance from a stream (that is, deserializes it).
4740N/A * @param s the stream
4740N/A */
4740N/A private void readObject(java.io.ObjectInputStream s)
4740N/A throws java.io.IOException, ClassNotFoundException {
4740N/A // Note: This must be changed if any additional fields are defined
4740N/A Object a = s.readFields().get("array", null);
4740N/A if (a == null || !a.getClass().isArray())
4740N/A throw new java.io.InvalidObjectException("Not array type");
4740N/A if (a.getClass() != Object[].class)
4740N/A a = Arrays.copyOf((Object[])a, Array.getLength(a), Object[].class);
4740N/A unsafe.putObjectVolatile(this, arrayFieldOffset, a);
4740N/A }
4740N/A
0N/A}