blob: f31e16686a2c9af90322986cd254c713827be159 [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.
32 AtomicInteger& operator = (int32_t new_value) {
33 value_ = new_value;
34 return *this;
35 }
36
Brian Carlstromdf629502013-07-17 22:39:56 -070037 operator int32_t() const {
Mathieu Chartierd8195f12012-10-05 12:21:28 -070038 return value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070039 }
40
41 int32_t get() const {
42 return value_;
43 }
44
45 int32_t operator += (const int32_t value) {
46 return android_atomic_add(value, &value_);
47 }
48
49 int32_t operator -= (const int32_t value) {
50 return android_atomic_add(-value, &value_);
51 }
52
53 int32_t operator |= (const int32_t value) {
54 return android_atomic_or(value, &value_);
55 }
56
57 int32_t operator &= (const int32_t value) {
58 return android_atomic_and(-value, &value_);
59 }
60
Brian Carlstrom38f85e42013-07-18 14:45:22 -070061 int32_t operator++ () {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070062 return android_atomic_inc(&value_) + 1;
63 }
64
Brian Carlstrom38f85e42013-07-18 14:45:22 -070065 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
Ian Rogers56edc432013-01-18 16:51:51 -080069 bool CompareAndSwap(int expected_value, int new_value) {
70 bool success = android_atomic_cas(expected_value, new_value, &value_) == 0;
71 return success;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070072 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070073
Mathieu Chartier2fde5332012-09-14 14:51:54 -070074 private:
Ian Rogers1d54e732013-05-02 21:10:01 -070075 volatile int32_t value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070076};
77
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070078} // namespace art
Mathieu Chartier2fde5332012-09-14 14:51:54 -070079
Brian Carlstromfc0e3212013-07-17 14:40:12 -070080#endif // ART_RUNTIME_ATOMIC_INTEGER_H_