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.VarHandle;
39 import java.util.function.LongBinaryOperator;
40 import java.util.function.LongUnaryOperator;
41
42 /**
43 * A {@code long} value that may be updated atomically. See the
44 * {@link VarHandle} specification for descriptions of the properties
45 * of atomic accesses. An {@code AtomicLong} is used in applications
46 * such as atomically incremented sequence numbers, and cannot be used
47 * as a replacement for a {@link java.lang.Long}. However, this class
48 * does extend {@code Number} to allow uniform access by tools and
49 * utilities that deal with numerically-based classes.
50 *
51 * @since 1.5
52 * @author Doug Lea
53 */
54 public class AtomicLong extends Number implements java.io.Serializable {
55 private static final long serialVersionUID = 1927816293512124184L;
56
57 /**
58 * Records whether the underlying JVM supports lockless
59 * compareAndSet for longs. While the intrinsic compareAndSetLong
60 * method works in either case, some constructions should be
61 * handled at Java level to avoid locking user-visible locks.
62 */
63 static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
64
65 /**
66 * Returns whether underlying JVM supports lockless CompareAndSet
67 * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
68 */
69 private static native boolean VMSupportsCS8();
70
71 /*
72 * This class intended to be implemented using VarHandles, but there
73 * are unresolved cyclic startup dependencies.
74 */
75 private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
76 private static final long VALUE = U.objectFieldOffset(AtomicLong.class, "value");
77
78 private volatile long value;
79
80 /**
81 * Creates a new AtomicLong with the given initial value.
82 *
83 * @param initialValue the initial value
84 */
85 public AtomicLong(long initialValue) {
86 value = initialValue;
87 }
88
89 /**
90 * Creates a new AtomicLong with initial value {@code 0}.
91 */
92 public AtomicLong() {
93 }
94
95 /**
96 * Returns the current value,
|
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.VarHandle;
39 import java.util.function.LongBinaryOperator;
40 import java.util.function.LongUnaryOperator;
41 import jdk.internal.misc.Unsafe;
42
43 /**
44 * A {@code long} value that may be updated atomically. See the
45 * {@link VarHandle} specification for descriptions of the properties
46 * of atomic accesses. An {@code AtomicLong} is used in applications
47 * such as atomically incremented sequence numbers, and cannot be used
48 * as a replacement for a {@link java.lang.Long}. However, this class
49 * does extend {@code Number} to allow uniform access by tools and
50 * utilities that deal with numerically-based classes.
51 *
52 * @since 1.5
53 * @author Doug Lea
54 */
55 public class AtomicLong extends Number implements java.io.Serializable {
56 private static final long serialVersionUID = 1927816293512124184L;
57
58 /**
59 * Records whether the underlying JVM supports lockless
60 * compareAndSet for longs. While the intrinsic compareAndSetLong
61 * method works in either case, some constructions should be
62 * handled at Java level to avoid locking user-visible locks.
63 */
64 static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
65
66 /**
67 * Returns whether underlying JVM supports lockless CompareAndSet
68 * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
69 */
70 private static native boolean VMSupportsCS8();
71
72 /*
73 * This class intended to be implemented using VarHandles, but there
74 * are unresolved cyclic startup dependencies.
75 */
76 private static final Unsafe U = Unsafe.getUnsafe();
77 private static final long VALUE
78 = U.objectFieldOffset(AtomicLong.class, "value");
79
80 private volatile long value;
81
82 /**
83 * Creates a new AtomicLong with the given initial value.
84 *
85 * @param initialValue the initial value
86 */
87 public AtomicLong(long initialValue) {
88 value = initialValue;
89 }
90
91 /**
92 * Creates a new AtomicLong with initial value {@code 0}.
93 */
94 public AtomicLong() {
95 }
96
97 /**
98 * Returns the current value,
|