author | mduigou |
Tue, 19 Feb 2013 11:56:49 -0800 | |
changeset 16011 | 890a7ed97f6c |
parent 15267 | c884f548a25f |
child 16906 | 44dfee24cb71 |
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; |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
37 |
import java.util.function.IntUnaryOperator; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
38 |
import java.util.function.IntBinaryOperator; |
2 | 39 |
import sun.misc.Unsafe; |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
40 |
import java.lang.reflect.Field; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
41 |
import java.lang.reflect.Modifier; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
42 |
import java.security.AccessController; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
43 |
import java.security.PrivilegedExceptionAction; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
44 |
import java.security.PrivilegedActionException; |
2 | 45 |
|
46 |
/** |
|
47 |
* A reflection-based utility that enables atomic updates to |
|
48 |
* designated {@code volatile int} fields of designated classes. |
|
49 |
* This class is designed for use in atomic data structures in which |
|
50 |
* several fields of the same node are independently subject to atomic |
|
51 |
* updates. |
|
52 |
* |
|
53 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
54 |
* method in this class are weaker than in other atomic classes. |
|
55 |
* Because this class cannot ensure that all uses of the field |
|
56 |
* are appropriate for purposes of atomic access, it can |
|
57 |
* guarantee atomicity only with respect to other invocations of |
|
58 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
59 |
* |
|
60 |
* @since 1.5 |
|
61 |
* @author Doug Lea |
|
62 |
* @param <T> The type of the object holding the updatable field |
|
63 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
64 |
public abstract class AtomicIntegerFieldUpdater<T> { |
2 | 65 |
/** |
66 |
* Creates and returns an updater for objects with the given field. |
|
67 |
* The Class argument is needed to check that reflective types and |
|
68 |
* generic types match. |
|
69 |
* |
|
70 |
* @param tclass the class of the objects holding the field |
|
71 |
* @param fieldName the name of the field to be updated |
|
72 |
* @return the updater |
|
73 |
* @throws IllegalArgumentException if the field is not a |
|
74 |
* volatile integer type |
|
75 |
* @throws RuntimeException with a nested reflection-based |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
76 |
* 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
|
77 |
* 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
|
78 |
* access control |
2 | 79 |
*/ |
80 |
public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
|
81 |
return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Protected do-nothing constructor for use by subclasses. |
|
86 |
*/ |
|
87 |
protected AtomicIntegerFieldUpdater() { |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Atomically sets the field of the given object managed by this updater |
|
92 |
* to the given updated value if the current value {@code ==} the |
|
93 |
* expected value. This method is guaranteed to be atomic with respect to |
|
94 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
95 |
* necessarily with respect to other changes in the field. |
|
96 |
* |
|
97 |
* @param obj An object whose field to conditionally set |
|
98 |
* @param expect the expected value |
|
99 |
* @param update the new value |
|
100 |
* @return true if successful |
|
101 |
* @throws ClassCastException if {@code obj} is not an instance |
|
102 |
* of the class possessing the field established in the constructor |
|
103 |
*/ |
|
104 |
public abstract boolean compareAndSet(T obj, int expect, int update); |
|
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 |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
|
114 |
* and does not provide ordering guarantees, so is only rarely an |
|
115 |
* appropriate alternative to {@code compareAndSet}. |
|
116 |
* |
|
117 |
* @param obj An object whose field to conditionally set |
|
118 |
* @param expect the expected value |
|
119 |
* @param update the new value |
|
120 |
* @return true if successful |
|
121 |
* @throws ClassCastException if {@code obj} is not an instance |
|
122 |
* of the class possessing the field established in the constructor |
|
123 |
*/ |
|
124 |
public abstract boolean weakCompareAndSet(T obj, int expect, int update); |
|
125 |
||
126 |
/** |
|
127 |
* Sets the field of the given object managed by this updater to the |
|
128 |
* given updated value. This operation is guaranteed to act as a volatile |
|
129 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
130 |
* |
|
131 |
* @param obj An object whose field to set |
|
132 |
* @param newValue the new value |
|
133 |
*/ |
|
134 |
public abstract void set(T obj, int newValue); |
|
135 |
||
136 |
/** |
|
137 |
* Eventually sets the field of the given object managed by this |
|
138 |
* updater to the given updated value. |
|
139 |
* |
|
140 |
* @param obj An object whose field to set |
|
141 |
* @param newValue the new value |
|
142 |
* @since 1.6 |
|
143 |
*/ |
|
144 |
public abstract void lazySet(T obj, int newValue); |
|
145 |
||
146 |
/** |
|
147 |
* Gets the current value held in the field of the given object managed |
|
148 |
* by this updater. |
|
149 |
* |
|
150 |
* @param obj An object whose field to get |
|
151 |
* @return the current value |
|
152 |
*/ |
|
153 |
public abstract int get(T obj); |
|
154 |
||
155 |
/** |
|
156 |
* Atomically sets the field of the given object managed by this updater |
|
157 |
* to the given value and returns the old value. |
|
158 |
* |
|
159 |
* @param obj An object whose field to get and set |
|
160 |
* @param newValue the new value |
|
161 |
* @return the previous value |
|
162 |
*/ |
|
163 |
public int getAndSet(T obj, int newValue) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
164 |
int prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
165 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
166 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
167 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
168 |
return prev; |
2 | 169 |
} |
170 |
||
171 |
/** |
|
172 |
* Atomically increments by one the current value of the field of the |
|
173 |
* given object managed by this updater. |
|
174 |
* |
|
175 |
* @param obj An object whose field to get and set |
|
176 |
* @return the previous value |
|
177 |
*/ |
|
178 |
public int getAndIncrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
179 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
180 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
181 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
182 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
183 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
184 |
return prev; |
2 | 185 |
} |
186 |
||
187 |
/** |
|
188 |
* Atomically decrements by one the current value of the field of the |
|
189 |
* given object managed by this updater. |
|
190 |
* |
|
191 |
* @param obj An object whose field to get and set |
|
192 |
* @return the previous value |
|
193 |
*/ |
|
194 |
public int getAndDecrement(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
195 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
196 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
197 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
198 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
199 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
200 |
return prev; |
2 | 201 |
} |
202 |
||
203 |
/** |
|
204 |
* Atomically adds the given value to the current value of the field of |
|
205 |
* the given object managed by this updater. |
|
206 |
* |
|
207 |
* @param obj An object whose field to get and set |
|
208 |
* @param delta the value to add |
|
209 |
* @return the previous value |
|
210 |
*/ |
|
211 |
public int getAndAdd(T obj, int delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
212 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
213 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
214 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
215 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
216 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
217 |
return prev; |
2 | 218 |
} |
219 |
||
220 |
/** |
|
221 |
* Atomically increments by one the current value of the field of the |
|
222 |
* given object managed by this updater. |
|
223 |
* |
|
224 |
* @param obj An object whose field to get and set |
|
225 |
* @return the updated value |
|
226 |
*/ |
|
227 |
public int incrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
228 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
229 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
230 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
231 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
232 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
233 |
return next; |
2 | 234 |
} |
235 |
||
236 |
/** |
|
237 |
* Atomically decrements by one the current value of the field of the |
|
238 |
* given object managed by this updater. |
|
239 |
* |
|
240 |
* @param obj An object whose field to get and set |
|
241 |
* @return the updated value |
|
242 |
*/ |
|
243 |
public int decrementAndGet(T obj) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
244 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
245 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
246 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
247 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
248 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
249 |
return next; |
2 | 250 |
} |
251 |
||
252 |
/** |
|
253 |
* Atomically adds the given value to the current value of the field of |
|
254 |
* the given object managed by this updater. |
|
255 |
* |
|
256 |
* @param obj An object whose field to get and set |
|
257 |
* @param delta the value to add |
|
258 |
* @return the updated value |
|
259 |
*/ |
|
260 |
public int addAndGet(T obj, int delta) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
261 |
int prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
262 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
263 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
264 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
265 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
266 |
return next; |
2 | 267 |
} |
268 |
||
269 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
270 |
* 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
|
271 |
* 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
|
272 |
* 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
|
273 |
* 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
|
274 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* @param obj An object whose field to get and set |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
284 |
next = updateFunction.applyAsInt(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
290 |
* 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
|
291 |
* 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
|
292 |
* 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
|
293 |
* 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
|
294 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
* @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
|
296 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
298 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
public final int updateAndGet(T obj, IntUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
302 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
304 |
next = updateFunction.applyAsInt(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
* 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
|
311 |
* 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
|
312 |
* 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
|
313 |
* 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
|
314 |
* 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
|
315 |
* 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
|
316 |
* 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
|
317 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
318 |
* @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
|
319 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
320 |
* @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
|
321 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
322 |
* @since 1.8 |
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 |
public final int getAndAccumulate(T obj, int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
325 |
IntBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
326 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
327 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
328 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
329 |
next = accumulatorFunction.applyAsInt(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
330 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
331 |
return prev; |
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 |
|
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 |
* 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
|
336 |
* 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
|
337 |
* 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
|
338 |
* 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
|
339 |
* 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
|
340 |
* 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
|
341 |
* 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
|
342 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
343 |
* @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
|
344 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
345 |
* @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
|
346 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
347 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
348 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
349 |
public final int accumulateAndGet(T obj, int x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
350 |
IntBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
351 |
int prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
352 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
353 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
354 |
next = accumulatorFunction.applyAsInt(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
355 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
356 |
return next; |
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 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
359 |
/** |
2 | 360 |
* Standard hotspot implementation using intrinsics |
361 |
*/ |
|
362 |
private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> { |
|
363 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
364 |
private final long offset; |
|
365 |
private final Class<T> tclass; |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
366 |
private final Class<?> cclass; |
2 | 367 |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
368 |
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName) { |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
369 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
370 |
final Class<?> caller; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
371 |
final int modifiers; |
2 | 372 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
373 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
374 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
375 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
376 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
377 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
378 |
}); |
2 | 379 |
caller = sun.reflect.Reflection.getCallerClass(3); |
380 |
modifiers = field.getModifiers(); |
|
381 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
382 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
383 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
384 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
385 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
386 |
((cl == null) || !isAncestor(cl, ccl))) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
387 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
388 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
389 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
390 |
throw new RuntimeException(pae.getException()); |
7518 | 391 |
} catch (Exception ex) { |
2 | 392 |
throw new RuntimeException(ex); |
393 |
} |
|
394 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
395 |
Class<?> fieldt = field.getType(); |
2 | 396 |
if (fieldt != int.class) |
397 |
throw new IllegalArgumentException("Must be integer type"); |
|
398 |
||
399 |
if (!Modifier.isVolatile(modifiers)) |
|
400 |
throw new IllegalArgumentException("Must be volatile type"); |
|
401 |
||
402 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
403 |
caller != tclass) ? caller : null; |
|
404 |
this.tclass = tclass; |
|
405 |
offset = unsafe.objectFieldOffset(field); |
|
406 |
} |
|
407 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
408 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
409 |
* 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
|
410 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
411 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
412 |
*/ |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
413 |
private static boolean isAncestor(ClassLoader first, ClassLoader second) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
414 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
415 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
416 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
417 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
418 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
419 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
420 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
421 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
422 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
423 |
|
2 | 424 |
private void fullCheck(T obj) { |
425 |
if (!tclass.isInstance(obj)) |
|
426 |
throw new ClassCastException(); |
|
427 |
if (cclass != null) |
|
428 |
ensureProtectedAccess(obj); |
|
429 |
} |
|
430 |
||
431 |
public boolean compareAndSet(T obj, int expect, int update) { |
|
432 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
433 |
return unsafe.compareAndSwapInt(obj, offset, expect, update); |
|
434 |
} |
|
435 |
||
436 |
public boolean weakCompareAndSet(T obj, int expect, int update) { |
|
437 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
438 |
return unsafe.compareAndSwapInt(obj, offset, expect, update); |
|
439 |
} |
|
440 |
||
441 |
public void set(T obj, int newValue) { |
|
442 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
443 |
unsafe.putIntVolatile(obj, offset, newValue); |
|
444 |
} |
|
445 |
||
446 |
public void lazySet(T obj, int newValue) { |
|
447 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
448 |
unsafe.putOrderedInt(obj, offset, newValue); |
|
449 |
} |
|
450 |
||
451 |
public final int get(T obj) { |
|
452 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
453 |
return unsafe.getIntVolatile(obj, offset); |
|
454 |
} |
|
455 |
||
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
456 |
public int getAndSet(T obj, int newValue) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
457 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
458 |
return unsafe.getAndSetInt(obj, offset, newValue); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
459 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
460 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
461 |
public int getAndIncrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
462 |
return getAndAdd(obj, 1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
463 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
464 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
465 |
public int getAndDecrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
466 |
return getAndAdd(obj, -1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
467 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
468 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
469 |
public int getAndAdd(T obj, int delta) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
470 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
471 |
return unsafe.getAndAddInt(obj, offset, delta); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
472 |
} |
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 |
public int incrementAndGet(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
475 |
return getAndAdd(obj, 1) + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
476 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
477 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
478 |
public int decrementAndGet(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
479 |
return getAndAdd(obj, -1) - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
480 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
481 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
482 |
public int addAndGet(T obj, int delta) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
483 |
return getAndAdd(obj, delta) + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
484 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
485 |
|
2 | 486 |
private void ensureProtectedAccess(T obj) { |
487 |
if (cclass.isInstance(obj)) { |
|
488 |
return; |
|
489 |
} |
|
490 |
throw new RuntimeException( |
|
491 |
new IllegalAccessException("Class " + |
|
492 |
cclass.getName() + |
|
493 |
" can not access a protected member of class " + |
|
494 |
tclass.getName() + |
|
495 |
" using an instance of " + |
|
496 |
obj.getClass().getName() |
|
497 |
) |
|
498 |
); |
|
499 |
} |
|
500 |
} |
|
501 |
} |