src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java

Print this page




 142     public abstract void lazySet(T obj, int newValue);
 143 
 144     /**
 145      * Gets the current value held in the field of the given object managed
 146      * by this updater.
 147      *
 148      * @param obj An object whose field to get
 149      * @return the current value
 150      */
 151     public abstract int get(T obj);
 152 
 153     /**
 154      * Atomically sets the field of the given object managed by this updater
 155      * to the given value and returns the old value.
 156      *
 157      * @param obj An object whose field to get and set
 158      * @param newValue the new value
 159      * @return the previous value
 160      */
 161     public int getAndSet(T obj, int newValue) {
 162         for (;;) {
 163             int current = get(obj);
 164             if (compareAndSet(obj, current, newValue))
 165                 return current;

 166         }
 167     }
 168 
 169     /**
 170      * Atomically increments by one the current value of the field of the
 171      * given object managed by this updater.
 172      *
 173      * @param obj An object whose field to get and set
 174      * @return the previous value
 175      */
 176     public int getAndIncrement(T obj) {
 177         for (;;) {
 178             int current = get(obj);
 179             int next = current + 1;
 180             if (compareAndSet(obj, current, next))
 181                 return current;

 182         }
 183     }
 184 
 185     /**
 186      * Atomically decrements by one the current value of the field of the
 187      * given object managed by this updater.
 188      *
 189      * @param obj An object whose field to get and set
 190      * @return the previous value
 191      */
 192     public int getAndDecrement(T obj) {
 193         for (;;) {
 194             int current = get(obj);
 195             int next = current - 1;
 196             if (compareAndSet(obj, current, next))
 197                 return current;

 198         }
 199     }
 200 
 201     /**
 202      * Atomically adds the given value to the current value of the field of
 203      * the given object managed by this updater.
 204      *
 205      * @param obj An object whose field to get and set
 206      * @param delta the value to add
 207      * @return the previous value
 208      */
 209     public int getAndAdd(T obj, int delta) {
 210         for (;;) {
 211             int current = get(obj);
 212             int next = current + delta;
 213             if (compareAndSet(obj, current, next))
 214                 return current;

 215         }
 216     }
 217 
 218     /**
 219      * Atomically increments by one the current value of the field of the
 220      * given object managed by this updater.
 221      *
 222      * @param obj An object whose field to get and set
 223      * @return the updated value
 224      */
 225     public int incrementAndGet(T obj) {
 226         for (;;) {
 227             int current = get(obj);
 228             int next = current + 1;
 229             if (compareAndSet(obj, current, next))

 230                 return next;
 231         }
 232     }
 233 
 234     /**
 235      * Atomically decrements by one the current value of the field of the
 236      * given object managed by this updater.
 237      *
 238      * @param obj An object whose field to get and set
 239      * @return the updated value
 240      */
 241     public int decrementAndGet(T obj) {
 242         for (;;) {
 243             int current = get(obj);
 244             int next = current - 1;
 245             if (compareAndSet(obj, current, next))

 246                 return next;
 247         }
 248     }
 249 
 250     /**
 251      * Atomically adds the given value to the current value of the field of
 252      * the given object managed by this updater.
 253      *
 254      * @param obj An object whose field to get and set
 255      * @param delta the value to add
 256      * @return the updated value
 257      */
 258     public int addAndGet(T obj, int delta) {
 259         for (;;) {
 260             int current = get(obj);
 261             int next = current + delta;
 262             if (compareAndSet(obj, current, next))

 263                 return next;
 264         }
 265     }
 266 
 267     /**
 268      * Standard hotspot implementation using intrinsics
 269      */
 270     private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
 271         private static final Unsafe unsafe = Unsafe.getUnsafe();
 272         private final long offset;
 273         private final Class<T> tclass;
 274         private final Class<?> cclass;
 275 
 276         AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName) {
 277             final Field field;
 278             final Class<?> caller;
 279             final int modifiers;
 280             try {
 281                 field = AccessController.doPrivileged(
 282                     new PrivilegedExceptionAction<Field>() {
 283                         public Field run() throws NoSuchFieldException {
 284                             return tclass.getDeclaredField(fieldName);
 285                         }


 344         public boolean weakCompareAndSet(T obj, int expect, int update) {
 345             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 346             return unsafe.compareAndSwapInt(obj, offset, expect, update);
 347         }
 348 
 349         public void set(T obj, int newValue) {
 350             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 351             unsafe.putIntVolatile(obj, offset, newValue);
 352         }
 353 
 354         public void lazySet(T obj, int newValue) {
 355             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 356             unsafe.putOrderedInt(obj, offset, newValue);
 357         }
 358 
 359         public final int get(T obj) {
 360             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 361             return unsafe.getIntVolatile(obj, offset);
 362         }
 363 






























 364         private void ensureProtectedAccess(T obj) {
 365             if (cclass.isInstance(obj)) {
 366                 return;
 367             }
 368             throw new RuntimeException(
 369                 new IllegalAccessException("Class " +
 370                     cclass.getName() +
 371                     " can not access a protected member of class " +
 372                     tclass.getName() +
 373                     " using an instance of " +
 374                     obj.getClass().getName()
 375                 )
 376             );
 377         }
 378     }
 379 }


 142     public abstract void lazySet(T obj, int newValue);
 143 
 144     /**
 145      * Gets the current value held in the field of the given object managed
 146      * by this updater.
 147      *
 148      * @param obj An object whose field to get
 149      * @return the current value
 150      */
 151     public abstract int get(T obj);
 152 
 153     /**
 154      * Atomically sets the field of the given object managed by this updater
 155      * to the given value and returns the old value.
 156      *
 157      * @param obj An object whose field to get and set
 158      * @param newValue the new value
 159      * @return the previous value
 160      */
 161     public int getAndSet(T obj, int newValue) {
 162         int prev;
 163         do {
 164             prev = get(obj);
 165         } while (!compareAndSet(obj, prev, newValue));
 166         return prev;
 167     }

 168 
 169     /**
 170      * Atomically increments by one the current value of the field of the
 171      * given object managed by this updater.
 172      *
 173      * @param obj An object whose field to get and set
 174      * @return the previous value
 175      */
 176     public int getAndIncrement(T obj) {
 177         int prev, next;
 178         do {
 179             prev = get(obj);
 180             next = prev + 1;
 181         } while (!compareAndSet(obj, prev, next));
 182         return prev;
 183     }

 184 
 185     /**
 186      * Atomically decrements by one the current value of the field of the
 187      * given object managed by this updater.
 188      *
 189      * @param obj An object whose field to get and set
 190      * @return the previous value
 191      */
 192     public int getAndDecrement(T obj) {
 193         int prev, next;
 194         do {
 195             prev = get(obj);
 196             next = prev - 1;
 197         } while (!compareAndSet(obj, prev, next));
 198         return prev;
 199     }

 200 
 201     /**
 202      * Atomically adds the given value to the current value of the field of
 203      * the given object managed by this updater.
 204      *
 205      * @param obj An object whose field to get and set
 206      * @param delta the value to add
 207      * @return the previous value
 208      */
 209     public int getAndAdd(T obj, int delta) {
 210         int prev, next;
 211         do {
 212             prev = get(obj);
 213             next = prev + delta;
 214         } while (!compareAndSet(obj, prev, next));
 215         return prev;
 216     }

 217 
 218     /**
 219      * Atomically increments by one the current value of the field of the
 220      * given object managed by this updater.
 221      *
 222      * @param obj An object whose field to get and set
 223      * @return the updated value
 224      */
 225     public int incrementAndGet(T obj) {
 226         int prev, next;
 227         do {
 228             prev = get(obj);
 229             next = prev + 1;
 230         } while (!compareAndSet(obj, prev, next));
 231         return next;
 232     }

 233 
 234     /**
 235      * Atomically decrements by one the current value of the field of the
 236      * given object managed by this updater.
 237      *
 238      * @param obj An object whose field to get and set
 239      * @return the updated value
 240      */
 241     public int decrementAndGet(T obj) {
 242         int prev, next;
 243         do {
 244             prev = get(obj);
 245             next = prev - 1;
 246         } while (!compareAndSet(obj, prev, next));
 247         return next;
 248     }

 249 
 250     /**
 251      * Atomically adds the given value to the current value of the field of
 252      * the given object managed by this updater.
 253      *
 254      * @param obj An object whose field to get and set
 255      * @param delta the value to add
 256      * @return the updated value
 257      */
 258     public int addAndGet(T obj, int delta) {
 259         int prev, next;
 260         do {
 261             prev = get(obj);
 262             next = prev + delta;
 263         } while (!compareAndSet(obj, prev, next));
 264         return next;
 265     }

 266 
 267     /**
 268      * Standard hotspot implementation using intrinsics
 269      */
 270     private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
 271         private static final Unsafe unsafe = Unsafe.getUnsafe();
 272         private final long offset;
 273         private final Class<T> tclass;
 274         private final Class<?> cclass;
 275 
 276         AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName) {
 277             final Field field;
 278             final Class<?> caller;
 279             final int modifiers;
 280             try {
 281                 field = AccessController.doPrivileged(
 282                     new PrivilegedExceptionAction<Field>() {
 283                         public Field run() throws NoSuchFieldException {
 284                             return tclass.getDeclaredField(fieldName);
 285                         }


 344         public boolean weakCompareAndSet(T obj, int expect, int update) {
 345             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 346             return unsafe.compareAndSwapInt(obj, offset, expect, update);
 347         }
 348 
 349         public void set(T obj, int newValue) {
 350             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 351             unsafe.putIntVolatile(obj, offset, newValue);
 352         }
 353 
 354         public void lazySet(T obj, int newValue) {
 355             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 356             unsafe.putOrderedInt(obj, offset, newValue);
 357         }
 358 
 359         public final int get(T obj) {
 360             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 361             return unsafe.getIntVolatile(obj, offset);
 362         }
 363 
 364         public int getAndSet(T obj, int newValue) {
 365             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 366             return unsafe.getAndSetInt(obj, offset, newValue);
 367         }
 368 
 369         public int getAndIncrement(T obj) {
 370             return getAndAdd(obj, 1);
 371         }
 372 
 373         public int getAndDecrement(T obj) {
 374             return getAndAdd(obj, -1);
 375         }
 376 
 377         public int getAndAdd(T obj, int delta) {
 378             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 379             return unsafe.getAndAddInt(obj, offset, delta);
 380         }
 381 
 382         public int incrementAndGet(T obj) {
 383             return getAndAdd(obj, 1) + 1;
 384         }
 385 
 386         public int decrementAndGet(T obj) {
 387              return getAndAdd(obj, -1) - 1;
 388         }
 389 
 390         public int addAndGet(T obj, int delta) {
 391             return getAndAdd(obj, delta) + delta;
 392         }
 393 
 394         private void ensureProtectedAccess(T obj) {
 395             if (cclass.isInstance(obj)) {
 396                 return;
 397             }
 398             throw new RuntimeException(
 399                 new IllegalAccessException("Class " +
 400                     cclass.getName() +
 401                     " can not access a protected member of class " +
 402                     tclass.getName() +
 403                     " using an instance of " +
 404                     obj.getClass().getName()
 405                 )
 406             );
 407         }
 408     }
 409 }