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