jdk/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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} reference fields of designated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * classes.  This class is designed for use in atomic data structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * in which several reference fields of the same node are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * independently subject to atomic updates. For example, a tree node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * might be declared as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * class Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   private volatile Node left, right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   private static final AtomicReferenceFieldUpdater&lt;Node, Node&gt; leftUpdater =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   private static AtomicReferenceFieldUpdater&lt;Node, Node&gt; rightUpdater =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   Node getLeft() { return left;  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   boolean compareAndSetLeft(Node expect, Node update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     return leftUpdater.compareAndSet(this, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   // ... and so on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>Note that the guarantees of the {@code compareAndSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * method in this class are weaker than in other atomic classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Because this class cannot ensure that all uses of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * are appropriate for purposes of atomic access, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * guarantee atomicity only with respect to other invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * {@code compareAndSet} and {@code set} on the same updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @param <T> The type of the object holding the updatable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @param <V> The type of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public abstract class AtomicReferenceFieldUpdater<T, V>  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates and returns an updater for objects with the given field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The Class arguments are needed to check that reflective types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * generic types match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param tclass the class of the objects holding the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param vclass the class of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param fieldName the name of the field to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return the updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws IllegalArgumentException if the field is not a volatile reference type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws RuntimeException with a nested reflection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * exception if the class does not hold field or is the wrong type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static <U, W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass, Class<W> vclass, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new AtomicReferenceFieldUpdaterImpl<U,W>(tclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                        vclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                        fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Protected do-nothing constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected AtomicReferenceFieldUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public abstract boolean compareAndSet(T obj, V expect, V update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * to the given updated value if the current value {@code ==} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * expected value. This method is guaranteed to be atomic with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * other calls to {@code compareAndSet} and {@code set}, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * necessarily with respect to other changes in the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param obj An object whose field to conditionally set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public abstract boolean weakCompareAndSet(T obj, V expect, V update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Sets the field of the given object managed by this updater to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * given updated value. This operation is guaranteed to act as a volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * store with respect to subsequent invocations of {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public abstract void set(T obj, V newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Eventually sets the field of the given object managed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * updater to the given updated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param obj An object whose field to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract void lazySet(T obj, V newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Gets the current value held in the field of the given object managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * by this updater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param obj An object whose field to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public abstract V get(T obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Atomically sets the field of the given object managed by this updater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param obj An object whose field to get and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param newValue the new value
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 V getAndSet(T obj, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            V current = get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (compareAndSet(obj, current, newValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return current;
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
    private static final class AtomicReferenceFieldUpdaterImpl<T,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        extends AtomicReferenceFieldUpdater<T,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        private final Class<T> tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        private final Class<V> vclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        private final Class cclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * Internal type checks within all update methods contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * internal inlined optimizations checking for the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * cases where the class is final (in which case a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * getClass comparison suffices) or is of type Object (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * which case no check is needed because all objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * instances of Object). The Object case is handled simply by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * setting vclass to null in constructor.  The targetCheck and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * updateCheck methods are invoked when these faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * screenings fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        AtomicReferenceFieldUpdaterImpl(Class<T> tclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                        Class<V> vclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                        String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            Field field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            Class fieldClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            Class caller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                field = tclass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                caller = sun.reflect.Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                modifiers = field.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    caller, tclass, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                fieldClass = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (vclass != fieldClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (!Modifier.isVolatile(modifiers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new IllegalArgumentException("Must be volatile type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.cclass = (Modifier.isProtected(modifiers) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                           caller != tclass) ? caller : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            this.tclass = tclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (vclass == Object.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                this.vclass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                this.vclass = vclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            offset = unsafe.objectFieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        void targetCheck(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (!tclass.isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        void updateCheck(T obj, V update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (!tclass.isInstance(obj) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                (update != null && vclass != null && !vclass.isInstance(update)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                ensureProtectedAccess(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public boolean compareAndSet(T obj, V expect, V update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (obj == null || obj.getClass() != tclass || cclass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                (update != null && vclass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                 vclass != update.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                updateCheck(obj, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return unsafe.compareAndSwapObject(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public boolean weakCompareAndSet(T obj, V expect, V update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // same implementation as strong form for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (obj == null || obj.getClass() != tclass || cclass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                (update != null && vclass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                 vclass != update.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                updateCheck(obj, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return unsafe.compareAndSwapObject(obj, offset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public void set(T obj, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (obj == null || obj.getClass() != tclass || cclass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                (newValue != null && vclass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                 vclass != newValue.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                updateCheck(obj, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            unsafe.putObjectVolatile(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        public void lazySet(T obj, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (obj == null || obj.getClass() != tclass || cclass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                (newValue != null && vclass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                 vclass != newValue.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                updateCheck(obj, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            unsafe.putOrderedObject(obj, offset, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        public V get(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (obj == null || obj.getClass() != tclass || cclass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                targetCheck(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return (V)unsafe.getObjectVolatile(obj, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        private void ensureProtectedAccess(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (cclass.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new RuntimeException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                new IllegalAccessException("Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    cclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    " can not access a protected member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    tclass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    " using an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    obj.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}