jdk/src/share/classes/java/util/concurrent/atomic/AtomicStampedReference.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An {@code AtomicStampedReference} maintains an object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * along with an integer "stamp", that can be updated atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> Implementation note. This implementation maintains stamped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * references by creating internal objects representing "boxed"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * [reference, integer] pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @param <V> The type of object referred to by this reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class AtomicStampedReference<V>  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static class ReferenceIntegerPair<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        private final T reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        private final int integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ReferenceIntegerPair(T r, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            reference = r; integer = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final AtomicReference<ReferenceIntegerPair<V>>  atomicRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates a new {@code AtomicStampedReference} with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * initial values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param initialRef the initial reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param initialStamp the initial stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public AtomicStampedReference(V initialRef, int initialStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        atomicRef = new AtomicReference<ReferenceIntegerPair<V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            (new ReferenceIntegerPair<V>(initialRef, initialStamp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the current value of the reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the current value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public V getReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return atomicRef.get().reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Returns the current value of the stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return the current value of the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int getStamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return atomicRef.get().integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns the current values of both the reference and the stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Typical usage is {@code int[1] holder; ref = v.get(holder); }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param stampHolder an array of size of at least one.  On return,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * {@code stampholder[0]} will hold the value of the stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return the current value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public V get(int[] stampHolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ReferenceIntegerPair<V> p = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        stampHolder[0] = p.integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return p.reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Atomically sets the value of both the reference and stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * to the given update values if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * current reference is {@code ==} to the expected reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * and the current stamp is equal to the expected stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param expectedStamp the expected value of the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param newStamp the new value for the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public boolean weakCompareAndSet(V      expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                     V      newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                     int    expectedStamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                     int    newStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ReferenceIntegerPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            expectedStamp == current.integer &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            ((newReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
              newStamp == current.integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             atomicRef.weakCompareAndSet(current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                     new ReferenceIntegerPair<V>(newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                              newStamp)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Atomically sets the value of both the reference and stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * to the given update values if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * current reference is {@code ==} to the expected reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * and the current stamp is equal to the expected stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param expectedStamp the expected value of the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param newStamp the new value for the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean compareAndSet(V      expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                 V      newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                 int    expectedStamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                 int    newStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ReferenceIntegerPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            expectedStamp == current.integer &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ((newReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
              newStamp == current.integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
             atomicRef.compareAndSet(current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                     new ReferenceIntegerPair<V>(newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                                              newStamp)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Unconditionally sets the value of both the reference and stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param newStamp the new value for the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void set(V newReference, int newStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ReferenceIntegerPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (newReference != current.reference || newStamp != current.integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            atomicRef.set(new ReferenceIntegerPair<V>(newReference, newStamp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Atomically sets the value of the stamp to the given update value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * if the current reference is {@code ==} to the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * reference.  Any given invocation of this operation may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * (return {@code false}) spuriously, but repeated invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * when the current value holds the expected value and no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * thread is also attempting to set the value will eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param newStamp the new value for the stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public boolean attemptStamp(V expectedReference, int newStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ReferenceIntegerPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            (newStamp == current.integer ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             atomicRef.compareAndSet(current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                     new ReferenceIntegerPair<V>(expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                                              newStamp)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}