author | psandoz |
Wed, 05 Oct 2016 11:30:16 -0700 | |
changeset 43207 | 569e0c1d851d |
parent 42322 | c3474fef4fe4 |
child 45518 | 4a116dd82fb5 |
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.IntBinaryOperator; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.function.IntUnaryOperator; |
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 int} fields of designated classes. |
|
53 |
* This class is designed for use in atomic data structures in which |
|
54 |
* several fields of the same node are independently subject to atomic |
|
55 |
* updates. |
|
56 |
* |
|
57 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
58 |
* method in this class are weaker than in other atomic classes. |
|
59 |
* Because this class cannot ensure that all uses of the field |
|
60 |
* are appropriate for purposes of atomic access, it can |
|
61 |
* guarantee atomicity only with respect to other invocations of |
|
62 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
63 |
* |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
64 |
* <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
|
65 |
* 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
|
66 |
* a {@link ClassCastException} being thrown. |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
67 |
* |
2 | 68 |
* @since 1.5 |
69 |
* @author Doug Lea |
|
70 |
* @param <T> The type of the object holding the updatable field |
|
71 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
72 |
public abstract class AtomicIntegerFieldUpdater<T> { |
2 | 73 |
/** |
74 |
* Creates and returns an updater for objects with the given field. |
|
75 |
* The Class argument is needed to check that reflective types and |
|
76 |
* generic types match. |
|
77 |
* |
|
78 |
* @param tclass the class of the objects holding the field |
|
79 |
* @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
|
80 |
* @param <U> the type of instances of tclass |
2 | 81 |
* @return the updater |
82 |
* @throws IllegalArgumentException if the field is not a |
|
83 |
* volatile integer type |
|
84 |
* @throws RuntimeException with a nested reflection-based |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
85 |
* 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
|
86 |
* 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
|
87 |
* access control |
2 | 88 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
89 |
@CallerSensitive |
18576 | 90 |
public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, |
91 |
String fieldName) { |
|
92 |
return new AtomicIntegerFieldUpdaterImpl<U> |
|
93 |
(tclass, fieldName, Reflection.getCallerClass()); |
|
2 | 94 |
} |
95 |
||
96 |
/** |
|
97 |
* Protected do-nothing constructor for use by subclasses. |
|
98 |
*/ |
|
99 |
protected AtomicIntegerFieldUpdater() { |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Atomically sets the field of the given object managed by this updater |
|
104 |
* to the given updated value if the current value {@code ==} the |
|
105 |
* expected value. This method is guaranteed to be atomic with respect to |
|
106 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
107 |
* necessarily with respect to other changes in the field. |
|
108 |
* |
|
109 |
* @param obj An object whose field to conditionally set |
|
110 |
* @param expect the expected value |
|
111 |
* @param update the new value |
|
18576 | 112 |
* @return {@code true} if successful |
2 | 113 |
*/ |
114 |
public abstract boolean compareAndSet(T obj, int expect, int update); |
|
115 |
||
116 |
/** |
|
117 |
* Atomically sets the field of the given object managed by this updater |
|
118 |
* to the given updated value if the current value {@code ==} the |
|
119 |
* expected value. This method is guaranteed to be atomic with respect to |
|
120 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
121 |
* necessarily with respect to other changes in the field. |
|
122 |
* |
|
18576 | 123 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
124 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
125 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 126 |
* |
127 |
* @param obj An object whose field to conditionally set |
|
128 |
* @param expect the expected value |
|
129 |
* @param update the new value |
|
18576 | 130 |
* @return {@code true} if successful |
2 | 131 |
*/ |
132 |
public abstract boolean weakCompareAndSet(T obj, int expect, int update); |
|
133 |
||
134 |
/** |
|
135 |
* Sets the field of the given object managed by this updater to the |
|
136 |
* given updated value. This operation is guaranteed to act as a volatile |
|
137 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
138 |
* |
|
139 |
* @param obj An object whose field to set |
|
140 |
* @param newValue the new value |
|
141 |
*/ |
|
142 |
public abstract void set(T obj, int newValue); |
|
143 |
||
144 |
/** |
|
145 |
* Eventually sets the field of the given object managed by this |
|
146 |
* updater to the given updated value. |
|
147 |
* |
|
148 |
* @param obj An object whose field to set |
|
149 |
* @param newValue the new value |
|
150 |
* @since 1.6 |
|
151 |
*/ |
|
152 |
public abstract void lazySet(T obj, int newValue); |
|
153 |
||
154 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
155 |
* 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
|
156 |
* managed by this updater. |
2 | 157 |
* |
158 |
* @param obj An object whose field to get |
|
159 |
* @return the current value |
|
160 |
*/ |
|
161 |
public abstract int get(T obj); |
|
162 |
||
163 |
/** |
|
164 |
* Atomically sets the field of the given object managed by this updater |
|
165 |
* to the given value and returns the old value. |
|
166 |
* |
|
167 |
* @param obj An object whose field to get and set |
|
168 |
* @param newValue the new value |
|
169 |
* @return the previous value |
|
170 |
*/ |
|
171 |
public int getAndSet(T obj, int newValue) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
172 |
int prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
173 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
174 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
175 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
176 |
return prev; |
2 | 177 |
} |
178 |
||
179 |
/** |
|
180 |
* Atomically increments by one the current value of the field of the |
|
181 |
* given object managed by this updater. |
|
182 |
* |
|
183 |
* @param obj An object whose field to get and set |
|
184 |
* @return the previous value |
|
185 |
*/ |
|
186 |
public int getAndIncrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
187 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
188 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
189 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
190 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
191 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
192 |
return prev; |
2 | 193 |
} |
194 |
||
195 |
/** |
|
196 |
* Atomically decrements by one the current value of the field of the |
|
197 |
* given object managed by this updater. |
|
198 |
* |
|
199 |
* @param obj An object whose field to get and set |
|
200 |
* @return the previous value |
|
201 |
*/ |
|
202 |
public int getAndDecrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
203 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
204 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
205 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
206 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
207 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
208 |
return prev; |
2 | 209 |
} |
210 |
||
211 |
/** |
|
212 |
* Atomically adds the given value to the current value of the field of |
|
213 |
* the given object managed by this updater. |
|
214 |
* |
|
215 |
* @param obj An object whose field to get and set |
|
216 |
* @param delta the value to add |
|
217 |
* @return the previous value |
|
218 |
*/ |
|
219 |
public int getAndAdd(T obj, int delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
220 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
221 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
222 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
223 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
224 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
225 |
return prev; |
2 | 226 |
} |
227 |
||
228 |
/** |
|
229 |
* Atomically increments by one the current value of the field of the |
|
230 |
* given object managed by this updater. |
|
231 |
* |
|
232 |
* @param obj An object whose field to get and set |
|
233 |
* @return the updated value |
|
234 |
*/ |
|
235 |
public int incrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
236 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
237 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
238 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
239 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
240 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
241 |
return next; |
2 | 242 |
} |
243 |
||
244 |
/** |
|
245 |
* Atomically decrements by one the current value of the field of the |
|
246 |
* given object managed by this updater. |
|
247 |
* |
|
248 |
* @param obj An object whose field to get and set |
|
249 |
* @return the updated value |
|
250 |
*/ |
|
251 |
public int decrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
252 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
253 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
254 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
255 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
256 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
257 |
return next; |
2 | 258 |
} |
259 |
||
260 |
/** |
|
261 |
* Atomically adds the given value to the current value of the field of |
|
262 |
* the given object managed by this updater. |
|
263 |
* |
|
264 |
* @param obj An object whose field to get and set |
|
265 |
* @param delta the value to add |
|
266 |
* @return the updated value |
|
267 |
*/ |
|
268 |
public int addAndGet(T obj, int delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
269 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
270 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
271 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
272 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
273 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
274 |
return next; |
2 | 275 |
} |
276 |
||
277 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
* 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
|
279 |
* 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
|
280 |
* 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
|
281 |
* 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
|
282 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
* @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
|
284 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
* @since 1.8 |
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 |
public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
290 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
292 |
next = updateFunction.applyAsInt(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
293 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
294 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
296 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
298 |
* Atomically updates the 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
|
299 |
* 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
|
300 |
* 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
|
301 |
* 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
|
302 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
* @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
|
304 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
public final int updateAndGet(T obj, IntUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
312 |
next = updateFunction.applyAsInt(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
313 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
314 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
315 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
316 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
317 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
318 |
* 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
|
319 |
* 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
|
320 |
* 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
|
321 |
* 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
|
322 |
* 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
|
323 |
* 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
|
324 |
* 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
|
325 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
326 |
* @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
|
327 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
328 |
* @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
|
329 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
330 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
331 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
332 |
public final int getAndAccumulate(T obj, int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
333 |
IntBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
334 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
335 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
336 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
337 |
next = accumulatorFunction.applyAsInt(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
338 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
339 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
340 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
341 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
342 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
343 |
* 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
|
344 |
* 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
|
345 |
* 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
|
346 |
* 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
|
347 |
* 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
|
348 |
* 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
|
349 |
* 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
|
350 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
351 |
* @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
|
352 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
353 |
* @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
|
354 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
355 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
356 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
357 |
public final int accumulateAndGet(T obj, int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
358 |
IntBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
359 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
360 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
361 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
362 |
next = accumulatorFunction.applyAsInt(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
363 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
364 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
365 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
366 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
367 |
/** |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
368 |
* Standard hotspot implementation using intrinsics. |
2 | 369 |
*/ |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
370 |
private static final class AtomicIntegerFieldUpdaterImpl<T> |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
371 |
extends AtomicIntegerFieldUpdater<T> { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
372 |
private static final Unsafe U = Unsafe.getUnsafe(); |
2 | 373 |
private final long offset; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
374 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
375 |
* 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
|
376 |
* the same as tclass |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
377 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
378 |
private final Class<?> cclass; |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
379 |
/** class holding the field */ |
2 | 380 |
private final Class<T> tclass; |
381 |
||
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
382 |
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
383 |
final String fieldName, |
18576 | 384 |
final Class<?> caller) { |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
385 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
386 |
final int modifiers; |
2 | 387 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
388 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
389 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
390 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
391 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
392 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
393 |
}); |
2 | 394 |
modifiers = field.getModifiers(); |
395 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
396 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
397 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
398 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
399 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
400 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
401 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
402 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
403 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
404 |
throw new RuntimeException(pae.getException()); |
7518 | 405 |
} catch (Exception ex) { |
2 | 406 |
throw new RuntimeException(ex); |
407 |
} |
|
408 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
409 |
if (field.getType() != int.class) |
2 | 410 |
throw new IllegalArgumentException("Must be integer type"); |
411 |
||
412 |
if (!Modifier.isVolatile(modifiers)) |
|
413 |
throw new IllegalArgumentException("Must be volatile type"); |
|
414 |
||
43207 | 415 |
// Access to protected field members is restricted to receivers only |
416 |
// of the accessing class, or one of its subclasses, and the |
|
417 |
// accessing class must in turn be a subclass (or package sibling) |
|
418 |
// of the protected member's defining class. |
|
419 |
// If the updater refers to a protected field of a declaring class |
|
420 |
// outside the current package, the receiver argument will be |
|
421 |
// narrowed to the type of the accessing class. |
|
422 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
423 |
tclass.isAssignableFrom(caller) && |
|
424 |
!isSamePackage(tclass, caller)) |
|
425 |
? caller : tclass; |
|
2 | 426 |
this.tclass = tclass; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
427 |
this.offset = U.objectFieldOffset(field); |
2 | 428 |
} |
429 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
430 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
431 |
* 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
|
432 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
433 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
434 |
*/ |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
435 |
private static boolean isAncestor(ClassLoader first, ClassLoader second) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
436 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
437 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
438 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
439 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
440 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
441 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
442 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
443 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
444 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
445 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
446 |
/** |
43207 | 447 |
* Returns true if the two classes have the same class loader and |
448 |
* package qualifier |
|
449 |
*/ |
|
450 |
private static boolean isSamePackage(Class<?> class1, Class<?> class2) { |
|
451 |
return class1.getClassLoader() == class2.getClassLoader() |
|
452 |
&& Objects.equals(class1.getPackageName(), class2.getPackageName()); |
|
453 |
} |
|
454 |
||
455 |
/** |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
456 |
* 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
|
457 |
* failure, throws cause. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
458 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
459 |
private final void accessCheck(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
460 |
if (!cclass.isInstance(obj)) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
461 |
throwAccessCheckException(obj); |
2 | 462 |
} |
463 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
464 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
465 |
* 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
|
466 |
* protected access, else ClassCastException. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
467 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
468 |
private final void throwAccessCheckException(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
469 |
if (cclass == tclass) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
470 |
throw new ClassCastException(); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
471 |
else |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
472 |
throw new RuntimeException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
473 |
new IllegalAccessException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
474 |
"Class " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
475 |
cclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
476 |
" 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
|
477 |
tclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
478 |
" using an instance of " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
479 |
obj.getClass().getName())); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
480 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
481 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
482 |
public final boolean compareAndSet(T obj, int expect, int update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
483 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
484 |
return U.compareAndSwapInt(obj, offset, expect, update); |
2 | 485 |
} |
486 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
487 |
public final boolean weakCompareAndSet(T obj, int expect, int update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
488 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
489 |
return U.compareAndSwapInt(obj, offset, expect, update); |
2 | 490 |
} |
491 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
492 |
public final void set(T obj, int newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
493 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
494 |
U.putIntVolatile(obj, offset, newValue); |
2 | 495 |
} |
496 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
497 |
public final void lazySet(T obj, int newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
498 |
accessCheck(obj); |
36936
bfcdf736a998
8152698: Remove obsolete Unsafe.putOrdered{X} methods, usages, runtime and compiler support
shade
parents:
34339
diff
changeset
|
499 |
U.putIntRelease(obj, offset, newValue); |
2 | 500 |
} |
501 |
||
502 |
public final int get(T obj) { |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
503 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
504 |
return U.getIntVolatile(obj, offset); |
2 | 505 |
} |
506 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
507 |
public final int getAndSet(T obj, int newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
508 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
509 |
return U.getAndSetInt(obj, offset, newValue); |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
510 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
511 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
512 |
public final int getAndAdd(T obj, int delta) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
513 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
514 |
return U.getAndAddInt(obj, offset, delta); |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
515 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
516 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
517 |
public final int getAndIncrement(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
518 |
return getAndAdd(obj, 1); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
519 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
520 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
521 |
public final int getAndDecrement(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
522 |
return getAndAdd(obj, -1); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
523 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
524 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
525 |
public final int incrementAndGet(T obj) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
526 |
return getAndAdd(obj, 1) + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
527 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
528 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
529 |
public final int decrementAndGet(T obj) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
530 |
return getAndAdd(obj, -1) - 1; |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
531 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
532 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
533 |
public final int addAndGet(T obj, int delta) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
534 |
return getAndAdd(obj, delta) + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
535 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
536 |
|
2 | 537 |
} |
538 |
} |