author | dl |
Fri, 08 Dec 2017 15:26:56 -0800 | |
changeset 48232 | bf476235671a |
parent 47216 | 71c04702a3d5 |
child 52729 | 0775f246731b |
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.LongBinaryOperator; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.function.LongUnaryOperator; |
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; |
45637
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
49 |
import java.lang.invoke.VarHandle; |
2 | 50 |
|
51 |
/** |
|
52 |
* A reflection-based utility that enables atomic updates to |
|
53 |
* designated {@code volatile long} fields of designated classes. |
|
54 |
* This class is designed for use in atomic data structures in which |
|
55 |
* several fields of the same node are independently subject to atomic |
|
56 |
* updates. |
|
57 |
* |
|
58 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
59 |
* method in this class are weaker than in other atomic classes. |
|
60 |
* Because this class cannot ensure that all uses of the field |
|
61 |
* are appropriate for purposes of atomic access, it can |
|
62 |
* guarantee atomicity only with respect to other invocations of |
|
63 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
64 |
* |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
65 |
* <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
|
66 |
* 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
|
67 |
* a {@link ClassCastException} being thrown. |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
68 |
* |
2 | 69 |
* @since 1.5 |
70 |
* @author Doug Lea |
|
71 |
* @param <T> The type of the object holding the updatable field |
|
72 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
73 |
public abstract class AtomicLongFieldUpdater<T> { |
2 | 74 |
/** |
75 |
* Creates and returns an updater for objects with the given field. |
|
76 |
* The Class argument is needed to check that reflective types and |
|
77 |
* generic types match. |
|
78 |
* |
|
79 |
* @param tclass the class of the objects holding the field |
|
18576 | 80 |
* @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
|
81 |
* @param <U> the type of instances of tclass |
2 | 82 |
* @return the updater |
83 |
* @throws IllegalArgumentException if the field is not a |
|
18576 | 84 |
* volatile long type |
2 | 85 |
* @throws RuntimeException with a nested reflection-based |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
86 |
* 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
|
87 |
* 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
|
88 |
* access control |
2 | 89 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
90 |
@CallerSensitive |
18576 | 91 |
public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, |
92 |
String fieldName) { |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
93 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 94 |
if (AtomicLong.VM_SUPPORTS_LONG_CAS) |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
95 |
return new CASUpdater<U>(tclass, fieldName, caller); |
2 | 96 |
else |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
97 |
return new LockedUpdater<U>(tclass, fieldName, caller); |
2 | 98 |
} |
99 |
||
100 |
/** |
|
101 |
* Protected do-nothing constructor for use by subclasses. |
|
102 |
*/ |
|
103 |
protected AtomicLongFieldUpdater() { |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Atomically sets the field of the given object managed by this updater |
|
108 |
* to the given updated value if the current value {@code ==} the |
|
109 |
* expected value. This method is guaranteed to be atomic with respect to |
|
110 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
111 |
* necessarily with respect to other changes in the field. |
|
112 |
* |
|
113 |
* @param obj An object whose field to conditionally set |
|
114 |
* @param expect the expected value |
|
115 |
* @param update the new value |
|
18576 | 116 |
* @return {@code true} if successful |
2 | 117 |
*/ |
118 |
public abstract boolean compareAndSet(T obj, long expect, long update); |
|
119 |
||
120 |
/** |
|
121 |
* Atomically sets the field of the given object managed by this updater |
|
122 |
* to the given updated value if the current value {@code ==} the |
|
123 |
* expected value. This method is guaranteed to be atomic with respect to |
|
124 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
125 |
* necessarily with respect to other changes in the field. |
|
126 |
* |
|
18576 | 127 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
128 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
129 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 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 weakCompareAndSet(T obj, long expect, long update); |
|
137 |
||
138 |
/** |
|
139 |
* Sets the field of the given object managed by this updater to the |
|
140 |
* given updated value. This operation is guaranteed to act as a volatile |
|
141 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
142 |
* |
|
143 |
* @param obj An object whose field to set |
|
144 |
* @param newValue the new value |
|
145 |
*/ |
|
146 |
public abstract void set(T obj, long newValue); |
|
147 |
||
148 |
/** |
|
149 |
* Eventually sets the field of the given object managed by this |
|
150 |
* updater to the given updated value. |
|
151 |
* |
|
152 |
* @param obj An object whose field to set |
|
153 |
* @param newValue the new value |
|
154 |
* @since 1.6 |
|
155 |
*/ |
|
156 |
public abstract void lazySet(T obj, long newValue); |
|
157 |
||
158 |
/** |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
159 |
* 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
|
160 |
* managed by this updater. |
2 | 161 |
* |
162 |
* @param obj An object whose field to get |
|
163 |
* @return the current value |
|
164 |
*/ |
|
165 |
public abstract long get(T obj); |
|
166 |
||
167 |
/** |
|
168 |
* Atomically sets the field of the given object managed by this updater |
|
169 |
* to the given value and returns the old value. |
|
170 |
* |
|
171 |
* @param obj An object whose field to get and set |
|
172 |
* @param newValue the new value |
|
173 |
* @return the previous value |
|
174 |
*/ |
|
175 |
public long getAndSet(T obj, long newValue) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
176 |
long prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
177 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
178 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
179 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
180 |
return prev; |
2 | 181 |
} |
182 |
||
183 |
/** |
|
184 |
* Atomically increments by one the current value of the field of the |
|
185 |
* given object managed by this updater. |
|
186 |
* |
|
187 |
* @param obj An object whose field to get and set |
|
188 |
* @return the previous value |
|
189 |
*/ |
|
190 |
public long getAndIncrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
191 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
192 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
193 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
194 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
195 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
196 |
return prev; |
2 | 197 |
} |
198 |
||
199 |
/** |
|
200 |
* Atomically decrements by one the current value of the field of the |
|
201 |
* given object managed by this updater. |
|
202 |
* |
|
203 |
* @param obj An object whose field to get and set |
|
204 |
* @return the previous value |
|
205 |
*/ |
|
206 |
public long getAndDecrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
207 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
208 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
209 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
210 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
211 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
212 |
return prev; |
2 | 213 |
} |
214 |
||
215 |
/** |
|
216 |
* Atomically adds the given value to the current value of the field of |
|
217 |
* the given object managed by this updater. |
|
218 |
* |
|
219 |
* @param obj An object whose field to get and set |
|
220 |
* @param delta the value to add |
|
221 |
* @return the previous value |
|
222 |
*/ |
|
223 |
public long getAndAdd(T obj, long delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
224 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
225 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
226 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
227 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
228 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
229 |
return prev; |
2 | 230 |
} |
231 |
||
232 |
/** |
|
233 |
* Atomically increments by one the current value of the field of the |
|
234 |
* given object managed by this updater. |
|
235 |
* |
|
236 |
* @param obj An object whose field to get and set |
|
237 |
* @return the updated value |
|
238 |
*/ |
|
239 |
public long incrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
240 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
241 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
242 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
243 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
244 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
245 |
return next; |
2 | 246 |
} |
247 |
||
248 |
/** |
|
249 |
* Atomically decrements by one the current value of the field of the |
|
250 |
* given object managed by this updater. |
|
251 |
* |
|
252 |
* @param obj An object whose field to get and set |
|
253 |
* @return the updated value |
|
254 |
*/ |
|
255 |
public long decrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
256 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
257 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
258 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
259 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
260 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
261 |
return next; |
2 | 262 |
} |
263 |
||
264 |
/** |
|
265 |
* Atomically adds the given value to the current value of the field of |
|
266 |
* the given object managed by this updater. |
|
267 |
* |
|
268 |
* @param obj An object whose field to get and set |
|
269 |
* @param delta the value to add |
|
270 |
* @return the updated value |
|
271 |
*/ |
|
272 |
public long addAndGet(T obj, long delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
273 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
274 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
275 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
276 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
277 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
278 |
return next; |
2 | 279 |
} |
280 |
||
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
/** |
45637
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
282 |
* Atomically updates (with memory effects as specified by {@link |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
283 |
* VarHandle#compareAndSet}) the field of the given object managed |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
284 |
* by this updater with the results of applying the given |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
285 |
* function, returning the previous value. The function should be |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
286 |
* side-effect-free, since it may be re-applied when attempted |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
287 |
* updates fail due to contention among threads. |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
* @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
|
290 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
293 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
294 |
public final long getAndUpdate(T obj, LongUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
296 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
298 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
} |
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 |
/** |
45637
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
304 |
* Atomically updates (with memory effects as specified by {@link |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
305 |
* VarHandle#compareAndSet}) the field of the given object managed |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
306 |
* by this updater with the results of applying the given |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
307 |
* function, returning the updated value. The function should be |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
308 |
* side-effect-free, since it may be re-applied when attempted |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
309 |
* updates fail due to contention among threads. |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
* @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
|
312 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
313 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
314 |
* @since 1.8 |
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 |
public final long updateAndGet(T obj, LongUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
317 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
318 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
319 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
320 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
321 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
322 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
323 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
324 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
325 |
/** |
45637
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
326 |
* Atomically updates (with memory effects as specified by {@link |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
327 |
* VarHandle#compareAndSet}) the field of the given object managed |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
328 |
* by this updater with the results of applying the given function |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
329 |
* to the current and given values, returning the previous value. |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
330 |
* The function should be side-effect-free, since it may be |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
331 |
* re-applied when attempted updates fail due to contention among |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
332 |
* threads. The function is applied with the current value as its |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
333 |
* first argument, and the given update as the second argument. |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
334 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
335 |
* @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
|
336 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
337 |
* @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
|
338 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
339 |
* @since 1.8 |
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 |
public final long getAndAccumulate(T obj, long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
342 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
343 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
344 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
345 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
346 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
347 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
348 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
349 |
} |
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 |
/** |
45637
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
352 |
* Atomically updates (with memory effects as specified by {@link |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
353 |
* VarHandle#compareAndSet}) the field of the given object managed |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
354 |
* by this updater with the results of applying the given function |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
355 |
* to the current and given values, returning the updated value. |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
356 |
* The function should be side-effect-free, since it may be |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
357 |
* re-applied when attempted updates fail due to contention among |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
358 |
* threads. The function is applied with the current value as its |
9e0c80381e32
8181104: Fix specs for updateAndGet and related methods
dl
parents:
45518
diff
changeset
|
359 |
* first argument, and the given update as the second argument. |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
360 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
361 |
* @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
|
362 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
363 |
* @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
|
364 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
365 |
* @since 1.8 |
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 |
public final long accumulateAndGet(T obj, long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
368 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
369 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
370 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
371 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
372 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
373 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
374 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
375 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
376 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
377 |
private static final class CASUpdater<T> extends AtomicLongFieldUpdater<T> { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
378 |
private static final Unsafe U = Unsafe.getUnsafe(); |
2 | 379 |
private final long offset; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
380 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
381 |
* 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
|
382 |
* the same as tclass |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
383 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
384 |
private final Class<?> cclass; |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
385 |
/** class holding the field */ |
2 | 386 |
private final Class<T> tclass; |
387 |
||
18576 | 388 |
CASUpdater(final Class<T> tclass, final String fieldName, |
389 |
final Class<?> caller) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
390 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
391 |
final int modifiers; |
2 | 392 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
393 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
394 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
395 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
396 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
397 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
398 |
}); |
2 | 399 |
modifiers = field.getModifiers(); |
400 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
401 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
402 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
403 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
404 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
405 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
406 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
407 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
408 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
409 |
throw new RuntimeException(pae.getException()); |
7518 | 410 |
} catch (Exception ex) { |
2 | 411 |
throw new RuntimeException(ex); |
412 |
} |
|
413 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
414 |
if (field.getType() != long.class) |
2 | 415 |
throw new IllegalArgumentException("Must be long type"); |
416 |
||
417 |
if (!Modifier.isVolatile(modifiers)) |
|
418 |
throw new IllegalArgumentException("Must be volatile type"); |
|
419 |
||
43207 | 420 |
// Access to protected field members is restricted to receivers only |
421 |
// of the accessing class, or one of its subclasses, and the |
|
422 |
// accessing class must in turn be a subclass (or package sibling) |
|
423 |
// of the protected member's defining class. |
|
424 |
// If the updater refers to a protected field of a declaring class |
|
425 |
// outside the current package, the receiver argument will be |
|
426 |
// narrowed to the type of the accessing class. |
|
427 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
428 |
tclass.isAssignableFrom(caller) && |
|
429 |
!isSamePackage(tclass, caller)) |
|
430 |
? caller : tclass; |
|
2 | 431 |
this.tclass = tclass; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
432 |
this.offset = U.objectFieldOffset(field); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
433 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
434 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
435 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
436 |
* 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
|
437 |
* failure, throws cause. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
438 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
439 |
private final void accessCheck(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
440 |
if (!cclass.isInstance(obj)) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
441 |
throwAccessCheckException(obj); |
2 | 442 |
} |
443 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
444 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
445 |
* 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
|
446 |
* protected access, else ClassCastException. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
447 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
448 |
private final void throwAccessCheckException(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
449 |
if (cclass == tclass) |
2 | 450 |
throw new ClassCastException(); |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
451 |
else |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
452 |
throw new RuntimeException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
453 |
new IllegalAccessException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
454 |
"Class " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
455 |
cclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
456 |
" 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
|
457 |
tclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
458 |
" using an instance of " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
459 |
obj.getClass().getName())); |
2 | 460 |
} |
461 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
462 |
public final boolean compareAndSet(T obj, long expect, long update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
463 |
accessCheck(obj); |
45518
4a116dd82fb5
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43207
diff
changeset
|
464 |
return U.compareAndSetLong(obj, offset, expect, update); |
2 | 465 |
} |
466 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
467 |
public final boolean weakCompareAndSet(T obj, long expect, long update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
468 |
accessCheck(obj); |
45518
4a116dd82fb5
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43207
diff
changeset
|
469 |
return U.compareAndSetLong(obj, offset, expect, update); |
2 | 470 |
} |
471 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
472 |
public final void set(T obj, long newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
473 |
accessCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
474 |
U.putLongVolatile(obj, offset, newValue); |
2 | 475 |
} |
476 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
477 |
public final void lazySet(T obj, long newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
478 |
accessCheck(obj); |
36936
bfcdf736a998
8152698: Remove obsolete Unsafe.putOrdered{X} methods, usages, runtime and compiler support
shade
parents:
34339
diff
changeset
|
479 |
U.putLongRelease(obj, offset, newValue); |
2 | 480 |
} |
481 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
482 |
public final long get(T obj) { |
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.getLongVolatile(obj, offset); |
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 long getAndSet(T obj, long newValue) { |
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.getAndSetLong(obj, offset, newValue); |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
490 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
491 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
492 |
public final long getAndAdd(T obj, long delta) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
493 |
accessCheck(obj); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
494 |
return U.getAndAddLong(obj, offset, delta); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
495 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
496 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
497 |
public final long getAndIncrement(T obj) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
498 |
return getAndAdd(obj, 1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
499 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
500 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
501 |
public final long getAndDecrement(T obj) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
502 |
return getAndAdd(obj, -1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
503 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
504 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
505 |
public final long incrementAndGet(T obj) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
506 |
return getAndAdd(obj, 1) + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
507 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
508 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
509 |
public final long decrementAndGet(T obj) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
510 |
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
|
511 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
512 |
|
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
513 |
public final long addAndGet(T obj, long delta) { |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
514 |
return getAndAdd(obj, delta) + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
515 |
} |
2 | 516 |
} |
517 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
518 |
private static final class LockedUpdater<T> extends AtomicLongFieldUpdater<T> { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
37363
diff
changeset
|
519 |
private static final Unsafe U = Unsafe.getUnsafe(); |
2 | 520 |
private final long offset; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
521 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
522 |
* 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
|
523 |
* the same as tclass |
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 |
private final Class<?> cclass; |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
526 |
/** class holding the field */ |
2 | 527 |
private final Class<T> tclass; |
528 |
||
18576 | 529 |
LockedUpdater(final Class<T> tclass, final String fieldName, |
530 |
final Class<?> caller) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
531 |
final Field field; |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
532 |
final int modifiers; |
2 | 533 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
534 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
535 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
536 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
537 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
538 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
539 |
}); |
2 | 540 |
modifiers = field.getModifiers(); |
541 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
542 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
543 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
544 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
545 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
546 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
547 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
548 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
549 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
550 |
throw new RuntimeException(pae.getException()); |
7518 | 551 |
} catch (Exception ex) { |
2 | 552 |
throw new RuntimeException(ex); |
553 |
} |
|
554 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
555 |
if (field.getType() != long.class) |
2 | 556 |
throw new IllegalArgumentException("Must be long type"); |
557 |
||
558 |
if (!Modifier.isVolatile(modifiers)) |
|
559 |
throw new IllegalArgumentException("Must be volatile type"); |
|
560 |
||
43207 | 561 |
// Access to protected field members is restricted to receivers only |
562 |
// of the accessing class, or one of its subclasses, and the |
|
563 |
// accessing class must in turn be a subclass (or package sibling) |
|
564 |
// of the protected member's defining class. |
|
565 |
// If the updater refers to a protected field of a declaring class |
|
566 |
// outside the current package, the receiver argument will be |
|
567 |
// narrowed to the type of the accessing class. |
|
568 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
569 |
tclass.isAssignableFrom(caller) && |
|
570 |
!isSamePackage(tclass, caller)) |
|
571 |
? caller : tclass; |
|
2 | 572 |
this.tclass = tclass; |
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
573 |
this.offset = U.objectFieldOffset(field); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
574 |
} |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
575 |
|
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
576 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
577 |
* 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
|
578 |
* failure, throws cause. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
579 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
580 |
private final void accessCheck(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
581 |
if (!cclass.isInstance(obj)) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
582 |
throw accessCheckException(obj); |
2 | 583 |
} |
584 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
585 |
/** |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
586 |
* Returns 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
|
587 |
* protected access, else ClassCastException. |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
588 |
*/ |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
589 |
private final RuntimeException accessCheckException(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
590 |
if (cclass == tclass) |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
591 |
return new ClassCastException(); |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
592 |
else |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
593 |
return new RuntimeException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
594 |
new IllegalAccessException( |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
595 |
"Class " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
596 |
cclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
597 |
" 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
|
598 |
tclass.getName() + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
599 |
" using an instance of " + |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
600 |
obj.getClass().getName())); |
2 | 601 |
} |
602 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
603 |
public final boolean compareAndSet(T obj, long expect, long update) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
604 |
accessCheck(obj); |
7518 | 605 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
606 |
long v = U.getLong(obj, offset); |
2 | 607 |
if (v != expect) |
608 |
return false; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
609 |
U.putLong(obj, offset, update); |
2 | 610 |
return true; |
611 |
} |
|
612 |
} |
|
613 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
614 |
public final boolean weakCompareAndSet(T obj, long expect, long update) { |
2 | 615 |
return compareAndSet(obj, expect, update); |
616 |
} |
|
617 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
618 |
public final void set(T obj, long newValue) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
619 |
accessCheck(obj); |
7518 | 620 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
621 |
U.putLong(obj, offset, newValue); |
2 | 622 |
} |
623 |
} |
|
624 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
625 |
public final void lazySet(T obj, long newValue) { |
2 | 626 |
set(obj, newValue); |
627 |
} |
|
628 |
||
34339
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
629 |
public final long get(T obj) { |
61d78c23fcdc
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
dl
parents:
33674
diff
changeset
|
630 |
accessCheck(obj); |
7518 | 631 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
632 |
return U.getLong(obj, offset); |
2 | 633 |
} |
634 |
} |
|
635 |
} |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
636 |
|
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
637 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
638 |
* 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
|
639 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
640 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
641 |
*/ |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
642 |
static boolean isAncestor(ClassLoader first, ClassLoader second) { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
643 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
644 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
645 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
646 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
647 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
648 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
649 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
650 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
651 |
} |
43207 | 652 |
|
653 |
/** |
|
654 |
* Returns true if the two classes have the same class loader and |
|
655 |
* package qualifier |
|
656 |
*/ |
|
657 |
static boolean isSamePackage(Class<?> class1, Class<?> class2) { |
|
658 |
return class1.getClassLoader() == class2.getClassLoader() |
|
659 |
&& Objects.equals(class1.getPackageName(), class2.getPackageName()); |
|
660 |
} |
|
2 | 661 |
} |