author | dl |
Wed, 16 Jan 2013 12:09:35 +0000 | |
changeset 15267 | c884f548a25f |
parent 15020 | 50394fa17c1b |
child 16011 | 890a7ed97f6c |
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.LongUnaryOperator; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
38 |
import java.util.function.LongBinaryOperator; |
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 long} 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 AtomicLongFieldUpdater<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 long 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> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
|
81 |
if (AtomicLong.VM_SUPPORTS_LONG_CAS) |
|
82 |
return new CASUpdater<U>(tclass, fieldName); |
|
83 |
else |
|
84 |
return new LockedUpdater<U>(tclass, fieldName); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Protected do-nothing constructor for use by subclasses. |
|
89 |
*/ |
|
90 |
protected AtomicLongFieldUpdater() { |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Atomically sets the field of the given object managed by this updater |
|
95 |
* to the given updated value if the current value {@code ==} the |
|
96 |
* expected value. This method is guaranteed to be atomic with respect to |
|
97 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
98 |
* necessarily with respect to other changes in the field. |
|
99 |
* |
|
100 |
* @param obj An object whose field to conditionally set |
|
101 |
* @param expect the expected value |
|
102 |
* @param update the new value |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
103 |
* @return true if successful |
2 | 104 |
* @throws ClassCastException if {@code obj} is not an instance |
105 |
* of the class possessing the field established in the constructor. |
|
106 |
*/ |
|
107 |
public abstract boolean compareAndSet(T obj, long expect, long update); |
|
108 |
||
109 |
/** |
|
110 |
* Atomically sets the field of the given object managed by this updater |
|
111 |
* to the given updated value if the current value {@code ==} the |
|
112 |
* expected value. This method is guaranteed to be atomic with respect to |
|
113 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
114 |
* necessarily with respect to other changes in the field. |
|
115 |
* |
|
116 |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
|
117 |
* and does not provide ordering guarantees, so is only rarely an |
|
118 |
* appropriate alternative to {@code compareAndSet}. |
|
119 |
* |
|
120 |
* @param obj An object whose field to conditionally set |
|
121 |
* @param expect the expected value |
|
122 |
* @param update the new value |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
123 |
* @return true if successful |
2 | 124 |
* @throws ClassCastException if {@code obj} is not an instance |
125 |
* of the class possessing the field established in the constructor. |
|
126 |
*/ |
|
127 |
public abstract boolean weakCompareAndSet(T obj, long expect, long update); |
|
128 |
||
129 |
/** |
|
130 |
* Sets the field of the given object managed by this updater to the |
|
131 |
* given updated value. This operation is guaranteed to act as a volatile |
|
132 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
133 |
* |
|
134 |
* @param obj An object whose field to set |
|
135 |
* @param newValue the new value |
|
136 |
*/ |
|
137 |
public abstract void set(T obj, long newValue); |
|
138 |
||
139 |
/** |
|
140 |
* Eventually sets the field of the given object managed by this |
|
141 |
* updater to the given updated value. |
|
142 |
* |
|
143 |
* @param obj An object whose field to set |
|
144 |
* @param newValue the new value |
|
145 |
* @since 1.6 |
|
146 |
*/ |
|
147 |
public abstract void lazySet(T obj, long newValue); |
|
148 |
||
149 |
/** |
|
150 |
* Gets the current value held in the field of the given object managed |
|
151 |
* by this updater. |
|
152 |
* |
|
153 |
* @param obj An object whose field to get |
|
154 |
* @return the current value |
|
155 |
*/ |
|
156 |
public abstract long get(T obj); |
|
157 |
||
158 |
/** |
|
159 |
* Atomically sets the field of the given object managed by this updater |
|
160 |
* to the given value and returns the old value. |
|
161 |
* |
|
162 |
* @param obj An object whose field to get and set |
|
163 |
* @param newValue the new value |
|
164 |
* @return the previous value |
|
165 |
*/ |
|
166 |
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
|
167 |
long prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
168 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
169 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
170 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
171 |
return prev; |
2 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Atomically increments by one the current value of the field of the |
|
176 |
* given object managed by this updater. |
|
177 |
* |
|
178 |
* @param obj An object whose field to get and set |
|
179 |
* @return the previous value |
|
180 |
*/ |
|
181 |
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
|
182 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
183 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
184 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
185 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
186 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
187 |
return prev; |
2 | 188 |
} |
189 |
||
190 |
/** |
|
191 |
* Atomically decrements by one the current value of the field of the |
|
192 |
* given object managed by this updater. |
|
193 |
* |
|
194 |
* @param obj An object whose field to get and set |
|
195 |
* @return the previous value |
|
196 |
*/ |
|
197 |
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
|
198 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
199 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
200 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
201 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
202 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
203 |
return prev; |
2 | 204 |
} |
205 |
||
206 |
/** |
|
207 |
* Atomically adds the given value to the current value of the field of |
|
208 |
* the given object managed by this updater. |
|
209 |
* |
|
210 |
* @param obj An object whose field to get and set |
|
211 |
* @param delta the value to add |
|
212 |
* @return the previous value |
|
213 |
*/ |
|
214 |
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
|
215 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
216 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
217 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
218 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
219 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
220 |
return prev; |
2 | 221 |
} |
222 |
||
223 |
/** |
|
224 |
* Atomically increments by one the current value of the field of the |
|
225 |
* given object managed by this updater. |
|
226 |
* |
|
227 |
* @param obj An object whose field to get and set |
|
228 |
* @return the updated value |
|
229 |
*/ |
|
230 |
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
|
231 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
232 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
233 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
234 |
next = prev + 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
235 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
236 |
return next; |
2 | 237 |
} |
238 |
||
239 |
/** |
|
240 |
* Atomically decrements by one the current value of the field of the |
|
241 |
* given object managed by this updater. |
|
242 |
* |
|
243 |
* @param obj An object whose field to get and set |
|
244 |
* @return the updated value |
|
245 |
*/ |
|
246 |
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
|
247 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
248 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
249 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
250 |
next = prev - 1; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
251 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
252 |
return next; |
2 | 253 |
} |
254 |
||
255 |
/** |
|
256 |
* Atomically adds the given value to the current value of the field of |
|
257 |
* the given object managed by this updater. |
|
258 |
* |
|
259 |
* @param obj An object whose field to get and set |
|
260 |
* @param delta the value to add |
|
261 |
* @return the updated value |
|
262 |
*/ |
|
263 |
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
|
264 |
long prev, next; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
265 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
266 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
267 |
next = prev + delta; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
268 |
} while (!compareAndSet(obj, prev, next)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
269 |
return next; |
2 | 270 |
} |
271 |
||
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
273 |
* 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
|
274 |
* 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
|
275 |
* 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
|
276 |
* 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
|
277 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
* @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
|
279 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
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
|
284 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
prev = get(obj); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
next = updateFunction.operateAsLong(prev); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
290 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
293 |
* 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
|
294 |
* 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
|
295 |
* 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
|
296 |
* 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
|
297 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
298 |
* @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
|
299 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
302 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
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
|
304 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
306 |
prev = get(obj); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
next = updateFunction.operateAsLong(prev); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
312 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
313 |
* 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
|
314 |
* 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
|
315 |
* 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
|
316 |
* 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
|
317 |
* 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
|
318 |
* 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
|
319 |
* 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
|
320 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
321 |
* @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
|
322 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
323 |
* @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
|
324 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
325 |
* @since 1.8 |
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 |
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
|
328 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
329 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
330 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
331 |
prev = get(obj); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
332 |
next = accumulatorFunction.operateAsLong(prev, x); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
333 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
334 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
335 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
336 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
337 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
338 |
* 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
|
339 |
* 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
|
340 |
* 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
|
341 |
* 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
|
342 |
* 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
|
343 |
* 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
|
344 |
* 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
|
345 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
346 |
* @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
|
347 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
348 |
* @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
|
349 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
350 |
* @since 1.8 |
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 |
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
|
353 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
354 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
355 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
356 |
prev = get(obj); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
357 |
next = accumulatorFunction.operateAsLong(prev, x); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
358 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
359 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
360 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
361 |
|
2 | 362 |
private static class CASUpdater<T> extends AtomicLongFieldUpdater<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 |
CASUpdater(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 != long.class) |
397 |
throw new IllegalArgumentException("Must be long 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 |
||
408 |
private void fullCheck(T obj) { |
|
409 |
if (!tclass.isInstance(obj)) |
|
410 |
throw new ClassCastException(); |
|
411 |
if (cclass != null) |
|
412 |
ensureProtectedAccess(obj); |
|
413 |
} |
|
414 |
||
415 |
public boolean compareAndSet(T obj, long expect, long update) { |
|
416 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
417 |
return unsafe.compareAndSwapLong(obj, offset, expect, update); |
|
418 |
} |
|
419 |
||
420 |
public boolean weakCompareAndSet(T obj, long expect, long update) { |
|
421 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
422 |
return unsafe.compareAndSwapLong(obj, offset, expect, update); |
|
423 |
} |
|
424 |
||
425 |
public void set(T obj, long newValue) { |
|
426 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
427 |
unsafe.putLongVolatile(obj, offset, newValue); |
|
428 |
} |
|
429 |
||
430 |
public void lazySet(T obj, long newValue) { |
|
431 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
432 |
unsafe.putOrderedLong(obj, offset, newValue); |
|
433 |
} |
|
434 |
||
435 |
public long get(T obj) { |
|
436 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
437 |
return unsafe.getLongVolatile(obj, offset); |
|
438 |
} |
|
439 |
||
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
440 |
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
|
441 |
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
|
442 |
return unsafe.getAndSetLong(obj, offset, newValue); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
443 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
444 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
445 |
public long getAndIncrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
446 |
return getAndAdd(obj, 1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
447 |
} |
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 |
public long getAndDecrement(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
450 |
return getAndAdd(obj, -1); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
451 |
} |
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 |
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
|
454 |
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
|
455 |
return unsafe.getAndAddLong(obj, offset, delta); |
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 incrementAndGet(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
459 |
return getAndAdd(obj, 1) + 1; |
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 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
462 |
public long decrementAndGet(T obj) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
463 |
return getAndAdd(obj, -1) - 1; |
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 |
|
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
466 |
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
|
467 |
return getAndAdd(obj, delta) + delta; |
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 |
|
2 | 470 |
private void ensureProtectedAccess(T obj) { |
471 |
if (cclass.isInstance(obj)) { |
|
472 |
return; |
|
473 |
} |
|
7518 | 474 |
throw new RuntimeException( |
2 | 475 |
new IllegalAccessException("Class " + |
476 |
cclass.getName() + |
|
477 |
" can not access a protected member of class " + |
|
478 |
tclass.getName() + |
|
479 |
" using an instance of " + |
|
480 |
obj.getClass().getName() |
|
481 |
) |
|
482 |
); |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
||
487 |
private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> { |
|
488 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
489 |
private final long offset; |
|
490 |
private final Class<T> tclass; |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
491 |
private final Class<?> cclass; |
2 | 492 |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
493 |
LockedUpdater(final Class<T> tclass, final String fieldName) { |
2 | 494 |
Field field = null; |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
495 |
Class<?> caller = null; |
2 | 496 |
int modifiers = 0; |
497 |
try { |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
498 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
499 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
500 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
501 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
502 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
503 |
}); |
2 | 504 |
caller = sun.reflect.Reflection.getCallerClass(3); |
505 |
modifiers = field.getModifiers(); |
|
506 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
507 |
caller, tclass, null, modifiers); |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
508 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
509 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
510 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
511 |
((cl == null) || !isAncestor(cl, ccl))) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
512 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
513 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
514 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
515 |
throw new RuntimeException(pae.getException()); |
7518 | 516 |
} catch (Exception ex) { |
2 | 517 |
throw new RuntimeException(ex); |
518 |
} |
|
519 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
520 |
Class<?> fieldt = field.getType(); |
2 | 521 |
if (fieldt != long.class) |
522 |
throw new IllegalArgumentException("Must be long type"); |
|
523 |
||
524 |
if (!Modifier.isVolatile(modifiers)) |
|
525 |
throw new IllegalArgumentException("Must be volatile type"); |
|
526 |
||
527 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
528 |
caller != tclass) ? caller : null; |
|
529 |
this.tclass = tclass; |
|
530 |
offset = unsafe.objectFieldOffset(field); |
|
531 |
} |
|
532 |
||
533 |
private void fullCheck(T obj) { |
|
534 |
if (!tclass.isInstance(obj)) |
|
535 |
throw new ClassCastException(); |
|
536 |
if (cclass != null) |
|
537 |
ensureProtectedAccess(obj); |
|
538 |
} |
|
539 |
||
540 |
public boolean compareAndSet(T obj, long expect, long update) { |
|
541 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 542 |
synchronized (this) { |
2 | 543 |
long v = unsafe.getLong(obj, offset); |
544 |
if (v != expect) |
|
545 |
return false; |
|
546 |
unsafe.putLong(obj, offset, update); |
|
547 |
return true; |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
public boolean weakCompareAndSet(T obj, long expect, long update) { |
|
552 |
return compareAndSet(obj, expect, update); |
|
553 |
} |
|
554 |
||
555 |
public void set(T obj, long newValue) { |
|
556 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 557 |
synchronized (this) { |
2 | 558 |
unsafe.putLong(obj, offset, newValue); |
559 |
} |
|
560 |
} |
|
561 |
||
562 |
public void lazySet(T obj, long newValue) { |
|
563 |
set(obj, newValue); |
|
564 |
} |
|
565 |
||
566 |
public long get(T obj) { |
|
567 |
if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj); |
|
7518 | 568 |
synchronized (this) { |
2 | 569 |
return unsafe.getLong(obj, offset); |
570 |
} |
|
571 |
} |
|
572 |
||
573 |
private void ensureProtectedAccess(T obj) { |
|
574 |
if (cclass.isInstance(obj)) { |
|
575 |
return; |
|
576 |
} |
|
7518 | 577 |
throw new RuntimeException( |
2 | 578 |
new IllegalAccessException("Class " + |
579 |
cclass.getName() + |
|
580 |
" can not access a protected member of class " + |
|
581 |
tclass.getName() + |
|
582 |
" using an instance of " + |
|
583 |
obj.getClass().getName() |
|
584 |
) |
|
585 |
); |
|
586 |
} |
|
587 |
} |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
588 |
|
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
589 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
590 |
* 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
|
591 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
592 |
* Equivalent to the inaccessible: first.isAncestor(second). |
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 |
private static boolean isAncestor(ClassLoader first, ClassLoader second) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
595 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
596 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
597 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
598 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
599 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
600 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
601 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
602 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
603 |
} |
2 | 604 |
} |