blob: 651ca4a6e91e60886ee22ab28967d276de1b9b0c [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
Ian Rogersb122a4b2013-11-19 18:00:50 -080020#include <stdint.h>
Mathieu Chartier2fde5332012-09-14 14:51:54 -070021
22namespace art {
23
24class AtomicInteger {
25 public:
Ian Rogers56edc432013-01-18 16:51:51 -080026 AtomicInteger() : value_(0) { }
Mathieu Chartier2b82db42012-11-14 17:29:05 -080027
Brian Carlstrom93ba8932013-07-17 21:31:49 -070028 explicit AtomicInteger(int32_t value) : value_(value) { }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070029
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070030 AtomicInteger& operator=(int32_t desired) {
Ian Rogersb122a4b2013-11-19 18:00:50 -080031 Store(desired);
Mathieu Chartierd8195f12012-10-05 12:21:28 -070032 return *this;
33 }
34
Ian Rogersb122a4b2013-11-19 18:00:50 -080035 int32_t Load() const {
Mathieu Chartierd8195f12012-10-05 12:21:28 -070036 return value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070037 }
38
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070039 operator int32_t() const {
Ian Rogersb122a4b2013-11-19 18:00:50 -080040 return Load();
Mathieu Chartier2fde5332012-09-14 14:51:54 -070041 }
42
Ian Rogersb122a4b2013-11-19 18:00:50 -080043 int32_t FetchAndAdd(const int32_t value) {
44 return __sync_fetch_and_add(&value_, value); // Return old_value.
Mathieu Chartier2fde5332012-09-14 14:51:54 -070045 }
46
Ian Rogersb122a4b2013-11-19 18:00:50 -080047 int32_t FetchAndSub(const int32_t value) {
48 return __sync_fetch_and_sub(&value_, value); // Return old value.
Mathieu Chartier2fde5332012-09-14 14:51:54 -070049 }
50
Ian Rogersb122a4b2013-11-19 18:00:50 -080051 int32_t operator++() { // Prefix operator.
52 return __sync_add_and_fetch(&value_, 1); // Return new value.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070053 }
54
Ian Rogersb122a4b2013-11-19 18:00:50 -080055 int32_t operator++(int32_t) { // Postfix operator.
56 return __sync_fetch_and_add(&value_, 1); // Return old value.
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070057 }
58
Ian Rogersb122a4b2013-11-19 18:00:50 -080059 int32_t operator--() { // Prefix operator.
60 return __sync_sub_and_fetch(&value_, 1); // Return new value.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070061 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -070062
Ian Rogersb122a4b2013-11-19 18:00:50 -080063 int32_t operator--(int32_t) { // Postfix operator.
64 return __sync_fetch_and_sub(&value_, 1); // Return old value.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070065 }
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070066
Ian Rogersb122a4b2013-11-19 18:00:50 -080067 bool CompareAndSwap(int32_t expected_value, int32_t desired_value) {
68 return __sync_bool_compare_and_swap(&value_, expected_value, desired_value);
69 }
70
71 volatile int32_t* Address() {
72 return &value_;
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070073 }
74
Mathieu Chartier2fde5332012-09-14 14:51:54 -070075 private:
Ian Rogersb122a4b2013-11-19 18:00:50 -080076 // Unsafe = operator for non atomic operations on the integer.
77 void Store(int32_t desired) {
78 value_ = desired;
79 }
80
Ian Rogers1d54e732013-05-02 21:10:01 -070081 volatile int32_t value_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070082};
83
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070084} // namespace art
Mathieu Chartier2fde5332012-09-14 14:51:54 -070085
Brian Carlstromfc0e3212013-07-17 14:40:12 -070086#endif // ART_RUNTIME_ATOMIC_INTEGER_H_