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 import sun.misc.Unsafe;
38
39 /**
40 * An object reference that may be updated atomically. See the {@link
41 * java.util.concurrent.atomic} package specification for description
42 * of the properties of atomic variables.
43 * @since 1.5
44 * @author Doug Lea
45 * @param <V> The type of object referred to by this reference
46 */
47 public class AtomicReference<V> implements java.io.Serializable {
48 private static final long serialVersionUID = -1848883965231344442L;
49
50 private static final Unsafe unsafe = Unsafe.getUnsafe();
51 private static final long valueOffset;
52
53 static {
54 try {
55 valueOffset = unsafe.objectFieldOffset
56 (AtomicReference.class.getDeclaredField("value"));
97 *
98 * @param newValue the new value
99 * @since 1.6
100 */
101 public final void lazySet(V newValue) {
102 unsafe.putOrderedObject(this, valueOffset, newValue);
103 }
104
105 /**
106 * Atomically sets the value to the given updated value
107 * if the current value {@code ==} the expected value.
108 * @param expect the expected value
109 * @param update the new value
110 * @return true if successful. False return indicates that
111 * the actual value was not equal to the expected value.
112 */
113 public final boolean compareAndSet(V expect, V update) {
114 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
115 }
116
117 /**
118 * Atomically sets the value to the given updated value
119 * if the current value {@code ==} the expected value.
120 *
121 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
122 * and does not provide ordering guarantees, so is only rarely an
123 * appropriate alternative to {@code compareAndSet}.
124 *
125 * @param expect the expected value
126 * @param update the new value
127 * @return true if successful.
128 */
129 public final boolean weakCompareAndSet(V expect, V update) {
130 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
131 }
132
133 /**
134 * Atomically sets to the given value and returns the old value.
135 *
136 * @param newValue the new value
|
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 import java.util.functions.UnaryOperator;
38 import sun.misc.Unsafe;
39
40 /**
41 * An object reference that may be updated atomically. See the {@link
42 * java.util.concurrent.atomic} package specification for description
43 * of the properties of atomic variables.
44 * @since 1.5
45 * @author Doug Lea
46 * @param <V> The type of object referred to by this reference
47 */
48 public class AtomicReference<V> implements java.io.Serializable {
49 private static final long serialVersionUID = -1848883965231344442L;
50
51 private static final Unsafe unsafe = Unsafe.getUnsafe();
52 private static final long valueOffset;
53
54 static {
55 try {
56 valueOffset = unsafe.objectFieldOffset
57 (AtomicReference.class.getDeclaredField("value"));
98 *
99 * @param newValue the new value
100 * @since 1.6
101 */
102 public final void lazySet(V newValue) {
103 unsafe.putOrderedObject(this, valueOffset, newValue);
104 }
105
106 /**
107 * Atomically sets the value to the given updated value
108 * if the current value {@code ==} the expected value.
109 * @param expect the expected value
110 * @param update the new value
111 * @return true if successful. False return indicates that
112 * the actual value was not equal to the expected value.
113 */
114 public final boolean compareAndSet(V expect, V update) {
115 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
116 }
117
118 /**
119 * Sets the value by applying the supplied operator to the old value,
120 * and updating it atomically as in {@link #compareAndSet(V, V)}.
121 *
122 * If the update results in a collision, we back off and retry the update
123 * (using the new current value) until successful.
124 *
125 * @param op the operator to use to compute the new value
126 * @return the new value
127 */
128 public final V updateAndGet(UnaryOperator<V> op) {
129 V oldValue, newValue;
130 do {
131 oldValue = get();
132 newValue = op.operate(oldValue);
133 } while (!compareAndSet(oldValue, newValue));
134 return newValue;
135 }
136
137 /**
138 * Sets the value by applying the supplied operator to the old value,
139 * and updating it atomically as in {@link #compareAndSet(V, V)}.
140 *
141 * If the update results in a collision, we back off and retry the update
142 * (using the new current value) until successful.
143 *
144 * @param op the operator to use to compute the new value
145 * @return the old value
146 */
147 public final V getAndUpdate(UnaryOperator<V> op) {
148 V oldValue, newValue;
149 do {
150 oldValue = get();
151 newValue = op.operate(oldValue);
152 } while (!compareAndSet(oldValue, newValue));
153 return oldValue;
154 }
155
156 /**
157 * Atomically sets the value to the given updated value
158 * if the current value {@code ==} the expected value.
159 *
160 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
161 * and does not provide ordering guarantees, so is only rarely an
162 * appropriate alternative to {@code compareAndSet}.
163 *
164 * @param expect the expected value
165 * @param update the new value
166 * @return true if successful.
167 */
168 public final boolean weakCompareAndSet(V expect, V update) {
169 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
170 }
171
172 /**
173 * Atomically sets to the given value and returns the old value.
174 *
175 * @param newValue the new value
|