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