001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util.concurrent.atomic;
037
038 import sun.misc.Unsafe;
039 import java.util.*;
040
041 /**
042 * A {@code long} array in which elements may be updated atomically.
043 * See the {@link java.util.concurrent.atomic} package specification
044 * for description of the properties of atomic variables.
045 * @since 1.5
046 * @author Doug Lea
047 */
048 public class AtomicLongArray implements java.io.Serializable {
049 private static final long serialVersionUID = -2308431214976778248L;
050
051 // setup to use Unsafe.compareAndSwapInt for updates
052 private static final Unsafe unsafe = Unsafe.getUnsafe();
053 private static final int base = unsafe
054 .arrayBaseOffset(long[].class);
055 private static final int scale = unsafe
056 .arrayIndexScale(long[].class);
057 private final long[] array;
058
059 private long rawIndex(int i) {
060 if (i < 0 || i >= array.length)
061 throw new IndexOutOfBoundsException("index " + i);
062 return base + i * scale;
063 }
064
065 /**
066 * Creates a new AtomicLongArray of given length.
067 *
068 * @param length the length of the array
069 */
070 public AtomicLongArray(int length) {
071 array = new long[length];
072 // must perform at least one volatile write to conform to JMM
073 if (length > 0)
074 unsafe.putLongVolatile(array, rawIndex(0), 0);
075 }
076
077 /**
078 * Creates a new AtomicLongArray with the same length as, and
079 * all elements copied from, the given array.
080 *
081 * @param array the array to copy elements from
082 * @throws NullPointerException if array is null
083 */
084 public AtomicLongArray(long[] array) {
085 if (array == null)
086 throw new NullPointerException();
087 int length = array.length;
088 this .array = new long[length];
089 if (length > 0) {
090 int last = length - 1;
091 for (int i = 0; i < last; ++i)
092 this .array[i] = array[i];
093 // Do the last write as volatile
094 unsafe.putLongVolatile(this .array, rawIndex(last),
095 array[last]);
096 }
097 }
098
099 /**
100 * Returns the length of the array.
101 *
102 * @return the length of the array
103 */
104 public final int length() {
105 return array.length;
106 }
107
108 /**
109 * Gets the current value at position {@code i}.
110 *
111 * @param i the index
112 * @return the current value
113 */
114 public final long get(int i) {
115 return unsafe.getLongVolatile(array, rawIndex(i));
116 }
117
118 /**
119 * Sets the element at position {@code i} to the given value.
120 *
121 * @param i the index
122 * @param newValue the new value
123 */
124 public final void set(int i, long newValue) {
125 unsafe.putLongVolatile(array, rawIndex(i), newValue);
126 }
127
128 /**
129 * Eventually sets the element at position {@code i} to the given value.
130 *
131 * @param i the index
132 * @param newValue the new value
133 * @since 1.6
134 */
135 public final void lazySet(int i, long newValue) {
136 unsafe.putOrderedLong(array, rawIndex(i), newValue);
137 }
138
139 /**
140 * Atomically sets the element at position {@code i} to the given value
141 * and returns the old value.
142 *
143 * @param i the index
144 * @param newValue the new value
145 * @return the previous value
146 */
147 public final long getAndSet(int i, long newValue) {
148 while (true) {
149 long current = get(i);
150 if (compareAndSet(i, current, newValue))
151 return current;
152 }
153 }
154
155 /**
156 * Atomically sets the value to the given updated value
157 * 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 unsafe.compareAndSwapLong(array, rawIndex(i), expect,
167 update);
168 }
169
170 /**
171 * Atomically sets the value to the given updated value
172 * if the current value {@code ==} the expected value.
173 *
174 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
175 * and does not provide ordering guarantees, so is only rarely an
176 * appropriate alternative to {@code compareAndSet}.
177 *
178 * @param i the index
179 * @param expect the expected value
180 * @param update the new value
181 * @return true if successful.
182 */
183 public final boolean weakCompareAndSet(int i, long expect,
184 long update) {
185 return compareAndSet(i, expect, update);
186 }
187
188 /**
189 * Atomically increments by one the element at index {@code i}.
190 *
191 * @param i the index
192 * @return the previous value
193 */
194 public final long getAndIncrement(int i) {
195 while (true) {
196 long current = get(i);
197 long next = current + 1;
198 if (compareAndSet(i, current, next))
199 return current;
200 }
201 }
202
203 /**
204 * Atomically decrements by one the element at index {@code i}.
205 *
206 * @param i the index
207 * @return the previous value
208 */
209 public final long getAndDecrement(int i) {
210 while (true) {
211 long current = get(i);
212 long next = current - 1;
213 if (compareAndSet(i, current, next))
214 return current;
215 }
216 }
217
218 /**
219 * Atomically adds the given value to the element at index {@code i}.
220 *
221 * @param i the index
222 * @param delta the value to add
223 * @return the previous value
224 */
225 public final long getAndAdd(int i, long delta) {
226 while (true) {
227 long current = get(i);
228 long next = current + delta;
229 if (compareAndSet(i, current, next))
230 return current;
231 }
232 }
233
234 /**
235 * Atomically increments by one the element at index {@code i}.
236 *
237 * @param i the index
238 * @return the updated value
239 */
240 public final long incrementAndGet(int i) {
241 while (true) {
242 long current = get(i);
243 long next = current + 1;
244 if (compareAndSet(i, current, next))
245 return next;
246 }
247 }
248
249 /**
250 * Atomically decrements by one the element at index {@code i}.
251 *
252 * @param i the index
253 * @return the updated value
254 */
255 public final long decrementAndGet(int i) {
256 while (true) {
257 long current = get(i);
258 long next = current - 1;
259 if (compareAndSet(i, current, next))
260 return next;
261 }
262 }
263
264 /**
265 * Atomically adds the given value to the element at index {@code i}.
266 *
267 * @param i the index
268 * @param delta the value to add
269 * @return the updated value
270 */
271 public long addAndGet(int i, long delta) {
272 while (true) {
273 long current = get(i);
274 long next = current + delta;
275 if (compareAndSet(i, current, next))
276 return next;
277 }
278 }
279
280 /**
281 * Returns the String representation of the current values of array.
282 * @return the String representation of the current values of array.
283 */
284 public String toString() {
285 if (array.length > 0) // force volatile read
286 get(0);
287 return Arrays.toString(array);
288 }
289
290 }
|