author | dl |
Thu, 20 Dec 2012 13:44:06 +0000 | |
changeset 14914 | 69bfd88e3ea1 |
parent 9242 | ef138d47df58 |
child 18576 | 7a5c231327af |
permissions | -rw-r--r-- |
2 | 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 |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7976
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent.atomic; |
|
37 |
||
38 |
/** |
|
39 |
* An {@code AtomicMarkableReference} maintains an object reference |
|
40 |
* along with a mark bit, that can be updated atomically. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
41 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
42 |
* <p>Implementation note: This implementation maintains markable |
2 | 43 |
* references by creating internal objects representing "boxed" |
44 |
* [reference, boolean] pairs. |
|
45 |
* |
|
46 |
* @since 1.5 |
|
47 |
* @author Doug Lea |
|
48 |
* @param <V> The type of object referred to by this reference |
|
49 |
*/ |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
50 |
public class AtomicMarkableReference<V> { |
2 | 51 |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
52 |
private static class Pair<T> { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
53 |
final T reference; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
54 |
final boolean mark; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
55 |
private Pair(T reference, boolean mark) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
56 |
this.reference = reference; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
57 |
this.mark = mark; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
58 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
59 |
static <T> Pair<T> of(T reference, boolean mark) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
60 |
return new Pair<T>(reference, mark); |
2 | 61 |
} |
62 |
} |
|
63 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
64 |
private volatile Pair<V> pair; |
2 | 65 |
|
66 |
/** |
|
67 |
* Creates a new {@code AtomicMarkableReference} with the given |
|
68 |
* initial values. |
|
69 |
* |
|
70 |
* @param initialRef the initial reference |
|
71 |
* @param initialMark the initial mark |
|
72 |
*/ |
|
73 |
public AtomicMarkableReference(V initialRef, boolean initialMark) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
74 |
pair = Pair.of(initialRef, initialMark); |
2 | 75 |
} |
76 |
||
77 |
/** |
|
78 |
* Returns the current value of the reference. |
|
79 |
* |
|
80 |
* @return the current value of the reference |
|
81 |
*/ |
|
82 |
public V getReference() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
83 |
return pair.reference; |
2 | 84 |
} |
85 |
||
86 |
/** |
|
87 |
* Returns the current value of the mark. |
|
88 |
* |
|
89 |
* @return the current value of the mark |
|
90 |
*/ |
|
91 |
public boolean isMarked() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
92 |
return pair.mark; |
2 | 93 |
} |
94 |
||
95 |
/** |
|
96 |
* Returns the current values of both the reference and the mark. |
|
97 |
* Typical usage is {@code boolean[1] holder; ref = v.get(holder); }. |
|
98 |
* |
|
99 |
* @param markHolder an array of size of at least one. On return, |
|
100 |
* {@code markholder[0]} will hold the value of the mark. |
|
101 |
* @return the current value of the reference |
|
102 |
*/ |
|
103 |
public V get(boolean[] markHolder) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
104 |
Pair<V> pair = this.pair; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
105 |
markHolder[0] = pair.mark; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
106 |
return pair.reference; |
2 | 107 |
} |
108 |
||
109 |
/** |
|
110 |
* Atomically sets the value of both the reference and mark |
|
111 |
* to the given update values if the |
|
112 |
* current reference is {@code ==} to the expected reference |
|
113 |
* and the current mark is equal to the expected mark. |
|
114 |
* |
|
115 |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
|
116 |
* and does not provide ordering guarantees, so is only rarely an |
|
117 |
* appropriate alternative to {@code compareAndSet}. |
|
118 |
* |
|
119 |
* @param expectedReference the expected value of the reference |
|
120 |
* @param newReference the new value for the reference |
|
121 |
* @param expectedMark the expected value of the mark |
|
122 |
* @param newMark the new value for the mark |
|
123 |
* @return true if successful |
|
124 |
*/ |
|
125 |
public boolean weakCompareAndSet(V expectedReference, |
|
126 |
V newReference, |
|
127 |
boolean expectedMark, |
|
128 |
boolean newMark) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
129 |
return compareAndSet(expectedReference, newReference, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
130 |
expectedMark, newMark); |
2 | 131 |
} |
132 |
||
133 |
/** |
|
134 |
* Atomically sets the value of both the reference and mark |
|
135 |
* to the given update values if the |
|
136 |
* current reference is {@code ==} to the expected reference |
|
137 |
* and the current mark is equal to the expected mark. |
|
138 |
* |
|
139 |
* @param expectedReference the expected value of the reference |
|
140 |
* @param newReference the new value for the reference |
|
141 |
* @param expectedMark the expected value of the mark |
|
142 |
* @param newMark the new value for the mark |
|
143 |
* @return true if successful |
|
144 |
*/ |
|
145 |
public boolean compareAndSet(V expectedReference, |
|
146 |
V newReference, |
|
147 |
boolean expectedMark, |
|
148 |
boolean newMark) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
149 |
Pair<V> current = pair; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
150 |
return |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
151 |
expectedReference == current.reference && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
152 |
expectedMark == current.mark && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
153 |
((newReference == current.reference && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
154 |
newMark == current.mark) || |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
155 |
casPair(current, Pair.of(newReference, newMark))); |
2 | 156 |
} |
157 |
||
158 |
/** |
|
159 |
* Unconditionally sets the value of both the reference and mark. |
|
160 |
* |
|
161 |
* @param newReference the new value for the reference |
|
162 |
* @param newMark the new value for the mark |
|
163 |
*/ |
|
164 |
public void set(V newReference, boolean newMark) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
165 |
Pair<V> current = pair; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
166 |
if (newReference != current.reference || newMark != current.mark) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
167 |
this.pair = Pair.of(newReference, newMark); |
2 | 168 |
} |
169 |
||
170 |
/** |
|
171 |
* Atomically sets the value of the mark to the given update value |
|
172 |
* if the current reference is {@code ==} to the expected |
|
173 |
* reference. Any given invocation of this operation may fail |
|
174 |
* (return {@code false}) spuriously, but repeated invocation |
|
175 |
* when the current value holds the expected value and no other |
|
176 |
* thread is also attempting to set the value will eventually |
|
177 |
* succeed. |
|
178 |
* |
|
179 |
* @param expectedReference the expected value of the reference |
|
180 |
* @param newMark the new value for the mark |
|
181 |
* @return true if successful |
|
182 |
*/ |
|
183 |
public boolean attemptMark(V expectedReference, boolean newMark) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
184 |
Pair<V> current = pair; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
185 |
return |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
186 |
expectedReference == current.reference && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
187 |
(newMark == current.mark || |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
188 |
casPair(current, Pair.of(expectedReference, newMark))); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
189 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
190 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
191 |
// Unsafe mechanics |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
192 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
193 |
private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
194 |
private static final long pairOffset = |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
195 |
objectFieldOffset(UNSAFE, "pair", AtomicMarkableReference.class); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
196 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
197 |
private boolean casPair(Pair<V> cmp, Pair<V> val) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
198 |
return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
199 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
200 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
201 |
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
202 |
String field, Class<?> klazz) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
203 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
204 |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
205 |
} catch (NoSuchFieldException e) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
206 |
// Convert Exception to corresponding Error |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
207 |
NoSuchFieldError error = new NoSuchFieldError(field); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
208 |
error.initCause(e); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
209 |
throw error; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
210 |
} |
2 | 211 |
} |
212 |
} |