1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent.atomic; 37 import sun.misc.Unsafe; 38 39 /** 40 * A {@code long} array in which elements may be updated atomically. 41 * See the {@link java.util.concurrent.atomic} package specification 42 * for description of the properties of atomic variables. 43 * @since 1.5 44 * @author Doug Lea 45 */ 46 public class AtomicLongArray implements java.io.Serializable { 47 private static final long serialVersionUID = -2308431214976778248L; 48 49 private static final Unsafe unsafe = Unsafe.getUnsafe(); 50 private static final int base = unsafe.arrayBaseOffset(long[].class); 51 private static final int shift; 52 private final long[] array; 53 54 static { 55 int scale = unsafe.arrayIndexScale(long[].class); 56 if ((scale & (scale - 1)) != 0) 57 throw new Error("data type scale not a power of two"); 58 shift = 31 - Integer.numberOfLeadingZeros(scale); 59 } 60 61 private long checkedByteOffset(int i) { 62 if (i < 0 || i >= array.length) 63 throw new IndexOutOfBoundsException("index " + i); 64 65 return byteOffset(i); 66 } 67 68 private static long byteOffset(int i) { 69 return ((long) i << shift) + base; 70 } 71 72 /** 73 * Creates a new AtomicLongArray of the given length, with all 74 * elements initially zero. 75 * 76 * @param length the length of the array 77 */ 78 public AtomicLongArray(int length) { 79 array = new long[length]; 80 } 81 82 /** 83 * Creates a new AtomicLongArray with the same length as, and 84 * all elements copied from, the given array. 85 * 86 * @param array the array to copy elements from 87 * @throws NullPointerException if array is null 88 */ 89 public AtomicLongArray(long[] array) { 90 // Visibility guaranteed by final field guarantees 91 this.array = array.clone(); 92 } 93 94 /** 95 * Returns the length of the array. 96 * 97 * @return the length of the array 98 */ 99 public final int length() { 100 return array.length; 101 } 102 103 /** 104 * Gets the current value at position {@code i}. 105 * 106 * @param i the index 107 * @return the current value 108 */ 109 public final long get(int i) { 110 return getRaw(checkedByteOffset(i)); 111 } 112 113 private long getRaw(long offset) { 114 return unsafe.getLongVolatile(array, offset); 115 } 116 117 /** 118 * Sets the element at position {@code i} to the given value. 119 * 120 * @param i the index 121 * @param newValue the new value 122 */ 123 public final void set(int i, long newValue) { 124 unsafe.putLongVolatile(array, checkedByteOffset(i), newValue); 125 } 126 127 /** 128 * Eventually sets the element at position {@code i} to the given value. 129 * 130 * @param i the index 131 * @param newValue the new value 132 * @since 1.6 133 */ 134 public final void lazySet(int i, long newValue) { 135 unsafe.putOrderedLong(array, checkedByteOffset(i), newValue); 136 } 137 138 /** 139 * Atomically sets the element at position {@code i} to the given value 140 * and returns the old value. 141 * 142 * @param i the index 143 * @param newValue the new value 144 * @return the previous value 145 */ 146 public final long getAndSet(int i, long newValue) { 147 return unsafe.getAndSetLong(array, checkedByteOffset(i), newValue); 148 } 149 150 /** 151 * Atomically sets the element at position {@code i} to the given 152 * updated value if the current value {@code ==} the expected value. 153 * 154 * @param i the index 155 * @param expect the expected value 156 * @param update the new value 157 * @return true if successful. False return indicates that 158 * the actual value was not equal to the expected value. 159 */ 160 public final boolean compareAndSet(int i, long expect, long update) { 161 return compareAndSetRaw(checkedByteOffset(i), expect, update); 162 } 163 164 private boolean compareAndSetRaw(long offset, long expect, long update) { 165 return unsafe.compareAndSwapLong(array, offset, expect, update); 166 } 167 168 /** 169 * Atomically sets the element at position {@code i} to the given 170 * updated value if the current value {@code ==} the expected value. 171 * 172 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 173 * and does not provide ordering guarantees, so is only rarely an 174 * appropriate alternative to {@code compareAndSet}. 175 * 176 * @param i the index 177 * @param expect the expected value 178 * @param update the new value 179 * @return true if successful 180 */ 181 public final boolean weakCompareAndSet(int i, long expect, long update) { 182 return compareAndSet(i, expect, update); 183 } 184 185 /** 186 * Atomically increments by one the element at index {@code i}. 187 * 188 * @param i the index 189 * @return the previous value 190 */ 191 public final long getAndIncrement(int i) { 192 return getAndAdd(i, 1); 193 } 194 195 /** 196 * Atomically decrements by one the element at index {@code i}. 197 * 198 * @param i the index 199 * @return the previous value 200 */ 201 public final long getAndDecrement(int i) { 202 return getAndAdd(i, -1); 203 } 204 205 /** 206 * Atomically adds the given value to the element at index {@code i}. 207 * 208 * @param i the index 209 * @param delta the value to add 210 * @return the previous value 211 */ 212 public final long getAndAdd(int i, long delta) { 213 return unsafe.getAndAddLong(array, checkedByteOffset(i), delta); 214 } 215 216 /** 217 * Atomically increments by one the element at index {@code i}. 218 * 219 * @param i the index 220 * @return the updated value 221 */ 222 public final long incrementAndGet(int i) { 223 return getAndAdd(i, 1) + 1; 224 } 225 226 /** 227 * Atomically decrements by one the element at index {@code i}. 228 * 229 * @param i the index 230 * @return the updated value 231 */ 232 public final long decrementAndGet(int i) { 233 return getAndAdd(i, -1) - 1; 234 } 235 236 /** 237 * Atomically adds the given value to the element at index {@code i}. 238 * 239 * @param i the index 240 * @param delta the value to add 241 * @return the updated value 242 */ 243 public long addAndGet(int i, long delta) { 244 return getAndAdd(i, delta) + delta; 245 } 246 247 /** 248 * Returns the String representation of the current values of array. 249 * @return the String representation of the current values of array 250 */ 251 public String toString() { 252 int iMax = array.length - 1; 253 if (iMax == -1) 254 return "[]"; 255 256 StringBuilder b = new StringBuilder(); 257 b.append('['); 258 for (int i = 0; ; i++) { 259 b.append(getRaw(byteOffset(i))); 260 if (i == iMax) 261 return b.append(']').toString(); 262 b.append(',').append(' '); 263 } 264 } 265 266 }