AtomicIntegerFieldUpdater.java revision 3203
286N/A/*
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A/*
286N/A * This file is available under and governed by the GNU General Public
286N/A * License version 2 only, as published by the Free Software Foundation.
286N/A * However, the following notice accompanied the original version of this
286N/A * file:
286N/A *
286N/A * Written by Doug Lea with assistance from members of JCP JSR-166
286N/A * Expert Group and released to the public domain, as explained at
286N/A * http://creativecommons.org/licenses/publicdomain
286N/A */
286N/A
286N/Apackage java.util.concurrent.atomic;
286N/Aimport sun.misc.Unsafe;
286N/Aimport java.lang.reflect.*;
286N/A
286N/A/**
286N/A * A reflection-based utility that enables atomic updates to
286N/A * designated {@code volatile int} fields of designated classes.
286N/A * This class is designed for use in atomic data structures in which
286N/A * several fields of the same node are independently subject to atomic
286N/A * updates.
286N/A *
286N/A * <p>Note that the guarantees of the {@code compareAndSet}
286N/A * method in this class are weaker than in other atomic classes.
286N/A * Because this class cannot ensure that all uses of the field
286N/A * are appropriate for purposes of atomic access, it can
286N/A * guarantee atomicity only with respect to other invocations of
286N/A * {@code compareAndSet} and {@code set} on the same updater.
286N/A *
286N/A * @since 1.5
286N/A * @author Doug Lea
286N/A * @param <T> The type of the object holding the updatable field
286N/A */
286N/Apublic abstract class AtomicIntegerFieldUpdater<T> {
286N/A /**
286N/A * Creates and returns an updater for objects with the given field.
286N/A * The Class argument is needed to check that reflective types and
286N/A * generic types match.
286N/A *
286N/A * @param tclass the class of the objects holding the field
286N/A * @param fieldName the name of the field to be updated
286N/A * @return the updater
286N/A * @throws IllegalArgumentException if the field is not a
286N/A * volatile integer type
286N/A * @throws RuntimeException with a nested reflection-based
286N/A * exception if the class does not hold field or is the wrong type
286N/A */
286N/A public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
286N/A return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName);
286N/A }
286N/A
286N/A /**
286N/A * Protected do-nothing constructor for use by subclasses.
286N/A */
286N/A protected AtomicIntegerFieldUpdater() {
286N/A }
286N/A
286N/A /**
286N/A * Atomically sets the field of the given object managed by this updater
286N/A * to the given updated value if the current value {@code ==} the
286N/A * expected value. This method is guaranteed to be atomic with respect to
286N/A * other calls to {@code compareAndSet} and {@code set}, but not
286N/A * necessarily with respect to other changes in the field.
286N/A *
286N/A * @param obj An object whose field to conditionally set
286N/A * @param expect the expected value
286N/A * @param update the new value
286N/A * @return true if successful
286N/A * @throws ClassCastException if {@code obj} is not an instance
286N/A * of the class possessing the field established in the constructor
286N/A */
286N/A public abstract boolean compareAndSet(T obj, int expect, int update);
286N/A
286N/A /**
286N/A * Atomically sets the field of the given object managed by this updater
286N/A * to the given updated value if the current value {@code ==} the
286N/A * expected value. This method is guaranteed to be atomic with respect to
286N/A * other calls to {@code compareAndSet} and {@code set}, but not
286N/A * necessarily with respect to other changes in the field.
286N/A *
286N/A * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
286N/A * and does not provide ordering guarantees, so is only rarely an
286N/A * appropriate alternative to {@code compareAndSet}.
286N/A *
286N/A * @param obj An object whose field to conditionally set
286N/A * @param expect the expected value
286N/A * @param update the new value
286N/A * @return true if successful
286N/A * @throws ClassCastException if {@code obj} is not an instance
286N/A * of the class possessing the field established in the constructor
286N/A */
286N/A public abstract boolean weakCompareAndSet(T obj, int expect, int update);
286N/A
286N/A /**
286N/A * Sets the field of the given object managed by this updater to the
286N/A * given updated value. This operation is guaranteed to act as a volatile
286N/A * store with respect to subsequent invocations of {@code compareAndSet}.
286N/A *
286N/A * @param obj An object whose field to set
286N/A * @param newValue the new value
286N/A */
286N/A public abstract void set(T obj, int newValue);
286N/A
286N/A /**
286N/A * Eventually sets the field of the given object managed by this
286N/A * updater to the given updated value.
286N/A *
286N/A * @param obj An object whose field to set
286N/A * @param newValue the new value
286N/A * @since 1.6
286N/A */
286N/A public abstract void lazySet(T obj, int newValue);
286N/A
286N/A
286N/A /**
286N/A * Gets the current value held in the field of the given object managed
286N/A * by this updater.
286N/A *
286N/A * @param obj An object whose field to get
286N/A * @return the current value
286N/A */
286N/A public abstract int get(T obj);
286N/A
286N/A /**
286N/A * Atomically sets the field of the given object managed by this updater
286N/A * to the given value and returns the old value.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @param newValue the new value
286N/A * @return the previous value
286N/A */
286N/A public int getAndSet(T obj, int newValue) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A if (compareAndSet(obj, current, newValue))
286N/A return current;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically increments by one the current value of the field of the
286N/A * given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @return the previous value
286N/A */
286N/A public int getAndIncrement(T obj) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current + 1;
286N/A if (compareAndSet(obj, current, next))
286N/A return current;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically decrements by one the current value of the field of the
286N/A * given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @return the previous value
286N/A */
286N/A public int getAndDecrement(T obj) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current - 1;
286N/A if (compareAndSet(obj, current, next))
286N/A return current;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically adds the given value to the current value of the field of
286N/A * the given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @param delta the value to add
286N/A * @return the previous value
286N/A */
286N/A public int getAndAdd(T obj, int delta) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current + delta;
286N/A if (compareAndSet(obj, current, next))
286N/A return current;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically increments by one the current value of the field of the
286N/A * given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @return the updated value
286N/A */
286N/A public int incrementAndGet(T obj) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current + 1;
286N/A if (compareAndSet(obj, current, next))
286N/A return next;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically decrements by one the current value of the field of the
286N/A * given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @return the updated value
286N/A */
286N/A public int decrementAndGet(T obj) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current - 1;
286N/A if (compareAndSet(obj, current, next))
286N/A return next;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Atomically adds the given value to the current value of the field of
286N/A * the given object managed by this updater.
286N/A *
286N/A * @param obj An object whose field to get and set
286N/A * @param delta the value to add
286N/A * @return the updated value
286N/A */
286N/A public int addAndGet(T obj, int delta) {
286N/A for (;;) {
286N/A int current = get(obj);
286N/A int next = current + delta;
286N/A if (compareAndSet(obj, current, next))
286N/A return next;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Standard hotspot implementation using intrinsics
286N/A */
286N/A private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
286N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
286N/A private final long offset;
286N/A private final Class<T> tclass;
286N/A private final Class cclass;
286N/A
286N/A AtomicIntegerFieldUpdaterImpl(Class<T> tclass, String fieldName) {
286N/A Field field = null;
286N/A Class caller = null;
286N/A int modifiers = 0;
286N/A try {
286N/A field = tclass.getDeclaredField(fieldName);
286N/A caller = sun.reflect.Reflection.getCallerClass(3);
286N/A modifiers = field.getModifiers();
286N/A sun.reflect.misc.ReflectUtil.ensureMemberAccess(
286N/A caller, tclass, null, modifiers);
286N/A sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
286N/A } catch (Exception ex) {
286N/A throw new RuntimeException(ex);
286N/A }
286N/A
286N/A Class fieldt = field.getType();
286N/A if (fieldt != int.class)
286N/A throw new IllegalArgumentException("Must be integer type");
286N/A
286N/A if (!Modifier.isVolatile(modifiers))
286N/A throw new IllegalArgumentException("Must be volatile type");
286N/A
286N/A this.cclass = (Modifier.isProtected(modifiers) &&
286N/A caller != tclass) ? caller : null;
286N/A this.tclass = tclass;
286N/A offset = unsafe.objectFieldOffset(field);
286N/A }
286N/A
286N/A private void fullCheck(T obj) {
286N/A if (!tclass.isInstance(obj))
286N/A throw new ClassCastException();
286N/A if (cclass != null)
286N/A ensureProtectedAccess(obj);
286N/A }
286N/A
286N/A public boolean compareAndSet(T obj, int expect, int update) {
286N/A if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
286N/A return unsafe.compareAndSwapInt(obj, offset, expect, update);
286N/A }
286N/A
286N/A public boolean weakCompareAndSet(T obj, int expect, int update) {
286N/A if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
286N/A return unsafe.compareAndSwapInt(obj, offset, expect, update);
286N/A }
286N/A
286N/A public void set(T obj, int newValue) {
286N/A if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
286N/A unsafe.putIntVolatile(obj, offset, newValue);
286N/A }
286N/A
286N/A public void lazySet(T obj, int newValue) {
286N/A if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
286N/A unsafe.putOrderedInt(obj, offset, newValue);
286N/A }
286N/A
286N/A public final int get(T obj) {
286N/A if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
286N/A return unsafe.getIntVolatile(obj, offset);
286N/A }
286N/A
286N/A private void ensureProtectedAccess(T obj) {
286N/A if (cclass.isInstance(obj)) {
286N/A return;
286N/A }
286N/A throw new RuntimeException(
286N/A new IllegalAccessException("Class " +
286N/A cclass.getName() +
286N/A " can not access a protected member of class " +
286N/A tclass.getName() +
286N/A " using an instance of " +
286N/A obj.getClass().getName()
286N/A )
286N/A );
286N/A }
286N/A }
286N/A}
286N/A