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 * An {@code int} array in which elements may be updated atomically.
043 * See the {@link java.util.concurrent.atomic} package
044 * specification for description of the properties of atomic
045 * variables.
046 * @since 1.5
047 * @author Doug Lea
048 */
049 public class AtomicIntegerArray implements java.io.Serializable {
050 private static final long serialVersionUID = 2862133569453604235L;
051
052 // setup to use Unsafe.compareAndSwapInt for updates
053 private static final Unsafe unsafe = Unsafe.getUnsafe();
054 private static final int base = unsafe.arrayBaseOffset(int[].class);
055 private static final int scale = unsafe
056 .arrayIndexScale(int[].class);
057 private final int[] 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 AtomicIntegerArray of given length.
067 *
068 * @param length the length of the array
069 */
070 public AtomicIntegerArray(int length) {
071 array = new int[length];
072 // must perform at least one volatile write to conform to JMM
073 if (length > 0)
074 unsafe.putIntVolatile(array, rawIndex(0), 0);
075 }
076
077 /**
078 * Creates a new AtomicIntegerArray 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 AtomicIntegerArray(int[] array) {
085 if (array == null)
086 throw new NullPointerException();
087 int length = array.length;
088 this .array = new int[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.putIntVolatile(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 int get(int i) {
115 return unsafe.getIntVolatile(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, int newValue) {
125 unsafe.putIntVolatile(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, int newValue) {
136 unsafe.putOrderedInt(array, rawIndex(i), newValue);
137 }
138
139 /**
140 * Atomically sets the element at position {@code i} to the given
141 * value 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 int getAndSet(int i, int newValue) {
148 while (true) {
149 int current = get(i);
150 if (compareAndSet(i, 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, int expect, int update) {
166 return unsafe.compareAndSwapInt(array, rawIndex(i), expect,
167 update);
168 }
169
170 /**
171 * Atomically sets the element at position {@code i} to the given
172 * updated value 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, int expect, int update) {
184 return compareAndSet(i, expect, update);
185 }
186
187 /**
188 * Atomically increments by one the element at index {@code i}.
189 *
190 * @param i the index
191 * @return the previous value
192 */
193 public final int getAndIncrement(int i) {
194 while (true) {
195 int current = get(i);
196 int next = current + 1;
197 if (compareAndSet(i, current, next))
198 return current;
199 }
200 }
201
202 /**
203 * Atomically decrements by one the element at index {@code i}.
204 *
205 * @param i the index
206 * @return the previous value
207 */
208 public final int getAndDecrement(int i) {
209 while (true) {
210 int current = get(i);
211 int next = current - 1;
212 if (compareAndSet(i, current, next))
213 return current;
214 }
215 }
216
217 /**
218 * Atomically adds the given value to the element at index {@code i}.
219 *
220 * @param i the index
221 * @param delta the value to add
222 * @return the previous value
223 */
224 public final int getAndAdd(int i, int delta) {
225 while (true) {
226 int current = get(i);
227 int next = current + delta;
228 if (compareAndSet(i, current, next))
229 return current;
230 }
231 }
232
233 /**
234 * Atomically increments by one the element at index {@code i}.
235 *
236 * @param i the index
237 * @return the updated value
238 */
239 public final int incrementAndGet(int i) {
240 while (true) {
241 int current = get(i);
242 int next = current + 1;
243 if (compareAndSet(i, current, next))
244 return next;
245 }
246 }
247
248 /**
249 * Atomically decrements by one the element at index {@code i}.
250 *
251 * @param i the index
252 * @return the updated value
253 */
254 public final int decrementAndGet(int i) {
255 while (true) {
256 int current = get(i);
257 int next = current - 1;
258 if (compareAndSet(i, current, next))
259 return next;
260 }
261 }
262
263 /**
264 * Atomically adds the given value to the element at index {@code i}.
265 *
266 * @param i the index
267 * @param delta the value to add
268 * @return the updated value
269 */
270 public final int addAndGet(int i, int delta) {
271 while (true) {
272 int current = get(i);
273 int next = current + delta;
274 if (compareAndSet(i, current, next))
275 return next;
276 }
277 }
278
279 /**
280 * Returns the String representation of the current values of array.
281 * @return the String representation of the current values of array.
282 */
283 public String toString() {
284 if (array.length > 0) // force volatile read
285 get(0);
286 return Arrays.toString(array);
287 }
288
289 }
|