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 long offset = checkedByteOffset(i); 148 while (true) { 149 long current = getRaw(offset); 150 if (compareAndSetRaw(offset, current, newValue)) 151 return current; 152 } 153 } 154 155 /** 156 * Atomically sets the element at position {@code i} to the given 157 * updated value if the current value {@code ==} the expected value. 158 * 159 * @param i the index 160 * @param expect the expected value 161 * @param update the new value 162 * @return true if successful. False return indicates that 163 * the actual value was not equal to the expected value. 164 */ 165 public final boolean compareAndSet(int i, long expect, long update) { 166 return compareAndSetRaw(checkedByteOffset(i), expect, update); 167 } 168 169 private boolean compareAndSetRaw(long offset, long expect, long update) { 170 return unsafe.compareAndSwapLong(array, offset, expect, update); 171 } 172 173 /** 174 * Atomically sets the element at position {@code i} to the given 175 * updated value if the current value {@code ==} the expected value. 176 * 177 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 178 * and does not provide ordering guarantees, so is only rarely an 179 * appropriate alternative to {@code compareAndSet}. 180 * 181 * @param i the index 182 * @param expect the expected value 183 * @param update the new value 184 * @return true if successful. 185 */ 186 public final boolean weakCompareAndSet(int i, long expect, long update) { 187 return compareAndSet(i, expect, update); 188 } 189 190 /** 191 * Atomically increments by one the element at index {@code i}. 192 * 193 * @param i the index 194 * @return the previous value 195 */ 196 public final long getAndIncrement(int i) { 197 return getAndAdd(i, 1); 198 } 199 200 /** 201 * Atomically decrements by one the element at index {@code i}. 202 * 203 * @param i the index 204 * @return the previous value 205 */ 206 public final long getAndDecrement(int i) { 207 return getAndAdd(i, -1); 208 } 209 210 /** 211 * Atomically adds the given value to the element at index {@code i}. 212 * 213 * @param i the index 214 * @param delta the value to add 215 * @return the previous value 216 */ 217 public final long getAndAdd(int i, long delta) { 218 long offset = checkedByteOffset(i); 219 while (true) { 220 long current = getRaw(offset); 221 if (compareAndSetRaw(offset, current, current + delta)) 222 return current; 223 } 224 } 225 226 /** 227 * Atomically increments by one the element at index {@code i}. 228 * 229 * @param i the index 230 * @return the updated value 231 */ 232 public final long incrementAndGet(int i) { 233 return addAndGet(i, 1); 234 } 235 236 /** 237 * Atomically decrements by one the element at index {@code i}. 238 * 239 * @param i the index 240 * @return the updated value 241 */ 242 public final long decrementAndGet(int i) { 243 return addAndGet(i, -1); 244 } 245 246 /** 247 * Atomically adds the given value to the element at index {@code i}. 248 * 249 * @param i the index 250 * @param delta the value to add 251 * @return the updated value 252 */ 253 public long addAndGet(int i, long delta) { 254 long offset = checkedByteOffset(i); 255 while (true) { 256 long current = getRaw(offset); 257 long next = current + delta; 258 if (compareAndSetRaw(offset, current, next)) 259 return next; 260 } 261 } 262 263 /** 264 * Returns the String representation of the current values of array. 265 * @return the String representation of the current values of array 266 */ 267 public String toString() { 268 int iMax = array.length - 1; 269 if (iMax == -1) 270 return "[]"; 271 272 StringBuilder b = new StringBuilder(); 273 b.append('['); 274 for (int i = 0; ; i++) { 275 b.append(getRaw(byteOffset(i))); 276 if (i == iMax) 277 return b.append(']').toString(); 278 b.append(',').append(' '); 279 } 280 } 281 282 }