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