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 | // |
| 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 | |
| 24 | namespace 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.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 30 | class Atomic32 { |
| 31 | public: |
| 32 | Atomic32(WebRtc_Word32 initial_value = 0); |
| 33 | ~Atomic32(); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 34 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 35 | // Prefix operator! |
| 36 | WebRtc_Word32 operator++(); |
| 37 | WebRtc_Word32 operator--(); |
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 | WebRtc_Word32 operator+=(WebRtc_Word32 value); |
| 40 | WebRtc_Word32 operator-=(WebRtc_Word32 value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 41 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 42 | // 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.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 46 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 47 | 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.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 52 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 53 | // Checks if |_value| is 32bit aligned. |
| 54 | inline bool Is32bitAligned() const { |
| 55 | return (reinterpret_cast<ptrdiff_t>(&value_) & 3) == 0; |
| 56 | } |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 57 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 58 | DISALLOW_COPY_AND_ASSIGN(Atomic32); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 59 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 60 | WebRtc_Word32 value_; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 61 | }; |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 62 | |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 63 | } // namespace webrtc |
| 64 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 65 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_ |