blob: 132f9689d64ae62d0ec6243cfa5c63596a10c365 [file] [log] [blame]
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ATOMIC_INTEGER_H_
18#define ART_RUNTIME_ATOMIC_INTEGER_H_
Mathieu Chartier2fde5332012-09-14 14:51:54 -070019
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070020#include "cutils/atomic.h"
21#include "cutils/atomic-inline.h"
Mathieu Chartier2fde5332012-09-14 14:51:54 -070022
23namespace art {
24
25class AtomicInteger {
26 public:
Ian Rogers56edc432013-01-18 16:51:51 -080027 AtomicInteger() : value_(0) { }
Mathieu Chartier2b82db42012-11-14 17:29:05 -080028
Brian Carlstrom93ba8932013-07-17 21:31:49 -070029 explicit AtomicInteger(int32_t value) : value_(value) { }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070030
Mathieu Chartierd8195f12012-10-05 12:21:28 -070031 // Unsafe = operator for non atomic operations on the integer.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070032 void store(int32_t desired) {
33 value_ = desired;
34 }
35
36 AtomicInteger& operator=(int32_t desired) {
37 store(desired);
Mathieu Chartierd8195f12012-10-05 12:21:28 -070038 return *this;
39 }
40
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070041 int32_t load() const {
Mathieu Chartierd8195f12012-10-05 12:21:28 -070042 return value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070043 }
44
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070045 operator int32_t() const {
46 return load();
Mathieu Chartier2fde5332012-09-14 14:51:54 -070047 }
48
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070049 int32_t fetch_add(const int32_t value) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070050 return android_atomic_add(value, &value_);
51 }
52
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070053 int32_t fetch_sub(const int32_t value) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070054 return android_atomic_add(-value, &value_);
55 }
56
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070057 int32_t operator++() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070058 return android_atomic_inc(&value_) + 1;
59 }
60
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070061 int32_t operator++(int32_t) {
62 return android_atomic_inc(&value_);
63 }
64
65 int32_t operator--() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070066 return android_atomic_dec(&value_) - 1;
67 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -070068
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070069 int32_t operator--(int32_t) {
70 return android_atomic_dec(&value_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070071 }
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070072
73 bool compare_and_swap(int32_t expected_value, int32_t desired_value) {
74 return android_atomic_cas(expected_value, desired_value, &value_) == 0;
75 }
76
Mathieu Chartier2fde5332012-09-14 14:51:54 -070077 private:
Ian Rogers1d54e732013-05-02 21:10:01 -070078 volatile int32_t value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070079};
80
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070081} // namespace art
Mathieu Chartier2fde5332012-09-14 14:51:54 -070082
Brian Carlstromfc0e3212013-07-17 14:40:12 -070083#endif // ART_RUNTIME_ATOMIC_INTEGER_H_