jtulach@1334: /* jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * This file is available under and governed by the GNU General Public jtulach@1334: * License version 2 only, as published by the Free Software Foundation. jtulach@1334: * However, the following notice accompanied the original version of this jtulach@1334: * file: jtulach@1334: * jtulach@1334: * Written by Doug Lea with assistance from members of JCP JSR-166 jtulach@1334: * Expert Group and released to the public domain, as explained at jtulach@1334: * http://creativecommons.org/publicdomain/zero/1.0/ jtulach@1334: */ jtulach@1334: jtulach@1334: package java.util.concurrent.atomic; jtulach@1334: import sun.misc.Unsafe; jtulach@1334: jtulach@1334: /** jtulach@1334: * A {@code boolean} value that may be updated atomically. See the jtulach@1334: * {@link java.util.concurrent.atomic} package specification for jtulach@1334: * description of the properties of atomic variables. An jtulach@1334: * {@code AtomicBoolean} is used in applications such as atomically jtulach@1334: * updated flags, and cannot be used as a replacement for a jtulach@1334: * {@link java.lang.Boolean}. jtulach@1334: * jtulach@1334: * @since 1.5 jtulach@1334: * @author Doug Lea jtulach@1334: */ jtulach@1334: public class AtomicBoolean implements java.io.Serializable { jtulach@1334: private static final long serialVersionUID = 4654671469794556979L; jtulach@1334: // setup to use Unsafe.compareAndSwapInt for updates jtulach@1334: private static final Unsafe unsafe = Unsafe.getUnsafe(); jtulach@1334: private static final long valueOffset; jtulach@1334: jtulach@1334: static { jtulach@1334: try { jtulach@1334: valueOffset = unsafe.objectFieldOffset jtulach@1334: (AtomicBoolean.class.getDeclaredField("value")); jtulach@1334: } catch (Exception ex) { throw new Error(ex); } jtulach@1334: } jtulach@1334: jtulach@1334: private volatile int value; jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new {@code AtomicBoolean} with the given initial value. jtulach@1334: * jtulach@1334: * @param initialValue the initial value jtulach@1334: */ jtulach@1334: public AtomicBoolean(boolean initialValue) { jtulach@1334: value = initialValue ? 1 : 0; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new {@code AtomicBoolean} with initial value {@code false}. jtulach@1334: */ jtulach@1334: public AtomicBoolean() { jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the current value. jtulach@1334: * jtulach@1334: * @return the current value jtulach@1334: */ jtulach@1334: public final boolean get() { jtulach@1334: return value != 0; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the value to the given updated value jtulach@1334: * if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. False return indicates that jtulach@1334: * the actual value was not equal to the expected value. jtulach@1334: */ jtulach@1334: public final boolean compareAndSet(boolean expect, boolean update) { jtulach@1334: int e = expect ? 1 : 0; jtulach@1334: int u = update ? 1 : 0; jtulach@1334: return unsafe.compareAndSwapInt(this, valueOffset, e, u); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the value to the given updated value jtulach@1334: * if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: *

May fail spuriously jtulach@1334: * and does not provide ordering guarantees, so is only rarely an jtulach@1334: * appropriate alternative to {@code compareAndSet}. jtulach@1334: * jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. jtulach@1334: */ jtulach@1334: public boolean weakCompareAndSet(boolean expect, boolean update) { jtulach@1334: int e = expect ? 1 : 0; jtulach@1334: int u = update ? 1 : 0; jtulach@1334: return unsafe.compareAndSwapInt(this, valueOffset, e, u); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Unconditionally sets to the given value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: */ jtulach@1334: public final void set(boolean newValue) { jtulach@1334: value = newValue ? 1 : 0; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Eventually sets to the given value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: * @since 1.6 jtulach@1334: */ jtulach@1334: public final void lazySet(boolean newValue) { jtulach@1334: int v = newValue ? 1 : 0; jtulach@1334: unsafe.putOrderedInt(this, valueOffset, v); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets to the given value and returns the previous value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final boolean getAndSet(boolean newValue) { jtulach@1334: for (;;) { jtulach@1334: boolean current = get(); jtulach@1334: if (compareAndSet(current, newValue)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the String representation of the current value. jtulach@1334: * @return the String representation of the current value. jtulach@1334: */ jtulach@1334: public String toString() { jtulach@1334: return Boolean.toString(get()); jtulach@1334: } jtulach@1334: jtulach@1334: }