2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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
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/
36 package java.util.concurrent.atomic;
37 import sun.misc.Unsafe;
40 * A {@code long} value that may be updated atomically. See the
41 * {@link java.util.concurrent.atomic} package specification for
42 * description of the properties of atomic variables. An
43 * {@code AtomicLong} is used in applications such as atomically
44 * incremented sequence numbers, and cannot be used as a replacement
45 * for a {@link java.lang.Long}. However, this class does extend
46 * {@code Number} to allow uniform access by tools and utilities that
47 * deal with numerically-based classes.
52 public class AtomicLong extends Number implements java.io.Serializable {
53 private static final long serialVersionUID = 1927816293512124184L;
55 // setup to use Unsafe.compareAndSwapLong for updates
56 private static final Unsafe unsafe = Unsafe.getUnsafe();
57 private static final long valueOffset;
60 * Records whether the underlying JVM supports lockless
61 * compareAndSwap for longs. While the Unsafe.compareAndSwapLong
62 * method works in either case, some constructions should be
63 * handled at Java level to avoid locking user-visible locks.
65 static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
68 * Returns whether underlying JVM supports lockless CompareAndSet
69 * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
71 private static native boolean VMSupportsCS8();
75 valueOffset = unsafe.objectFieldOffset
76 (AtomicLong.class.getDeclaredField("value"));
77 } catch (Exception ex) { throw new Error(ex); }
80 private volatile long value;
83 * Creates a new AtomicLong with the given initial value.
85 * @param initialValue the initial value
87 public AtomicLong(long initialValue) {
92 * Creates a new AtomicLong with initial value {@code 0}.
98 * Gets the current value.
100 * @return the current value
102 public final long get() {
107 * Sets to the given value.
109 * @param newValue the new value
111 public final void set(long newValue) {
116 * Eventually sets to the given value.
118 * @param newValue the new value
121 public final void lazySet(long newValue) {
122 unsafe.putOrderedLong(this, valueOffset, newValue);
126 * Atomically sets to the given value and returns the old value.
128 * @param newValue the new value
129 * @return the previous value
131 public final long getAndSet(long newValue) {
133 long current = get();
134 if (compareAndSet(current, newValue))
140 * Atomically sets the value to the given updated value
141 * if the current value {@code ==} the expected value.
143 * @param expect the expected value
144 * @param update the new value
145 * @return true if successful. False return indicates that
146 * the actual value was not equal to the expected value.
148 public final boolean compareAndSet(long expect, long update) {
149 return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
153 * Atomically sets the value to the given updated value
154 * if the current value {@code ==} the expected value.
156 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
157 * and does not provide ordering guarantees, so is only rarely an
158 * appropriate alternative to {@code compareAndSet}.
160 * @param expect the expected value
161 * @param update the new value
162 * @return true if successful.
164 public final boolean weakCompareAndSet(long expect, long update) {
165 return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
169 * Atomically increments by one the current value.
171 * @return the previous value
173 public final long getAndIncrement() {
175 long current = get();
176 long next = current + 1;
177 if (compareAndSet(current, next))
183 * Atomically decrements by one the current value.
185 * @return the previous value
187 public final long getAndDecrement() {
189 long current = get();
190 long next = current - 1;
191 if (compareAndSet(current, next))
197 * Atomically adds the given value to the current value.
199 * @param delta the value to add
200 * @return the previous value
202 public final long getAndAdd(long delta) {
204 long current = get();
205 long next = current + delta;
206 if (compareAndSet(current, next))
212 * Atomically increments by one the current value.
214 * @return the updated value
216 public final long incrementAndGet() {
218 long current = get();
219 long next = current + 1;
220 if (compareAndSet(current, next))
226 * Atomically decrements by one the current value.
228 * @return the updated value
230 public final long decrementAndGet() {
232 long current = get();
233 long next = current - 1;
234 if (compareAndSet(current, next))
240 * Atomically adds the given value to the current value.
242 * @param delta the value to add
243 * @return the updated value
245 public final long addAndGet(long delta) {
247 long current = get();
248 long next = current + delta;
249 if (compareAndSet(current, next))
255 * Returns the String representation of the current value.
256 * @return the String representation of the current value.
258 public String toString() {
259 return Long.toString(get());
263 public int intValue() {
267 public long longValue() {
271 public float floatValue() {
275 public double doubleValue() {
276 return (double)get();