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 /**
39  * A {@code boolean} value that may be updated atomically. See the
40  * {@link java.util.concurrent.atomic} package specification for
41  * description of the properties of atomic variables. An
42  * {@code AtomicBoolean} is used in applications such as atomically
43  * updated flags, and cannot be used as a replacement for a
44  * {@link java.lang.Boolean}.
45  *
46  * @since 1.5
47  * @author Doug Lea
48  */
49 public class AtomicBoolean implements java.io.Serializable {
50     private static final long serialVersionUID = 4654671469794556979L;
51 
52     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
53     private static final long VALUE;
54 
55     static {
56         try {
57             VALUE = U.objectFieldOffset
58                 (AtomicBoolean.class.getDeclaredField("value"));
59         } catch (ReflectiveOperationException e) {
60             throw new Error(e);
61         }
62     }
63 
64     private volatile int value;
65 
66     /**
67      * Creates a new {@code AtomicBoolean} with the given initial value.
68      *
69      * @param initialValue the initial value
70      */
AtomicBoolean(boolean initialValue)71     public AtomicBoolean(boolean initialValue) {
72         value = initialValue ? 1 : 0;
73     }
74 
75     /**
76      * Creates a new {@code AtomicBoolean} with initial value {@code false}.
77      */
AtomicBoolean()78     public AtomicBoolean() {
79     }
80 
81     /**
82      * Returns the current value.
83      *
84      * @return the current value
85      */
get()86     public final boolean get() {
87         return value != 0;
88     }
89 
90     /**
91      * Atomically sets the value to the given updated value
92      * if the current value {@code ==} the expected value.
93      *
94      * @param expect the expected value
95      * @param update the new value
96      * @return {@code true} if successful. False return indicates that
97      * the actual value was not equal to the expected value.
98      */
compareAndSet(boolean expect, boolean update)99     public final boolean compareAndSet(boolean expect, boolean update) {
100         return U.compareAndSwapInt(this, VALUE,
101                                    (expect ? 1 : 0),
102                                    (update ? 1 : 0));
103     }
104 
105     /**
106      * Atomically sets the value to the given updated value
107      * if the current value {@code ==} the expected value.
108      *
109      * <p><a href="package-summary.html#weakCompareAndSet">May fail
110      * spuriously and does not provide ordering guarantees</a>, so is
111      * only rarely an appropriate alternative to {@code compareAndSet}.
112      *
113      * @param expect the expected value
114      * @param update the new value
115      * @return {@code true} if successful
116      */
weakCompareAndSet(boolean expect, boolean update)117     public boolean weakCompareAndSet(boolean expect, boolean update) {
118         return U.compareAndSwapInt(this, VALUE,
119                                    (expect ? 1 : 0),
120                                    (update ? 1 : 0));
121     }
122 
123     /**
124      * Unconditionally sets to the given value.
125      *
126      * @param newValue the new value
127      */
set(boolean newValue)128     public final void set(boolean newValue) {
129         value = newValue ? 1 : 0;
130     }
131 
132     /**
133      * Eventually sets to the given value.
134      *
135      * @param newValue the new value
136      * @since 1.6
137      */
lazySet(boolean newValue)138     public final void lazySet(boolean newValue) {
139         U.putOrderedInt(this, VALUE, (newValue ? 1 : 0));
140     }
141 
142     /**
143      * Atomically sets to the given value and returns the previous value.
144      *
145      * @param newValue the new value
146      * @return the previous value
147      */
getAndSet(boolean newValue)148     public final boolean getAndSet(boolean newValue) {
149         boolean prev;
150         do {
151             prev = get();
152         } while (!compareAndSet(prev, newValue));
153         return prev;
154     }
155 
156     /**
157      * Returns the String representation of the current value.
158      * @return the String representation of the current value
159      */
toString()160     public String toString() {
161         return Boolean.toString(get());
162     }
163 
164 }
165