author | dl |
Tue, 26 Jul 2016 09:49:25 -0700 | |
changeset 39777 | a9b80432c521 |
parent 39725 | 9548f8d846e9 |
child 40733 | 8d1263354d62 |
permissions | -rw-r--r-- |
2 | 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 |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
5506
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent.atomic; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
37 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
38 |
import java.lang.invoke.VarHandle; |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
39 |
import java.util.function.IntBinaryOperator; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
40 |
import java.util.function.IntUnaryOperator; |
2 | 41 |
|
42 |
/** |
|
43 |
* An {@code int} value that may be updated atomically. See the |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
44 |
* {@link VarHandle} specification for descriptions of the properties |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
45 |
* of atomic accesses. An {@code AtomicInteger} is used in |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
46 |
* applications such as atomically incremented counters, and cannot be |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
47 |
* used as a replacement for an {@link java.lang.Integer}. However, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
48 |
* this class does extend {@code Number} to allow uniform access by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
49 |
* tools and utilities that deal with numerically-based classes. |
2 | 50 |
* |
51 |
* @since 1.5 |
|
52 |
* @author Doug Lea |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
53 |
*/ |
2 | 54 |
public class AtomicInteger extends Number implements java.io.Serializable { |
55 |
private static final long serialVersionUID = 6214790243416807050L; |
|
39777 | 56 |
|
57 |
/* |
|
58 |
* This class intended to be implemented using VarHandles, but there |
|
59 |
* are unresolved cyclic startup dependencies. |
|
60 |
*/ |
|
61 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
|
62 |
private static final long VALUE; |
|
63 |
||
2 | 64 |
static { |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
65 |
try { |
39777 | 66 |
VALUE = U.objectFieldOffset |
67 |
(AtomicInteger.class.getDeclaredField("value")); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
68 |
} catch (ReflectiveOperationException e) { |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
69 |
throw new Error(e); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
70 |
} |
2 | 71 |
} |
72 |
||
73 |
private volatile int value; |
|
74 |
||
75 |
/** |
|
76 |
* Creates a new AtomicInteger with the given initial value. |
|
77 |
* |
|
78 |
* @param initialValue the initial value |
|
79 |
*/ |
|
80 |
public AtomicInteger(int initialValue) { |
|
81 |
value = initialValue; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Creates a new AtomicInteger with initial value {@code 0}. |
|
86 |
*/ |
|
87 |
public AtomicInteger() { |
|
88 |
} |
|
89 |
||
90 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
91 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
92 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
2 | 93 |
* |
94 |
* @return the current value |
|
95 |
*/ |
|
96 |
public final int get() { |
|
97 |
return value; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
101 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
102 |
* with memory effects as specified by {@link VarHandle#setVolatile}. |
2 | 103 |
* |
104 |
* @param newValue the new value |
|
105 |
*/ |
|
106 |
public final void set(int newValue) { |
|
107 |
value = newValue; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
111 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
112 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
2 | 113 |
* |
114 |
* @param newValue the new value |
|
115 |
* @since 1.6 |
|
116 |
*/ |
|
117 |
public final void lazySet(int newValue) { |
|
39777 | 118 |
U.putIntRelease(this, VALUE, newValue); |
2 | 119 |
} |
120 |
||
121 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
122 |
* Atomically sets the value to {@code newValue} and returns the old value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
123 |
* with memory effects as specified by {@link VarHandle#getAndSet}. |
2 | 124 |
* |
125 |
* @param newValue the new value |
|
126 |
* @return the previous value |
|
127 |
*/ |
|
128 |
public final int getAndSet(int newValue) { |
|
39777 | 129 |
return U.getAndSetInt(this, VALUE, newValue); |
2 | 130 |
} |
131 |
||
132 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
133 |
* Atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
134 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
135 |
* with memory effects as specified by {@link VarHandle#compareAndSet}. |
2 | 136 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
137 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
138 |
* @param newValue the new value |
18576 | 139 |
* @return {@code true} if successful. False return indicates that |
2 | 140 |
* the actual value was not equal to the expected value. |
141 |
*/ |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
142 |
public final boolean compareAndSet(int expectedValue, int newValue) { |
39777 | 143 |
return U.compareAndSwapInt(this, VALUE, expectedValue, newValue); |
2 | 144 |
} |
145 |
||
146 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
147 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
148 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
149 |
* with memory effects as specified by {@link VarHandle#weakCompareAndSet}. |
2 | 150 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
151 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
152 |
* @param newValue the new value |
18576 | 153 |
* @return {@code true} if successful |
2 | 154 |
*/ |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
155 |
public final boolean weakCompareAndSet(int expectedValue, int newValue) { |
39777 | 156 |
return U.weakCompareAndSwapInt(this, VALUE, expectedValue, newValue); |
2 | 157 |
} |
158 |
||
159 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
160 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
161 |
* with memory effects as specified by {@link VarHandle#getAndAdd}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
162 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
163 |
* <p>Equivalent to {@code getAndAdd(1)}. |
2 | 164 |
* |
165 |
* @return the previous value |
|
166 |
*/ |
|
167 |
public final int getAndIncrement() { |
|
39777 | 168 |
return U.getAndAddInt(this, VALUE, 1); |
2 | 169 |
} |
170 |
||
171 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
172 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
173 |
* with memory effects as specified by {@link VarHandle#getAndAdd}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
174 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
175 |
* <p>Equivalent to {@code getAndAdd(-1)}. |
2 | 176 |
* |
177 |
* @return the previous value |
|
178 |
*/ |
|
179 |
public final int getAndDecrement() { |
|
39777 | 180 |
return U.getAndAddInt(this, VALUE, -1); |
2 | 181 |
} |
182 |
||
183 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
184 |
* Atomically adds the given value to the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
185 |
* with memory effects as specified by {@link VarHandle#getAndAdd}. |
2 | 186 |
* |
187 |
* @param delta the value to add |
|
188 |
* @return the previous value |
|
189 |
*/ |
|
190 |
public final int getAndAdd(int delta) { |
|
39777 | 191 |
return U.getAndAddInt(this, VALUE, delta); |
2 | 192 |
} |
193 |
||
194 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
195 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
196 |
* with memory effects as specified by {@link VarHandle#addAndGet}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
197 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
198 |
* <p>Equivalent to {@code addAndGet(1)}. |
2 | 199 |
* |
200 |
* @return the updated value |
|
201 |
*/ |
|
202 |
public final int incrementAndGet() { |
|
39777 | 203 |
return U.getAndAddInt(this, VALUE, 1) + 1; |
2 | 204 |
} |
205 |
||
206 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
207 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
208 |
* with memory effects as specified by {@link VarHandle#addAndGet}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
209 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
210 |
* <p>Equivalent to {@code addAndGet(-1)}. |
2 | 211 |
* |
212 |
* @return the updated value |
|
213 |
*/ |
|
214 |
public final int decrementAndGet() { |
|
39777 | 215 |
return U.getAndAddInt(this, VALUE, -1) - 1; |
2 | 216 |
} |
217 |
||
218 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
219 |
* Atomically adds the given value to the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
220 |
* with memory effects as specified by {@link VarHandle#addAndGet}. |
2 | 221 |
* |
222 |
* @param delta the value to add |
|
223 |
* @return the updated value |
|
224 |
*/ |
|
225 |
public final int addAndGet(int delta) { |
|
39777 | 226 |
return U.getAndAddInt(this, VALUE, delta) + delta; |
2 | 227 |
} |
228 |
||
229 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
* applying the given function, returning the previous value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
234 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
235 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
236 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
237 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
238 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
239 |
public final int getAndUpdate(IntUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
240 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
241 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
242 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
243 |
next = updateFunction.applyAsInt(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
244 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
245 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
246 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
247 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
248 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
249 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* applying the given function, returning the updated value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
253 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
256 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
257 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
258 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
259 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
260 |
public final int updateAndGet(IntUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
261 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
262 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
263 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
264 |
next = updateFunction.applyAsInt(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
265 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
266 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
267 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
268 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
269 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
270 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
271 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
273 |
* applying the given function to the current and given values, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
* returning the previous value. The function should be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
* updates fail due to contention among threads. The function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
* is applied with the current value as its first argument, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
* and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
284 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
public final int getAndAccumulate(int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
IntBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
287 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
288 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
289 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
290 |
next = accumulatorFunction.applyAsInt(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
291 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
292 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
293 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
294 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
296 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
298 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
* applying the given function to the current and given values, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
* returning the updated value. The function should be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
* side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
302 |
* updates fail due to contention among threads. The function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
* is applied with the current value as its first argument, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
304 |
* and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
public final int accumulateAndGet(int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
312 |
IntBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
313 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
314 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
315 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
316 |
next = accumulatorFunction.applyAsInt(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
317 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
318 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
319 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
320 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
321 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
322 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
323 |
/** |
2 | 324 |
* Returns the String representation of the current value. |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
325 |
* @return the String representation of the current value |
2 | 326 |
*/ |
327 |
public String toString() { |
|
328 |
return Integer.toString(get()); |
|
329 |
} |
|
330 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
331 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
332 |
* Returns the current value of this {@code AtomicInteger} as an |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
333 |
* {@code int}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
334 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
335 |
* |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
336 |
* Equivalent to {@link #get()}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
337 |
*/ |
2 | 338 |
public int intValue() { |
339 |
return get(); |
|
340 |
} |
|
341 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
342 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
343 |
* Returns the current value of this {@code AtomicInteger} as a |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
344 |
* {@code long} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
345 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
346 |
* @jls 5.1.2 Widening Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
347 |
*/ |
2 | 348 |
public long longValue() { |
349 |
return (long)get(); |
|
350 |
} |
|
351 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
352 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
353 |
* Returns the current value of this {@code AtomicInteger} as a |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
354 |
* {@code float} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
355 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
356 |
* @jls 5.1.2 Widening Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
357 |
*/ |
2 | 358 |
public float floatValue() { |
359 |
return (float)get(); |
|
360 |
} |
|
361 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
362 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
363 |
* Returns the current value of this {@code AtomicInteger} as a |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
364 |
* {@code double} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
365 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
366 |
* @jls 5.1.2 Widening Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
367 |
*/ |
2 | 368 |
public double doubleValue() { |
369 |
return (double)get(); |
|
370 |
} |
|
371 |
||
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
372 |
// jdk9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
373 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
374 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
375 |
* Returns the current value, with memory semantics of reading as |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
376 |
* if the variable was declared non-{@code volatile}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
377 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
378 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
379 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
380 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
381 |
public final int getPlain() { |
39777 | 382 |
return U.getInt(this, VALUE); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
383 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
384 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
385 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
386 |
* Sets the value to {@code newValue}, with memory semantics |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
387 |
* of setting as if the variable was declared non-{@code volatile} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
388 |
* and non-{@code final}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
389 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
390 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
391 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
392 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
393 |
public final void setPlain(int newValue) { |
39777 | 394 |
U.putInt(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
395 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
396 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
397 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
398 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
399 |
* with memory effects as specified by {@link VarHandle#getOpaque}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
400 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
401 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
402 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
403 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
404 |
public final int getOpaque() { |
39777 | 405 |
return U.getIntOpaque(this, VALUE); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
406 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
407 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
408 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
409 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
410 |
* with memory effects as specified by {@link VarHandle#setOpaque}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
411 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
412 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
413 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
414 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
415 |
public final void setOpaque(int newValue) { |
39777 | 416 |
U.putIntOpaque(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
417 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
418 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
419 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
420 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
421 |
* with memory effects as specified by {@link VarHandle#getAcquire}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
422 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
423 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
424 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
425 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
426 |
public final int getAcquire() { |
39777 | 427 |
return U.getIntAcquire(this, VALUE); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
428 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
429 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
430 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
431 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
432 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
433 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
434 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
435 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
436 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
437 |
public final void setRelease(int newValue) { |
39777 | 438 |
U.putIntRelease(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
439 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
440 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
441 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
442 |
* Atomically sets the value to {@code newValue} if the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
443 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
444 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
445 |
* {@link VarHandle#compareAndExchange}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
446 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
447 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
448 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
449 |
* @return the witness value, which will be the same as the |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
450 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
451 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
452 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
453 |
public final int compareAndExchange(int expectedValue, int newValue) { |
39777 | 454 |
return U.compareAndExchangeIntVolatile(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
455 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
456 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
457 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
458 |
* Atomically sets the value to {@code newValue} if the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
459 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
460 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
461 |
* {@link VarHandle#compareAndExchangeAcquire}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
462 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
463 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
464 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
465 |
* @return the witness value, which will be the same as the |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
466 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
467 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
468 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
469 |
public final int compareAndExchangeAcquire(int expectedValue, int newValue) { |
39777 | 470 |
return U.compareAndExchangeIntAcquire(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
471 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
472 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
473 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
474 |
* Atomically sets the value to {@code newValue} if the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
475 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
476 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
477 |
* {@link VarHandle#compareAndExchangeRelease}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
478 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
479 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
480 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
481 |
* @return the witness value, which will be the same as the |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
482 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
483 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
484 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
485 |
public final int compareAndExchangeRelease(int expectedValue, int newValue) { |
39777 | 486 |
return U.compareAndExchangeIntRelease(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
487 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
488 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
489 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
490 |
* Possibly atomically sets the value to {@code newValue} if |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
491 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
492 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
493 |
* {@link VarHandle#weakCompareAndSetVolatile}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
494 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
495 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
496 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
497 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
498 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
499 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
500 |
public final boolean weakCompareAndSetVolatile(int expectedValue, int newValue) { |
39777 | 501 |
return U.weakCompareAndSwapIntVolatile(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
502 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
503 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
504 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
505 |
* Possibly atomically sets the value to {@code newValue} if |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
506 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
507 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
508 |
* {@link VarHandle#weakCompareAndSetAcquire}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
509 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
510 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
511 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
512 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
513 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
514 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
515 |
public final boolean weakCompareAndSetAcquire(int expectedValue, int newValue) { |
39777 | 516 |
return U.weakCompareAndSwapIntAcquire(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
517 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
518 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
519 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
520 |
* Possibly atomically sets the value to {@code newValue} if |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
521 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
522 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
523 |
* {@link VarHandle#weakCompareAndSetRelease}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
524 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
525 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
526 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
527 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
528 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
529 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
530 |
public final boolean weakCompareAndSetRelease(int expectedValue, int newValue) { |
39777 | 531 |
return U.weakCompareAndSwapIntRelease(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
532 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
533 |
|
2 | 534 |
} |