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 import java.lang.invoke.MethodHandles; 39 import java.lang.invoke.VarHandle; 40 41 /** 42 * A {@code boolean} value that may be updated atomically. See the 43 * {@link VarHandle} specification for descriptions of the properties 44 * of atomic accesses. An {@code AtomicBoolean} is used in 45 * applications such as atomically updated flags, and cannot be used 46 * as a replacement for a {@link java.lang.Boolean}. 47 * 48 * @since 1.5 49 * @author Doug Lea 50 */ 51 public class AtomicBoolean implements java.io.Serializable { 52 private static final long serialVersionUID = 4654671469794556979L; 53 private static final VarHandle VALUE; 54 static { 55 try { 56 MethodHandles.Lookup l = MethodHandles.lookup(); 57 VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class); 58 } catch (ReflectiveOperationException e) { 59 throw new ExceptionInInitializerError(e); 60 } 61 } 62 63 private volatile int value; 64 65 /** 66 * Creates a new {@code AtomicBoolean} with the given initial value. 67 * 68 * @param initialValue the initial value 69 */ AtomicBoolean(boolean initialValue)70 public AtomicBoolean(boolean initialValue) { 71 if (initialValue) 72 value = 1; 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 * with memory effects as specified by {@link VarHandle#getVolatile}. 84 * 85 * @return the current value 86 */ get()87 public final boolean get() { 88 return value != 0; 89 } 90 91 /** 92 * Atomically sets the value to {@code newValue} 93 * if the current value {@code == expectedValue}, 94 * with memory effects as specified by {@link VarHandle#compareAndSet}. 95 * 96 * @param expectedValue the expected value 97 * @param newValue the new value 98 * @return {@code true} if successful. False return indicates that 99 * the actual value was not equal to the expected value. 100 */ compareAndSet(boolean expectedValue, boolean newValue)101 public final boolean compareAndSet(boolean expectedValue, boolean newValue) { 102 return VALUE.compareAndSet(this, 103 (expectedValue ? 1 : 0), 104 (newValue ? 1 : 0)); 105 } 106 107 /** 108 * Possibly atomically sets the value to {@code newValue} 109 * if the current value {@code == expectedValue}, 110 * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. 111 * 112 * @deprecated This method has plain memory effects but the method 113 * name implies volatile memory effects (see methods such as 114 * {@link #compareAndExchange} and {@link #compareAndSet}). To avoid 115 * confusion over plain or volatile memory effects it is recommended that 116 * the method {@link #weakCompareAndSetPlain} be used instead. 117 * 118 * @param expectedValue the expected value 119 * @param newValue the new value 120 * @return {@code true} if successful 121 * @see #weakCompareAndSetPlain 122 */ 123 @Deprecated(since="9") weakCompareAndSet(boolean expectedValue, boolean newValue)124 public boolean weakCompareAndSet(boolean expectedValue, boolean newValue) { 125 return VALUE.weakCompareAndSetPlain(this, 126 (expectedValue ? 1 : 0), 127 (newValue ? 1 : 0)); 128 } 129 130 /** 131 * Possibly atomically sets the value to {@code newValue} 132 * if the current value {@code == expectedValue}, 133 * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. 134 * 135 * @param expectedValue the expected value 136 * @param newValue the new value 137 * @return {@code true} if successful 138 * @since 9 139 */ weakCompareAndSetPlain(boolean expectedValue, boolean newValue)140 public boolean weakCompareAndSetPlain(boolean expectedValue, boolean newValue) { 141 return VALUE.weakCompareAndSetPlain(this, 142 (expectedValue ? 1 : 0), 143 (newValue ? 1 : 0)); 144 } 145 146 /** 147 * Sets the value to {@code newValue}, 148 * with memory effects as specified by {@link VarHandle#setVolatile}. 149 * 150 * @param newValue the new value 151 */ set(boolean newValue)152 public final void set(boolean newValue) { 153 value = newValue ? 1 : 0; 154 } 155 156 /** 157 * Sets the value to {@code newValue}, 158 * with memory effects as specified by {@link VarHandle#setRelease}. 159 * 160 * @param newValue the new value 161 * @since 1.6 162 */ lazySet(boolean newValue)163 public final void lazySet(boolean newValue) { 164 VALUE.setRelease(this, (newValue ? 1 : 0)); 165 } 166 167 /** 168 * Atomically sets the value to {@code newValue} and returns the old value, 169 * with memory effects as specified by {@link VarHandle#getAndSet}. 170 * 171 * @param newValue the new value 172 * @return the previous value 173 */ getAndSet(boolean newValue)174 public final boolean getAndSet(boolean newValue) { 175 return (int)VALUE.getAndSet(this, (newValue ? 1 : 0)) != 0; 176 } 177 178 /** 179 * Returns the String representation of the current value. 180 * @return the String representation of the current value 181 */ toString()182 public String toString() { 183 return Boolean.toString(get()); 184 } 185 186 // jdk9 187 188 /** 189 * Returns the current value, with memory semantics of reading as 190 * if the variable was declared non-{@code volatile}. 191 * 192 * @return the value 193 * @since 9 194 */ getPlain()195 public final boolean getPlain() { 196 return (int)VALUE.get(this) != 0; 197 } 198 199 /** 200 * Sets the value to {@code newValue}, with memory semantics 201 * of setting as if the variable was declared non-{@code volatile} 202 * and non-{@code final}. 203 * 204 * @param newValue the new value 205 * @since 9 206 */ setPlain(boolean newValue)207 public final void setPlain(boolean newValue) { 208 VALUE.set(this, newValue ? 1 : 0); 209 } 210 211 /** 212 * Returns the current value, 213 * with memory effects as specified by {@link VarHandle#getOpaque}. 214 * 215 * @return the value 216 * @since 9 217 */ getOpaque()218 public final boolean getOpaque() { 219 return (int)VALUE.getOpaque(this) != 0; 220 } 221 222 /** 223 * Sets the value to {@code newValue}, 224 * with memory effects as specified by {@link VarHandle#setOpaque}. 225 * 226 * @param newValue the new value 227 * @since 9 228 */ setOpaque(boolean newValue)229 public final void setOpaque(boolean newValue) { 230 VALUE.setOpaque(this, newValue ? 1 : 0); 231 } 232 233 /** 234 * Returns the current value, 235 * with memory effects as specified by {@link VarHandle#getAcquire}. 236 * 237 * @return the value 238 * @since 9 239 */ getAcquire()240 public final boolean getAcquire() { 241 return (int)VALUE.getAcquire(this) != 0; 242 } 243 244 /** 245 * Sets the value to {@code newValue}, 246 * with memory effects as specified by {@link VarHandle#setRelease}. 247 * 248 * @param newValue the new value 249 * @since 9 250 */ setRelease(boolean newValue)251 public final void setRelease(boolean newValue) { 252 VALUE.setRelease(this, newValue ? 1 : 0); 253 } 254 255 /** 256 * Atomically sets the value to {@code newValue} if the current value, 257 * referred to as the <em>witness value</em>, {@code == expectedValue}, 258 * with memory effects as specified by 259 * {@link VarHandle#compareAndExchange}. 260 * 261 * @param expectedValue the expected value 262 * @param newValue the new value 263 * @return the witness value, which will be the same as the 264 * expected value if successful 265 * @since 9 266 */ compareAndExchange(boolean expectedValue, boolean newValue)267 public final boolean compareAndExchange(boolean expectedValue, boolean newValue) { 268 return (int)VALUE.compareAndExchange(this, 269 (expectedValue ? 1 : 0), 270 (newValue ? 1 : 0)) != 0; 271 } 272 273 /** 274 * Atomically sets the value to {@code newValue} if the current value, 275 * referred to as the <em>witness value</em>, {@code == expectedValue}, 276 * with memory effects as specified by 277 * {@link VarHandle#compareAndExchangeAcquire}. 278 * 279 * @param expectedValue the expected value 280 * @param newValue the new value 281 * @return the witness value, which will be the same as the 282 * expected value if successful 283 * @since 9 284 */ compareAndExchangeAcquire(boolean expectedValue, boolean newValue)285 public final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue) { 286 return (int)VALUE.compareAndExchangeAcquire(this, 287 (expectedValue ? 1 : 0), 288 (newValue ? 1 : 0)) != 0; 289 } 290 291 /** 292 * Atomically sets the value to {@code newValue} if the current value, 293 * referred to as the <em>witness value</em>, {@code == expectedValue}, 294 * with memory effects as specified by 295 * {@link VarHandle#compareAndExchangeRelease}. 296 * 297 * @param expectedValue the expected value 298 * @param newValue the new value 299 * @return the witness value, which will be the same as the 300 * expected value if successful 301 * @since 9 302 */ compareAndExchangeRelease(boolean expectedValue, boolean newValue)303 public final boolean compareAndExchangeRelease(boolean expectedValue, boolean newValue) { 304 return (int)VALUE.compareAndExchangeRelease(this, 305 (expectedValue ? 1 : 0), 306 (newValue ? 1 : 0)) != 0; 307 } 308 309 /** 310 * Possibly atomically sets the value to {@code newValue} if the current 311 * value {@code == expectedValue}, 312 * with memory effects as specified by 313 * {@link VarHandle#weakCompareAndSet}. 314 * 315 * @param expectedValue the expected value 316 * @param newValue the new value 317 * @return {@code true} if successful 318 * @since 9 319 */ weakCompareAndSetVolatile(boolean expectedValue, boolean newValue)320 public final boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue) { 321 return VALUE.weakCompareAndSet(this, 322 (expectedValue ? 1 : 0), 323 (newValue ? 1 : 0)); 324 } 325 326 /** 327 * Possibly atomically sets the value to {@code newValue} if the current 328 * value {@code == expectedValue}, 329 * with memory effects as specified by 330 * {@link VarHandle#weakCompareAndSetAcquire}. 331 * 332 * @param expectedValue the expected value 333 * @param newValue the new value 334 * @return {@code true} if successful 335 * @since 9 336 */ weakCompareAndSetAcquire(boolean expectedValue, boolean newValue)337 public final boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue) { 338 return VALUE.weakCompareAndSetAcquire(this, 339 (expectedValue ? 1 : 0), 340 (newValue ? 1 : 0)); 341 } 342 343 /** 344 * Possibly atomically sets the value to {@code newValue} if the current 345 * value {@code == expectedValue}, 346 * with memory effects as specified by 347 * {@link VarHandle#weakCompareAndSetRelease}. 348 * 349 * @param expectedValue the expected value 350 * @param newValue the new value 351 * @return {@code true} if successful 352 * @since 9 353 */ weakCompareAndSetRelease(boolean expectedValue, boolean newValue)354 public final boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue) { 355 return VALUE.weakCompareAndSetRelease(this, 356 (expectedValue ? 1 : 0), 357 (newValue ? 1 : 0)); 358 } 359 360 } 361