blob: 74a540ed8913c694e7df909d16ac37a7d6124ec3 [file] [log] [blame]
tommi@webrtc.orge84373c2012-04-19 14:28:45 +00001/*
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.orge84373c2012-04-19 14:28:45 +000014// Note: assumes 32-bit (or higher) system
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#ifndef SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_
16#define SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000017
Yuwei Huang160c0202017-10-20 15:47:42 -070018#include <atomic>
19
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000020#include <stddef.h>
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000021
Mirko Bonadei71207422017-09-15 13:58:09 +020022#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/constructormagic.h"
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000024
25namespace webrtc {
26
Yuwei Huang160c0202017-10-20 15:47:42 -070027// 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.orge84373c2012-04-19 14:28:45 +000030// 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.org9cb9fc12012-11-09 08:57:25 +000034class Atomic32 {
35 public:
pbos@webrtc.org046deb92013-04-09 09:06:11 +000036 Atomic32(int32_t initial_value = 0);
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000037 ~Atomic32();
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000038
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000039 // Prefix operator!
pbos@webrtc.org046deb92013-04-09 09:06:11 +000040 int32_t operator++();
41 int32_t operator--();
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000042
pbos@webrtc.org046deb92013-04-09 09:06:11 +000043 int32_t operator+=(int32_t value);
44 int32_t operator-=(int32_t value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000045
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000046 // Sets the value atomically to new_value if the value equals compare value.
47 // The function returns true if the exchange happened.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000048 bool CompareExchange(int32_t new_value, int32_t compare_value);
Yuwei Huang160c0202017-10-20 15:47:42 -070049 int32_t Value() const;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000050
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000051 private:
henrikg3c089d72015-09-16 05:37:44 -070052 RTC_DISALLOW_COPY_AND_ASSIGN(Atomic32);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000053
Yuwei Huang160c0202017-10-20 15:47:42 -070054 std::atomic<int32_t> value_;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000055};
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000056
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000057} // namespace webrtc
58
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#endif // SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_