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/AtomicInteger.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,265 @@
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 + * An {@code int} 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 AtomicInteger} is used in applications such as atomically
1.47 + * incremented counters, and cannot be used as a replacement for an
1.48 + * {@link java.lang.Integer}. However, this class does extend
1.49 + * {@code Number} to allow uniform access by tools and utilities that
1.50 + * deal with numerically-based classes.
1.51 + *
1.52 + * @since 1.5
1.53 + * @author Doug Lea
1.54 +*/
1.55 +public class AtomicInteger extends Number implements java.io.Serializable {
1.56 + private static final long serialVersionUID = 6214790243416807050L;
1.57 +
1.58 + // setup to use Unsafe.compareAndSwapInt for updates
1.59 + private static final Unsafe unsafe = Unsafe.getUnsafe();
1.60 + private static final long valueOffset;
1.61 +
1.62 + static {
1.63 + try {
1.64 + valueOffset = unsafe.objectFieldOffset
1.65 + (AtomicInteger.class.getDeclaredField("value"));
1.66 + } catch (Exception ex) { throw new Error(ex); }
1.67 + }
1.68 +
1.69 + private volatile int value;
1.70 +
1.71 + /**
1.72 + * Creates a new AtomicInteger with the given initial value.
1.73 + *
1.74 + * @param initialValue the initial value
1.75 + */
1.76 + public AtomicInteger(int initialValue) {
1.77 + value = initialValue;
1.78 + }
1.79 +
1.80 + /**
1.81 + * Creates a new AtomicInteger with initial value {@code 0}.
1.82 + */
1.83 + public AtomicInteger() {
1.84 + }
1.85 +
1.86 + /**
1.87 + * Gets the current value.
1.88 + *
1.89 + * @return the current value
1.90 + */
1.91 + public final int get() {
1.92 + return value;
1.93 + }
1.94 +
1.95 + /**
1.96 + * Sets to the given value.
1.97 + *
1.98 + * @param newValue the new value
1.99 + */
1.100 + public final void set(int newValue) {
1.101 + value = newValue;
1.102 + }
1.103 +
1.104 + /**
1.105 + * Eventually sets to the given value.
1.106 + *
1.107 + * @param newValue the new value
1.108 + * @since 1.6
1.109 + */
1.110 + public final void lazySet(int newValue) {
1.111 + unsafe.putOrderedInt(this, valueOffset, newValue);
1.112 + }
1.113 +
1.114 + /**
1.115 + * Atomically sets to the given value and returns the old value.
1.116 + *
1.117 + * @param newValue the new value
1.118 + * @return the previous value
1.119 + */
1.120 + public final int getAndSet(int newValue) {
1.121 + for (;;) {
1.122 + int current = get();
1.123 + if (compareAndSet(current, newValue))
1.124 + return current;
1.125 + }
1.126 + }
1.127 +
1.128 + /**
1.129 + * Atomically sets the value to the given updated value
1.130 + * if the current value {@code ==} the expected value.
1.131 + *
1.132 + * @param expect the expected value
1.133 + * @param update the new value
1.134 + * @return true if successful. False return indicates that
1.135 + * the actual value was not equal to the expected value.
1.136 + */
1.137 + public final boolean compareAndSet(int expect, int update) {
1.138 + return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
1.139 + }
1.140 +
1.141 + /**
1.142 + * Atomically sets the value to the given updated value
1.143 + * if the current value {@code ==} the expected value.
1.144 + *
1.145 + * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
1.146 + * and does not provide ordering guarantees, so is only rarely an
1.147 + * appropriate alternative to {@code compareAndSet}.
1.148 + *
1.149 + * @param expect the expected value
1.150 + * @param update the new value
1.151 + * @return true if successful.
1.152 + */
1.153 + public final boolean weakCompareAndSet(int expect, int update) {
1.154 + return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
1.155 + }
1.156 +
1.157 + /**
1.158 + * Atomically increments by one the current value.
1.159 + *
1.160 + * @return the previous value
1.161 + */
1.162 + public final int getAndIncrement() {
1.163 + for (;;) {
1.164 + int current = get();
1.165 + int next = current + 1;
1.166 + if (compareAndSet(current, next))
1.167 + return current;
1.168 + }
1.169 + }
1.170 +
1.171 + /**
1.172 + * Atomically decrements by one the current value.
1.173 + *
1.174 + * @return the previous value
1.175 + */
1.176 + public final int getAndDecrement() {
1.177 + for (;;) {
1.178 + int current = get();
1.179 + int next = current - 1;
1.180 + if (compareAndSet(current, next))
1.181 + return current;
1.182 + }
1.183 + }
1.184 +
1.185 + /**
1.186 + * Atomically adds the given value to the current value.
1.187 + *
1.188 + * @param delta the value to add
1.189 + * @return the previous value
1.190 + */
1.191 + public final int getAndAdd(int delta) {
1.192 + for (;;) {
1.193 + int current = get();
1.194 + int next = current + delta;
1.195 + if (compareAndSet(current, next))
1.196 + return current;
1.197 + }
1.198 + }
1.199 +
1.200 + /**
1.201 + * Atomically increments by one the current value.
1.202 + *
1.203 + * @return the updated value
1.204 + */
1.205 + public final int incrementAndGet() {
1.206 + for (;;) {
1.207 + int current = get();
1.208 + int next = current + 1;
1.209 + if (compareAndSet(current, next))
1.210 + return next;
1.211 + }
1.212 + }
1.213 +
1.214 + /**
1.215 + * Atomically decrements by one the current value.
1.216 + *
1.217 + * @return the updated value
1.218 + */
1.219 + public final int decrementAndGet() {
1.220 + for (;;) {
1.221 + int current = get();
1.222 + int next = current - 1;
1.223 + if (compareAndSet(current, next))
1.224 + return next;
1.225 + }
1.226 + }
1.227 +
1.228 + /**
1.229 + * Atomically adds the given value to the current value.
1.230 + *
1.231 + * @param delta the value to add
1.232 + * @return the updated value
1.233 + */
1.234 + public final int addAndGet(int delta) {
1.235 + for (;;) {
1.236 + int current = get();
1.237 + int next = current + delta;
1.238 + if (compareAndSet(current, next))
1.239 + return next;
1.240 + }
1.241 + }
1.242 +
1.243 + /**
1.244 + * Returns the String representation of the current value.
1.245 + * @return the String representation of the current value.
1.246 + */
1.247 + public String toString() {
1.248 + return Integer.toString(get());
1.249 + }
1.250 +
1.251 +
1.252 + public int intValue() {
1.253 + return get();
1.254 + }
1.255 +
1.256 + public long longValue() {
1.257 + return (long)get();
1.258 + }
1.259 +
1.260 + public float floatValue() {
1.261 + return (float)get();
1.262 + }
1.263 +
1.264 + public double doubleValue() {
1.265 + return (double)get();
1.266 + }
1.267 +
1.268 +}