001: /*
002: * Written by Doug Lea with assistance from members of JCP JSR-166
003: * Expert Group and released to the public domain, as explained at
004: * http://creativecommons.org/licenses/publicdomain
005: */
006:
007: package java.util.concurrent.atomic;
008:
009: import sun.misc.Unsafe;
010: import java.lang.reflect.*;
011:
012: /**
013: * A reflection-based utility that enables atomic updates to
014: * designated <tt>volatile int</tt> fields of designated classes.
015: * This class is designed for use in atomic data structures in which
016: * several fields of the same node are independently subject to atomic
017: * updates.
018: *
019: * <p> Note that the guarantees of the <tt>compareAndSet</tt> method
020: * in this class are weaker than in other atomic classes. Because this
021: * class cannot ensure that all uses of the field are appropriate for
022: * purposes of atomic access, it can guarantee atomicity and volatile
023: * semantics only with respect to other invocations of
024: * <tt>compareAndSet</tt> and <tt>set</tt>.
025: * @since 1.5
026: * @author Doug Lea
027: * @param <T> The type of the object holding the updatable field
028: */
029: public abstract class AtomicIntegerFieldUpdater<T> {
030: /**
031: * Creates an updater for objects with the given field. The Class
032: * argument is needed to check that reflective types and generic
033: * types match.
034: * @param tclass the class of the objects holding the field
035: * @param fieldName the name of the field to be updated.
036: * @return the updater
037: * @throws IllegalArgumentException if the field is not a
038: * volatile integer type.
039: * @throws RuntimeException with a nested reflection-based
040: * exception if the class does not hold field or is the wrong type.
041: */
042: public static <U> AtomicIntegerFieldUpdater<U> newUpdater(
043: Class<U> tclass, String fieldName) {
044: return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName);
045: }
046:
047: /**
048: * Protected do-nothing constructor for use by subclasses.
049: */
050: protected AtomicIntegerFieldUpdater() {
051: }
052:
053: /**
054: * Atomically set the value of the field of the given object managed
055: * by this Updater to the given updated value if the current value
056: * <tt>==</tt> the expected value. This method is guaranteed to be
057: * atomic with respect to other calls to <tt>compareAndSet</tt> and
058: * <tt>set</tt>, but not necessarily with respect to other
059: * changes in the field.
060: * @param obj An object whose field to conditionally set
061: * @param expect the expected value
062: * @param update the new value
063: * @return true if successful.
064: * @throws ClassCastException if <tt>obj</tt> is not an instance
065: * of the class possessing the field established in the constructor.
066: */
067:
068: public abstract boolean compareAndSet(T obj, int expect, int update);
069:
070: /**
071: * Atomically set the value of the field of the given object managed
072: * by this Updater to the given updated value if the current value
073: * <tt>==</tt> the expected value. This method is guaranteed to be
074: * atomic with respect to other calls to <tt>compareAndSet</tt> and
075: * <tt>set</tt>, but not necessarily with respect to other
076: * changes in the field, and may fail spuriously.
077: * @param obj An object whose field to conditionally set
078: * @param expect the expected value
079: * @param update the new value
080: * @return true if successful.
081: * @throws ClassCastException if <tt>obj</tt> is not an instance
082: * of the class possessing the field established in the constructor.
083: */
084:
085: public abstract boolean weakCompareAndSet(T obj, int expect,
086: int update);
087:
088: /**
089: * Set the field of the given object managed by this updater. This
090: * operation is guaranteed to act as a volatile store with respect
091: * to subsequent invocations of <tt>compareAndSet</tt>.
092: * @param obj An object whose field to set
093: * @param newValue the new value
094: */
095: public abstract void set(T obj, int newValue);
096:
097: /**
098: * Get the current value held in the field by the given object.
099: * @param obj An object whose field to get
100: * @return the current value
101: */
102: public abstract int get(T obj);
103:
104: /**
105: * Set to the given value and return the old value.
106: *
107: * @param obj An object whose field to get and set
108: * @param newValue the new value
109: * @return the previous value
110: */
111: public int getAndSet(T obj, int newValue) {
112: for (;;) {
113: int current = get(obj);
114: if (compareAndSet(obj, current, newValue))
115: return current;
116: }
117: }
118:
119: /**
120: * Atomically increment by one the current value.
121: * @param obj An object whose field to get and set
122: * @return the previous value;
123: */
124: public int getAndIncrement(T obj) {
125: for (;;) {
126: int current = get(obj);
127: int next = current + 1;
128: if (compareAndSet(obj, current, next))
129: return current;
130: }
131: }
132:
133: /**
134: * Atomically decrement by one the current value.
135: * @param obj An object whose field to get and set
136: * @return the previous value;
137: */
138: public int getAndDecrement(T obj) {
139: for (;;) {
140: int current = get(obj);
141: int next = current - 1;
142: if (compareAndSet(obj, current, next))
143: return current;
144: }
145: }
146:
147: /**
148: * Atomically add the given value to current value.
149: * @param obj An object whose field to get and set
150: * @param delta the value to add
151: * @return the previous value;
152: */
153: public int getAndAdd(T obj, int delta) {
154: for (;;) {
155: int current = get(obj);
156: int next = current + delta;
157: if (compareAndSet(obj, current, next))
158: return current;
159: }
160: }
161:
162: /**
163: * Atomically increment by one the current value.
164: * @param obj An object whose field to get and set
165: * @return the updated value;
166: */
167: public int incrementAndGet(T obj) {
168: for (;;) {
169: int current = get(obj);
170: int next = current + 1;
171: if (compareAndSet(obj, current, next))
172: return next;
173: }
174: }
175:
176: /**
177: * Atomically decrement by one the current value.
178: * @param obj An object whose field to get and set
179: * @return the updated value;
180: */
181: public int decrementAndGet(T obj) {
182: for (;;) {
183: int current = get(obj);
184: int next = current - 1;
185: if (compareAndSet(obj, current, next))
186: return next;
187: }
188: }
189:
190: /**
191: * Atomically add the given value to current value.
192: * @param obj An object whose field to get and set
193: * @param delta the value to add
194: * @return the updated value;
195: */
196: public int addAndGet(T obj, int delta) {
197: for (;;) {
198: int current = get(obj);
199: int next = current + delta;
200: if (compareAndSet(obj, current, next))
201: return next;
202: }
203: }
204:
205: /**
206: * Standard hotspot implementation using intrinsics
207: */
208: private static class AtomicIntegerFieldUpdaterImpl<T> extends
209: AtomicIntegerFieldUpdater<T> {
210: private static final Unsafe unsafe = Unsafe.getUnsafe();
211: private final long offset;
212: private final Class<T> tclass;
213:
214: AtomicIntegerFieldUpdaterImpl(Class<T> tclass, String fieldName) {
215: Field field = null;
216: try {
217: field = tclass.getDeclaredField(fieldName);
218: } catch (Exception ex) {
219: throw new RuntimeException(ex);
220: }
221:
222: Class fieldt = field.getType();
223: if (fieldt != int.class)
224: throw new IllegalArgumentException(
225: "Must be integer type");
226:
227: if (!Modifier.isVolatile(field.getModifiers()))
228: throw new IllegalArgumentException(
229: "Must be volatile type");
230:
231: this .tclass = tclass;
232: offset = unsafe.objectFieldOffset(field);
233: }
234:
235: public boolean compareAndSet(T obj, int expect, int update) {
236: if (!tclass.isInstance(obj))
237: throw new ClassCastException();
238: return unsafe
239: .compareAndSwapInt(obj, offset, expect, update);
240: }
241:
242: public boolean weakCompareAndSet(T obj, int expect, int update) {
243: if (!tclass.isInstance(obj))
244: throw new ClassCastException();
245: return unsafe
246: .compareAndSwapInt(obj, offset, expect, update);
247: }
248:
249: public void set(T obj, int newValue) {
250: if (!tclass.isInstance(obj))
251: throw new ClassCastException();
252: unsafe.putIntVolatile(obj, offset, newValue);
253: }
254:
255: public final int get(T obj) {
256: if (!tclass.isInstance(obj))
257: throw new ClassCastException();
258: return unsafe.getIntVolatile(obj, offset);
259: }
260: }
261: }
|