author | chegar |
Wed, 11 Nov 2015 09:19:12 +0000 | |
changeset 33674 | 566777f73c32 |
parent 32838 | caeef2c79243 |
child 34339 | 61d78c23fcdc |
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; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
43 |
import java.util.function.LongBinaryOperator; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
44 |
import java.util.function.LongUnaryOperator; |
18576 | 45 |
import sun.reflect.CallerSensitive; |
46 |
import sun.reflect.Reflection; |
|
2 | 47 |
|
48 |
/** |
|
49 |
* A reflection-based utility that enables atomic updates to |
|
50 |
* designated {@code volatile long} fields of designated classes. |
|
51 |
* This class is designed for use in atomic data structures in which |
|
52 |
* several fields of the same node are independently subject to atomic |
|
53 |
* updates. |
|
54 |
* |
|
55 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
56 |
* method in this class are weaker than in other atomic classes. |
|
57 |
* Because this class cannot ensure that all uses of the field |
|
58 |
* are appropriate for purposes of atomic access, it can |
|
59 |
* guarantee atomicity only with respect to other invocations of |
|
60 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
61 |
* |
|
62 |
* @since 1.5 |
|
63 |
* @author Doug Lea |
|
64 |
* @param <T> The type of the object holding the updatable field |
|
65 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
66 |
public abstract class AtomicLongFieldUpdater<T> { |
2 | 67 |
/** |
68 |
* Creates and returns an updater for objects with the given field. |
|
69 |
* The Class argument is needed to check that reflective types and |
|
70 |
* generic types match. |
|
71 |
* |
|
72 |
* @param tclass the class of the objects holding the field |
|
18576 | 73 |
* @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
|
74 |
* @param <U> the type of instances of tclass |
2 | 75 |
* @return the updater |
76 |
* @throws IllegalArgumentException if the field is not a |
|
18576 | 77 |
* volatile long type |
2 | 78 |
* @throws RuntimeException with a nested reflection-based |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
79 |
* 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
|
80 |
* 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
|
81 |
* access control |
2 | 82 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
83 |
@CallerSensitive |
18576 | 84 |
public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, |
85 |
String fieldName) { |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
86 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 87 |
if (AtomicLong.VM_SUPPORTS_LONG_CAS) |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
88 |
return new CASUpdater<U>(tclass, fieldName, caller); |
2 | 89 |
else |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
90 |
return new LockedUpdater<U>(tclass, fieldName, caller); |
2 | 91 |
} |
92 |
||
93 |
/** |
|
94 |
* Protected do-nothing constructor for use by subclasses. |
|
95 |
*/ |
|
96 |
protected AtomicLongFieldUpdater() { |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Atomically sets the field of the given object managed by this updater |
|
101 |
* to the given updated value if the current value {@code ==} the |
|
102 |
* expected value. This method is guaranteed to be atomic with respect to |
|
103 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
104 |
* necessarily with respect to other changes in the field. |
|
105 |
* |
|
106 |
* @param obj An object whose field to conditionally set |
|
107 |
* @param expect the expected value |
|
108 |
* @param update the new value |
|
18576 | 109 |
* @return {@code true} if successful |
2 | 110 |
* @throws ClassCastException if {@code obj} is not an instance |
18576 | 111 |
* of the class possessing the field established in the constructor |
2 | 112 |
*/ |
113 |
public abstract boolean compareAndSet(T obj, long expect, long update); |
|
114 |
||
115 |
/** |
|
116 |
* Atomically sets the field of the given object managed by this updater |
|
117 |
* to the given updated value if the current value {@code ==} the |
|
118 |
* expected value. This method is guaranteed to be atomic with respect to |
|
119 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
120 |
* necessarily with respect to other changes in the field. |
|
121 |
* |
|
18576 | 122 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
123 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
124 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 125 |
* |
126 |
* @param obj An object whose field to conditionally set |
|
127 |
* @param expect the expected value |
|
128 |
* @param update the new value |
|
18576 | 129 |
* @return {@code true} if successful |
2 | 130 |
* @throws ClassCastException if {@code obj} is not an instance |
18576 | 131 |
* of the class possessing the field established in the constructor |
2 | 132 |
*/ |
133 |
public abstract boolean weakCompareAndSet(T obj, long expect, long update); |
|
134 |
||
135 |
/** |
|
136 |
* Sets the field of the given object managed by this updater to the |
|
137 |
* given updated value. This operation is guaranteed to act as a volatile |
|
138 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
139 |
* |
|
140 |
* @param obj An object whose field to set |
|
141 |
* @param newValue the new value |
|
142 |
*/ |
|
143 |
public abstract void set(T obj, long newValue); |
|
144 |
||
145 |
/** |
|
146 |
* Eventually sets the field of the given object managed by this |
|
147 |
* updater to the given updated value. |
|
148 |
* |
|
149 |
* @param obj An object whose field to set |
|
150 |
* @param newValue the new value |
|
151 |
* @since 1.6 |
|
152 |
*/ |
|
153 |
public abstract void lazySet(T obj, long newValue); |
|
154 |
||
155 |
/** |
|
156 |
* Gets the current value held in the field of the given object managed |
|
157 |
* by this updater. |
|
158 |
* |
|
159 |
* @param obj An object whose field to get |
|
160 |
* @return the current value |
|
161 |
*/ |
|
162 |
public abstract long get(T obj); |
|
163 |
||
164 |
/** |
|
165 |
* Atomically sets the field of the given object managed by this updater |
|
166 |
* to the given value and returns the old value. |
|
167 |
* |
|
168 |
* @param obj An object whose field to get and set |
|
169 |
* @param newValue the new value |
|
170 |
* @return the previous value |
|
171 |
*/ |
|
172 |
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
|
173 |
long prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
174 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
175 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
176 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
177 |
return prev; |
2 | 178 |
} |
179 |
||
180 |
/** |
|
181 |
* Atomically increments by one the current value of the field of the |
|
182 |
* given object managed by this updater. |
|
183 |
* |
|
184 |
* @param obj An object whose field to get and set |
|
185 |
* @return the previous value |
|
186 |
*/ |
|
187 |
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
|
188 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
189 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
190 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
191 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
192 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
193 |
return prev; |
2 | 194 |
} |
195 |
||
196 |
/** |
|
197 |
* Atomically decrements by one the current value of the field of the |
|
198 |
* given object managed by this updater. |
|
199 |
* |
|
200 |
* @param obj An object whose field to get and set |
|
201 |
* @return the previous value |
|
202 |
*/ |
|
203 |
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
|
204 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
205 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
206 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
207 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
208 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
209 |
return prev; |
2 | 210 |
} |
211 |
||
212 |
/** |
|
213 |
* Atomically adds the given value to the current value of the field of |
|
214 |
* the given object managed by this updater. |
|
215 |
* |
|
216 |
* @param obj An object whose field to get and set |
|
217 |
* @param delta the value to add |
|
218 |
* @return the previous value |
|
219 |
*/ |
|
220 |
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
|
221 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
222 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
223 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
224 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
225 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
226 |
return prev; |
2 | 227 |
} |
228 |
||
229 |
/** |
|
230 |
* Atomically increments by one the current value of the field of the |
|
231 |
* given object managed by this updater. |
|
232 |
* |
|
233 |
* @param obj An object whose field to get and set |
|
234 |
* @return the updated value |
|
235 |
*/ |
|
236 |
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
|
237 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
238 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
239 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
240 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
241 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
242 |
return next; |
2 | 243 |
} |
244 |
||
245 |
/** |
|
246 |
* Atomically decrements by one the current value of the field of the |
|
247 |
* given object managed by this updater. |
|
248 |
* |
|
249 |
* @param obj An object whose field to get and set |
|
250 |
* @return the updated value |
|
251 |
*/ |
|
252 |
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
|
253 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
254 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
255 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
256 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
257 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
258 |
return next; |
2 | 259 |
} |
260 |
||
261 |
/** |
|
262 |
* Atomically adds the given value to the current value of the field of |
|
263 |
* the given object managed by this updater. |
|
264 |
* |
|
265 |
* @param obj An object whose field to get and set |
|
266 |
* @param delta the value to add |
|
267 |
* @return the updated value |
|
268 |
*/ |
|
269 |
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
|
270 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
271 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
272 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
273 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
274 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
275 |
return next; |
2 | 276 |
} |
277 |
||
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
* 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
|
280 |
* 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
|
281 |
* 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
|
282 |
* 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
|
283 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
284 |
* @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
|
285 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
* @since 1.8 |
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 |
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
|
290 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
293 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
294 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
return prev; |
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 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
* 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
|
300 |
* 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
|
301 |
* 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
|
302 |
* 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
|
303 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
304 |
* @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
|
305 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
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
|
310 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
312 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
313 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
314 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
315 |
return next; |
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 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
319 |
* 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
|
320 |
* 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
|
321 |
* 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
|
322 |
* 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
|
323 |
* 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
|
324 |
* 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
|
325 |
* 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
|
326 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
327 |
* @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
|
328 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
329 |
* @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
|
330 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
331 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
332 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
333 |
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
|
334 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
335 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
336 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
337 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
338 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
339 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
340 |
return prev; |
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 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
344 |
* 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
|
345 |
* 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
|
346 |
* 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
|
347 |
* 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
|
348 |
* 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
|
349 |
* 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
|
350 |
* 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
|
351 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
352 |
* @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
|
353 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
354 |
* @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
|
355 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
356 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
357 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
358 |
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
|
359 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
360 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
361 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
362 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
363 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
364 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
365 |
return next; |
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 |
|
2 | 368 |
private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> { |
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
32838
diff
changeset
|
369 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
2 | 370 |
private final long offset; |
371 |
private final Class<T> tclass; |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
372 |
private final Class<?> cclass; |
2 | 373 |
|
18576 | 374 |
CASUpdater(final Class<T> tclass, final String fieldName, |
375 |
final Class<?> caller) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
376 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
377 |
final int modifiers; |
2 | 378 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
379 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
380 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
381 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
382 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
383 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
384 |
}); |
2 | 385 |
modifiers = field.getModifiers(); |
386 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
387 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
388 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
389 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
390 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
391 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
392 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
393 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
394 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
395 |
throw new RuntimeException(pae.getException()); |
7518 | 396 |
} catch (Exception ex) { |
2 | 397 |
throw new RuntimeException(ex); |
398 |
} |
|
399 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
400 |
Class<?> fieldt = field.getType(); |
2 | 401 |
if (fieldt != long.class) |
402 |
throw new IllegalArgumentException("Must be long type"); |
|
403 |
||
404 |
if (!Modifier.isVolatile(modifiers)) |
|
405 |
throw new IllegalArgumentException("Must be volatile type"); |
|
406 |
||
407 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
408 |
caller != tclass) ? caller : null; |
|
409 |
this.tclass = tclass; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
410 |
offset = U.objectFieldOffset(field); |
2 | 411 |
} |
412 |
||
413 |
private void fullCheck(T obj) { |
|
414 |
if (!tclass.isInstance(obj)) |
|
415 |
throw new ClassCastException(); |
|
416 |
if (cclass != null) |
|
417 |
ensureProtectedAccess(obj); |
|
418 |
} |
|
419 |
||
420 |
public boolean compareAndSet(T obj, long expect, long update) { |
|
421 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
422 |
return U.compareAndSwapLong(obj, offset, expect, update); |
2 | 423 |
} |
424 |
||
425 |
public boolean weakCompareAndSet(T obj, long expect, long update) { |
|
426 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
427 |
return U.compareAndSwapLong(obj, offset, expect, update); |
2 | 428 |
} |
429 |
||
430 |
public void set(T obj, long newValue) { |
|
431 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
432 |
U.putLongVolatile(obj, offset, newValue); |
2 | 433 |
} |
434 |
||
435 |
public void lazySet(T obj, long newValue) { |
|
436 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
437 |
U.putOrderedLong(obj, offset, newValue); |
2 | 438 |
} |
439 |
||
440 |
public long get(T obj) { |
|
441 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
442 |
return U.getLongVolatile(obj, offset); |
2 | 443 |
} |
444 |
||
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
445 |
public long getAndSet(T obj, long newValue) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
446 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
447 |
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
|
448 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
449 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
450 |
public long getAndIncrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
451 |
return getAndAdd(obj, 1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
452 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
453 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
454 |
public long getAndDecrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
455 |
return getAndAdd(obj, -1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
456 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
457 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
458 |
public long getAndAdd(T obj, long delta) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
459 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
460 |
return U.getAndAddLong(obj, offset, delta); |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
461 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
462 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
463 |
public long incrementAndGet(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
464 |
return getAndAdd(obj, 1) + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
465 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
466 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
467 |
public long decrementAndGet(T obj) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
468 |
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
|
469 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
470 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
471 |
public long addAndGet(T obj, long delta) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
472 |
return getAndAdd(obj, delta) + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
473 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
474 |
|
2 | 475 |
private void ensureProtectedAccess(T obj) { |
476 |
if (cclass.isInstance(obj)) { |
|
477 |
return; |
|
478 |
} |
|
7518 | 479 |
throw new RuntimeException( |
2 | 480 |
new IllegalAccessException("Class " + |
481 |
cclass.getName() + |
|
482 |
" can not access a protected member of class " + |
|
483 |
tclass.getName() + |
|
484 |
" using an instance of " + |
|
485 |
obj.getClass().getName() |
|
486 |
) |
|
487 |
); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
||
492 |
private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> { |
|
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
32838
diff
changeset
|
493 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
2 | 494 |
private final long offset; |
495 |
private final Class<T> tclass; |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
496 |
private final Class<?> cclass; |
2 | 497 |
|
18576 | 498 |
LockedUpdater(final Class<T> tclass, final String fieldName, |
499 |
final Class<?> caller) { |
|
2 | 500 |
Field field = null; |
501 |
int modifiers = 0; |
|
502 |
try { |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
503 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
504 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
505 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
506 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
507 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
508 |
}); |
2 | 509 |
modifiers = field.getModifiers(); |
510 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
511 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
512 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
513 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
514 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
515 |
((cl == null) || !isAncestor(cl, ccl))) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
516 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
517 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
518 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
519 |
throw new RuntimeException(pae.getException()); |
7518 | 520 |
} catch (Exception ex) { |
2 | 521 |
throw new RuntimeException(ex); |
522 |
} |
|
523 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
524 |
Class<?> fieldt = field.getType(); |
2 | 525 |
if (fieldt != long.class) |
526 |
throw new IllegalArgumentException("Must be long type"); |
|
527 |
||
528 |
if (!Modifier.isVolatile(modifiers)) |
|
529 |
throw new IllegalArgumentException("Must be volatile type"); |
|
530 |
||
531 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
532 |
caller != tclass) ? caller : null; |
|
533 |
this.tclass = tclass; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
534 |
offset = U.objectFieldOffset(field); |
2 | 535 |
} |
536 |
||
537 |
private void fullCheck(T obj) { |
|
538 |
if (!tclass.isInstance(obj)) |
|
539 |
throw new ClassCastException(); |
|
540 |
if (cclass != null) |
|
541 |
ensureProtectedAccess(obj); |
|
542 |
} |
|
543 |
||
544 |
public boolean compareAndSet(T obj, long expect, long update) { |
|
545 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 546 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
547 |
long v = U.getLong(obj, offset); |
2 | 548 |
if (v != expect) |
549 |
return false; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
550 |
U.putLong(obj, offset, update); |
2 | 551 |
return true; |
552 |
} |
|
553 |
} |
|
554 |
||
555 |
public boolean weakCompareAndSet(T obj, long expect, long update) { |
|
556 |
return compareAndSet(obj, expect, update); |
|
557 |
} |
|
558 |
||
559 |
public void set(T obj, long newValue) { |
|
560 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 561 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
562 |
U.putLong(obj, offset, newValue); |
2 | 563 |
} |
564 |
} |
|
565 |
||
566 |
public void lazySet(T obj, long newValue) { |
|
567 |
set(obj, newValue); |
|
568 |
} |
|
569 |
||
570 |
public long get(T obj) { |
|
571 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 572 |
synchronized (this) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
573 |
return U.getLong(obj, offset); |
2 | 574 |
} |
575 |
} |
|
576 |
||
577 |
private void ensureProtectedAccess(T obj) { |
|
578 |
if (cclass.isInstance(obj)) { |
|
579 |
return; |
|
580 |
} |
|
7518 | 581 |
throw new RuntimeException( |
2 | 582 |
new IllegalAccessException("Class " + |
583 |
cclass.getName() + |
|
584 |
" can not access a protected member of class " + |
|
585 |
tclass.getName() + |
|
586 |
" using an instance of " + |
|
587 |
obj.getClass().getName() |
|
588 |
) |
|
589 |
); |
|
590 |
} |
|
591 |
} |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
592 |
|
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
593 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
594 |
* 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
|
595 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
596 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
597 |
*/ |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
598 |
static boolean isAncestor(ClassLoader first, ClassLoader second) { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
599 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
600 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
601 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
602 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
603 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
604 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
605 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
606 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
607 |
} |
2 | 608 |
} |