Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 20 | #include "cutils/atomic.h" |
| 21 | #include "cutils/atomic-inline.h" |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class AtomicInteger { |
| 26 | public: |
| 27 | AtomicInteger(int32_t value) : value_(value) { } |
| 28 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 29 | // Unsafe = operator for non atomic operations on the integer. |
| 30 | AtomicInteger& operator = (int32_t new_value) { |
| 31 | value_ = new_value; |
| 32 | return *this; |
| 33 | } |
| 34 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 35 | operator int32_t () const { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 36 | return value_; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int32_t get() const { |
| 40 | return value_; |
| 41 | } |
| 42 | |
| 43 | int32_t operator += (const int32_t value) { |
| 44 | return android_atomic_add(value, &value_); |
| 45 | } |
| 46 | |
| 47 | int32_t operator -= (const int32_t value) { |
| 48 | return android_atomic_add(-value, &value_); |
| 49 | } |
| 50 | |
| 51 | int32_t operator |= (const int32_t value) { |
| 52 | return android_atomic_or(value, &value_); |
| 53 | } |
| 54 | |
| 55 | int32_t operator &= (const int32_t value) { |
| 56 | return android_atomic_and(-value, &value_); |
| 57 | } |
| 58 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 59 | int32_t operator ++ (int32_t) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 60 | return android_atomic_inc(&value_); |
| 61 | } |
| 62 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 63 | int32_t operator -- (int32_t) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 64 | return android_atomic_dec(&value_); |
| 65 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 66 | |
| 67 | int32_t operator ++ () { |
| 68 | return android_atomic_inc(&value_) + 1; |
| 69 | } |
| 70 | |
| 71 | int32_t operator -- () { |
| 72 | return android_atomic_dec(&value_) - 1; |
| 73 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 74 | |
| 75 | int CompareAndSwap(int expected_value, int new_value) { |
| 76 | return android_atomic_cas(expected_value, new_value, &value_); |
| 77 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 78 | private: |
| 79 | int32_t value_; |
| 80 | }; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | #endif // ART_SRC_ATOMIC_INTEGER_H_ |