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