summaryrefslogtreecommitdiff
path: root/runtime/atomic_integer.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/atomic_integer.h')
-rw-r--r--runtime/atomic_integer.h51
1 files changed, 27 insertions, 24 deletions
diff --git a/runtime/atomic_integer.h b/runtime/atomic_integer.h
index 132f9689d6..651ca4a6e9 100644
--- a/runtime/atomic_integer.h
+++ b/runtime/atomic_integer.h
@@ -17,8 +17,7 @@
#ifndef ART_RUNTIME_ATOMIC_INTEGER_H_
#define ART_RUNTIME_ATOMIC_INTEGER_H_
-#include "cutils/atomic.h"
-#include "cutils/atomic-inline.h"
+#include <stdint.h>
namespace art {
@@ -28,53 +27,57 @@ class AtomicInteger {
explicit AtomicInteger(int32_t value) : value_(value) { }
- // Unsafe = operator for non atomic operations on the integer.
- void store(int32_t desired) {
- value_ = desired;
- }
-
AtomicInteger& operator=(int32_t desired) {
- store(desired);
+ Store(desired);
return *this;
}
- int32_t load() const {
+ int32_t Load() const {
return value_;
}
operator int32_t() const {
- return load();
+ return Load();
+ }
+
+ int32_t FetchAndAdd(const int32_t value) {
+ return __sync_fetch_and_add(&value_, value); // Return old_value.
}
- int32_t fetch_add(const int32_t value) {
- return android_atomic_add(value, &value_);
+ int32_t FetchAndSub(const int32_t value) {
+ return __sync_fetch_and_sub(&value_, value); // Return old value.
}
- int32_t fetch_sub(const int32_t value) {
- return android_atomic_add(-value, &value_);
+ int32_t operator++() { // Prefix operator.
+ return __sync_add_and_fetch(&value_, 1); // Return new value.
}
- int32_t operator++() {
- return android_atomic_inc(&value_) + 1;
+ int32_t operator++(int32_t) { // Postfix operator.
+ return __sync_fetch_and_add(&value_, 1); // Return old value.
}
- int32_t operator++(int32_t) {
- return android_atomic_inc(&value_);
+ int32_t operator--() { // Prefix operator.
+ return __sync_sub_and_fetch(&value_, 1); // Return new value.
}
- int32_t operator--() {
- return android_atomic_dec(&value_) - 1;
+ int32_t operator--(int32_t) { // Postfix operator.
+ return __sync_fetch_and_sub(&value_, 1); // Return old value.
}
- int32_t operator--(int32_t) {
- return android_atomic_dec(&value_);
+ bool CompareAndSwap(int32_t expected_value, int32_t desired_value) {
+ return __sync_bool_compare_and_swap(&value_, expected_value, desired_value);
}
- bool compare_and_swap(int32_t expected_value, int32_t desired_value) {
- return android_atomic_cas(expected_value, desired_value, &value_) == 0;
+ volatile int32_t* Address() {
+ return &value_;
}
private:
+ // Unsafe = operator for non atomic operations on the integer.
+ void Store(int32_t desired) {
+ value_ = desired;
+ }
+
volatile int32_t value_;
};