jdk/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.atomic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A reflection-based utility that enables atomic updates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * designated {@code volatile int} fields of designated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class is designed for use in atomic data structures in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * several fields of the same node are independently subject to atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public abstract class  AtomicIntegerFieldUpdater<T>  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The Class argument is needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param tclass the class of the objects holding the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param fieldName the name of the field to be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @throws IllegalArgumentException if the field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * volatile integer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws RuntimeException with a nested reflection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * exception if the class does not hold field or is the wrong type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected AtomicIntegerFieldUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public abstract boolean compareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public abstract boolean weakCompareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public abstract void set(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public abstract void lazySet(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public abstract int get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public int getAndSet(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (compareAndSet(obj, current, newValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public int getAndIncrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public int getAndDecrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            int next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public int getAndAdd(T obj, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            int next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public int incrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            int next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int decrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public int addAndGet(T obj, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Standard hotspot implementation using intrinsics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        private final Class<T> tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        private final Class cclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        AtomicIntegerFieldUpdaterImpl(Class<T> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            Field field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Class caller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                field = tclass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    caller, tclass, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } catch(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Class fieldt = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (fieldt != int.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new IllegalArgumentException("Must be integer type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        public boolean compareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        public boolean weakCompareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public void set(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            unsafe.putIntVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        public void lazySet(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            unsafe.putOrderedInt(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public final int get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return unsafe.getIntVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}