src/java.base/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


 267      * and the given update as the second argument.
 268      *
 269      * @param obj An object whose field to get and set
 270      * @param x the update value
 271      * @param accumulatorFunction a side-effect-free function of two arguments
 272      * @return the updated value
 273      * @since 1.8
 274      */
 275     public final V accumulateAndGet(T obj, V x,
 276                                     BinaryOperator<V> accumulatorFunction) {
 277         V prev, next;
 278         do {
 279             prev = get(obj);
 280             next = accumulatorFunction.apply(prev, x);
 281         } while (!compareAndSet(obj, prev, next));
 282         return next;
 283     }
 284 
 285     private static final class AtomicReferenceFieldUpdaterImpl<T,V>
 286         extends AtomicReferenceFieldUpdater<T,V> {
 287         private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 288         private final long offset;
 289         private final Class<T> tclass;
 290         private final Class<V> vclass;
 291         private final Class<?> cclass;
 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 
 305         AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
 306                                         final Class<V> vclass,
 307                                         final String fieldName,




 267      * and the given update as the second argument.
 268      *
 269      * @param obj An object whose field to get and set
 270      * @param x the update value
 271      * @param accumulatorFunction a side-effect-free function of two arguments
 272      * @return the updated value
 273      * @since 1.8
 274      */
 275     public final V accumulateAndGet(T obj, V x,
 276                                     BinaryOperator<V> accumulatorFunction) {
 277         V prev, next;
 278         do {
 279             prev = get(obj);
 280             next = accumulatorFunction.apply(prev, x);
 281         } while (!compareAndSet(obj, prev, next));
 282         return next;
 283     }
 284 
 285     private static final class AtomicReferenceFieldUpdaterImpl<T,V>
 286         extends AtomicReferenceFieldUpdater<T,V> {
 287         private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 288         private final long offset;
 289         private final Class<T> tclass;
 290         private final Class<V> vclass;
 291         private final Class<?> cclass;
 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 
 305         AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
 306                                         final Class<V> vclass,
 307                                         final String fieldName,