author | mchung |
Tue, 16 Apr 2013 21:39:52 -0700 | |
changeset 16906 | 44dfee24cb71 |
parent 16011 | 890a7ed97f6c |
child 18576 | 7a5c231327af |
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.UnaryOperator; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
38 |
import java.util.function.BinaryOperator; |
2 | 39 |
import sun.misc.Unsafe; |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
40 |
import sun.reflect.CallerSensitive; |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
41 |
import sun.reflect.Reflection; |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
42 |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
43 |
import java.lang.reflect.Field; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
44 |
import java.lang.reflect.Modifier; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
45 |
import java.security.AccessController; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
46 |
import java.security.PrivilegedExceptionAction; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
47 |
import java.security.PrivilegedActionException; |
2 | 48 |
|
49 |
/** |
|
50 |
* A reflection-based utility that enables atomic updates to |
|
51 |
* designated {@code volatile} reference fields of designated |
|
52 |
* classes. This class is designed for use in atomic data structures |
|
53 |
* in which several reference fields of the same node are |
|
54 |
* independently subject to atomic updates. For example, a tree node |
|
55 |
* might be declared as |
|
56 |
* |
|
7518 | 57 |
* <pre> {@code |
2 | 58 |
* class Node { |
59 |
* private volatile Node left, right; |
|
60 |
* |
|
7518 | 61 |
* private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater = |
2 | 62 |
* AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left"); |
7518 | 63 |
* private static AtomicReferenceFieldUpdater<Node, Node> rightUpdater = |
2 | 64 |
* AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right"); |
65 |
* |
|
66 |
* Node getLeft() { return left; } |
|
67 |
* boolean compareAndSetLeft(Node expect, Node update) { |
|
68 |
* return leftUpdater.compareAndSet(this, expect, update); |
|
69 |
* } |
|
70 |
* // ... and so on |
|
7518 | 71 |
* }}</pre> |
2 | 72 |
* |
73 |
* <p>Note that the guarantees of the {@code compareAndSet} |
|
74 |
* method in this class are weaker than in other atomic classes. |
|
75 |
* Because this class cannot ensure that all uses of the field |
|
76 |
* are appropriate for purposes of atomic access, it can |
|
77 |
* guarantee atomicity only with respect to other invocations of |
|
78 |
* {@code compareAndSet} and {@code set} on the same updater. |
|
79 |
* |
|
80 |
* @since 1.5 |
|
81 |
* @author Doug Lea |
|
82 |
* @param <T> The type of the object holding the updatable field |
|
83 |
* @param <V> The type of the field |
|
84 |
*/ |
|
7518 | 85 |
public abstract class AtomicReferenceFieldUpdater<T, V> { |
2 | 86 |
|
87 |
/** |
|
88 |
* Creates and returns an updater for objects with the given field. |
|
89 |
* The Class arguments are needed to check that reflective types and |
|
90 |
* generic types match. |
|
91 |
* |
|
92 |
* @param tclass the class of the objects holding the field. |
|
93 |
* @param vclass the class of the field |
|
94 |
* @param fieldName the name of the field to be updated. |
|
95 |
* @return the updater |
|
96 |
* @throws IllegalArgumentException if the field is not a volatile reference type. |
|
97 |
* @throws RuntimeException with a nested reflection-based |
|
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
98 |
* 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
|
99 |
* 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
|
100 |
* access control |
2 | 101 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
102 |
@CallerSensitive |
2 | 103 |
public static <U, W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass, Class<W> vclass, String fieldName) { |
104 |
return new AtomicReferenceFieldUpdaterImpl<U,W>(tclass, |
|
105 |
vclass, |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
106 |
fieldName, |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
107 |
Reflection.getCallerClass()); |
2 | 108 |
} |
109 |
||
110 |
/** |
|
111 |
* Protected do-nothing constructor for use by subclasses. |
|
112 |
*/ |
|
113 |
protected AtomicReferenceFieldUpdater() { |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Atomically sets the field of the given object managed by this updater |
|
118 |
* to the given updated value if the current value {@code ==} the |
|
119 |
* expected value. This method is guaranteed to be atomic with respect to |
|
120 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
121 |
* necessarily with respect to other changes in the field. |
|
122 |
* |
|
123 |
* @param obj An object whose field to conditionally set |
|
124 |
* @param expect the expected value |
|
125 |
* @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
|
126 |
* @return true if successful |
2 | 127 |
*/ |
128 |
public abstract boolean compareAndSet(T obj, V expect, V update); |
|
129 |
||
130 |
/** |
|
131 |
* Atomically sets the field of the given object managed by this updater |
|
132 |
* to the given updated value if the current value {@code ==} the |
|
133 |
* expected value. This method is guaranteed to be atomic with respect to |
|
134 |
* other calls to {@code compareAndSet} and {@code set}, but not |
|
135 |
* necessarily with respect to other changes in the field. |
|
136 |
* |
|
137 |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
|
138 |
* and does not provide ordering guarantees, so is only rarely an |
|
139 |
* appropriate alternative to {@code compareAndSet}. |
|
140 |
* |
|
141 |
* @param obj An object whose field to conditionally set |
|
142 |
* @param expect the expected value |
|
143 |
* @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
|
144 |
* @return true if successful |
2 | 145 |
*/ |
146 |
public abstract boolean weakCompareAndSet(T obj, V expect, V update); |
|
147 |
||
148 |
/** |
|
149 |
* Sets the field of the given object managed by this updater to the |
|
150 |
* given updated value. This operation is guaranteed to act as a volatile |
|
151 |
* store with respect to subsequent invocations of {@code compareAndSet}. |
|
152 |
* |
|
153 |
* @param obj An object whose field to set |
|
154 |
* @param newValue the new value |
|
155 |
*/ |
|
156 |
public abstract void set(T obj, V newValue); |
|
157 |
||
158 |
/** |
|
159 |
* Eventually sets the field of the given object managed by this |
|
160 |
* updater to the given updated value. |
|
161 |
* |
|
162 |
* @param obj An object whose field to set |
|
163 |
* @param newValue the new value |
|
164 |
* @since 1.6 |
|
165 |
*/ |
|
166 |
public abstract void lazySet(T obj, V newValue); |
|
167 |
||
168 |
/** |
|
169 |
* Gets the current value held in the field of the given object managed |
|
170 |
* by this updater. |
|
171 |
* |
|
172 |
* @param obj An object whose field to get |
|
173 |
* @return the current value |
|
174 |
*/ |
|
175 |
public abstract V get(T obj); |
|
176 |
||
177 |
/** |
|
178 |
* Atomically sets the field of the given object managed by this updater |
|
179 |
* to the given value and returns the old value. |
|
180 |
* |
|
181 |
* @param obj An object whose field to get and set |
|
182 |
* @param newValue the new value |
|
183 |
* @return the previous value |
|
184 |
*/ |
|
185 |
public V getAndSet(T obj, V newValue) { |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
186 |
V prev; |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
187 |
do { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
188 |
prev = get(obj); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
189 |
} while (!compareAndSet(obj, prev, newValue)); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
190 |
return prev; |
2 | 191 |
} |
192 |
||
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
193 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
194 |
* 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
|
195 |
* 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
|
196 |
* 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
|
197 |
* 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
|
198 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
199 |
* @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
|
200 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
201 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
202 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
203 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
204 |
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
205 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
206 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
207 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
208 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
209 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
210 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
211 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
212 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
213 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
214 |
* 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
|
215 |
* 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
|
216 |
* 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
|
217 |
* 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
|
218 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
219 |
* @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
|
220 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
221 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
222 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
223 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
224 |
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
225 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
226 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
227 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
228 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
229 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
234 |
* 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
|
235 |
* 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
|
236 |
* 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
|
237 |
* 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
|
238 |
* 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
|
239 |
* 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
|
240 |
* 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
|
241 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
242 |
* @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
|
243 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
244 |
* @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
|
245 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
246 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
247 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
248 |
public final V getAndAccumulate(T obj, V x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
249 |
BinaryOperator<V> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
253 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
256 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
257 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
258 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
259 |
* 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
|
260 |
* 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
|
261 |
* 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
|
262 |
* 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
|
263 |
* 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
|
264 |
* 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
|
265 |
* 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
|
266 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
267 |
* @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
|
268 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
269 |
* @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
|
270 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
271 |
* @since 1.8 |
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 |
public final V accumulateAndGet(T obj, V x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
BinaryOperator<V> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
V prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
prev = get(obj); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
278 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
} while (!compareAndSet(obj, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
|
2 | 283 |
private static final class AtomicReferenceFieldUpdaterImpl<T,V> |
284 |
extends AtomicReferenceFieldUpdater<T,V> { |
|
285 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
286 |
private final long offset; |
|
287 |
private final Class<T> tclass; |
|
288 |
private final Class<V> vclass; |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
289 |
private final Class<?> cclass; |
2 | 290 |
|
291 |
/* |
|
292 |
* Internal type checks within all update methods contain |
|
293 |
* internal inlined optimizations checking for the common |
|
294 |
* cases where the class is final (in which case a simple |
|
295 |
* getClass comparison suffices) or is of type Object (in |
|
296 |
* which case no check is needed because all objects are |
|
297 |
* instances of Object). The Object case is handled simply by |
|
298 |
* setting vclass to null in constructor. The targetCheck and |
|
299 |
* updateCheck methods are invoked when these faster |
|
300 |
* screenings fail. |
|
301 |
*/ |
|
302 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
303 |
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, |
2 | 304 |
Class<V> vclass, |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
305 |
final String fieldName, |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
306 |
final Class<?> caller) |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16011
diff
changeset
|
307 |
{ |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
308 |
final Field field; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
309 |
final Class<?> fieldClass; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
310 |
final int modifiers; |
2 | 311 |
try { |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
312 |
field = AccessController.doPrivileged( |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
313 |
new PrivilegedExceptionAction<Field>() { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
314 |
public Field run() throws NoSuchFieldException { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
315 |
return tclass.getDeclaredField(fieldName); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
316 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
317 |
}); |
2 | 318 |
modifiers = field.getModifiers(); |
319 |
sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
320 |
caller, tclass, null, modifiers); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
321 |
ClassLoader cl = tclass.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
322 |
ClassLoader ccl = caller.getClassLoader(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
323 |
if ((ccl != null) && (ccl != cl) && |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
324 |
((cl == null) || !isAncestor(cl, ccl))) { |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
325 |
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
326 |
} |
2 | 327 |
fieldClass = field.getType(); |
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
328 |
} catch (PrivilegedActionException pae) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
329 |
throw new RuntimeException(pae.getException()); |
2 | 330 |
} catch (Exception ex) { |
331 |
throw new RuntimeException(ex); |
|
332 |
} |
|
333 |
||
334 |
if (vclass != fieldClass) |
|
335 |
throw new ClassCastException(); |
|
336 |
||
337 |
if (!Modifier.isVolatile(modifiers)) |
|
338 |
throw new IllegalArgumentException("Must be volatile type"); |
|
339 |
||
340 |
this.cclass = (Modifier.isProtected(modifiers) && |
|
341 |
caller != tclass) ? caller : null; |
|
342 |
this.tclass = tclass; |
|
343 |
if (vclass == Object.class) |
|
344 |
this.vclass = null; |
|
345 |
else |
|
346 |
this.vclass = vclass; |
|
347 |
offset = unsafe.objectFieldOffset(field); |
|
348 |
} |
|
349 |
||
12674
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
350 |
/** |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
351 |
* 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
|
352 |
* classloader's delegation chain. |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
353 |
* Equivalent to the inaccessible: first.isAncestor(second). |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
354 |
*/ |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
355 |
private static boolean isAncestor(ClassLoader first, ClassLoader second) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
356 |
ClassLoader acl = first; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
357 |
do { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
358 |
acl = acl.getParent(); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
359 |
if (second == acl) { |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
360 |
return true; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
361 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
362 |
} while (acl != null); |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
363 |
return false; |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
364 |
} |
5f9829b7fc7e
7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents:
11134
diff
changeset
|
365 |
|
2 | 366 |
void targetCheck(T obj) { |
367 |
if (!tclass.isInstance(obj)) |
|
368 |
throw new ClassCastException(); |
|
369 |
if (cclass != null) |
|
370 |
ensureProtectedAccess(obj); |
|
371 |
} |
|
372 |
||
373 |
void updateCheck(T obj, V update) { |
|
374 |
if (!tclass.isInstance(obj) || |
|
375 |
(update != null && vclass != null && !vclass.isInstance(update))) |
|
376 |
throw new ClassCastException(); |
|
377 |
if (cclass != null) |
|
378 |
ensureProtectedAccess(obj); |
|
379 |
} |
|
380 |
||
381 |
public boolean compareAndSet(T obj, V expect, V update) { |
|
382 |
if (obj == null || obj.getClass() != tclass || cclass != null || |
|
383 |
(update != null && vclass != null && |
|
384 |
vclass != update.getClass())) |
|
385 |
updateCheck(obj, update); |
|
386 |
return unsafe.compareAndSwapObject(obj, offset, expect, update); |
|
387 |
} |
|
388 |
||
389 |
public boolean weakCompareAndSet(T obj, V expect, V update) { |
|
390 |
// same implementation as strong form for now |
|
391 |
if (obj == null || obj.getClass() != tclass || cclass != null || |
|
392 |
(update != null && vclass != null && |
|
393 |
vclass != update.getClass())) |
|
394 |
updateCheck(obj, update); |
|
395 |
return unsafe.compareAndSwapObject(obj, offset, expect, update); |
|
396 |
} |
|
397 |
||
398 |
public void set(T obj, V newValue) { |
|
399 |
if (obj == null || obj.getClass() != tclass || cclass != null || |
|
400 |
(newValue != null && vclass != null && |
|
401 |
vclass != newValue.getClass())) |
|
402 |
updateCheck(obj, newValue); |
|
403 |
unsafe.putObjectVolatile(obj, offset, newValue); |
|
404 |
} |
|
405 |
||
406 |
public void lazySet(T obj, V newValue) { |
|
407 |
if (obj == null || obj.getClass() != tclass || cclass != null || |
|
408 |
(newValue != null && vclass != null && |
|
409 |
vclass != newValue.getClass())) |
|
410 |
updateCheck(obj, newValue); |
|
411 |
unsafe.putOrderedObject(obj, offset, newValue); |
|
412 |
} |
|
413 |
||
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
414 |
@SuppressWarnings("unchecked") |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
415 |
public V get(T obj) { |
2 | 416 |
if (obj == null || obj.getClass() != tclass || cclass != null) |
417 |
targetCheck(obj); |
|
418 |
return (V)unsafe.getObjectVolatile(obj, offset); |
|
419 |
} |
|
420 |
||
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
421 |
@SuppressWarnings("unchecked") |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
422 |
public V getAndSet(T obj, V newValue) { |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
423 |
if (obj == null || obj.getClass() != tclass || cclass != null || |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
424 |
(newValue != null && vclass != null && |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
425 |
vclass != newValue.getClass())) |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
426 |
updateCheck(obj, newValue); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
427 |
return (V)unsafe.getAndSetObject(obj, offset, newValue); |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
428 |
} |
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
429 |
|
2 | 430 |
private void ensureProtectedAccess(T obj) { |
431 |
if (cclass.isInstance(obj)) { |
|
432 |
return; |
|
433 |
} |
|
7518 | 434 |
throw new RuntimeException( |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
435 |
new IllegalAccessException("Class " + |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
436 |
cclass.getName() + |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
437 |
" can not access a protected member of class " + |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
438 |
tclass.getName() + |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
439 |
" using an instance of " + |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
440 |
obj.getClass().getName() |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
441 |
) |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
12674
diff
changeset
|
442 |
); |
2 | 443 |
} |
444 |
} |
|
445 |
} |