author | psandoz |
Mon, 05 Jun 2017 16:05:24 -0700 | |
changeset 45518 | 4a116dd82fb5 |
parent 43207 | 569e0c1d851d |
child 45637 | 9e0c80381e32 |
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:
7518
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 |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
38 |
import java.lang.reflect.Field; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
39 |
import java.lang.reflect.Modifier; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
40 |
import java.security.AccessController; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
41 |
import java.security.PrivilegedActionException; |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
42 |
import java.security.PrivilegedExceptionAction; |
43207 | 43 |
import java.util.Objects; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
44 |
import java.util.function.BinaryOperator; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.function.UnaryOperator; |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
46 |
import jdk.internal.misc.Unsafe; |
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36936
diff
changeset
|
47 |
import jdk.internal.reflect.CallerSensitive; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36936
diff
changeset
|
48 |
import jdk.internal.reflect.Reflection; |
2 | 49 |
|
50 |
/** |
|
51 |
* A reflection-based utility that enables atomic updates to |
|
52 |
* designated {@code volatile} reference fields of designated |
|
53 |
* classes. This class is designed for use in atomic data structures |
|
54 |
* in which several reference fields of the same node are |
|
55 |
* independently subject to atomic updates. For example, a tree node |
|
56 |
* might be declared as |
|
57 |
* |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
58 |
* <pre> {@code |
2 | 59 |
* class Node { |
60 |
* private volatile Node left, right; |
|
61 |
* |
|
7518 | 62 |
* private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater = |
2 | 63 |
* AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left"); |
7518 | 64 |
* private static AtomicReferenceFieldUpdater<Node, Node> rightUpdater = |
2 | 65 |
* AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right"); |
66 |
* |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
67 |
* Node getLeft() { return left; } |
2 | 68 |
* boolean compareAndSetLeft(Node expect, Node update) { |
69 |
* return leftUpdater.compareAndSet(this, expect, update); |
|
70 |
* } |
|
71 |
* // ... and so on |
|
7518 | 72 |
* }}</pre> |
2 | 73 |
* |
74 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
75 |
* method in this class are weaker than in other atomic classes. |
|
76 |
* Because this class cannot ensure that all uses of the field |
|
77 |
* are appropriate for purposes of atomic access, it can |
|
78 |
* guarantee atomicity only with respect to other invocations of |
|
79 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
80 |
* |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
81 |
* <p>Object arguments for parameters of type {@code T} that are not |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
82 |
* instances of the class passed to {@link #newUpdater} will result in |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
83 |
* a {@link ClassCastException} being thrown. |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
84 |
* |
2 | 85 |
* @since 1.5 |
86 |
* @author Doug Lea |
|
87 |
* @param <T> The type of the object holding the updatable field |
|
88 |
* @param <V> The type of the field |
|
89 |
*/ |
|
18576 | 90 |
public abstract class AtomicReferenceFieldUpdater<T,V> { |
2 | 91 |
|
92 |
/** |
|
93 |
* Creates and returns an updater for objects with the given field. |
|
94 |
* The Class arguments are needed to check that reflective types and |
|
95 |
* generic types match. |
|
96 |
* |
|
18576 | 97 |
* @param tclass the class of the objects holding the field |
2 | 98 |
* @param vclass the class of the field |
18576 | 99 |
* @param fieldName the name of the field to be updated |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18576
diff
changeset
|
100 |
* @param <U> the type of instances of tclass |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18576
diff
changeset
|
101 |
* @param <W> the type of instances of vclass |
2 | 102 |
* @return the updater |
18576 | 103 |
* @throws ClassCastException if the field is of the wrong type |
104 |
* @throws IllegalArgumentException if the field is not volatile |
|
2 | 105 |
* @throws RuntimeException with a nested reflection-based |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
106 |
* exception if the class does not hold field or is the wrong type, |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
107 |
* or the field is inaccessible to the caller according to Java language |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
108 |
* access control |
2 | 109 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
110 |
@CallerSensitive |
18576 | 111 |
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass, |
112 |
Class<W> vclass, |
|
113 |
String fieldName) { |
|
114 |
return new AtomicReferenceFieldUpdaterImpl<U,W> |
|
115 |
(tclass, vclass, fieldName, Reflection.getCallerClass()); |
|
2 | 116 |
} |
117 |
||
118 |
/** |
|
119 |
* Protected do-nothing constructor for use by subclasses. |
|
120 |
*/ |
|
121 |
protected AtomicReferenceFieldUpdater() { |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Atomically sets the field of the given object managed by this updater |
|
126 |
* to the given updated value if the current value {@code ==} the |
|
127 |
* expected value. This method is guaranteed to be atomic with respect to |
|
128 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
129 |
* necessarily with respect to other changes in the field. |
|
130 |
* |
|
131 |
* @param obj An object whose field to conditionally set |
|
132 |
* @param expect the expected value |
|
133 |
* @param update the new value |
|
18576 | 134 |
* @return {@code true} if successful |
2 | 135 |
*/ |
136 |
public abstract boolean compareAndSet(T obj, V expect, V update); |
|
137 |
||
138 |
/** |
|
139 |
* Atomically sets the field of the given object managed by this updater |
|
140 |
* to the given updated value if the current value {@code ==} the |
|
141 |
* expected value. This method is guaranteed to be atomic with respect to |
|
142 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
143 |
* necessarily with respect to other changes in the field. |
|
144 |
* |
|
18576 | 145 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
146 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
147 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 148 |
* |
149 |
* @param obj An object whose field to conditionally set |
|
150 |
* @param expect the expected value |
|
151 |
* @param update the new value |
|
18576 | 152 |
* @return {@code true} if successful |
2 | 153 |
*/ |
154 |
public abstract boolean weakCompareAndSet(T obj, V expect, V update); |
|
155 |
||
156 |
/** |
|
157 |
* Sets the field of the given object managed by this updater to the |
|
158 |
* given updated value. This operation is guaranteed to act as a volatile |
|
159 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
160 |
* |
|
161 |
* @param obj An object whose field to set |
|
162 |
* @param newValue the new value |
|
163 |
*/ |
|
164 |
public abstract void set(T obj, V newValue); |
|
165 |
||
166 |
/** |
|
167 |
* Eventually sets the field of the given object managed by this |
|
168 |
* updater to the given updated value. |
|
169 |
* |
|
170 |
* @param obj An object whose field to set |
|
171 |
* @param newValue the new value |
|
172 |
* @since 1.6 |
|
173 |
*/ |
|
174 |
public abstract void lazySet(T obj, V newValue); |
|
175 |
||
176 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
177 |
* Returns the current value held in the field of the given object |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
178 |
* managed by this updater. |
2 | 179 |
* |
180 |
* @param obj An object whose field to get |
|
181 |
* @return the current value |
|
182 |
*/ |
|
183 |
public abstract V get(T obj); |
|
184 |
||
185 |
/** |
|
186 |
* Atomically sets the field of the given object managed by this updater |
|
187 |
* to the given value and returns the old value. |
|
188 |
* |
|
189 |
* @param obj An object whose field to get and set |
|
190 |
* @param newValue the new value |
|
191 |
* @return the previous value |
|
192 |
*/ |
|
193 |
public V getAndSet(T obj, V newValue) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
194 |
V prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
195 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
196 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
197 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
198 |
return prev; |
2 | 199 |
} |
200 |
||
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
201 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
202 |
* Atomically updates the field of the given object managed by this updater |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
203 |
* with the results of applying the given function, returning the previous |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
204 |
* value. The function should be side-effect-free, since it may be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
205 |
* re-applied 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
|
206 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
207 |
* @param obj An object whose field to get and set |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
208 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
209 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
210 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
211 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
212 |
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
213 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
214 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
215 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
216 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
217 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
218 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
219 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
220 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
221 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
222 |
* Atomically updates the field of the given object managed by this updater |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
223 |
* with the results of applying the given function, returning the updated |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
224 |
* value. The function should be side-effect-free, since it may be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
225 |
* re-applied 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
|
226 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
227 |
* @param obj An object whose field to get and set |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
228 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
229 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
234 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
235 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
236 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
237 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
238 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
239 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
240 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
241 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
242 |
* Atomically updates the field of the given object managed by this |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
243 |
* updater with the results of applying the given function to the |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
244 |
* current and given values, returning the previous value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
245 |
* 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
|
246 |
* when attempted updates fail due to contention among threads. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
247 |
* function 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
|
248 |
* 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
|
249 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
* @param obj An object whose field to get and set |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* @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
|
253 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
256 |
public final V getAndAccumulate(T obj, V x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
257 |
BinaryOperator<V> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
258 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
259 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
260 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
261 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
262 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
263 |
return prev; |
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 field of the given object managed by this |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
268 |
* updater with the results of applying the given function to the |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
269 |
* current and given values, returning the updated value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
270 |
* 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
|
271 |
* when attempted updates fail due to contention among threads. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
* function 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
|
273 |
* 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
|
274 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* @param obj An object whose field to get and set |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
* @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
|
278 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
public final V accumulateAndGet(T obj, V x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
BinaryOperator<V> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
284 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
286 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
return next; |
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 |
|
2 | 291 |
private static final class AtomicReferenceFieldUpdaterImpl<T,V> |
292 |
extends AtomicReferenceFieldUpdater<T,V> { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
293 |
private static final Unsafe U = Unsafe.getUnsafe(); |
2 | 294 |
private final long offset; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
295 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
296 |
* if field is protected, the subclass constructing updater, else |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
297 |
* the same as tclass |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
298 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
299 |
private final Class<?> cclass; |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
300 |
/** class holding the field */ |
2 | 301 |
private final Class<T> tclass; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
302 |
/** field value type */ |
2 | 303 |
private final Class<V> vclass; |
304 |
||
305 |
/* |
|
306 |
* Internal type checks within all update methods contain |
|
307 |
* internal inlined optimizations checking for the common |
|
308 |
* cases where the class is final (in which case a simple |
|
309 |
* getClass comparison suffices) or is of type Object (in |
|
310 |
* which case no check is needed because all objects are |
|
311 |
* instances of Object). The Object case is handled simply by |
|
312 |
* setting vclass to null in constructor. The targetCheck and |
|
313 |
* updateCheck methods are invoked when these faster |
|
314 |
* screenings fail. |
|
315 |
*/ |
|
316 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
317 |
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, |
18576 | 318 |
final Class<V> vclass, |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
319 |
final String fieldName, |
18576 | 320 |
final Class<?> caller) { |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
321 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
322 |
final Class<?> fieldClass; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
323 |
final int modifiers; |
2 | 324 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
325 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
326 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
327 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
328 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
329 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
330 |
}); |
2 | 331 |
modifiers = field.getModifiers(); |
332 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
333 |
caller, tclass, null, modifiers); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
334 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
335 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
336 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
337 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
338 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
339 |
} |
2 | 340 |
fieldClass = field.getType(); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
341 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
342 |
throw new RuntimeException(pae.getException()); |
2 | 343 |
} catch (Exception ex) { |
344 |
throw new RuntimeException(ex); |
|
345 |
} |
|
346 |
||
347 |
if (vclass != fieldClass) |
|
348 |
throw new ClassCastException(); |
|
25545 | 349 |
if (vclass.isPrimitive()) |
350 |
throw new IllegalArgumentException("Must be reference type"); |
|
2 | 351 |
|
352 |
if (!Modifier.isVolatile(modifiers)) |
|
353 |
throw new IllegalArgumentException("Must be volatile type"); |
|
354 |
||
43207 | 355 |
// Access to protected field members is restricted to receivers only |
356 |
// of the accessing class, or one of its subclasses, and the |
|
357 |
// accessing class must in turn be a subclass (or package sibling) |
|
358 |
// of the protected member's defining class. |
|
359 |
// If the updater refers to a protected field of a declaring class |
|
360 |
// outside the current package, the receiver argument will be |
|
361 |
// narrowed to the type of the accessing class. |
|
362 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
363 |
tclass.isAssignableFrom(caller) && |
|
364 |
!isSamePackage(tclass, caller)) |
|
365 |
? caller : tclass; |
|
2 | 366 |
this.tclass = tclass; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
367 |
this.vclass = vclass; |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
368 |
this.offset = U.objectFieldOffset(field); |
2 | 369 |
} |
370 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
371 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
372 |
* Returns true if the second classloader can be found in the first |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
373 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
374 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
375 |
*/ |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
376 |
private static boolean isAncestor(ClassLoader first, ClassLoader second) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
377 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
378 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
379 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
380 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
381 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
382 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
383 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
384 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
385 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
386 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
387 |
/** |
43207 | 388 |
* Returns true if the two classes have the same class loader and |
389 |
* package qualifier |
|
390 |
*/ |
|
391 |
private static boolean isSamePackage(Class<?> class1, Class<?> class2) { |
|
392 |
return class1.getClassLoader() == class2.getClassLoader() |
|
393 |
&& Objects.equals(class1.getPackageName(), class2.getPackageName()); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
397 |
* Checks that target argument is instance of cclass. On |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
398 |
* failure, throws cause. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
399 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
400 |
private final void accessCheck(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
401 |
if (!cclass.isInstance(obj)) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
402 |
throwAccessCheckException(obj); |
2 | 403 |
} |
404 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
405 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
406 |
* Throws access exception if accessCheck failed due to |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
407 |
* protected access, else ClassCastException. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
408 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
409 |
private final void throwAccessCheckException(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
410 |
if (cclass == tclass) |
2 | 411 |
throw new ClassCastException(); |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
412 |
else |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
413 |
throw new RuntimeException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
414 |
new IllegalAccessException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
415 |
"Class " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
416 |
cclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
417 |
" can not access a protected member of class " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
418 |
tclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
419 |
" using an instance of " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
420 |
obj.getClass().getName())); |
2 | 421 |
} |
422 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
423 |
private final void valueCheck(V v) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
424 |
if (v != null && !(vclass.isInstance(v))) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
425 |
throwCCE(); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
426 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
427 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
428 |
static void throwCCE() { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
429 |
throw new ClassCastException(); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
430 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
431 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
432 |
public final boolean compareAndSet(T obj, V expect, V update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
433 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
434 |
valueCheck(update); |
45518
4a116dd82fb5
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43207
diff
changeset
|
435 |
return U.compareAndSetObject(obj, offset, expect, update); |
2 | 436 |
} |
437 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
438 |
public final boolean weakCompareAndSet(T obj, V expect, V update) { |
2 | 439 |
// same implementation as strong form for now |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
440 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
441 |
valueCheck(update); |
45518
4a116dd82fb5
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43207
diff
changeset
|
442 |
return U.compareAndSetObject(obj, offset, expect, update); |
2 | 443 |
} |
444 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
445 |
public final void set(T obj, V newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
446 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
447 |
valueCheck(newValue); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
448 |
U.putObjectVolatile(obj, offset, newValue); |
2 | 449 |
} |
450 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
451 |
public final void lazySet(T obj, V newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
452 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
453 |
valueCheck(newValue); |
36936
bfcdf736a998
8152698: Remove obsolete Unsafe.putOrdered{X} methods, usages, runtime and compiler support
shade
parents:
34339
diff
changeset
|
454 |
U.putObjectRelease(obj, offset, newValue); |
2 | 455 |
} |
456 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
457 |
@SuppressWarnings("unchecked") |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
458 |
public final V get(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
459 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
460 |
return (V)U.getObjectVolatile(obj, offset); |
2 | 461 |
} |
462 |
||
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
463 |
@SuppressWarnings("unchecked") |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
464 |
public final V getAndSet(T obj, V newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
465 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
466 |
valueCheck(newValue); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
467 |
return (V)U.getAndSetObject(obj, offset, newValue); |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
468 |
} |
2 | 469 |
} |
470 |
} |