jdk/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
author mchung
Tue, 16 Apr 2013 21:39:52 -0700
changeset 16906 44dfee24cb71
parent 16011 890a7ed97f6c
child 18576 7a5c231327af
permissions -rw-r--r--
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive Reviewed-by: jrose, alanb, twisti
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;
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
    37
import java.util.function.IntUnaryOperator;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
    38
import java.util.function.IntBinaryOperator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.Unsafe;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
    40
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
    41
import sun.reflect.Reflection;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
    42
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    43
import java.lang.reflect.Field;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    44
import java.lang.reflect.Modifier;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    45
import java.security.AccessController;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    46
import java.security.PrivilegedExceptionAction;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    47
import java.security.PrivilegedActionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * A reflection-based utility that enables atomic updates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * designated {@code volatile int} fields of designated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This class is designed for use in atomic data structures in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * several fields of the same node are independently subject to atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
    67
public abstract class AtomicIntegerFieldUpdater<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The Class argument is needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param tclass the class of the objects holding the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param fieldName the name of the field to be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @throws IllegalArgumentException if the field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * volatile integer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @throws RuntimeException with a nested reflection-based
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    79
     * 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
    80
     * 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
    81
     * access control
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
    83
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
    85
        return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName, Reflection.getCallerClass());
2
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
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected AtomicIntegerFieldUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public abstract boolean compareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of the class possessing the field established in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public abstract boolean weakCompareAndSet(T obj, int expect, int update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public abstract void set(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public abstract void lazySet(T obj, int newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public abstract int get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int getAndSet(T obj, int newValue) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   168
        int prev;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   169
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   170
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   171
        } while (!compareAndSet(obj, prev, newValue));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   172
        return prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int getAndIncrement(T obj) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   183
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   184
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   185
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   186
            next = prev + 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   187
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   188
        return prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public int getAndDecrement(T obj) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   199
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   200
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   201
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   202
            next = prev - 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   203
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   204
        return prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public int getAndAdd(T obj, int delta) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   216
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   217
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   218
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   219
            next = prev + delta;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   220
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   221
        return prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public int incrementAndGet(T obj) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   232
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   233
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   234
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   235
            next = prev + 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   236
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   237
        return next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public int decrementAndGet(T obj) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   248
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   249
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   250
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   251
            next = prev - 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   252
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   253
        return next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public int addAndGet(T obj, int delta) {
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   265
        int prev, next;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   266
        do {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   267
            prev = get(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   268
            next = prev + delta;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   269
        } while (!compareAndSet(obj, prev, next));
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   270
        return next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   274
     * Atomically updates the field of the given object managed by this updater
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   275
     * with the results of applying the given function, returning the previous
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   276
     * value. The function should be side-effect-free, since it may be
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   277
     * re-applied when attempted updates fail due to contention among threads.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   278
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   279
     * @param obj An object whose field to get and set
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   280
     * @param updateFunction a side-effect-free function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   281
     * @return the previous value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   282
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   283
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   284
    public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   285
        int prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   286
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   287
            prev = get(obj);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   288
            next = updateFunction.applyAsInt(prev);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   289
        } while (!compareAndSet(obj, prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   290
        return prev;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   291
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   292
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   293
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   294
     * Atomically updates the field of the given object managed by this updater
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   295
     * with the results of applying the given function, returning the updated
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   296
     * value. The function should be side-effect-free, since it may be
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   297
     * re-applied when attempted updates fail due to contention among threads.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   298
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   299
     * @param obj An object whose field to get and set
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   300
     * @param updateFunction a side-effect-free function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   301
     * @return the updated value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   302
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   303
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   304
    public final int updateAndGet(T obj, IntUnaryOperator updateFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   305
        int prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   306
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   307
            prev = get(obj);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   308
            next = updateFunction.applyAsInt(prev);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   309
        } while (!compareAndSet(obj, prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   310
        return next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   311
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   312
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   313
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   314
     * Atomically updates the field of the given object managed by this
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   315
     * updater with the results of applying the given function to the
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   316
     * current and given values, returning the previous value. The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   317
     * function should be side-effect-free, since it may be re-applied
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   318
     * when attempted updates fail due to contention among threads.  The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   319
     * function is applied with the current value as its first argument,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   320
     * and the given update as the second argument.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   321
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   322
     * @param obj An object whose field to get and set
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   323
     * @param x the update value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   324
     * @param accumulatorFunction a side-effect-free function of two arguments
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   325
     * @return the previous value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   326
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   327
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   328
    public final int getAndAccumulate(T obj, int x,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   329
                                      IntBinaryOperator accumulatorFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   330
        int prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   331
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   332
            prev = get(obj);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   333
            next = accumulatorFunction.applyAsInt(prev, x);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   334
        } while (!compareAndSet(obj, prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   335
        return prev;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   336
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   337
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   338
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   339
     * Atomically updates the field of the given object managed by this
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   340
     * updater with the results of applying the given function to the
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   341
     * current and given values, returning the updated value. The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   342
     * function should be side-effect-free, since it may be re-applied
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   343
     * when attempted updates fail due to contention among threads.  The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   344
     * function is applied with the current value as its first argument,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   345
     * and the given update as the second argument.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   346
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   347
     * @param obj An object whose field to get and set
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   348
     * @param x the update value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   349
     * @param accumulatorFunction a side-effect-free function of two arguments
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   350
     * @return the updated value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   351
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   352
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   353
    public final int accumulateAndGet(T obj, int x,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   354
                                      IntBinaryOperator accumulatorFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   355
        int prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   356
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   357
            prev = get(obj);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   358
            next = accumulatorFunction.applyAsInt(prev, x);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   359
        } while (!compareAndSet(obj, prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   360
        return next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   361
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   362
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   363
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Standard hotspot implementation using intrinsics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private static class AtomicIntegerFieldUpdaterImpl<T> extends AtomicIntegerFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        private final Class<T> tclass;
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   370
        private final Class<?> cclass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
   372
        AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
   373
                                      final String fieldName,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
   374
                                      final Class<?> caller)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16011
diff changeset
   375
        {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   376
            final Field field;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   377
            final int modifiers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            try {
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   379
                field = AccessController.doPrivileged(
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   380
                    new PrivilegedExceptionAction<Field>() {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   381
                        public Field run() throws NoSuchFieldException {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   382
                            return tclass.getDeclaredField(fieldName);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   383
                        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   384
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    caller, tclass, null, modifiers);
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   388
                ClassLoader cl = tclass.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   389
                ClassLoader ccl = caller.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   390
                if ((ccl != null) && (ccl != cl) &&
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   391
                    ((cl == null) || !isAncestor(cl, ccl))) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   392
                  sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   393
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   394
            } catch (PrivilegedActionException pae) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   395
                throw new RuntimeException(pae.getException());
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   396
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   400
            Class<?> fieldt = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (fieldt != int.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                throw new IllegalArgumentException("Must be integer type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   413
        /**
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   414
         * 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
   415
         * classloader's delegation chain.
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   416
         * Equivalent to the inaccessible: first.isAncestor(second).
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   417
         */
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   418
        private static boolean isAncestor(ClassLoader first, ClassLoader second) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   419
            ClassLoader acl = first;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   420
            do {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   421
                acl = acl.getParent();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   422
                if (second == acl) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   423
                    return true;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   424
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   425
            } while (acl != null);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   426
            return false;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   427
        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   428
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        public boolean compareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        public boolean weakCompareAndSet(T obj, int expect, int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return unsafe.compareAndSwapInt(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        public void set(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            unsafe.putIntVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        public void lazySet(T obj, int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            unsafe.putOrderedInt(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        public final int get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return unsafe.getIntVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   461
        public int getAndSet(T obj, int newValue) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   462
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   463
            return unsafe.getAndSetInt(obj, offset, newValue);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   464
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   465
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   466
        public int getAndIncrement(T obj) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   467
            return getAndAdd(obj, 1);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   468
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   469
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   470
        public int getAndDecrement(T obj) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   471
            return getAndAdd(obj, -1);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   472
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   473
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   474
        public int getAndAdd(T obj, int delta) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   475
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   476
            return unsafe.getAndAddInt(obj, offset, delta);
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   477
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   478
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   479
        public int incrementAndGet(T obj) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   480
            return getAndAdd(obj, 1) + 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   481
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   482
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   483
        public int decrementAndGet(T obj) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   484
             return getAndAdd(obj, -1) - 1;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   485
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   486
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   487
        public int addAndGet(T obj, int delta) {
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   488
            return getAndAdd(obj, delta) + delta;
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   489
        }
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   490
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}