author | dl |
Fri, 15 Jul 2016 14:04:09 -0700 | |
changeset 39725 | 9548f8d846e9 |
parent 36936 | bfcdf736a998 |
child 39777 | a9b80432c521 |
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.MethodHandles; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
39 |
import java.lang.invoke.VarHandle; |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
40 |
import java.util.function.IntBinaryOperator; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
41 |
import java.util.function.IntUnaryOperator; |
2 | 42 |
|
43 |
/** |
|
44 |
* 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
|
45 |
* {@link VarHandle} specification for descriptions of the properties |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
46 |
* 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
|
47 |
* 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
|
48 |
* 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
|
49 |
* 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
|
50 |
* tools and utilities that deal with numerically-based classes. |
2 | 51 |
* |
52 |
* @since 1.5 |
|
53 |
* @author Doug Lea |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
54 |
*/ |
2 | 55 |
public class AtomicInteger extends Number implements java.io.Serializable { |
56 |
private static final long serialVersionUID = 6214790243416807050L; |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
57 |
private static final VarHandle VALUE; |
2 | 58 |
static { |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
59 |
try { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
60 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
61 |
VALUE = l.findVarHandle(AtomicInteger.class, "value", int.class); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
62 |
} catch (ReflectiveOperationException e) { |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
63 |
throw new Error(e); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
64 |
} |
2 | 65 |
} |
66 |
||
67 |
private volatile int value; |
|
68 |
||
69 |
/** |
|
70 |
* Creates a new AtomicInteger with the given initial value. |
|
71 |
* |
|
72 |
* @param initialValue the initial value |
|
73 |
*/ |
|
74 |
public AtomicInteger(int initialValue) { |
|
75 |
value = initialValue; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Creates a new AtomicInteger with initial value {@code 0}. |
|
80 |
*/ |
|
81 |
public AtomicInteger() { |
|
82 |
} |
|
83 |
||
84 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
85 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
86 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
2 | 87 |
* |
88 |
* @return the current value |
|
89 |
*/ |
|
90 |
public final int get() { |
|
91 |
return value; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
95 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
96 |
* with memory effects as specified by {@link VarHandle#setVolatile}. |
2 | 97 |
* |
98 |
* @param newValue the new value |
|
99 |
*/ |
|
100 |
public final void set(int newValue) { |
|
101 |
value = newValue; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
105 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
106 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
2 | 107 |
* |
108 |
* @param newValue the new value |
|
109 |
* @since 1.6 |
|
110 |
*/ |
|
111 |
public final void lazySet(int newValue) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
112 |
VALUE.setRelease(this, newValue); |
2 | 113 |
} |
114 |
||
115 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
116 |
* 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
|
117 |
* with memory effects as specified by {@link VarHandle#getAndSet}. |
2 | 118 |
* |
119 |
* @param newValue the new value |
|
120 |
* @return the previous value |
|
121 |
*/ |
|
122 |
public final int getAndSet(int newValue) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
123 |
return (int)VALUE.getAndSet(this, newValue); |
2 | 124 |
} |
125 |
||
126 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
127 |
* Atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
128 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
129 |
* with memory effects as specified by {@link VarHandle#compareAndSet}. |
2 | 130 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
131 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
132 |
* @param newValue the new value |
18576 | 133 |
* @return {@code true} if successful. False return indicates that |
2 | 134 |
* the actual value was not equal to the expected value. |
135 |
*/ |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
136 |
public final boolean compareAndSet(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
137 |
return VALUE.compareAndSet(this, expectedValue, newValue); |
2 | 138 |
} |
139 |
||
140 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
141 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
142 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
143 |
* with memory effects as specified by {@link VarHandle#weakCompareAndSet}. |
2 | 144 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
145 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
146 |
* @param newValue the new value |
18576 | 147 |
* @return {@code true} if successful |
2 | 148 |
*/ |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
149 |
public final boolean weakCompareAndSet(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
150 |
return VALUE.weakCompareAndSet(this, expectedValue, newValue); |
2 | 151 |
} |
152 |
||
153 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
154 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
155 |
* 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
|
156 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
157 |
* <p>Equivalent to {@code getAndAdd(1)}. |
2 | 158 |
* |
159 |
* @return the previous value |
|
160 |
*/ |
|
161 |
public final int getAndIncrement() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
162 |
return (int)VALUE.getAndAdd(this, 1); |
2 | 163 |
} |
164 |
||
165 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
166 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
167 |
* 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
|
168 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
169 |
* <p>Equivalent to {@code getAndAdd(-1)}. |
2 | 170 |
* |
171 |
* @return the previous value |
|
172 |
*/ |
|
173 |
public final int getAndDecrement() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
174 |
return (int)VALUE.getAndAdd(this, -1); |
2 | 175 |
} |
176 |
||
177 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
178 |
* 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
|
179 |
* with memory effects as specified by {@link VarHandle#getAndAdd}. |
2 | 180 |
* |
181 |
* @param delta the value to add |
|
182 |
* @return the previous value |
|
183 |
*/ |
|
184 |
public final int getAndAdd(int delta) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
185 |
return (int)VALUE.getAndAdd(this, delta); |
2 | 186 |
} |
187 |
||
188 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
189 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
190 |
* 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
|
191 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
192 |
* <p>Equivalent to {@code addAndGet(1)}. |
2 | 193 |
* |
194 |
* @return the updated value |
|
195 |
*/ |
|
196 |
public final int incrementAndGet() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
197 |
return (int)VALUE.addAndGet(this, 1); |
2 | 198 |
} |
199 |
||
200 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
201 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
202 |
* 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
|
203 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
204 |
* <p>Equivalent to {@code addAndGet(-1)}. |
2 | 205 |
* |
206 |
* @return the updated value |
|
207 |
*/ |
|
208 |
public final int decrementAndGet() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
209 |
return (int)VALUE.addAndGet(this, -1); |
2 | 210 |
} |
211 |
||
212 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
213 |
* 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
|
214 |
* with memory effects as specified by {@link VarHandle#addAndGet}. |
2 | 215 |
* |
216 |
* @param delta the value to add |
|
217 |
* @return the updated value |
|
218 |
*/ |
|
219 |
public final int addAndGet(int delta) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
220 |
return (int)VALUE.addAndGet(this, delta); |
2 | 221 |
} |
222 |
||
223 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
224 |
* 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
|
225 |
* 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
|
226 |
* 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
|
227 |
* 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
|
228 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
229 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
public final int getAndUpdate(IntUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
234 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
235 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
236 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
237 |
next = updateFunction.applyAsInt(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
238 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
239 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
240 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
241 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
242 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
243 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
244 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
245 |
* 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
|
246 |
* 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
|
247 |
* 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
|
248 |
* 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
|
249 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
253 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
public final int updateAndGet(IntUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
255 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
256 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
257 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
258 |
next = updateFunction.applyAsInt(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
259 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
260 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
261 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
262 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
263 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
264 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
265 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
266 |
* 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
|
267 |
* 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
|
268 |
* 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
|
269 |
* 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
|
270 |
* 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
|
271 |
* 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
|
272 |
* 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
|
273 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* @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
|
276 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
public final int getAndAccumulate(int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
IntBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
281 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
282 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
283 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
284 |
next = accumulatorFunction.applyAsInt(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
285 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
286 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
287 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
288 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
290 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
* 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
|
293 |
* 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
|
294 |
* 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
|
295 |
* 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
|
296 |
* 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
|
297 |
* 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
|
298 |
* 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
|
299 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
* @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
|
302 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
304 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
public final int accumulateAndGet(int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
IntBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
307 |
int prev = get(), next = 0; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
308 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
309 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
310 |
next = accumulatorFunction.applyAsInt(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
311 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
312 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
313 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
314 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
315 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
316 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
317 |
/** |
2 | 318 |
* 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
|
319 |
* @return the String representation of the current value |
2 | 320 |
*/ |
321 |
public String toString() { |
|
322 |
return Integer.toString(get()); |
|
323 |
} |
|
324 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
325 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
326 |
* 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
|
327 |
* {@code int}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
328 |
* 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
|
329 |
* |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
330 |
* Equivalent to {@link #get()}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
331 |
*/ |
2 | 332 |
public int intValue() { |
333 |
return get(); |
|
334 |
} |
|
335 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
336 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
337 |
* 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
|
338 |
* {@code long} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
339 |
* 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
|
340 |
* @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
|
341 |
*/ |
2 | 342 |
public long longValue() { |
343 |
return (long)get(); |
|
344 |
} |
|
345 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
346 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
347 |
* 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
|
348 |
* {@code float} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
349 |
* 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
|
350 |
* @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
|
351 |
*/ |
2 | 352 |
public float floatValue() { |
353 |
return (float)get(); |
|
354 |
} |
|
355 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
356 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
357 |
* 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
|
358 |
* {@code double} after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
359 |
* 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
|
360 |
* @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
|
361 |
*/ |
2 | 362 |
public double doubleValue() { |
363 |
return (double)get(); |
|
364 |
} |
|
365 |
||
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
366 |
// jdk9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
367 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
368 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
369 |
* 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
|
370 |
* if the variable was declared non-{@code volatile}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
371 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
372 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
373 |
* @since 9 |
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 |
public final int getPlain() { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
376 |
return (int)VALUE.get(this); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
379 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
380 |
* 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
|
381 |
* 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
|
382 |
* and non-{@code final}. |
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 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
385 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
386 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
387 |
public final void setPlain(int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
388 |
VALUE.set(this, newValue); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
391 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
392 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
393 |
* 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
|
394 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
395 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
396 |
* @since 9 |
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 |
public final int getOpaque() { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
399 |
return (int)VALUE.getOpaque(this); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
402 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
403 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
404 |
* 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
|
405 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
406 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
407 |
* @since 9 |
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 |
public final void setOpaque(int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
410 |
VALUE.setOpaque(this, newValue); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
413 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
414 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
415 |
* 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
|
416 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
417 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
418 |
* @since 9 |
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 |
public final int getAcquire() { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
421 |
return (int)VALUE.getAcquire(this); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
424 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
425 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
426 |
* 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
|
427 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
428 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
429 |
* @since 9 |
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 |
public final void setRelease(int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
432 |
VALUE.setRelease(this, newValue); |
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
435 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
436 |
* 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
|
437 |
* 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
|
438 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
439 |
* {@link VarHandle#compareAndExchange}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
442 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
443 |
* @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
|
444 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
445 |
* @since 9 |
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 |
public final int compareAndExchange(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
448 |
return (int)VALUE.compareAndExchange(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
449 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
450 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
451 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
452 |
* 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
|
453 |
* 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
|
454 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
455 |
* {@link VarHandle#compareAndExchangeAcquire}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
458 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
459 |
* @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
|
460 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
461 |
* @since 9 |
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 |
public final int compareAndExchangeAcquire(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
464 |
return (int)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
465 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
466 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
467 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
468 |
* 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
|
469 |
* 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
|
470 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
471 |
* {@link VarHandle#compareAndExchangeRelease}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
474 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
475 |
* @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
|
476 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
477 |
* @since 9 |
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 |
public final int compareAndExchangeRelease(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
480 |
return (int)VALUE.compareAndExchangeRelease(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
481 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
482 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
483 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
484 |
* 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
|
485 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
486 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
487 |
* {@link VarHandle#weakCompareAndSetVolatile}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
490 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
491 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
492 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
493 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
494 |
public final boolean weakCompareAndSetVolatile(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
495 |
return VALUE.weakCompareAndSetVolatile(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
496 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
497 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
498 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
499 |
* 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
|
500 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
501 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
502 |
* {@link VarHandle#weakCompareAndSetAcquire}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
505 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
506 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
507 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
508 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
509 |
public final boolean weakCompareAndSetAcquire(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
510 |
return VALUE.weakCompareAndSetAcquire(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
511 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
512 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
513 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
514 |
* 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
|
515 |
* the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
516 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
517 |
* {@link VarHandle#weakCompareAndSetRelease}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
520 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
521 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
522 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
523 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
524 |
public final boolean weakCompareAndSetRelease(int expectedValue, int newValue) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
525 |
return VALUE.weakCompareAndSetRelease(this, expectedValue, newValue); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
526 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
527 |
|
2 | 528 |
} |