jdk/src/share/classes/java/util/concurrent/atomic/AtomicMarkableReference.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7976 f273c0d04215
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, 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
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 AtomicMarkableReference} maintains an object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * along with a mark bit, that can be updated atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> Implementation note. This implementation maintains markable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * references by creating internal objects representing "boxed"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * [reference, boolean] 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 AtomicMarkableReference<V>  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static class ReferenceBooleanPair<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        private final T reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        private final boolean bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ReferenceBooleanPair(T r, boolean i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            reference = r; bit = 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<ReferenceBooleanPair<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 AtomicMarkableReference} 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 initialMark the initial mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public AtomicMarkableReference(V initialRef, boolean initialMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        atomicRef = new AtomicReference<ReferenceBooleanPair<V>> (new ReferenceBooleanPair<V>(initialRef, initialMark));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Returns the current value of the reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return the current value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public V getReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return atomicRef.get().reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the current value of the mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return the current value of the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public boolean isMarked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return atomicRef.get().bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns the current values of both the reference and the mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Typical usage is {@code boolean[1] holder; ref = v.get(holder); }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param markHolder an array of size of at least one. On return,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * {@code markholder[0]} will hold the value of the mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return the current value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public V get(boolean[] markHolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ReferenceBooleanPair<V> p = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        markHolder[0] = p.bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return p.reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Atomically sets the value of both the reference and mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * to the given update values if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * current reference is {@code ==} to the expected reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * and the current mark is equal to the expected mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * and does not provide ordering guarantees, so is only rarely an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * appropriate alternative to {@code compareAndSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param expectedMark the expected value of the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param newMark the new value for the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public boolean weakCompareAndSet(V       expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                     V       newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                     boolean expectedMark,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                     boolean newMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ReferenceBooleanPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            expectedMark == current.bit &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ((newReference == current.reference && newMark == current.bit) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             atomicRef.weakCompareAndSet(current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                     new ReferenceBooleanPair<V>(newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                              newMark)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Atomically sets the value of both the reference and mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * to the given update values if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * current reference is {@code ==} to the expected reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * and the current mark is equal to the expected mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param expectedMark the expected value of the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param newMark the new value for the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public boolean compareAndSet(V       expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                 V       newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                 boolean expectedMark,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                 boolean newMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ReferenceBooleanPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            expectedMark == current.bit &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            ((newReference == current.reference && newMark == current.bit) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             atomicRef.compareAndSet(current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                     new ReferenceBooleanPair<V>(newReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                                              newMark)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Unconditionally sets the value of both the reference and mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param newReference the new value for the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param newMark the new value for the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void set(V newReference, boolean newMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ReferenceBooleanPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (newReference != current.reference || newMark != current.bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            atomicRef.set(new ReferenceBooleanPair<V>(newReference, newMark));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Atomically sets the value of the mark to the given update value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * if the current reference is {@code ==} to the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * reference.  Any given invocation of this operation may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * (return {@code false}) spuriously, but repeated invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * when the current value holds the expected value and no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * thread is also attempting to set the value will eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param expectedReference the expected value of the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param newMark the new value for the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return true if successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public boolean attemptMark(V expectedReference, boolean newMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ReferenceBooleanPair<V> current = atomicRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return  expectedReference == current.reference &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            (newMark == current.bit ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             atomicRef.compareAndSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             (current, new ReferenceBooleanPair<V>(expectedReference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                newMark)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}