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

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


 349      * function is applied with the current value as its first argument,
 350      * and the given update as the second argument.
 351      *
 352      * @param obj An object whose field to get and set
 353      * @param x the update value
 354      * @param accumulatorFunction a side-effect-free function of two arguments
 355      * @return the updated value
 356      * @since 1.8
 357      */
 358     public final long accumulateAndGet(T obj, long x,
 359                                        LongBinaryOperator accumulatorFunction) {
 360         long prev, next;
 361         do {
 362             prev = get(obj);
 363             next = accumulatorFunction.applyAsLong(prev, x);
 364         } while (!compareAndSet(obj, prev, next));
 365         return next;
 366     }
 367 
 368     private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
 369         private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 370         private final long offset;
 371         private final Class<T> tclass;
 372         private final Class<?> cclass;
 373 
 374         CASUpdater(final Class<T> tclass, final String fieldName,
 375                    final Class<?> caller) {
 376             final Field field;
 377             final int modifiers;
 378             try {
 379                 field = AccessController.doPrivileged(
 380                     new PrivilegedExceptionAction<Field>() {
 381                         public Field run() throws NoSuchFieldException {
 382                             return tclass.getDeclaredField(fieldName);
 383                         }
 384                     });
 385                 modifiers = field.getModifiers();
 386                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 387                     caller, tclass, null, modifiers);
 388                 ClassLoader cl = tclass.getClassLoader();
 389                 ClassLoader ccl = caller.getClassLoader();


 473         }
 474 
 475         private void ensureProtectedAccess(T obj) {
 476             if (cclass.isInstance(obj)) {
 477                 return;
 478             }
 479             throw new RuntimeException(
 480                 new IllegalAccessException("Class " +
 481                     cclass.getName() +
 482                     " can not access a protected member of class " +
 483                     tclass.getName() +
 484                     " using an instance of " +
 485                     obj.getClass().getName()
 486                 )
 487             );
 488         }
 489     }
 490 
 491 
 492     private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
 493         private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 494         private final long offset;
 495         private final Class<T> tclass;
 496         private final Class<?> cclass;
 497 
 498         LockedUpdater(final Class<T> tclass, final String fieldName,
 499                       final Class<?> caller) {
 500             Field field = null;
 501             int modifiers = 0;
 502             try {
 503                 field = AccessController.doPrivileged(
 504                     new PrivilegedExceptionAction<Field>() {
 505                         public Field run() throws NoSuchFieldException {
 506                             return tclass.getDeclaredField(fieldName);
 507                         }
 508                     });
 509                 modifiers = field.getModifiers();
 510                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 511                     caller, tclass, null, modifiers);
 512                 ClassLoader cl = tclass.getClassLoader();
 513                 ClassLoader ccl = caller.getClassLoader();




 349      * function is applied with the current value as its first argument,
 350      * and the given update as the second argument.
 351      *
 352      * @param obj An object whose field to get and set
 353      * @param x the update value
 354      * @param accumulatorFunction a side-effect-free function of two arguments
 355      * @return the updated value
 356      * @since 1.8
 357      */
 358     public final long accumulateAndGet(T obj, long x,
 359                                        LongBinaryOperator accumulatorFunction) {
 360         long prev, next;
 361         do {
 362             prev = get(obj);
 363             next = accumulatorFunction.applyAsLong(prev, x);
 364         } while (!compareAndSet(obj, prev, next));
 365         return next;
 366     }
 367 
 368     private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
 369         private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 370         private final long offset;
 371         private final Class<T> tclass;
 372         private final Class<?> cclass;
 373 
 374         CASUpdater(final Class<T> tclass, final String fieldName,
 375                    final Class<?> caller) {
 376             final Field field;
 377             final int modifiers;
 378             try {
 379                 field = AccessController.doPrivileged(
 380                     new PrivilegedExceptionAction<Field>() {
 381                         public Field run() throws NoSuchFieldException {
 382                             return tclass.getDeclaredField(fieldName);
 383                         }
 384                     });
 385                 modifiers = field.getModifiers();
 386                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 387                     caller, tclass, null, modifiers);
 388                 ClassLoader cl = tclass.getClassLoader();
 389                 ClassLoader ccl = caller.getClassLoader();


 473         }
 474 
 475         private void ensureProtectedAccess(T obj) {
 476             if (cclass.isInstance(obj)) {
 477                 return;
 478             }
 479             throw new RuntimeException(
 480                 new IllegalAccessException("Class " +
 481                     cclass.getName() +
 482                     " can not access a protected member of class " +
 483                     tclass.getName() +
 484                     " using an instance of " +
 485                     obj.getClass().getName()
 486                 )
 487             );
 488         }
 489     }
 490 
 491 
 492     private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
 493         private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 494         private final long offset;
 495         private final Class<T> tclass;
 496         private final Class<?> cclass;
 497 
 498         LockedUpdater(final Class<T> tclass, final String fieldName,
 499                       final Class<?> caller) {
 500             Field field = null;
 501             int modifiers = 0;
 502             try {
 503                 field = AccessController.doPrivileged(
 504                     new PrivilegedExceptionAction<Field>() {
 505                         public Field run() throws NoSuchFieldException {
 506                             return tclass.getDeclaredField(fieldName);
 507                         }
 508                     });
 509                 modifiers = field.getModifiers();
 510                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 511                     caller, tclass, null, modifiers);
 512                 ClassLoader cl = tclass.getClassLoader();
 513                 ClassLoader ccl = caller.getClassLoader();