blob: 54d5fd88a9f89f86119d96ddff93f4bcfacdf507 [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
17#ifndef ART_SRC_ATOMIC_INTEGER_H_
18#define ART_SRC_ATOMIC_INTEGER_H_
19
20#include "atomic.h"
21
22namespace art {
23
24class AtomicInteger {
25 public:
26 AtomicInteger(int32_t value) : value_(value) { }
27
Mathieu Chartierd8195f12012-10-05 12:21:28 -070028 // Unsafe = operator for non atomic operations on the integer.
29 AtomicInteger& operator = (int32_t new_value) {
30 value_ = new_value;
31 return *this;
32 }
33
Mathieu Chartier2fde5332012-09-14 14:51:54 -070034 operator int32_t () const {
Mathieu Chartierd8195f12012-10-05 12:21:28 -070035 return value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070036 }
37
38 int32_t get() const {
39 return value_;
40 }
41
42 int32_t operator += (const int32_t value) {
43 return android_atomic_add(value, &value_);
44 }
45
46 int32_t operator -= (const int32_t value) {
47 return android_atomic_add(-value, &value_);
48 }
49
50 int32_t operator |= (const int32_t value) {
51 return android_atomic_or(value, &value_);
52 }
53
54 int32_t operator &= (const int32_t value) {
55 return android_atomic_and(-value, &value_);
56 }
57
Mathieu Chartierd8195f12012-10-05 12:21:28 -070058 int32_t operator ++ (int32_t) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070059 return android_atomic_inc(&value_);
60 }
61
Mathieu Chartierd8195f12012-10-05 12:21:28 -070062 int32_t operator -- (int32_t) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070063 return android_atomic_dec(&value_);
64 }
65 private:
66 int32_t value_;
67};
68
69}
70
71#endif // ART_SRC_ATOMIC_INTEGER_H_