rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicReferenceArray.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/AtomicReferenceArray.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,213 @@
     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 array of object references in which elements may be updated
    1.45 + * atomically.  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 + * @param <E> The base class of elements held in this array
    1.51 + */
    1.52 +public class AtomicReferenceArray<E> implements java.io.Serializable {
    1.53 +    private static final long serialVersionUID = -6209656149925076980L;
    1.54 +
    1.55 +    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.56 +    private static final int base = unsafe.arrayBaseOffset(Object[].class);
    1.57 +    private static final int shift;
    1.58 +    private final Object[] array;
    1.59 +
    1.60 +    static {
    1.61 +        int scale = unsafe.arrayIndexScale(Object[].class);
    1.62 +        if ((scale & (scale - 1)) != 0)
    1.63 +            throw new Error("data type scale not a power of two");
    1.64 +        shift = 31 - Integer.numberOfLeadingZeros(scale);
    1.65 +    }
    1.66 +
    1.67 +    private long checkedByteOffset(int i) {
    1.68 +        if (i < 0 || i >= array.length)
    1.69 +            throw new IndexOutOfBoundsException("index " + i);
    1.70 +
    1.71 +        return byteOffset(i);
    1.72 +    }
    1.73 +
    1.74 +    private static long byteOffset(int i) {
    1.75 +        return ((long) i << shift) + base;
    1.76 +    }
    1.77 +
    1.78 +    /**
    1.79 +     * Creates a new AtomicReferenceArray of the given length, with all
    1.80 +     * elements initially null.
    1.81 +     *
    1.82 +     * @param length the length of the array
    1.83 +     */
    1.84 +    public AtomicReferenceArray(int length) {
    1.85 +        array = new Object[length];
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Creates a new AtomicReferenceArray with the same length as, and
    1.90 +     * all elements copied from, the given array.
    1.91 +     *
    1.92 +     * @param array the array to copy elements from
    1.93 +     * @throws NullPointerException if array is null
    1.94 +     */
    1.95 +    public AtomicReferenceArray(E[] array) {
    1.96 +        // Visibility guaranteed by final field guarantees
    1.97 +        this.array = array.clone();
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Returns the length of the array.
   1.102 +     *
   1.103 +     * @return the length of the array
   1.104 +     */
   1.105 +    public final int length() {
   1.106 +        return array.length;
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Gets the current value at position {@code i}.
   1.111 +     *
   1.112 +     * @param i the index
   1.113 +     * @return the current value
   1.114 +     */
   1.115 +    public final E get(int i) {
   1.116 +        return getRaw(checkedByteOffset(i));
   1.117 +    }
   1.118 +
   1.119 +    private E getRaw(long offset) {
   1.120 +        return (E) unsafe.getObjectVolatile(array, offset);
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Sets the element at position {@code i} to the given value.
   1.125 +     *
   1.126 +     * @param i the index
   1.127 +     * @param newValue the new value
   1.128 +     */
   1.129 +    public final void set(int i, E newValue) {
   1.130 +        unsafe.putObjectVolatile(array, checkedByteOffset(i), newValue);
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Eventually sets the element at position {@code i} to the given value.
   1.135 +     *
   1.136 +     * @param i the index
   1.137 +     * @param newValue the new value
   1.138 +     * @since 1.6
   1.139 +     */
   1.140 +    public final void lazySet(int i, E newValue) {
   1.141 +        unsafe.putOrderedObject(array, checkedByteOffset(i), newValue);
   1.142 +    }
   1.143 +
   1.144 +
   1.145 +    /**
   1.146 +     * Atomically sets the element at position {@code i} to the given
   1.147 +     * value and returns the old value.
   1.148 +     *
   1.149 +     * @param i the index
   1.150 +     * @param newValue the new value
   1.151 +     * @return the previous value
   1.152 +     */
   1.153 +    public final E getAndSet(int i, E newValue) {
   1.154 +        long offset = checkedByteOffset(i);
   1.155 +        while (true) {
   1.156 +            E current = (E) getRaw(offset);
   1.157 +            if (compareAndSetRaw(offset, current, newValue))
   1.158 +                return current;
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    /**
   1.163 +     * Atomically sets the element at position {@code i} to the given
   1.164 +     * updated value if the current value {@code ==} the expected value.
   1.165 +     *
   1.166 +     * @param i the index
   1.167 +     * @param expect the expected value
   1.168 +     * @param update the new value
   1.169 +     * @return true if successful. False return indicates that
   1.170 +     * the actual value was not equal to the expected value.
   1.171 +     */
   1.172 +    public final boolean compareAndSet(int i, E expect, E update) {
   1.173 +        return compareAndSetRaw(checkedByteOffset(i), expect, update);
   1.174 +    }
   1.175 +
   1.176 +    private boolean compareAndSetRaw(long offset, E expect, E update) {
   1.177 +        return unsafe.compareAndSwapObject(array, offset, expect, update);
   1.178 +    }
   1.179 +
   1.180 +    /**
   1.181 +     * Atomically sets the element at position {@code i} to the given
   1.182 +     * updated value if the current value {@code ==} the expected value.
   1.183 +     *
   1.184 +     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   1.185 +     * and does not provide ordering guarantees, so is only rarely an
   1.186 +     * appropriate alternative to {@code compareAndSet}.
   1.187 +     *
   1.188 +     * @param i the index
   1.189 +     * @param expect the expected value
   1.190 +     * @param update the new value
   1.191 +     * @return true if successful.
   1.192 +     */
   1.193 +    public final boolean weakCompareAndSet(int i, E expect, E update) {
   1.194 +        return compareAndSet(i, expect, update);
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * Returns the String representation of the current values of array.
   1.199 +     * @return the String representation of the current values of array
   1.200 +     */
   1.201 +    public String toString() {
   1.202 +           int iMax = array.length - 1;
   1.203 +        if (iMax == -1)
   1.204 +            return "[]";
   1.205 +
   1.206 +        StringBuilder b = new StringBuilder();
   1.207 +        b.append('[');
   1.208 +        for (int i = 0; ; i++) {
   1.209 +            b.append(getRaw(byteOffset(i)));
   1.210 +            if (i == iMax)
   1.211 +                return b.append(']').toString();
   1.212 +            b.append(',').append(' ');
   1.213 +        }
   1.214 +    }
   1.215 +
   1.216 +}