2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;
9 import java.lang.reflect.*;
12 * A reflection-based utility that enables atomic updates to
13 * designated <tt>volatile long</tt> fields of designated classes.
14 * This class is designed for use in atomic data structures in which
15 * several fields of the same node are independently subject to atomic
18 * <p>Note that the guarantees of the {@code compareAndSet}
19 * method in this class are weaker than in other atomic classes.
20 * Because this class cannot ensure that all uses of the field
21 * are appropriate for purposes of atomic access, it can
22 * guarantee atomicity only with respect to other invocations of
23 * {@code compareAndSet} and {@code set} on the same updater.
27 * @param <T> The type of the object holding the updatable field
29 public abstract class AtomicLongFieldUpdater<T> {
31 * Creates and returns an updater for objects with the given field.
32 * The Class argument is needed to check that reflective types and
33 * generic types match.
35 * @param tclass the class of the objects holding the field
36 * @param fieldName the name of the field to be updated.
38 * @throws IllegalArgumentException if the field is not a
40 * @throws RuntimeException with a nested reflection-based
41 * exception if the class does not hold field or is the wrong type.
43 public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
44 if (AtomicLong.VM_SUPPORTS_LONG_CAS)
45 return new CASUpdater<U>(tclass, fieldName);
47 return new LockedUpdater<U>(tclass, fieldName);
51 * Protected do-nothing constructor for use by subclasses.
53 protected AtomicLongFieldUpdater() {
57 * Atomically sets the field of the given object managed by this updater
58 * to the given updated value if the current value <tt>==</tt> the
59 * expected value. This method is guaranteed to be atomic with respect to
60 * other calls to <tt>compareAndSet</tt> and <tt>set</tt>, but not
61 * necessarily with respect to other changes in the field.
63 * @param obj An object whose field to conditionally set
64 * @param expect the expected value
65 * @param update the new value
66 * @return true if successful.
67 * @throws ClassCastException if <tt>obj</tt> is not an instance
68 * of the class possessing the field established in the constructor.
70 public abstract boolean compareAndSet(T obj, long expect, long update);
73 * Atomically sets the field of the given object managed by this updater
74 * to the given updated value if the current value <tt>==</tt> the
75 * expected value. This method is guaranteed to be atomic with respect to
76 * other calls to <tt>compareAndSet</tt> and <tt>set</tt>, but not
77 * necessarily with respect to other changes in the field.
78 * May fail spuriously and does not provide ordering guarantees,
79 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
81 * @param obj An object whose field to conditionally set
82 * @param expect the expected value
83 * @param update the new value
84 * @return true if successful.
85 * @throws ClassCastException if <tt>obj</tt> is not an instance
86 * of the class possessing the field established in the constructor.
88 public abstract boolean weakCompareAndSet(T obj, long expect, long update);
91 * Sets the field of the given object managed by this updater to the
92 * given updated value. This operation is guaranteed to act as a volatile
93 * store with respect to subsequent invocations of
94 * <tt>compareAndSet</tt>.
96 * @param obj An object whose field to set
97 * @param newValue the new value
99 public abstract void set(T obj, long newValue);
102 * Eventually sets the field of the given object managed by this
103 * updater to the given updated value.
105 * @param obj An object whose field to set
106 * @param newValue the new value
109 public abstract void lazySet(T obj, long newValue);
112 * Gets the current value held in the field of the given object managed
115 * @param obj An object whose field to get
116 * @return the current value
118 public abstract long get(T obj);
121 * Atomically sets the field of the given object managed by this updater
122 * to the given value and returns the old value.
124 * @param obj An object whose field to get and set
125 * @param newValue the new value
126 * @return the previous value
128 public long getAndSet(T obj, long newValue) {
130 long current = get(obj);
131 if (compareAndSet(obj, current, newValue))
137 * Atomically increments by one the current value of the field of the
138 * given object managed by this updater.
140 * @param obj An object whose field to get and set
141 * @return the previous value
143 public long getAndIncrement(T obj) {
145 long current = get(obj);
146 long next = current + 1;
147 if (compareAndSet(obj, current, next))
153 * Atomically decrements by one the current value of the field of the
154 * given object managed by this updater.
156 * @param obj An object whose field to get and set
157 * @return the previous value
159 public long getAndDecrement(T obj) {
161 long current = get(obj);
162 long next = current - 1;
163 if (compareAndSet(obj, current, next))
169 * Atomically adds the given value to the current value of the field of
170 * the given object managed by this updater.
172 * @param obj An object whose field to get and set
173 * @param delta the value to add
174 * @return the previous value
176 public long getAndAdd(T obj, long delta) {
178 long current = get(obj);
179 long next = current + delta;
180 if (compareAndSet(obj, current, next))
186 * Atomically increments by one the current value of the field of the
187 * given object managed by this updater.
189 * @param obj An object whose field to get and set
190 * @return the updated value
192 public long incrementAndGet(T obj) {
194 long current = get(obj);
195 long next = current + 1;
196 if (compareAndSet(obj, current, next))
202 * Atomically decrements by one the current value of the field of the
203 * given object managed by this updater.
205 * @param obj An object whose field to get and set
206 * @return the updated value
208 public long decrementAndGet(T obj) {
210 long current = get(obj);
211 long next = current - 1;
212 if (compareAndSet(obj, current, next))
218 * Atomically adds the given value to the current value of the field of
219 * the given object managed by this updater.
221 * @param obj An object whose field to get and set
222 * @param delta the value to add
223 * @return the updated value
225 public long addAndGet(T obj, long delta) {
227 long current = get(obj);
228 long next = current + delta;
229 if (compareAndSet(obj, current, next))
234 private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
235 private static final Unsafe unsafe = Unsafe.getUnsafe();
236 private final long offset;
237 private final Class<T> tclass;
238 private final Class cclass;
240 CASUpdater(Class<T> tclass, String fieldName) {
245 field = tclass.getDeclaredField(fieldName);
246 caller = sun.reflect.Reflection.getCallerClass(3);
247 modifiers = field.getModifiers();
248 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
249 caller, tclass, null, modifiers);
250 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
251 } catch(Exception ex) {
252 throw new RuntimeException(ex);
255 Class fieldt = field.getType();
256 if (fieldt != long.class)
257 throw new IllegalArgumentException("Must be long type");
259 if (!Modifier.isVolatile(modifiers))
260 throw new IllegalArgumentException("Must be volatile type");
262 this.cclass = (Modifier.isProtected(modifiers) &&
263 caller != tclass) ? caller : null;
264 this.tclass = tclass;
265 offset = unsafe.objectFieldOffset(field);
268 private void fullCheck(T obj) {
269 if (!tclass.isInstance(obj))
270 throw new ClassCastException();
272 ensureProtectedAccess(obj);
275 public boolean compareAndSet(T obj, long expect, long update) {
276 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
277 return unsafe.compareAndSwapLong(obj, offset, expect, update);
280 public boolean weakCompareAndSet(T obj, long expect, long update) {
281 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
282 return unsafe.compareAndSwapLong(obj, offset, expect, update);
285 public void set(T obj, long newValue) {
286 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
287 unsafe.putLongVolatile(obj, offset, newValue);
290 public void lazySet(T obj, long newValue) {
291 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
292 unsafe.putOrderedLong(obj, offset, newValue);
295 public long get(T obj) {
296 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
297 return unsafe.getLongVolatile(obj, offset);
300 private void ensureProtectedAccess(T obj) {
301 if (cclass.isInstance(obj)) {
304 throw new RuntimeException (
305 new IllegalAccessException("Class " +
307 " can not access a protected member of class " +
309 " using an instance of " +
310 obj.getClass().getName()
317 private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
318 private static final Unsafe unsafe = Unsafe.getUnsafe();
319 private final long offset;
320 private final Class<T> tclass;
321 private final Class cclass;
323 LockedUpdater(Class<T> tclass, String fieldName) {
328 field = tclass.getDeclaredField(fieldName);
329 caller = sun.reflect.Reflection.getCallerClass(3);
330 modifiers = field.getModifiers();
331 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
332 caller, tclass, null, modifiers);
333 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
334 } catch(Exception ex) {
335 throw new RuntimeException(ex);
338 Class fieldt = field.getType();
339 if (fieldt != long.class)
340 throw new IllegalArgumentException("Must be long type");
342 if (!Modifier.isVolatile(modifiers))
343 throw new IllegalArgumentException("Must be volatile type");
345 this.cclass = (Modifier.isProtected(modifiers) &&
346 caller != tclass) ? caller : null;
347 this.tclass = tclass;
348 offset = unsafe.objectFieldOffset(field);
351 private void fullCheck(T obj) {
352 if (!tclass.isInstance(obj))
353 throw new ClassCastException();
355 ensureProtectedAccess(obj);
358 public boolean compareAndSet(T obj, long expect, long update) {
359 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
361 long v = unsafe.getLong(obj, offset);
364 unsafe.putLong(obj, offset, update);
369 public boolean weakCompareAndSet(T obj, long expect, long update) {
370 return compareAndSet(obj, expect, update);
373 public void set(T obj, long newValue) {
374 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
376 unsafe.putLong(obj, offset, newValue);
380 public void lazySet(T obj, long newValue) {
384 public long get(T obj) {
385 if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
387 return unsafe.getLong(obj, offset);
391 private void ensureProtectedAccess(T obj) {
392 if (cclass.isInstance(obj)) {
395 throw new RuntimeException (
396 new IllegalAccessException("Class " +
398 " can not access a protected member of class " +
400 " using an instance of " +
401 obj.getClass().getName()