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.LongBinaryOperator; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
40 |
import java.util.function.LongUnaryOperator; |
2 | 41 |
|
42 |
/** |
|
43 |
* A {@code long} 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 AtomicLong} is used in applications |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
46 |
* such as atomically incremented sequence numbers, and cannot be used |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
47 |
* as a replacement for a {@link java.lang.Long}. However, this class |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
48 |
* does extend {@code Number} to allow uniform access by tools and |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
49 |
* utilities that deal with numerically-based classes. |
2 | 50 |
* |
51 |
* @since 1.5 |
|
52 |
* @author Doug Lea |
|
53 |
*/ |
|
54 |
public class AtomicLong extends Number implements java.io.Serializable { |
|
55 |
private static final long serialVersionUID = 1927816293512124184L; |
|
56 |
||
57 |
/** |
|
58 |
* Records whether the underlying JVM supports lockless |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
59 |
* compareAndSwap for longs. While the intrinsic compareAndSwapLong |
2 | 60 |
* method works in either case, some constructions should be |
61 |
* handled at Java level to avoid locking user-visible locks. |
|
62 |
*/ |
|
63 |
static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8(); |
|
64 |
||
65 |
/** |
|
66 |
* Returns whether underlying JVM supports lockless CompareAndSet |
|
67 |
* for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS. |
|
68 |
*/ |
|
69 |
private static native boolean VMSupportsCS8(); |
|
70 |
||
39777 | 71 |
/* |
72 |
* This class intended to be implemented using VarHandles, but there |
|
73 |
* are unresolved cyclic startup dependencies. |
|
74 |
*/ |
|
75 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
|
76 |
private static final long VALUE; |
|
77 |
||
2 | 78 |
static { |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
79 |
try { |
39777 | 80 |
VALUE = U.objectFieldOffset |
81 |
(AtomicLong.class.getDeclaredField("value")); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
82 |
} catch (ReflectiveOperationException e) { |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
83 |
throw new Error(e); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
84 |
} |
2 | 85 |
} |
86 |
||
87 |
private volatile long value; |
|
88 |
||
89 |
/** |
|
90 |
* Creates a new AtomicLong with the given initial value. |
|
91 |
* |
|
92 |
* @param initialValue the initial value |
|
93 |
*/ |
|
94 |
public AtomicLong(long initialValue) { |
|
95 |
value = initialValue; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Creates a new AtomicLong with initial value {@code 0}. |
|
100 |
*/ |
|
101 |
public AtomicLong() { |
|
102 |
} |
|
103 |
||
104 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
105 |
* Returns the current value, |
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#getVolatile}. |
2 | 107 |
* |
108 |
* @return the current value |
|
109 |
*/ |
|
110 |
public final long get() { |
|
111 |
return value; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
115 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
116 |
* with memory effects as specified by {@link VarHandle#setVolatile}. |
2 | 117 |
* |
118 |
* @param newValue the new value |
|
119 |
*/ |
|
120 |
public final void set(long newValue) { |
|
39777 | 121 |
// Use putLongVolatile instead of ordinary volatile store when |
122 |
// using compareAndSwapLong, for sake of some 32bit systems. |
|
123 |
U.putLongVolatile(this, VALUE, newValue); |
|
2 | 124 |
} |
125 |
||
126 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
127 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
128 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
2 | 129 |
* |
130 |
* @param newValue the new value |
|
131 |
* @since 1.6 |
|
132 |
*/ |
|
133 |
public final void lazySet(long newValue) { |
|
39777 | 134 |
U.putLongRelease(this, VALUE, newValue); |
2 | 135 |
} |
136 |
||
137 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
138 |
* 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
|
139 |
* with memory effects as specified by {@link VarHandle#getAndSet}. |
2 | 140 |
* |
141 |
* @param newValue the new value |
|
142 |
* @return the previous value |
|
143 |
*/ |
|
144 |
public final long getAndSet(long newValue) { |
|
39777 | 145 |
return U.getAndSetLong(this, VALUE, newValue); |
2 | 146 |
} |
147 |
||
148 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
149 |
* Atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
150 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
151 |
* with memory effects as specified by {@link VarHandle#compareAndSet}. |
2 | 152 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
153 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
154 |
* @param newValue the new value |
18576 | 155 |
* @return {@code true} if successful. False return indicates that |
2 | 156 |
* the actual value was not equal to the expected value. |
157 |
*/ |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
158 |
public final boolean compareAndSet(long expectedValue, long newValue) { |
39777 | 159 |
return U.compareAndSwapLong(this, VALUE, expectedValue, newValue); |
2 | 160 |
} |
161 |
||
162 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
163 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
164 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
165 |
* with memory effects as specified by {@link VarHandle#weakCompareAndSet}. |
2 | 166 |
* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
167 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
168 |
* @param newValue the new value |
18576 | 169 |
* @return {@code true} if successful |
2 | 170 |
*/ |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
171 |
public final boolean weakCompareAndSet(long expectedValue, long newValue) { |
39777 | 172 |
return U.weakCompareAndSwapLong(this, VALUE, expectedValue, newValue); |
2 | 173 |
} |
174 |
||
175 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
176 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
177 |
* 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
|
178 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
179 |
* <p>Equivalent to {@code getAndAdd(1)}. |
2 | 180 |
* |
181 |
* @return the previous value |
|
182 |
*/ |
|
183 |
public final long getAndIncrement() { |
|
39777 | 184 |
return U.getAndAddLong(this, VALUE, 1L); |
2 | 185 |
} |
186 |
||
187 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
188 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
189 |
* 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
|
190 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
191 |
* <p>Equivalent to {@code getAndAdd(-1)}. |
2 | 192 |
* |
193 |
* @return the previous value |
|
194 |
*/ |
|
195 |
public final long getAndDecrement() { |
|
39777 | 196 |
return U.getAndAddLong(this, VALUE, -1L); |
2 | 197 |
} |
198 |
||
199 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
200 |
* 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
|
201 |
* with memory effects as specified by {@link VarHandle#getAndAdd}. |
2 | 202 |
* |
203 |
* @param delta the value to add |
|
204 |
* @return the previous value |
|
205 |
*/ |
|
206 |
public final long getAndAdd(long delta) { |
|
39777 | 207 |
return U.getAndAddLong(this, VALUE, delta); |
2 | 208 |
} |
209 |
||
210 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
211 |
* Atomically increments the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
212 |
* 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
|
213 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
214 |
* <p>Equivalent to {@code addAndGet(1)}. |
2 | 215 |
* |
216 |
* @return the updated value |
|
217 |
*/ |
|
218 |
public final long incrementAndGet() { |
|
39777 | 219 |
return U.getAndAddLong(this, VALUE, 1L) + 1L; |
2 | 220 |
} |
221 |
||
222 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
223 |
* Atomically decrements the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
224 |
* 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
|
225 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
226 |
* <p>Equivalent to {@code addAndGet(-1)}. |
2 | 227 |
* |
228 |
* @return the updated value |
|
229 |
*/ |
|
230 |
public final long decrementAndGet() { |
|
39777 | 231 |
return U.getAndAddLong(this, VALUE, -1L) - 1L; |
2 | 232 |
} |
233 |
||
234 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
235 |
* 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
|
236 |
* with memory effects as specified by {@link VarHandle#addAndGet}. |
2 | 237 |
* |
238 |
* @param delta the value to add |
|
239 |
* @return the updated value |
|
240 |
*/ |
|
241 |
public final long addAndGet(long delta) { |
|
39777 | 242 |
return U.getAndAddLong(this, VALUE, delta) + delta; |
2 | 243 |
} |
244 |
||
245 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
246 |
* 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
|
247 |
* 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
|
248 |
* 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
|
249 |
* 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
|
250 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
253 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
public final long getAndUpdate(LongUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
256 |
long prev = get(), next = 0L; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
257 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
258 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
259 |
next = updateFunction.applyAsLong(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
260 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
261 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
262 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
263 |
} |
15267
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 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
267 |
* 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
|
268 |
* 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
|
269 |
* 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
|
270 |
* 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
|
271 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
273 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
public final long updateAndGet(LongUnaryOperator updateFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
277 |
long prev = get(), next = 0L; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
278 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
279 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
280 |
next = updateFunction.applyAsLong(prev); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
281 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
282 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
283 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
284 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
* 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
|
289 |
* 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
|
290 |
* 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
|
291 |
* 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
|
292 |
* 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
|
293 |
* 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
|
294 |
* 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
|
295 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
296 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
* @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
|
298 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
public final long getAndAccumulate(long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
302 |
LongBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
303 |
long prev = get(), next = 0L; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
304 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
305 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
306 |
next = accumulatorFunction.applyAsLong(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
307 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
308 |
return prev; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
309 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
310 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
312 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
313 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
314 |
* 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
|
315 |
* 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
|
316 |
* 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
|
317 |
* 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
|
318 |
* 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
|
319 |
* 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
|
320 |
* 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
|
321 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
322 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
323 |
* @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
|
324 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
325 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
326 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
327 |
public final long accumulateAndGet(long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
328 |
LongBinaryOperator accumulatorFunction) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
329 |
long prev = get(), next = 0L; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
330 |
for (boolean haveNext = false;;) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
331 |
if (!haveNext) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
332 |
next = accumulatorFunction.applyAsLong(prev, x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
333 |
if (weakCompareAndSetVolatile(prev, next)) |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
334 |
return next; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
335 |
haveNext = (prev == (prev = get())); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
336 |
} |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
337 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
338 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
339 |
/** |
2 | 340 |
* 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
|
341 |
* @return the String representation of the current value |
2 | 342 |
*/ |
343 |
public String toString() { |
|
344 |
return Long.toString(get()); |
|
345 |
} |
|
346 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
347 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
348 |
* Returns the current value of this {@code AtomicLong} as an {@code int} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
349 |
* after a narrowing primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
350 |
* 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
|
351 |
* @jls 5.1.3 Narrowing Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
352 |
*/ |
2 | 353 |
public int intValue() { |
354 |
return (int)get(); |
|
355 |
} |
|
356 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
357 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
358 |
* Returns the current value of this {@code AtomicLong} as a {@code long}, |
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}. |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
360 |
* Equivalent to {@link #get()}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
361 |
*/ |
2 | 362 |
public long longValue() { |
363 |
return get(); |
|
364 |
} |
|
365 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
366 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
367 |
* Returns the current value of this {@code AtomicLong} as a {@code float} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
368 |
* after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
369 |
* 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
|
370 |
* @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
|
371 |
*/ |
2 | 372 |
public float floatValue() { |
373 |
return (float)get(); |
|
374 |
} |
|
375 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
376 |
/** |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
377 |
* Returns the current value of this {@code AtomicLong} as a {@code double} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
378 |
* after a widening primitive conversion, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
379 |
* 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
|
380 |
* @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
|
381 |
*/ |
2 | 382 |
public double doubleValue() { |
383 |
return (double)get(); |
|
384 |
} |
|
385 |
||
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
386 |
// jdk9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
387 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
388 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
389 |
* Returns the current value, with memory semantics of reading as if the |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
390 |
* variable was declared non-{@code volatile}. |
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 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
393 |
* @since 9 |
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 |
public final long getPlain() { |
39777 | 396 |
return U.getLong(this, VALUE); |
39725
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 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
399 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
400 |
* 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
|
401 |
* 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
|
402 |
* and non-{@code final}. |
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 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
405 |
* @since 9 |
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 |
public final void setPlain(long newValue) { |
39777 | 408 |
U.putLong(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
409 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
410 |
|
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 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
413 |
* 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
|
414 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
415 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
416 |
* @since 9 |
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 |
public final long getOpaque() { |
39777 | 419 |
return U.getLongOpaque(this, VALUE); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
420 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
421 |
|
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 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
424 |
* 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
|
425 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
426 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
427 |
* @since 9 |
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 |
public final void setOpaque(long newValue) { |
39777 | 430 |
U.putLongOpaque(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
431 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
432 |
|
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 |
* Returns the current value, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
435 |
* 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
|
436 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
437 |
* @return the value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
438 |
* @since 9 |
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 |
public final long getAcquire() { |
39777 | 441 |
return U.getLongAcquire(this, VALUE); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
442 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
443 |
|
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
444 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
445 |
* Sets the value to {@code newValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
446 |
* 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
|
447 |
* |
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 |
* @since 9 |
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 |
public final void setRelease(long newValue) { |
39777 | 452 |
U.putLongRelease(this, VALUE, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
453 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
454 |
|
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 |
* 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
|
457 |
* 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
|
458 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
459 |
* {@link VarHandle#compareAndExchange}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
460 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
461 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
462 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
463 |
* @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
|
464 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
465 |
* @since 9 |
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 |
public final long compareAndExchange(long expectedValue, long newValue) { |
39777 | 468 |
return U.compareAndExchangeLongVolatile(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
469 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
470 |
|
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 |
* 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
|
473 |
* 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
|
474 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
475 |
* {@link VarHandle#compareAndExchangeAcquire}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
476 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
477 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
478 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
479 |
* @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
|
480 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
481 |
* @since 9 |
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 |
public final long compareAndExchangeAcquire(long expectedValue, long newValue) { |
39777 | 484 |
return U.compareAndExchangeLongAcquire(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
485 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
486 |
|
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 |
* 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
|
489 |
* 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
|
490 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
491 |
* {@link VarHandle#compareAndExchangeRelease}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
492 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
493 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
494 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
495 |
* @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
|
496 |
* expected value if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
497 |
* @since 9 |
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 |
public final long compareAndExchangeRelease(long expectedValue, long newValue) { |
39777 | 500 |
return U.compareAndExchangeLongRelease(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
501 |
} |
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 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
505 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
506 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
507 |
* {@link VarHandle#weakCompareAndSetVolatile}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
510 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
511 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
512 |
* @since 9 |
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 |
public final boolean weakCompareAndSetVolatile(long expectedValue, long newValue) { |
39777 | 515 |
return U.weakCompareAndSwapLongVolatile(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
516 |
} |
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 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
520 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
521 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
522 |
* {@link VarHandle#weakCompareAndSetAcquire}. |
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 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
525 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
526 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
527 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
528 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
529 |
public final boolean weakCompareAndSetAcquire(long expectedValue, long newValue) { |
39777 | 530 |
return U.weakCompareAndSwapLongAcquire(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
531 |
} |
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 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
534 |
* Possibly atomically sets the value to {@code newValue} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
535 |
* if the current value {@code == expectedValue}, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
536 |
* with memory effects as specified by |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
537 |
* {@link VarHandle#weakCompareAndSetRelease}. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
538 |
* |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
539 |
* @param expectedValue the expected value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
540 |
* @param newValue the new value |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
541 |
* @return {@code true} if successful |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
542 |
* @since 9 |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
543 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
544 |
public final boolean weakCompareAndSetRelease(long expectedValue, long newValue) { |
39777 | 545 |
return U.weakCompareAndSwapLongRelease(this, VALUE, expectedValue, newValue); |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
546 |
} |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
36936
diff
changeset
|
547 |
|
2 | 548 |
} |