jtulach@1334: /* jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * This file is available under and governed by the GNU General Public jtulach@1334: * License version 2 only, as published by the Free Software Foundation. jtulach@1334: * However, the following notice accompanied the original version of this jtulach@1334: * file: jtulach@1334: * jtulach@1334: * Written by Doug Lea with assistance from members of JCP JSR-166 jtulach@1334: * Expert Group and released to the public domain, as explained at jtulach@1334: * http://creativecommons.org/publicdomain/zero/1.0/ jtulach@1334: */ jtulach@1334: jtulach@1334: package java.util.concurrent.atomic; jtulach@1334: import sun.misc.Unsafe; jtulach@1334: import java.util.*; jtulach@1334: jtulach@1334: /** jtulach@1334: * An {@code int} array in which elements may be updated atomically. jtulach@1334: * See the {@link java.util.concurrent.atomic} package jtulach@1334: * specification for description of the properties of atomic jtulach@1334: * variables. jtulach@1334: * @since 1.5 jtulach@1334: * @author Doug Lea jtulach@1334: */ jtulach@1334: public class AtomicIntegerArray implements java.io.Serializable { jtulach@1334: private static final long serialVersionUID = 2862133569453604235L; jtulach@1334: jtulach@1334: private static final Unsafe unsafe = Unsafe.getUnsafe(); jtulach@1334: private static final int base = unsafe.arrayBaseOffset(int[].class); jtulach@1334: private static final int shift; jtulach@1334: private final int[] array; jtulach@1334: jtulach@1334: static { jtulach@1334: int scale = unsafe.arrayIndexScale(int[].class); jtulach@1334: if ((scale & (scale - 1)) != 0) jtulach@1334: throw new Error("data type scale not a power of two"); jtulach@1334: shift = 31 - Integer.numberOfLeadingZeros(scale); jtulach@1334: } jtulach@1334: jtulach@1334: private long checkedByteOffset(int i) { jtulach@1334: if (i < 0 || i >= array.length) jtulach@1334: throw new IndexOutOfBoundsException("index " + i); jtulach@1334: jtulach@1334: return byteOffset(i); jtulach@1334: } jtulach@1334: jtulach@1334: private static long byteOffset(int i) { jtulach@1334: return ((long) i << shift) + base; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicIntegerArray of the given length, with all jtulach@1334: * elements initially zero. jtulach@1334: * jtulach@1334: * @param length the length of the array jtulach@1334: */ jtulach@1334: public AtomicIntegerArray(int length) { jtulach@1334: array = new int[length]; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicIntegerArray with the same length as, and jtulach@1334: * all elements copied from, the given array. jtulach@1334: * jtulach@1334: * @param array the array to copy elements from jtulach@1334: * @throws NullPointerException if array is null jtulach@1334: */ jtulach@1334: public AtomicIntegerArray(int[] array) { jtulach@1334: // Visibility guaranteed by final field guarantees jtulach@1334: this.array = array.clone(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the length of the array. jtulach@1334: * jtulach@1334: * @return the length of the array jtulach@1334: */ jtulach@1334: public final int length() { jtulach@1334: return array.length; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the current value at position {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the current value jtulach@1334: */ jtulach@1334: public final int get(int i) { jtulach@1334: return getRaw(checkedByteOffset(i)); jtulach@1334: } jtulach@1334: jtulach@1334: private int getRaw(long offset) { jtulach@1334: return unsafe.getIntVolatile(array, offset); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the element at position {@code i} to the given value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: */ jtulach@1334: public final void set(int i, int newValue) { jtulach@1334: unsafe.putIntVolatile(array, checkedByteOffset(i), newValue); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Eventually sets the element at position {@code i} to the given value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: * @since 1.6 jtulach@1334: */ jtulach@1334: public final void lazySet(int i, int newValue) { jtulach@1334: unsafe.putOrderedInt(array, checkedByteOffset(i), newValue); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given jtulach@1334: * value and returns the old value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndSet(int i, int newValue) { jtulach@1334: long offset = checkedByteOffset(i); jtulach@1334: while (true) { jtulach@1334: int current = getRaw(offset); jtulach@1334: if (compareAndSetRaw(offset, current, newValue)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given jtulach@1334: * updated value if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. False return indicates that jtulach@1334: * the actual value was not equal to the expected value. jtulach@1334: */ jtulach@1334: public final boolean compareAndSet(int i, int expect, int update) { jtulach@1334: return compareAndSetRaw(checkedByteOffset(i), expect, update); jtulach@1334: } jtulach@1334: jtulach@1334: private boolean compareAndSetRaw(long offset, int expect, int update) { jtulach@1334: return unsafe.compareAndSwapInt(array, offset, expect, update); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given jtulach@1334: * updated value if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: *

May fail spuriously jtulach@1334: * and does not provide ordering guarantees, so is only rarely an jtulach@1334: * appropriate alternative to {@code compareAndSet}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. jtulach@1334: */ jtulach@1334: public final boolean weakCompareAndSet(int i, int expect, int update) { jtulach@1334: return compareAndSet(i, expect, update); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndIncrement(int i) { jtulach@1334: return getAndAdd(i, 1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndDecrement(int i) { jtulach@1334: return getAndAdd(i, -1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param delta the value to add jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndAdd(int i, int delta) { jtulach@1334: long offset = checkedByteOffset(i); jtulach@1334: while (true) { jtulach@1334: int current = getRaw(offset); jtulach@1334: if (compareAndSetRaw(offset, current, current + delta)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int incrementAndGet(int i) { jtulach@1334: return addAndGet(i, 1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int decrementAndGet(int i) { jtulach@1334: return addAndGet(i, -1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param delta the value to add jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int addAndGet(int i, int delta) { jtulach@1334: long offset = checkedByteOffset(i); jtulach@1334: while (true) { jtulach@1334: int current = getRaw(offset); jtulach@1334: int next = current + delta; jtulach@1334: if (compareAndSetRaw(offset, current, next)) jtulach@1334: return next; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the String representation of the current values of array. jtulach@1334: * @return the String representation of the current values of array jtulach@1334: */ jtulach@1334: public String toString() { jtulach@1334: int iMax = array.length - 1; jtulach@1334: if (iMax == -1) jtulach@1334: return "[]"; jtulach@1334: jtulach@1334: StringBuilder b = new StringBuilder(); jtulach@1334: b.append('['); jtulach@1334: for (int i = 0; ; i++) { jtulach@1334: b.append(getRaw(byteOffset(i))); jtulach@1334: if (i == iMax) jtulach@1334: return b.append(']').toString(); jtulach@1334: b.append(',').append(' '); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: }