tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Atomic, system independent 32-bit integer. Unless you know what you're |
| 12 | // doing, use locks instead! :-) |
| 13 | // |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 14 | // Note: assumes 32-bit (or higher) system |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #ifndef SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_ |
| 16 | #define SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_ |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 17 | |
Yuwei Huang | 160c020 | 2017-10-20 15:47:42 -0700 | [diff] [blame] | 18 | #include <atomic> |
| 19 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 21 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 22 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/constructormagic.h" |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Yuwei Huang | 160c020 | 2017-10-20 15:47:42 -0700 | [diff] [blame] | 27 | // DEPRECATED: Please use std::atomic<int32_t> instead. |
| 28 | // TODO(yuweih): Replace Atomic32 uses with std::atomic<int32_t> and remove this |
| 29 | // class. (bugs.webrtc.org/8428) |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 30 | // 32 bit atomic variable. Note that this class relies on the compiler to |
| 31 | // align the 32 bit value correctly (on a 32 bit boundary), so as long as you're |
| 32 | // not doing things like reinterpret_cast over some custom allocated memory |
| 33 | // without being careful with alignment, you should be fine. |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 34 | class Atomic32 { |
| 35 | public: |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 36 | Atomic32(int32_t initial_value = 0); |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 37 | ~Atomic32(); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 38 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 39 | // Prefix operator! |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 40 | int32_t operator++(); |
| 41 | int32_t operator--(); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 42 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 43 | int32_t operator+=(int32_t value); |
| 44 | int32_t operator-=(int32_t value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 45 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 46 | // Sets the value atomically to new_value if the value equals compare value. |
| 47 | // The function returns true if the exchange happened. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 48 | bool CompareExchange(int32_t new_value, int32_t compare_value); |
Yuwei Huang | 160c020 | 2017-10-20 15:47:42 -0700 | [diff] [blame] | 49 | int32_t Value() const; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 50 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 51 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 52 | RTC_DISALLOW_COPY_AND_ASSIGN(Atomic32); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 53 | |
Yuwei Huang | 160c020 | 2017-10-20 15:47:42 -0700 | [diff] [blame] | 54 | std::atomic<int32_t> value_; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 55 | }; |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 56 | |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 57 | } // namespace webrtc |
| 58 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 59 | #endif // SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_ |