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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ATOMIC_INTEGER_H_ |
| 18 | #define ART_RUNTIME_ATOMIC_INTEGER_H_ |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 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: |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 27 | AtomicInteger() : value_(0) { } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 28 | |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 29 | explicit AtomicInteger(int32_t value) : value_(value) { } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 30 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 31 | // 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 Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 37 | operator int32_t() const { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 38 | return value_; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 39 | } |
| 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 Carlstrom | 38f85e4 | 2013-07-18 14:45:22 -0700 | [diff] [blame^] | 61 | int32_t operator++ () { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 62 | return android_atomic_inc(&value_) + 1; |
| 63 | } |
| 64 | |
Brian Carlstrom | 38f85e4 | 2013-07-18 14:45:22 -0700 | [diff] [blame^] | 65 | int32_t operator-- () { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 66 | return android_atomic_dec(&value_) - 1; |
| 67 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 68 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 69 | bool CompareAndSwap(int expected_value, int new_value) { |
| 70 | bool success = android_atomic_cas(expected_value, new_value, &value_) == 0; |
| 71 | return success; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 72 | } |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 73 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 74 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 75 | volatile int32_t value_; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 78 | } // namespace art |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 79 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 80 | #endif // ART_RUNTIME_ATOMIC_INTEGER_H_ |