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