1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicBoolean.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,164 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.6 + *
1.7 + * This code is free software; you can redistribute it and/or modify it
1.8 + * under the terms of the GNU General Public License version 2 only, as
1.9 + * published by the Free Software Foundation. Oracle designates this
1.10 + * particular file as subject to the "Classpath" exception as provided
1.11 + * by Oracle in the LICENSE file that accompanied this code.
1.12 + *
1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.16 + * version 2 for more details (a copy is included in the LICENSE file that
1.17 + * accompanied this code).
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License version
1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.22 + *
1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.24 + * or visit www.oracle.com if you need additional information or have any
1.25 + * questions.
1.26 + */
1.27 +
1.28 +/*
1.29 + * This file is available under and governed by the GNU General Public
1.30 + * License version 2 only, as published by the Free Software Foundation.
1.31 + * However, the following notice accompanied the original version of this
1.32 + * file:
1.33 + *
1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
1.35 + * Expert Group and released to the public domain, as explained at
1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
1.37 + */
1.38 +
1.39 +package java.util.concurrent.atomic;
1.40 +import sun.misc.Unsafe;
1.41 +
1.42 +/**
1.43 + * A {@code boolean} value that may be updated atomically. See the
1.44 + * {@link java.util.concurrent.atomic} package specification for
1.45 + * description of the properties of atomic variables. An
1.46 + * {@code AtomicBoolean} is used in applications such as atomically
1.47 + * updated flags, and cannot be used as a replacement for a
1.48 + * {@link java.lang.Boolean}.
1.49 + *
1.50 + * @since 1.5
1.51 + * @author Doug Lea
1.52 + */
1.53 +public class AtomicBoolean implements java.io.Serializable {
1.54 + private static final long serialVersionUID = 4654671469794556979L;
1.55 + // setup to use Unsafe.compareAndSwapInt for updates
1.56 + private static final Unsafe unsafe = Unsafe.getUnsafe();
1.57 + private static final long valueOffset;
1.58 +
1.59 + static {
1.60 + try {
1.61 + valueOffset = unsafe.objectFieldOffset
1.62 + (AtomicBoolean.class.getDeclaredField("value"));
1.63 + } catch (Exception ex) { throw new Error(ex); }
1.64 + }
1.65 +
1.66 + private volatile int value;
1.67 +
1.68 + /**
1.69 + * Creates a new {@code AtomicBoolean} with the given initial value.
1.70 + *
1.71 + * @param initialValue the initial value
1.72 + */
1.73 + public AtomicBoolean(boolean initialValue) {
1.74 + value = initialValue ? 1 : 0;
1.75 + }
1.76 +
1.77 + /**
1.78 + * Creates a new {@code AtomicBoolean} with initial value {@code false}.
1.79 + */
1.80 + public AtomicBoolean() {
1.81 + }
1.82 +
1.83 + /**
1.84 + * Returns the current value.
1.85 + *
1.86 + * @return the current value
1.87 + */
1.88 + public final boolean get() {
1.89 + return value != 0;
1.90 + }
1.91 +
1.92 + /**
1.93 + * Atomically sets the value to the given updated value
1.94 + * if the current value {@code ==} the expected value.
1.95 + *
1.96 + * @param expect the expected value
1.97 + * @param update the new value
1.98 + * @return true if successful. False return indicates that
1.99 + * the actual value was not equal to the expected value.
1.100 + */
1.101 + public final boolean compareAndSet(boolean expect, boolean update) {
1.102 + int e = expect ? 1 : 0;
1.103 + int u = update ? 1 : 0;
1.104 + return unsafe.compareAndSwapInt(this, valueOffset, e, u);
1.105 + }
1.106 +
1.107 + /**
1.108 + * Atomically sets the value to the given updated value
1.109 + * if the current value {@code ==} the expected value.
1.110 + *
1.111 + * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
1.112 + * and does not provide ordering guarantees, so is only rarely an
1.113 + * appropriate alternative to {@code compareAndSet}.
1.114 + *
1.115 + * @param expect the expected value
1.116 + * @param update the new value
1.117 + * @return true if successful.
1.118 + */
1.119 + public boolean weakCompareAndSet(boolean expect, boolean update) {
1.120 + int e = expect ? 1 : 0;
1.121 + int u = update ? 1 : 0;
1.122 + return unsafe.compareAndSwapInt(this, valueOffset, e, u);
1.123 + }
1.124 +
1.125 + /**
1.126 + * Unconditionally sets to the given value.
1.127 + *
1.128 + * @param newValue the new value
1.129 + */
1.130 + public final void set(boolean newValue) {
1.131 + value = newValue ? 1 : 0;
1.132 + }
1.133 +
1.134 + /**
1.135 + * Eventually sets to the given value.
1.136 + *
1.137 + * @param newValue the new value
1.138 + * @since 1.6
1.139 + */
1.140 + public final void lazySet(boolean newValue) {
1.141 + int v = newValue ? 1 : 0;
1.142 + unsafe.putOrderedInt(this, valueOffset, v);
1.143 + }
1.144 +
1.145 + /**
1.146 + * Atomically sets to the given value and returns the previous value.
1.147 + *
1.148 + * @param newValue the new value
1.149 + * @return the previous value
1.150 + */
1.151 + public final boolean getAndSet(boolean newValue) {
1.152 + for (;;) {
1.153 + boolean current = get();
1.154 + if (compareAndSet(current, newValue))
1.155 + return current;
1.156 + }
1.157 + }
1.158 +
1.159 + /**
1.160 + * Returns the String representation of the current value.
1.161 + * @return the String representation of the current value.
1.162 + */
1.163 + public String toString() {
1.164 + return Boolean.toString(get());
1.165 + }
1.166 +
1.167 +}