blob: 2e9865df961cc43ad9622be1d7f1c4ef7a0b4532 [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//
14// Note: uses full memory barriers.
15// Note: assumes 32-bit (or higher) system
16#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_
17#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_
18
19#include <cstddef>
20
21#include "common_types.h"
22#include "constructor_magic.h"
23
24namespace webrtc {
25
26// 32 bit atomic variable. Note that this class relies on the compiler to
27// align the 32 bit value correctly (on a 32 bit boundary), so as long as you're
28// not doing things like reinterpret_cast over some custom allocated memory
29// without being careful with alignment, you should be fine.
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000030class Atomic32 {
31 public:
32 Atomic32(WebRtc_Word32 initial_value = 0);
33 ~Atomic32();
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000034
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000035 // Prefix operator!
36 WebRtc_Word32 operator++();
37 WebRtc_Word32 operator--();
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000038
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000039 WebRtc_Word32 operator+=(WebRtc_Word32 value);
40 WebRtc_Word32 operator-=(WebRtc_Word32 value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000041
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000042 // Sets the value atomically to new_value if the value equals compare value.
43 // The function returns true if the exchange happened.
44 bool CompareExchange(WebRtc_Word32 new_value, WebRtc_Word32 compare_value);
45 WebRtc_Word32 Value() const;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000046
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000047 private:
48 // Disable the + and - operator since it's unclear what these operations
49 // should do.
50 Atomic32 operator+(const Atomic32& other);
51 Atomic32 operator-(const Atomic32& other);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000052
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000053 // Checks if |_value| is 32bit aligned.
54 inline bool Is32bitAligned() const {
55 return (reinterpret_cast<ptrdiff_t>(&value_) & 3) == 0;
56 }
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000057
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000058 DISALLOW_COPY_AND_ASSIGN(Atomic32);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000059
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000060 WebRtc_Word32 value_;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000061};
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000062
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000063} // namespace webrtc
64
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000065#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_