jdk/src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
author dl
Wed, 01 Dec 2010 21:46:52 +0000
changeset 7518 0282db800fe1
parent 5506 202f599c92aa
child 9242 ef138d47df58
permissions -rw-r--r--
7003745: Code style cleanups (sync from Dougs CVS) 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.atomic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A reflection-based utility that enables atomic updates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * designated {@code volatile long} fields of designated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class is designed for use in atomic data structures in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * several fields of the same node are independently subject to atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    58
public abstract class  AtomicLongFieldUpdater<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The Class argument is needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param tclass the class of the objects holding the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param fieldName the name of the field to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @throws IllegalArgumentException if the field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * volatile long type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws RuntimeException with a nested reflection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * exception if the class does not hold field or is the wrong type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (AtomicLong.VM_SUPPORTS_LONG_CAS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return new CASUpdater<U>(tclass, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return new LockedUpdater<U>(tclass, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected AtomicLongFieldUpdater() {
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
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * of the class possessing the field established in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public abstract boolean compareAndSet(T obj, long expect, long update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws ClassCastException if {@code obj} is not an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * of the class possessing the field established in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public abstract boolean weakCompareAndSet(T obj, long expect, long update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public abstract void set(T obj, long newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public abstract void lazySet(T obj, long newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public abstract long get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public long getAndSet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (compareAndSet(obj, current, newValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public long getAndIncrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            long next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public long getAndDecrement(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            long next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public long getAndAdd(T obj, long delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            long next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Atomically increments by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public long incrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            long next = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Atomically decrements by one the current value of the field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public long decrementAndGet(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            long next = current - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Atomically adds the given value to the current value of the field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the given object managed by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public long addAndGet(T obj, long delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            long current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            long next = current + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (compareAndSet(obj, current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private final Class<T> tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        private final Class cclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        CASUpdater(Class<T> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Field field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            Class caller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                field = tclass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    caller, tclass, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   281
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Class fieldt = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (fieldt != long.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new IllegalArgumentException("Must be long type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public boolean compareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return unsafe.compareAndSwapLong(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public boolean weakCompareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return unsafe.compareAndSwapLong(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        public void set(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            unsafe.putLongVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        public void lazySet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            unsafe.putOrderedLong(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        public long get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return unsafe.getLongVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   334
            throw new RuntimeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        private final Class<T> tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        private final Class cclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        LockedUpdater(Class<T> tclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            Field field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            Class caller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                field = tclass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    caller, tclass, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   364
            } catch (Exception ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            Class fieldt = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (fieldt != long.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw new IllegalArgumentException("Must be long type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        private void fullCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public boolean compareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            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
   390
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                long v = unsafe.getLong(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (v != expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                unsafe.putLong(obj, offset, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public boolean weakCompareAndSet(T obj, long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return compareAndSet(obj, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        public void set(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            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
   405
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                unsafe.putLong(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        public void lazySet(T obj, long newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            set(obj, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        public long get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            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
   416
            synchronized (this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return unsafe.getLong(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   425
            throw new RuntimeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
}