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/AtomicLongArray.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 + * A {@code long} array in which elements may be updated atomically.
1.45 + * See the {@link java.util.concurrent.atomic} package specification
1.46 + * for description of the properties of atomic variables.
1.47 + * @since 1.5
1.48 + * @author Doug Lea
1.49 + */
1.50 +public class AtomicLongArray implements java.io.Serializable {
1.51 + private static final long serialVersionUID = -2308431214976778248L;
1.52 +
1.53 + private static final Unsafe unsafe = Unsafe.getUnsafe();
1.54 + private static final int base = unsafe.arrayBaseOffset(long[].class);
1.55 + private static final int shift;
1.56 + private final long[] array;
1.57 +
1.58 + static {
1.59 + int scale = unsafe.arrayIndexScale(long[].class);
1.60 + if ((scale & (scale - 1)) != 0)
1.61 + throw new Error("data type scale not a power of two");
1.62 + shift = 31 - Integer.numberOfLeadingZeros(scale);
1.63 + }
1.64 +
1.65 + private long checkedByteOffset(int i) {
1.66 + if (i < 0 || i >= array.length)
1.67 + throw new IndexOutOfBoundsException("index " + i);
1.68 +
1.69 + return byteOffset(i);
1.70 + }
1.71 +
1.72 + private static long byteOffset(int i) {
1.73 + return ((long) i << shift) + base;
1.74 + }
1.75 +
1.76 + /**
1.77 + * Creates a new AtomicLongArray of the given length, with all
1.78 + * elements initially zero.
1.79 + *
1.80 + * @param length the length of the array
1.81 + */
1.82 + public AtomicLongArray(int length) {
1.83 + array = new long[length];
1.84 + }
1.85 +
1.86 + /**
1.87 + * Creates a new AtomicLongArray with the same length as, and
1.88 + * all elements copied from, the given array.
1.89 + *
1.90 + * @param array the array to copy elements from
1.91 + * @throws NullPointerException if array is null
1.92 + */
1.93 + public AtomicLongArray(long[] array) {
1.94 + // Visibility guaranteed by final field guarantees
1.95 + this.array = array.clone();
1.96 + }
1.97 +
1.98 + /**
1.99 + * Returns the length of the array.
1.100 + *
1.101 + * @return the length of the array
1.102 + */
1.103 + public final int length() {
1.104 + return array.length;
1.105 + }
1.106 +
1.107 + /**
1.108 + * Gets the current value at position {@code i}.
1.109 + *
1.110 + * @param i the index
1.111 + * @return the current value
1.112 + */
1.113 + public final long get(int i) {
1.114 + return getRaw(checkedByteOffset(i));
1.115 + }
1.116 +
1.117 + private long getRaw(long offset) {
1.118 + return unsafe.getLongVolatile(array, offset);
1.119 + }
1.120 +
1.121 + /**
1.122 + * Sets the element at position {@code i} to the given value.
1.123 + *
1.124 + * @param i the index
1.125 + * @param newValue the new value
1.126 + */
1.127 + public final void set(int i, long newValue) {
1.128 + unsafe.putLongVolatile(array, checkedByteOffset(i), newValue);
1.129 + }
1.130 +
1.131 + /**
1.132 + * Eventually sets the element at position {@code i} to the given value.
1.133 + *
1.134 + * @param i the index
1.135 + * @param newValue the new value
1.136 + * @since 1.6
1.137 + */
1.138 + public final void lazySet(int i, long newValue) {
1.139 + unsafe.putOrderedLong(array, checkedByteOffset(i), newValue);
1.140 + }
1.141 +
1.142 +
1.143 + /**
1.144 + * Atomically sets the element at position {@code i} to the given value
1.145 + * 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 long getAndSet(int i, long newValue) {
1.152 + long offset = checkedByteOffset(i);
1.153 + while (true) {
1.154 + long 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, long expect, long update) {
1.171 + return compareAndSetRaw(checkedByteOffset(i), expect, update);
1.172 + }
1.173 +
1.174 + private boolean compareAndSetRaw(long offset, long expect, long update) {
1.175 + return unsafe.compareAndSwapLong(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, long expect, long 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 long 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 long 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 long getAndAdd(int i, long delta) {
1.223 + long offset = checkedByteOffset(i);
1.224 + while (true) {
1.225 + long 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 long 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 long 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 long addAndGet(int i, long delta) {
1.259 + long offset = checkedByteOffset(i);
1.260 + while (true) {
1.261 + long current = getRaw(offset);
1.262 + long 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 +}