niklase@google.com | f0779a2 | 2011-05-30 11:39:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 independant 32-bit signed integer. |
| 12 | // Windows implementation. |
| 13 | #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ |
| 14 | #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ |
| 15 | |
| 16 | #include <malloc.h> |
| 17 | #include <windows.h> |
| 18 | |
| 19 | #include "common_types.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | class Atomic32Impl |
| 23 | { |
| 24 | public: |
| 25 | inline Atomic32Impl(WebRtc_Word32 initialValue); |
| 26 | inline ~Atomic32Impl(); |
| 27 | |
| 28 | inline WebRtc_Word32 operator++(); |
| 29 | inline WebRtc_Word32 operator--(); |
| 30 | |
| 31 | inline Atomic32Impl& operator=(const Atomic32Impl& rhs); |
| 32 | inline Atomic32Impl& operator=(WebRtc_Word32 rhs); |
| 33 | inline WebRtc_Word32 operator+=(WebRtc_Word32 rhs); |
| 34 | inline WebRtc_Word32 operator-=(WebRtc_Word32 rhs); |
| 35 | |
| 36 | inline bool CompareExchange(WebRtc_Word32 newValue, |
| 37 | WebRtc_Word32 compareValue); |
| 38 | |
| 39 | inline WebRtc_Word32 Value() const; |
| 40 | private: |
| 41 | void* _ptrMemory; |
| 42 | // Volatile ensures full memory barriers. |
| 43 | volatile LONG* _value; |
| 44 | }; |
| 45 | |
| 46 | // TODO (hellner) use aligned_malloc instead of doing it manually. |
| 47 | inline Atomic32Impl::Atomic32Impl(WebRtc_Word32 initialValue) |
| 48 | : _ptrMemory(NULL), |
| 49 | _value(NULL) |
| 50 | { // Align the memory associated with _value on a 32-bit boundary. This is a |
| 51 | // requirement for the used Windows APIs to be atomic. |
| 52 | // Keep _ptrMemory to be able to reclaim memory. |
| 53 | _ptrMemory = malloc(sizeof(WebRtc_Word32)*2); |
| 54 | _value = reinterpret_cast<LONG*> (((uintptr_t)_ptrMemory+3)&(~0x3)); |
| 55 | *_value = initialValue; |
| 56 | } |
| 57 | |
| 58 | inline Atomic32Impl::~Atomic32Impl() |
| 59 | { |
| 60 | if(_ptrMemory != NULL) |
| 61 | { |
| 62 | free(_ptrMemory); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | inline WebRtc_Word32 Atomic32Impl::operator++() |
| 67 | { |
| 68 | return (WebRtc_Word32)InterlockedIncrement(_value); |
| 69 | } |
| 70 | |
| 71 | inline WebRtc_Word32 Atomic32Impl::operator--() |
| 72 | { |
| 73 | return (WebRtc_Word32)InterlockedDecrement(_value); |
| 74 | } |
| 75 | |
| 76 | inline Atomic32Impl& Atomic32Impl::operator=(const Atomic32Impl& rhs) |
| 77 | { |
| 78 | *_value = *rhs._value; |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | inline Atomic32Impl& Atomic32Impl::operator=(WebRtc_Word32 rhs) |
| 83 | { |
| 84 | *_value = rhs; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | inline WebRtc_Word32 Atomic32Impl::operator+=(WebRtc_Word32 rhs) |
| 89 | { |
| 90 | return InterlockedExchangeAdd(_value,rhs); |
| 91 | } |
| 92 | |
| 93 | inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs) |
| 94 | { |
| 95 | return InterlockedExchangeAdd(_value,-rhs); |
| 96 | } |
| 97 | |
| 98 | inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue, |
| 99 | WebRtc_Word32 compareValue) |
| 100 | { |
| 101 | const LONG oldValue = InterlockedCompareExchange(_value,newValue, |
| 102 | compareValue); |
| 103 | // If the old value and the compare value is the same an exchange happened. |
| 104 | return (oldValue == compareValue); |
| 105 | } |
| 106 | |
| 107 | inline WebRtc_Word32 Atomic32Impl::Value() const |
| 108 | { |
| 109 | return *_value; |
| 110 | } |
| 111 | } // namespace webrtc |
| 112 | |
| 113 | #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ |