jdk/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
author dl
Fri, 26 Oct 2012 21:34:24 +0100
changeset 14325 622c473a21aa
parent 12674 5f9829b7fc7e
child 15020 50394fa17c1b
permissions -rw-r--r--
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012 Reviewed-by: chegar, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.atomic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.misc.Unsafe;
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    38
import java.lang.reflect.Field;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    39
import java.lang.reflect.Modifier;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    40
import java.security.AccessController;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    41
import java.security.PrivilegedExceptionAction;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    42
import java.security.PrivilegedActionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A reflection-based utility that enables atomic updates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * designated {@code volatile long} fields of designated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class is designed for use in atomic data structures in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * several fields of the same node are independently subject to atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
    62
public abstract class AtomicLongFieldUpdater<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The Class argument is needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param tclass the class of the objects holding the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param fieldName the name of the field to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws IllegalArgumentException if the field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * volatile long type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @throws RuntimeException with a nested reflection-based
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    74
     * exception if the class does not hold field or is the wrong type,
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    75
     * or the field is inaccessible to the caller according to Java language
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
    76
     * access control
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (AtomicLong.VM_SUPPORTS_LONG_CAS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return new CASUpdater<U>(tclass, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return new LockedUpdater<U>(tclass, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected AtomicLongFieldUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * of the class possessing the field established in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public abstract boolean compareAndSet(T obj, long expect, long update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * of the class possessing the field established in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public abstract boolean weakCompareAndSet(T obj, long expect, long update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public abstract void set(T obj, long newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public abstract void lazySet(T obj, long newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract long get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public long getAndSet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (compareAndSet(obj, current, newValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public long getAndIncrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            long next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public long getAndDecrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            long next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public long getAndAdd(T obj, long delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            long next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public long incrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            long next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public long decrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            long next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public long addAndGet(T obj, long delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            long next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        private final Class<T> tclass;
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   274
        private final Class<?> cclass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   276
        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
   277
            final Field field;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   278
            final Class<?> caller;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 12674
diff changeset
   279
            final int modifiers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   281
                field = AccessController.doPrivileged(
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   282
                    new PrivilegedExceptionAction<Field>() {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   283
                        public Field run() throws NoSuchFieldException {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   284
                            return tclass.getDeclaredField(fieldName);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   285
                        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   286
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    caller, tclass, null, modifiers);
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   291
                ClassLoader cl = tclass.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   292
                ClassLoader ccl = caller.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   293
                if ((ccl != null) && (ccl != cl) &&
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   294
                    ((cl == null) || !isAncestor(cl, ccl))) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   295
                  sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   296
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   297
            } catch (PrivilegedActionException pae) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   298
                throw new RuntimeException(pae.getException());
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   299
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   303
            Class<?> fieldt = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (fieldt != long.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                throw new IllegalArgumentException("Must be long type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        public boolean compareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return unsafe.compareAndSwapLong(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public boolean weakCompareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return unsafe.compareAndSwapLong(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public void set(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            unsafe.putLongVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        public void lazySet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            unsafe.putOrderedLong(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public long get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return unsafe.getLongVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   352
            throw new RuntimeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private final Class<T> tclass;
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   369
        private final Class<?> cclass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   371
        LockedUpdater(final Class<T> tclass, final String fieldName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            Field field = null;
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   373
            Class<?> caller = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            try {
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   376
                field = AccessController.doPrivileged(
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   377
                    new PrivilegedExceptionAction<Field>() {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   378
                        public Field run() throws NoSuchFieldException {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   379
                            return tclass.getDeclaredField(fieldName);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   380
                        }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   381
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    caller, tclass, null, modifiers);
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   386
                ClassLoader cl = tclass.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   387
                ClassLoader ccl = caller.getClassLoader();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   388
                if ((ccl != null) && (ccl != cl) &&
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   389
                    ((cl == null) || !isAncestor(cl, ccl))) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   390
                  sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   391
                }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   392
            } catch (PrivilegedActionException pae) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   393
                throw new RuntimeException(pae.getException());
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   394
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
   398
            Class<?> fieldt = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (fieldt != long.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                throw new IllegalArgumentException("Must be long type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        public boolean compareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            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
   420
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                long v = unsafe.getLong(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (v != expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                unsafe.putLong(obj, offset, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        public boolean weakCompareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return compareAndSet(obj, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public void set(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            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
   435
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                unsafe.putLong(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        public void lazySet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            set(obj, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        public long get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            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
   446
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                return unsafe.getLong(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   455
            throw new RuntimeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
12674
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   466
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   467
    /**
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   468
     * 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
   469
     * classloader's delegation chain.
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   470
     * Equivalent to the inaccessible: first.isAncestor(second).
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   471
     */
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   472
    private static boolean isAncestor(ClassLoader first, ClassLoader second) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   473
        ClassLoader acl = first;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   474
        do {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   475
            acl = acl.getParent();
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   476
            if (second == acl) {
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   477
                return true;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   478
            }
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   479
        } while (acl != null);
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   480
        return false;
5f9829b7fc7e 7103570: AtomicIntegerFieldUpdater does not work when SecurityManager is installed
dholmes
parents: 11134
diff changeset
   481
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}