jdk/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
author dl
Thu, 20 Dec 2012 13:44:06 +0000
changeset 14914 69bfd88e3ea1
parent 14325 622c473a21aa
child 15020 50394fa17c1b
permissions -rw-r--r--
8002356: Add ForkJoin common pool and CountedCompleter Reviewed-by: chegar, mduigou
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
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
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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;
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    38
import java.lang.reflect.Field;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    39
import java.lang.reflect.Modifier;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    40
import java.security.AccessController;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    41
import java.security.PrivilegedExceptionAction;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    42
import java.security.PrivilegedActionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A reflection-based utility that enables atomic updates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * designated {@code volatile int} fields of designated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class is designed for use in atomic data structures in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * several fields of the same node are independently subject to atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
    62
public abstract class AtomicIntegerFieldUpdater<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The Class argument is needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param tclass the class of the objects holding the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param fieldName the name of the field to be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws IllegalArgumentException if the field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * volatile integer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @throws RuntimeException with a nested reflection-based
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    74
     * exception if the class does not hold field or is the wrong type,
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    75
     * or the field is inaccessible to the caller according to Java language
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    76
     * access control
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName);
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
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected AtomicIntegerFieldUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public abstract boolean compareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public abstract boolean weakCompareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public abstract void set(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public abstract void lazySet(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public abstract int get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int getAndSet(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (compareAndSet(obj, current, newValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int getAndIncrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            int next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int getAndDecrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            int next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int getAndAdd(T obj, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public int incrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public int decrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            int next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int addAndGet(T obj, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Standard hotspot implementation using intrinsics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        private final Class<T> tclass;
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   274
        private final Class<?> cclass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   276
        AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   277
            final Field field;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   278
            final Class<?> caller;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   279
            final int modifiers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   281
                field = AccessController.doPrivileged(
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   282
                    new PrivilegedExceptionAction<Field>() {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   283
                        public Field run() throws NoSuchFieldException {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   284
                            return tclass.getDeclaredField(fieldName);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   285
                        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   286
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    caller, tclass, null, modifiers);
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   291
                ClassLoader cl = tclass.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   292
                ClassLoader ccl = caller.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   293
                if ((ccl != null) && (ccl != cl) &&
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   294
                    ((cl == null) || !isAncestor(cl, ccl))) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   295
                  sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   296
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   297
            } catch (PrivilegedActionException pae) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   298
                throw new RuntimeException(pae.getException());
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   299
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   303
            Class<?> fieldt = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (fieldt != int.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                throw new IllegalArgumentException("Must be integer type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   316
        /**
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   317
         * Returns true if the second classloader can be found in the first
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   318
         * classloader's delegation chain.
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   319
         * Equivalent to the inaccessible: first.isAncestor(second).
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   320
         */
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   321
        private static boolean isAncestor(ClassLoader first, ClassLoader second) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   322
            ClassLoader acl = first;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   323
            do {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   324
                acl = acl.getParent();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   325
                if (second == acl) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   326
                    return true;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   327
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   328
            } while (acl != null);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   329
            return false;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   330
        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   331
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        public boolean compareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public boolean weakCompareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        public void set(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            unsafe.putIntVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public void lazySet(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            unsafe.putOrderedInt(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public final int get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return unsafe.getIntVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
}