rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1338 aa70afac4eca
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.6 + *
     1.7 + * This code is free software; you can redistribute it and/or modify it
     1.8 + * under the terms of the GNU General Public License version 2 only, as
     1.9 + * published by the Free Software Foundation.  Oracle designates this
    1.10 + * particular file as subject to the "Classpath" exception as provided
    1.11 + * by Oracle in the LICENSE file that accompanied this code.
    1.12 + *
    1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 + * version 2 for more details (a copy is included in the LICENSE file that
    1.17 + * accompanied this code).
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License version
    1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 + *
    1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 + * or visit www.oracle.com if you need additional information or have any
    1.25 + * questions.
    1.26 + */
    1.27 +
    1.28 +/*
    1.29 + * This file is available under and governed by the GNU General Public
    1.30 + * License version 2 only, as published by the Free Software Foundation.
    1.31 + * However, the following notice accompanied the original version of this
    1.32 + * file:
    1.33 + *
    1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
    1.35 + * Expert Group and released to the public domain, as explained at
    1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
    1.37 + */
    1.38 +
    1.39 +package java.util.concurrent.atomic;
    1.40 +import sun.misc.Unsafe;
    1.41 +import java.util.*;
    1.42 +
    1.43 +/**
    1.44 + * An {@code int} array in which elements may be updated atomically.
    1.45 + * See the {@link java.util.concurrent.atomic} package
    1.46 + * specification for description of the properties of atomic
    1.47 + * variables.
    1.48 + * @since 1.5
    1.49 + * @author Doug Lea
    1.50 + */
    1.51 +public class AtomicIntegerArray implements java.io.Serializable {
    1.52 +    private static final long serialVersionUID = 2862133569453604235L;
    1.53 +
    1.54 +    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.55 +    private static final int base = unsafe.arrayBaseOffset(int[].class);
    1.56 +    private static final int shift;
    1.57 +    private final int[] array;
    1.58 +
    1.59 +    static {
    1.60 +        int scale = unsafe.arrayIndexScale(int[].class);
    1.61 +        if ((scale & (scale - 1)) != 0)
    1.62 +            throw new Error("data type scale not a power of two");
    1.63 +        shift = 31 - Integer.numberOfLeadingZeros(scale);
    1.64 +    }
    1.65 +
    1.66 +    private long checkedByteOffset(int i) {
    1.67 +        if (i < 0 || i >= array.length)
    1.68 +            throw new IndexOutOfBoundsException("index " + i);
    1.69 +
    1.70 +        return byteOffset(i);
    1.71 +    }
    1.72 +
    1.73 +    private static long byteOffset(int i) {
    1.74 +        return ((long) i << shift) + base;
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Creates a new AtomicIntegerArray of the given length, with all
    1.79 +     * elements initially zero.
    1.80 +     *
    1.81 +     * @param length the length of the array
    1.82 +     */
    1.83 +    public AtomicIntegerArray(int length) {
    1.84 +        array = new int[length];
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Creates a new AtomicIntegerArray with the same length as, and
    1.89 +     * all elements copied from, the given array.
    1.90 +     *
    1.91 +     * @param array the array to copy elements from
    1.92 +     * @throws NullPointerException if array is null
    1.93 +     */
    1.94 +    public AtomicIntegerArray(int[] array) {
    1.95 +        // Visibility guaranteed by final field guarantees
    1.96 +        this.array = array.clone();
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Returns the length of the array.
   1.101 +     *
   1.102 +     * @return the length of the array
   1.103 +     */
   1.104 +    public final int length() {
   1.105 +        return array.length;
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Gets the current value at position {@code i}.
   1.110 +     *
   1.111 +     * @param i the index
   1.112 +     * @return the current value
   1.113 +     */
   1.114 +    public final int get(int i) {
   1.115 +        return getRaw(checkedByteOffset(i));
   1.116 +    }
   1.117 +
   1.118 +    private int getRaw(long offset) {
   1.119 +        return unsafe.getIntVolatile(array, offset);
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Sets the element at position {@code i} to the given value.
   1.124 +     *
   1.125 +     * @param i the index
   1.126 +     * @param newValue the new value
   1.127 +     */
   1.128 +    public final void set(int i, int newValue) {
   1.129 +        unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Eventually sets the element at position {@code i} to the given value.
   1.134 +     *
   1.135 +     * @param i the index
   1.136 +     * @param newValue the new value
   1.137 +     * @since 1.6
   1.138 +     */
   1.139 +    public final void lazySet(int i, int newValue) {
   1.140 +        unsafe.putOrderedInt(array, checkedByteOffset(i), newValue);
   1.141 +    }
   1.142 +
   1.143 +    /**
   1.144 +     * Atomically sets the element at position {@code i} to the given
   1.145 +     * value and returns the old value.
   1.146 +     *
   1.147 +     * @param i the index
   1.148 +     * @param newValue the new value
   1.149 +     * @return the previous value
   1.150 +     */
   1.151 +    public final int getAndSet(int i, int newValue) {
   1.152 +        long offset = checkedByteOffset(i);
   1.153 +        while (true) {
   1.154 +            int current = getRaw(offset);
   1.155 +            if (compareAndSetRaw(offset, current, newValue))
   1.156 +                return current;
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Atomically sets the element at position {@code i} to the given
   1.162 +     * updated value if the current value {@code ==} the expected value.
   1.163 +     *
   1.164 +     * @param i the index
   1.165 +     * @param expect the expected value
   1.166 +     * @param update the new value
   1.167 +     * @return true if successful. False return indicates that
   1.168 +     * the actual value was not equal to the expected value.
   1.169 +     */
   1.170 +    public final boolean compareAndSet(int i, int expect, int update) {
   1.171 +        return compareAndSetRaw(checkedByteOffset(i), expect, update);
   1.172 +    }
   1.173 +
   1.174 +    private boolean compareAndSetRaw(long offset, int expect, int update) {
   1.175 +        return unsafe.compareAndSwapInt(array, offset, expect, update);
   1.176 +    }
   1.177 +
   1.178 +    /**
   1.179 +     * Atomically sets the element at position {@code i} to the given
   1.180 +     * updated value if the current value {@code ==} the expected value.
   1.181 +     *
   1.182 +     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   1.183 +     * and does not provide ordering guarantees, so is only rarely an
   1.184 +     * appropriate alternative to {@code compareAndSet}.
   1.185 +     *
   1.186 +     * @param i the index
   1.187 +     * @param expect the expected value
   1.188 +     * @param update the new value
   1.189 +     * @return true if successful.
   1.190 +     */
   1.191 +    public final boolean weakCompareAndSet(int i, int expect, int update) {
   1.192 +        return compareAndSet(i, expect, update);
   1.193 +    }
   1.194 +
   1.195 +    /**
   1.196 +     * Atomically increments by one the element at index {@code i}.
   1.197 +     *
   1.198 +     * @param i the index
   1.199 +     * @return the previous value
   1.200 +     */
   1.201 +    public final int getAndIncrement(int i) {
   1.202 +        return getAndAdd(i, 1);
   1.203 +    }
   1.204 +
   1.205 +    /**
   1.206 +     * Atomically decrements by one the element at index {@code i}.
   1.207 +     *
   1.208 +     * @param i the index
   1.209 +     * @return the previous value
   1.210 +     */
   1.211 +    public final int getAndDecrement(int i) {
   1.212 +        return getAndAdd(i, -1);
   1.213 +    }
   1.214 +
   1.215 +    /**
   1.216 +     * Atomically adds the given value to the element at index {@code i}.
   1.217 +     *
   1.218 +     * @param i the index
   1.219 +     * @param delta the value to add
   1.220 +     * @return the previous value
   1.221 +     */
   1.222 +    public final int getAndAdd(int i, int delta) {
   1.223 +        long offset = checkedByteOffset(i);
   1.224 +        while (true) {
   1.225 +            int current = getRaw(offset);
   1.226 +            if (compareAndSetRaw(offset, current, current + delta))
   1.227 +                return current;
   1.228 +        }
   1.229 +    }
   1.230 +
   1.231 +    /**
   1.232 +     * Atomically increments by one the element at index {@code i}.
   1.233 +     *
   1.234 +     * @param i the index
   1.235 +     * @return the updated value
   1.236 +     */
   1.237 +    public final int incrementAndGet(int i) {
   1.238 +        return addAndGet(i, 1);
   1.239 +    }
   1.240 +
   1.241 +    /**
   1.242 +     * Atomically decrements by one the element at index {@code i}.
   1.243 +     *
   1.244 +     * @param i the index
   1.245 +     * @return the updated value
   1.246 +     */
   1.247 +    public final int decrementAndGet(int i) {
   1.248 +        return addAndGet(i, -1);
   1.249 +    }
   1.250 +
   1.251 +    /**
   1.252 +     * Atomically adds the given value to the element at index {@code i}.
   1.253 +     *
   1.254 +     * @param i the index
   1.255 +     * @param delta the value to add
   1.256 +     * @return the updated value
   1.257 +     */
   1.258 +    public final int addAndGet(int i, int delta) {
   1.259 +        long offset = checkedByteOffset(i);
   1.260 +        while (true) {
   1.261 +            int current = getRaw(offset);
   1.262 +            int next = current + delta;
   1.263 +            if (compareAndSetRaw(offset, current, next))
   1.264 +                return next;
   1.265 +        }
   1.266 +    }
   1.267 +
   1.268 +    /**
   1.269 +     * Returns the String representation of the current values of array.
   1.270 +     * @return the String representation of the current values of array
   1.271 +     */
   1.272 +    public String toString() {
   1.273 +        int iMax = array.length - 1;
   1.274 +        if (iMax == -1)
   1.275 +            return "[]";
   1.276 +
   1.277 +        StringBuilder b = new StringBuilder();
   1.278 +        b.append('[');
   1.279 +        for (int i = 0; ; i++) {
   1.280 +            b.append(getRaw(byteOffset(i)));
   1.281 +            if (i == iMax)
   1.282 +                return b.append(']').toString();
   1.283 +            b.append(',').append(' ');
   1.284 +        }
   1.285 +    }
   1.286 +
   1.287 +}