/*
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.max.atomic;
import com.sun.max.annotate.*;
import com.sun.max.vm.*;
import com.sun.max.vm.actor.holder.*;
import com.sun.max.vm.reference.*;
/**
* An {@code int} value that may be updated atomically.
*/
public class AtomicInteger {
private static final int valueOffset = ClassActor.fromJava(AtomicInteger.class).findLocalInstanceFieldActor("value").offset();
private volatile int value;
/**
* Creates a new AtomicInteger with the given initial value.
*
* @param initialValue the initial value
*/
public AtomicInteger(int initialValue) {
value = initialValue;
}
/**
* Creates a new AtomicInteger with null initial value.
*/
public AtomicInteger() {
}
/**
* Gets the current value.
*
* @return the current value
*/
@INLINE
public final int get() {
return value;
}
/**
* Sets to the given value.
*
* @param newValue the new value
*/
@INLINE
public final void set(int newValue) {
value = newValue;
}
/**
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
*
* @param expect the expected value
* @param update the new value
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
*/
@INLINE
public final boolean compareAndSet(int expect, int update) {
if (MaxineVM.isHosted()) {
synchronized (this) {
if (expect == value) {
value = update;
return true;
}
return false;
}
}
return Reference.fromJava(this).compareAndSwapInt(valueOffset, expect, update) == expect;
}
/**
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
*
* @param expect the expected value
* @param update the new value
* @return the actual value compared against {@code expect}. The update is guaranteed to have occurred iff {@code expect != update} and
* the returned value {@code == expect}
*/
@INLINE
public final int compareAndSwap(int expect, int update) {
if (MaxineVM.isHosted()) {
synchronized (this) {
if (expect == value) {
value = update;
return expect;
}
return value;
}
}
return Reference.fromJava(this).compareAndSwapInt(valueOffset, expect, update);
}
/**
* Atomically sets to the given value and returns the old value.
*
* @param newValue the new value
* @return the previous value
*/
public final int getAndSet(int newValue) {
while (true) {
final int currentValue = get();
if (compareAndSet(currentValue, newValue)) {
return currentValue;
}
}
}
/**
* Atomically adds the given value to the current value.
*
* @param delta the value to add
* @return the previous value
*/
public final int getAndAdd(int delta) {
while (true) {
final int current = get();
final int next = current + delta;
if (compareAndSet(current, next)) {
return current;
}
}
}
/**
* Returns the string representation of the current value.
*/
@Override
public String toString() {
return String.valueOf(get());
}
}