• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * This file is available under and governed by the GNU General Public
27  * License version 2 only, as published by the Free Software Foundation.
28  * However, the following notice accompanied the original version of this
29  * file:
30  *
31  * Written by Doug Lea with assistance from members of JCP JSR-166
32  * Expert Group and released to the public domain, as explained at
33  * http://creativecommons.org/publicdomain/zero/1.0/
34  */
35 
36 package java.util.concurrent.atomic;
37 
38 import java.lang.invoke.MethodHandles;
39 import java.lang.invoke.VarHandle;
40 
41 /**
42  * An {@code AtomicMarkableReference} maintains an object reference
43  * along with a mark bit, that can be updated atomically.
44  *
45  * <p>Implementation note: This implementation maintains markable
46  * references by creating internal objects representing "boxed"
47  * [reference, boolean] pairs.
48  *
49  * @since 1.5
50  * @author Doug Lea
51  * @param <V> The type of object referred to by this reference
52  */
53 public class AtomicMarkableReference<V> {
54 
55     private static class Pair<T> {
56         final T reference;
57         final boolean mark;
Pair(T reference, boolean mark)58         private Pair(T reference, boolean mark) {
59             this.reference = reference;
60             this.mark = mark;
61         }
of(T reference, boolean mark)62         static <T> Pair<T> of(T reference, boolean mark) {
63             return new Pair<T>(reference, mark);
64         }
65     }
66 
67     private volatile Pair<V> pair;
68 
69     /**
70      * Creates a new {@code AtomicMarkableReference} with the given
71      * initial values.
72      *
73      * @param initialRef the initial reference
74      * @param initialMark the initial mark
75      */
AtomicMarkableReference(V initialRef, boolean initialMark)76     public AtomicMarkableReference(V initialRef, boolean initialMark) {
77         pair = Pair.of(initialRef, initialMark);
78     }
79 
80     /**
81      * Returns the current value of the reference.
82      *
83      * @return the current value of the reference
84      */
getReference()85     public V getReference() {
86         return pair.reference;
87     }
88 
89     /**
90      * Returns the current value of the mark.
91      *
92      * @return the current value of the mark
93      */
isMarked()94     public boolean isMarked() {
95         return pair.mark;
96     }
97 
98     /**
99      * Returns the current values of both the reference and the mark.
100      * Typical usage is {@code boolean[1] holder; ref = v.get(holder); }.
101      *
102      * @param markHolder an array of size of at least one. On return,
103      * {@code markHolder[0]} will hold the value of the mark.
104      * @return the current value of the reference
105      */
get(boolean[] markHolder)106     public V get(boolean[] markHolder) {
107         Pair<V> pair = this.pair;
108         markHolder[0] = pair.mark;
109         return pair.reference;
110     }
111 
112     /**
113      * Atomically sets the value of both the reference and mark
114      * to the given update values if the
115      * current reference is {@code ==} to the expected reference
116      * and the current mark is equal to the expected mark.
117      *
118      * <p><a href="package-summary.html#weakCompareAndSet">May fail
119      * spuriously and does not provide ordering guarantees</a>, so is
120      * only rarely an appropriate alternative to {@code compareAndSet}.
121      *
122      * @param expectedReference the expected value of the reference
123      * @param newReference the new value for the reference
124      * @param expectedMark the expected value of the mark
125      * @param newMark the new value for the mark
126      * @return {@code true} if successful
127      */
weakCompareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)128     public boolean weakCompareAndSet(V       expectedReference,
129                                      V       newReference,
130                                      boolean expectedMark,
131                                      boolean newMark) {
132         return compareAndSet(expectedReference, newReference,
133                              expectedMark, newMark);
134     }
135 
136     /**
137      * Atomically sets the value of both the reference and mark
138      * to the given update values if the
139      * current reference is {@code ==} to the expected reference
140      * and the current mark is equal to the expected mark.
141      *
142      * @param expectedReference the expected value of the reference
143      * @param newReference the new value for the reference
144      * @param expectedMark the expected value of the mark
145      * @param newMark the new value for the mark
146      * @return {@code true} if successful
147      */
compareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)148     public boolean compareAndSet(V       expectedReference,
149                                  V       newReference,
150                                  boolean expectedMark,
151                                  boolean newMark) {
152         Pair<V> current = pair;
153         return
154             expectedReference == current.reference &&
155             expectedMark == current.mark &&
156             ((newReference == current.reference &&
157               newMark == current.mark) ||
158              casPair(current, Pair.of(newReference, newMark)));
159     }
160 
161     /**
162      * Unconditionally sets the value of both the reference and mark.
163      *
164      * @param newReference the new value for the reference
165      * @param newMark the new value for the mark
166      */
set(V newReference, boolean newMark)167     public void set(V newReference, boolean newMark) {
168         Pair<V> current = pair;
169         if (newReference != current.reference || newMark != current.mark)
170             this.pair = Pair.of(newReference, newMark);
171     }
172 
173     /**
174      * Atomically sets the value of the mark to the given update value
175      * if the current reference is {@code ==} to the expected
176      * reference.  Any given invocation of this operation may fail
177      * (return {@code false}) spuriously, but repeated invocation
178      * when the current value holds the expected value and no other
179      * thread is also attempting to set the value will eventually
180      * succeed.
181      *
182      * @param expectedReference the expected value of the reference
183      * @param newMark the new value for the mark
184      * @return {@code true} if successful
185      */
attemptMark(V expectedReference, boolean newMark)186     public boolean attemptMark(V expectedReference, boolean newMark) {
187         Pair<V> current = pair;
188         return
189             expectedReference == current.reference &&
190             (newMark == current.mark ||
191              casPair(current, Pair.of(expectedReference, newMark)));
192     }
193 
194     // VarHandle mechanics
195     private static final VarHandle PAIR;
196     static {
197         try {
198             MethodHandles.Lookup l = MethodHandles.lookup();
199             PAIR = l.findVarHandle(AtomicMarkableReference.class, "pair",
200                                    Pair.class);
201         } catch (ReflectiveOperationException e) {
202             throw new ExceptionInInitializerError(e);
203         }
204     }
205 
casPair(Pair<V> cmp, Pair<V> val)206     private boolean casPair(Pair<V> cmp, Pair<V> val) {
207         return PAIR.compareAndSet(this, cmp, val);
208     }
209 }
210