From e84373c38f0eb41ce457a2b69f6c5bf05c511f1b Mon Sep 17 00:00:00 2001 From: "tommi@webrtc.org" Date: Thu, 19 Apr 2012 14:28:45 +0000 Subject: [PATCH] Atomic32Wrapper -> Atomic32 This change does the following: * Remove the Atomic32Wrapper and AtomicImpl classes and provide the Atomic32 implementation directly via platform specific source files. * Move/rename/delete all source files accordingly * The atomic value itself is now volatile. Previously it was only the pointer to the memory that was volatile, but not the actual value. * No additional heap allocations are now done for the atomic value. In a follow up cl I plan to start using Atomic32 in the RefCount class in order to fix issues reported by Coverity. Review URL: https://webrtc-codereview.appspot.com/490004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@2065 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../source/memory_pool_win.h | 8 +- .../source/udp_socket2_manager_windows.h | 8 +- .../source/udp_socket2_windows.h | 8 +- src/system_wrappers/interface/atomic32.h | 65 ++++++++++ .../interface/atomic32_wrapper.h | 55 -------- src/system_wrappers/interface/ref_count.h | 6 +- src/system_wrappers/source/atomic32.cc | 84 ------------- src/system_wrappers/source/atomic32_linux.cc | 64 ++++++++++ src/system_wrappers/source/atomic32_linux.h | 119 ------------------ src/system_wrappers/source/atomic32_mac.cc | 60 +++++++++ src/system_wrappers/source/atomic32_mac.h | 117 ----------------- src/system_wrappers/source/atomic32_win.cc | 72 +++++++++++ src/system_wrappers/source/atomic32_win.h | 113 ----------------- .../source/system_wrappers.gyp | 11 +- 14 files changed, 281 insertions(+), 509 deletions(-) create mode 100644 src/system_wrappers/interface/atomic32.h delete mode 100644 src/system_wrappers/interface/atomic32_wrapper.h delete mode 100644 src/system_wrappers/source/atomic32.cc create mode 100644 src/system_wrappers/source/atomic32_linux.cc delete mode 100644 src/system_wrappers/source/atomic32_linux.h create mode 100644 src/system_wrappers/source/atomic32_mac.cc delete mode 100644 src/system_wrappers/source/atomic32_mac.h create mode 100644 src/system_wrappers/source/atomic32_win.cc delete mode 100644 src/system_wrappers/source/atomic32_win.h diff --git a/src/modules/audio_conference_mixer/source/memory_pool_win.h b/src/modules/audio_conference_mixer/source/memory_pool_win.h index 1275ca1bab..8ff97f87b7 100644 --- a/src/modules/audio_conference_mixer/source/memory_pool_win.h +++ b/src/modules/audio_conference_mixer/source/memory_pool_win.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source @@ -15,7 +15,7 @@ #include #include "aligned_malloc.h" -#include "atomic32_wrapper.h" +#include "atomic32.h" #include "typedefs.h" namespace webrtc { @@ -66,8 +66,8 @@ private: // Atomic single linked list head. PSLIST_HEADER _pListHead; - Atomic32Wrapper _createdMemory; - Atomic32Wrapper _outstandingMemory; + Atomic32 _createdMemory; + Atomic32 _outstandingMemory; }; template diff --git a/src/modules/udp_transport/source/udp_socket2_manager_windows.h b/src/modules/udp_transport/source/udp_socket2_manager_windows.h index 5adb293653..782cb41cfe 100644 --- a/src/modules/udp_transport/source/udp_socket2_manager_windows.h +++ b/src/modules/udp_transport/source/udp_socket2_manager_windows.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source @@ -17,7 +17,7 @@ #include -#include "atomic32_wrapper.h" +#include "atomic32.h" #include "critical_section_wrapper.h" #include "event_wrapper.h" #include "list_wrapper.h" @@ -88,8 +88,8 @@ private: PSLIST_HEADER _pListHead; bool _init; - Atomic32Wrapper _size; - Atomic32Wrapper _inUse; + Atomic32 _size; + Atomic32 _inUse; }; diff --git a/src/modules/udp_transport/source/udp_socket2_windows.h b/src/modules/udp_transport/source/udp_socket2_windows.h index 3bb0753218..8cc46eefa7 100644 --- a/src/modules/udp_transport/source/udp_socket2_windows.h +++ b/src/modules/udp_transport/source/udp_socket2_windows.h @@ -19,7 +19,7 @@ #include #include -#include "atomic32_wrapper.h" +#include "atomic32.h" #include "condition_variable_wrapper.h" #include "critical_section_wrapper.h" #include "event_wrapper.h" @@ -129,8 +129,8 @@ private: UdpSocket2ManagerWindows* _mgr; CriticalSectionWrapper* _pCrit; - Atomic32Wrapper _outstandingCalls; - Atomic32Wrapper _outstandingCallComplete; + Atomic32 _outstandingCalls; + Atomic32 _outstandingCallComplete; volatile bool _terminate; volatile bool _addedToMgr; @@ -165,7 +165,7 @@ private: // Holds the current pcp value. Can be -2 or 0 - 7. int _pcp; - Atomic32Wrapper _receiveBuffers; + Atomic32 _receiveBuffers; }; } // namespace webrtc #endif // WEBRTC_MODULES_UDP_TRANSPORT_SOURCE_UDP_SOCKET2_WINDOWS_H_ diff --git a/src/system_wrappers/interface/atomic32.h b/src/system_wrappers/interface/atomic32.h new file mode 100644 index 0000000000..e2b12589d7 --- /dev/null +++ b/src/system_wrappers/interface/atomic32.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// Atomic, system independent 32-bit integer. Unless you know what you're +// doing, use locks instead! :-) +// +// Note: uses full memory barriers. +// Note: assumes 32-bit (or higher) system +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_ + +#include + +#include "common_types.h" +#include "constructor_magic.h" + +namespace webrtc { + +// 32 bit atomic variable. Note that this class relies on the compiler to +// align the 32 bit value correctly (on a 32 bit boundary), so as long as you're +// not doing things like reinterpret_cast over some custom allocated memory +// without being careful with alignment, you should be fine. +class Atomic32 +{ +public: + Atomic32(WebRtc_Word32 initialValue = 0); + ~Atomic32(); + + // Prefix operator! + WebRtc_Word32 operator++(); + WebRtc_Word32 operator--(); + + WebRtc_Word32 operator+=(WebRtc_Word32 value); + WebRtc_Word32 operator-=(WebRtc_Word32 value); + + // Sets the value atomically to newValue if the value equals compare value. + // The function returns true if the exchange happened. + bool CompareExchange(WebRtc_Word32 newValue, WebRtc_Word32 compareValue); + WebRtc_Word32 Value() const; + +private: + // Disable the + and - operator since it's unclear what these operations + // should do. + Atomic32 operator+(const Atomic32& other); + Atomic32 operator-(const Atomic32& other); + + // Checks if |_value| is 32bit aligned. + inline bool Is32bitAligned() const { + return (reinterpret_cast(&_value) & 3) == 0; + } + + DISALLOW_COPY_AND_ASSIGN(Atomic32); + + WebRtc_Word32 _value; +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_ diff --git a/src/system_wrappers/interface/atomic32_wrapper.h b/src/system_wrappers/interface/atomic32_wrapper.h deleted file mode 100644 index 40862fb492..0000000000 --- a/src/system_wrappers/interface/atomic32_wrapper.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -// Atomic system independant 32-bit integer. -// Note: uses full memory barriers. -// Note: assumes 32-bit (or higher) system -#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ -#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ - -#include "common_types.h" - -namespace webrtc { -class Atomic32Impl; -class Atomic32Wrapper -{ -public: - Atomic32Wrapper(WebRtc_Word32 initialValue = 0); - ~Atomic32Wrapper(); - - // Prefix operator! - WebRtc_Word32 operator++(); - WebRtc_Word32 operator--(); - - Atomic32Wrapper& operator=(const Atomic32Wrapper& rhs); - Atomic32Wrapper& operator=(WebRtc_Word32 rhs); - - WebRtc_Word32 operator+=(WebRtc_Word32 rhs); - WebRtc_Word32 operator-=(WebRtc_Word32 rhs); - - // Sets the value atomically to newValue if the value equals compare value. - // The function returns true if the exchange happened. - bool CompareExchange(WebRtc_Word32 newValue, WebRtc_Word32 compareValue); - WebRtc_Word32 Value() const; -private: - // Disable the + and - operator since it's unclear what these operations - // should do. - Atomic32Wrapper operator+(const Atomic32Wrapper& rhs); - Atomic32Wrapper operator-(const Atomic32Wrapper& rhs); - - WebRtc_Word32& operator++(int); - WebRtc_Word32& operator--(int); - - // Cheshire cat to hide the implementation (faster than - // using virtual functions) - Atomic32Impl& _impl; -}; -} // namespace webrtc -#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ diff --git a/src/system_wrappers/interface/ref_count.h b/src/system_wrappers/interface/ref_count.h index f90b0b3609..5112bd9037 100644 --- a/src/system_wrappers/interface/ref_count.h +++ b/src/system_wrappers/interface/ref_count.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source @@ -11,7 +11,7 @@ #ifndef SYSTEM_WRAPPERS_INTERFACE_REF_COUNT_H_ #define SYSTEM_WRAPPERS_INTERFACE_REF_COUNT_H_ -#include "system_wrappers/interface/atomic32_wrapper.h" +#include "system_wrappers/interface/atomic32.h" namespace webrtc { @@ -74,7 +74,7 @@ class RefCountImpl : public T { } protected: - Atomic32Wrapper ref_count_; + Atomic32 ref_count_; }; } // namespace webrtc diff --git a/src/system_wrappers/source/atomic32.cc b/src/system_wrappers/source/atomic32.cc deleted file mode 100644 index 588dd3e07d..0000000000 --- a/src/system_wrappers/source/atomic32.cc +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "atomic32_wrapper.h" - -#if defined(_WIN32) - #include "atomic32_win.h" -#elif defined(WEBRTC_LINUX) - #include "atomic32_linux.h" -#elif defined(WEBRTC_MAC) - #include "atomic32_mac.h" -#else - #error unsupported os! -#endif - -namespace webrtc { -Atomic32Wrapper::Atomic32Wrapper(WebRtc_Word32 initialValue) - : _impl(*new Atomic32Impl(initialValue)) -{ -} - -Atomic32Wrapper::~Atomic32Wrapper() -{ - delete &_impl; -} - -WebRtc_Word32 Atomic32Wrapper::operator++() -{ - return ++_impl; -} - -WebRtc_Word32 Atomic32Wrapper::operator--() -{ - return --_impl; -} - -// Read and write to properly aligned variables are atomic operations. -// Ex reference (for Windows): http://msdn.microsoft.com/en-us/library/ms684122(v=VS.85).aspx -// TODO (hellner) operator= and Atomic32Wrapper::Value() can be fully -// implemented here. -Atomic32Wrapper& Atomic32Wrapper::operator=(const Atomic32Wrapper& rhs) -{ - if(this == &rhs) - { - return *this; - } - _impl = rhs._impl; - return *this; -} - -Atomic32Wrapper& Atomic32Wrapper::operator=(WebRtc_Word32 rhs) -{ - _impl = rhs; - return *this; -} - -WebRtc_Word32 Atomic32Wrapper::operator+=(WebRtc_Word32 rhs) -{ - return _impl += rhs; -} - -WebRtc_Word32 Atomic32Wrapper::operator-=(WebRtc_Word32 rhs) -{ - return _impl -= rhs; -} - -bool Atomic32Wrapper::CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue) -{ - return _impl.CompareExchange(newValue,compareValue); -} - -WebRtc_Word32 Atomic32Wrapper::Value() const -{ - return _impl.Value(); -} -} // namespace webrtc diff --git a/src/system_wrappers/source/atomic32_linux.cc b/src/system_wrappers/source/atomic32_linux.cc new file mode 100644 index 0000000000..05b0e57456 --- /dev/null +++ b/src/system_wrappers/source/atomic32_linux.cc @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "atomic32.h" + +#include +#include +#include + +#include "common_types.h" + +namespace webrtc { + +Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue) +{ + assert(Is32bitAligned()); +} + +Atomic32::~Atomic32() +{ +} + +WebRtc_Word32 Atomic32::operator++() +{ + return __sync_fetch_and_add(&_value, 1) + 1; +} + +WebRtc_Word32 Atomic32::operator--() +{ + return __sync_fetch_and_sub(&_value, 1) - 1; +} + +WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) +{ + WebRtc_Word32 returnValue = __sync_fetch_and_add(&_value, value); + returnValue += value; + return returnValue; +} + +WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) +{ + WebRtc_Word32 returnValue = __sync_fetch_and_sub(&_value, value); + returnValue -= value; + return returnValue; +} + +bool Atomic32::CompareExchange(WebRtc_Word32 newValue, + WebRtc_Word32 compareValue) +{ + return __sync_bool_compare_and_swap(&_value, compareValue, newValue); +} + +WebRtc_Word32 Atomic32::Value() const +{ + return _value; +} +} // namespace webrtc diff --git a/src/system_wrappers/source/atomic32_linux.h b/src/system_wrappers/source/atomic32_linux.h deleted file mode 100644 index f9f5650f2a..0000000000 --- a/src/system_wrappers/source/atomic32_linux.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -// Atomic system independant 32-bit signed integer. -// Linux implementation. -// Note: Requires gcc 4.1.2 or later. -#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_ -#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_ - -#include -#include - -#include "common_types.h" - -namespace webrtc { -class Atomic32Impl -{ -public: - inline Atomic32Impl(WebRtc_Word32 initialValue); - inline ~Atomic32Impl(); - - inline WebRtc_Word32 operator++(); - inline WebRtc_Word32 operator--(); - - inline Atomic32Impl& operator=(const Atomic32Impl& rhs); - inline Atomic32Impl& operator=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator+=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator-=(WebRtc_Word32 rhs); - - inline bool CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue); - - inline WebRtc_Word32 Value() const; -private: - void* _ptrMemory; - // Volatile ensures full memory barriers. - volatile WebRtc_Word32* _value; -}; - -// TODO (hellner) use aligned_malloc instead of doing it manually. -inline Atomic32Impl::Atomic32Impl(WebRtc_Word32 initialValue) - : _ptrMemory(NULL), - _value(NULL) -{ // Align the memory associated with _value on a 32-bit boundary. This is a - // requirement for the used Linux APIs to be atomic. - // Keep _ptrMemory to be able to reclaim memory. - _ptrMemory = malloc(sizeof(WebRtc_Word32)*2); - _value = (WebRtc_Word32*) (((uintptr_t)_ptrMemory+3)&(~0x3)); - *_value = initialValue; -} - -inline Atomic32Impl::~Atomic32Impl() -{ - if(_ptrMemory != NULL) - { - free(_ptrMemory); - } -} - -inline WebRtc_Word32 Atomic32Impl::operator++() -{ - WebRtc_Word32 returnValue = __sync_fetch_and_add(_value,1); - returnValue++; - return returnValue; -} - -inline WebRtc_Word32 Atomic32Impl::operator--() -{ - WebRtc_Word32 returnValue = __sync_fetch_and_sub(_value,1); - returnValue--; - return returnValue; -} - -inline Atomic32Impl& Atomic32Impl::operator=(const Atomic32Impl& rhs) -{ - *_value = *rhs._value; - return *this; -} - -inline Atomic32Impl& Atomic32Impl::operator=(WebRtc_Word32 rhs) -{ - *_value = rhs; - return *this; -} - -inline WebRtc_Word32 Atomic32Impl::operator+=(WebRtc_Word32 rhs) -{ - WebRtc_Word32 returnValue = __sync_fetch_and_add(_value,rhs); - returnValue += rhs; - return returnValue; -} - -inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs) -{ - WebRtc_Word32 returnValue = __sync_fetch_and_sub(_value,rhs); - returnValue -= rhs; - return returnValue; -} - -inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue) -{ - return __sync_bool_compare_and_swap(_value,compareValue,newValue); -} - -inline WebRtc_Word32 Atomic32Impl::Value() const -{ - return *_value; -} -} // namespace webrtc - -#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_ diff --git a/src/system_wrappers/source/atomic32_mac.cc b/src/system_wrappers/source/atomic32_mac.cc new file mode 100644 index 0000000000..9a493b580c --- /dev/null +++ b/src/system_wrappers/source/atomic32_mac.cc @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "atomic32.h" + +#include +#include +#include + +#include "common_types.h" + +namespace webrtc { + +Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue) +{ + assert(Is32bitAligned()); +} + +Atomic32::~Atomic32() +{ +} + +WebRtc_Word32 Atomic32::operator++() +{ + return OSAtomicIncrement32Barrier(&_value); +} + +WebRtc_Word32 Atomic32::operator--() +{ + return OSAtomicDecrement32Barrier(&_value); +} + +WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) +{ + return OSAtomicAdd32Barrier(value, &_value); +} + +WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) +{ + return OSAtomicAdd32Barrier(-value, &_value); +} + +bool Atomic32::CompareExchange(WebRtc_Word32 newValue, + WebRtc_Word32 compareValue) +{ + return OSAtomicCompareAndSwap32Barrier(compareValue, newValue, &_value); +} + +WebRtc_Word32 Atomic32::Value() const +{ + return _value; +} +} // namespace webrtc diff --git a/src/system_wrappers/source/atomic32_mac.h b/src/system_wrappers/source/atomic32_mac.h deleted file mode 100644 index bf8febcdb5..0000000000 --- a/src/system_wrappers/source/atomic32_mac.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -// Atomic system independant 32-bit signed integer. -// Mac implementation. -#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_MAC_H_ -#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_MAC_H_ - -#include -#include - -#include "common_types.h" - -namespace webrtc { -class Atomic32Impl -{ -public: - inline Atomic32Impl(WebRtc_Word32 initialValue); - inline ~Atomic32Impl(); - - inline WebRtc_Word32 operator++(); - inline WebRtc_Word32 operator--(); - - inline Atomic32Impl& operator=(const Atomic32Impl& rhs); - inline Atomic32Impl& operator=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator+=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator-=(WebRtc_Word32 rhs); - - inline bool CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue); - - inline WebRtc_Word32 Value() const; -private: - void* _ptrMemory; - // Volatile ensures full memory barriers. - volatile WebRtc_Word32* _value; -}; - -// TODO (hellner) use aligned_malloc instead of doing it manually. -inline Atomic32Impl::Atomic32Impl(WebRtc_Word32 initialValue) - : - _ptrMemory(NULL), - _value(NULL) -{ // Align the memory associated with _value on a 32-bit boundary. This is a - // requirement for the used Mac APIs to be atomic. - // Keep _ptrMemory to be able to reclaim memory. - _ptrMemory = malloc(sizeof(WebRtc_Word32)*2); - _value = (WebRtc_Word32*) (((uintptr_t)_ptrMemory+3)&(~0x3)); - *_value = initialValue; -} - -inline Atomic32Impl::~Atomic32Impl() -{ - if(_ptrMemory != NULL) - { - free(_ptrMemory); - } -} - -inline WebRtc_Word32 Atomic32Impl::operator++() -{ - return OSAtomicIncrement32Barrier( - reinterpret_cast(_value)); -} - -inline WebRtc_Word32 Atomic32Impl::operator--() -{ - return OSAtomicDecrement32Barrier( - reinterpret_cast(_value)); -} - -inline Atomic32Impl& Atomic32Impl::operator=(const Atomic32Impl& rhs) -{ - *_value = *rhs._value; - return *this; -} - -inline Atomic32Impl& Atomic32Impl::operator=(WebRtc_Word32 rhs) -{ - *_value = rhs; - return *this; -} - -inline WebRtc_Word32 Atomic32Impl::operator+=(WebRtc_Word32 rhs) -{ - return OSAtomicAdd32Barrier(rhs, - reinterpret_cast(_value)); -} - -inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs) -{ - return OSAtomicAdd32Barrier(-rhs, - reinterpret_cast(_value)); -} - -inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue) -{ - return OSAtomicCompareAndSwap32Barrier( - compareValue, - newValue, - reinterpret_cast(_value)); -} - -inline WebRtc_Word32 Atomic32Impl::Value() const -{ - return *_value; -} -} // namespace webrtc -#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_MAC_H_ diff --git a/src/system_wrappers/source/atomic32_win.cc b/src/system_wrappers/source/atomic32_win.cc new file mode 100644 index 0000000000..3bc7f77f0e --- /dev/null +++ b/src/system_wrappers/source/atomic32_win.cc @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "atomic32.h" + +#include +#include + +#include "common_types.h" +#include "compile_assert.h" + +namespace webrtc { + +Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue) +{ + // Make sure that the counter variable we're using is of the same size + // as what the API expects. + COMPILE_ASSERT(sizeof(_value) == sizeof(LONG), atomic_size_mismatch); + assert(Is32bitAligned()); +} + +Atomic32::~Atomic32() +{ +} + +WebRtc_Word32 Atomic32::operator++() +{ + return static_cast(InterlockedIncrement( + reinterpret_cast(&_value))); +} + +WebRtc_Word32 Atomic32::operator--() +{ + return static_cast(InterlockedDecrement( + reinterpret_cast(&_value))); +} + +WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) +{ + return InterlockedExchangeAdd(reinterpret_cast(&_value), + value); +} + +WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) +{ + return InterlockedExchangeAdd(reinterpret_cast(&_value), + -value); +} + +bool Atomic32::CompareExchange(WebRtc_Word32 newValue, + WebRtc_Word32 compareValue) +{ + const LONG oldValue = InterlockedCompareExchange( + reinterpret_cast(&_value), + newValue, + compareValue); + // If the old value and the compare value is the same an exchange happened. + return (oldValue == compareValue); +} + +WebRtc_Word32 Atomic32::Value() const +{ + return _value; +} +} // namespace webrtc diff --git a/src/system_wrappers/source/atomic32_win.h b/src/system_wrappers/source/atomic32_win.h deleted file mode 100644 index c27e48ea44..0000000000 --- a/src/system_wrappers/source/atomic32_win.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -// Atomic system independant 32-bit signed integer. -// Windows implementation. -#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ -#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ - -#include -#include - -#include "common_types.h" - -namespace webrtc { -class Atomic32Impl -{ -public: - inline Atomic32Impl(WebRtc_Word32 initialValue); - inline ~Atomic32Impl(); - - inline WebRtc_Word32 operator++(); - inline WebRtc_Word32 operator--(); - - inline Atomic32Impl& operator=(const Atomic32Impl& rhs); - inline Atomic32Impl& operator=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator+=(WebRtc_Word32 rhs); - inline WebRtc_Word32 operator-=(WebRtc_Word32 rhs); - - inline bool CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue); - - inline WebRtc_Word32 Value() const; -private: - void* _ptrMemory; - // Volatile ensures full memory barriers. - volatile LONG* _value; -}; - -// TODO (hellner) use aligned_malloc instead of doing it manually. -inline Atomic32Impl::Atomic32Impl(WebRtc_Word32 initialValue) - : _ptrMemory(NULL), - _value(NULL) -{ // Align the memory associated with _value on a 32-bit boundary. This is a - // requirement for the used Windows APIs to be atomic. - // Keep _ptrMemory to be able to reclaim memory. - _ptrMemory = malloc(sizeof(WebRtc_Word32)*2); - _value = reinterpret_cast (((uintptr_t)_ptrMemory+3)&(~0x3)); - *_value = initialValue; -} - -inline Atomic32Impl::~Atomic32Impl() -{ - if(_ptrMemory != NULL) - { - free(_ptrMemory); - } -} - -inline WebRtc_Word32 Atomic32Impl::operator++() -{ - return (WebRtc_Word32)InterlockedIncrement(_value); -} - -inline WebRtc_Word32 Atomic32Impl::operator--() -{ - return (WebRtc_Word32)InterlockedDecrement(_value); -} - -inline Atomic32Impl& Atomic32Impl::operator=(const Atomic32Impl& rhs) -{ - *_value = *rhs._value; - return *this; -} - -inline Atomic32Impl& Atomic32Impl::operator=(WebRtc_Word32 rhs) -{ - *_value = rhs; - return *this; -} - -inline WebRtc_Word32 Atomic32Impl::operator+=(WebRtc_Word32 rhs) -{ - return InterlockedExchangeAdd(_value,rhs); -} - -inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs) -{ - return InterlockedExchangeAdd(_value,-rhs); -} - -inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue, - WebRtc_Word32 compareValue) -{ - const LONG oldValue = InterlockedCompareExchange(_value,newValue, - compareValue); - // If the old value and the compare value is the same an exchange happened. - return (oldValue == compareValue); -} - -inline WebRtc_Word32 Atomic32Impl::Value() const -{ - return *_value; -} -} // namespace webrtc - -#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_WINDOWS_H_ diff --git a/src/system_wrappers/source/system_wrappers.gyp b/src/system_wrappers/source/system_wrappers.gyp index 1d066b49d7..5433b7b149 100644 --- a/src/system_wrappers/source/system_wrappers.gyp +++ b/src/system_wrappers/source/system_wrappers.gyp @@ -1,4 +1,4 @@ -# Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. +# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. # # Use of this source code is governed by a BSD-style license # that can be found in the LICENSE file in the root of the source @@ -23,7 +23,7 @@ }, 'sources': [ '../interface/aligned_malloc.h', - '../interface/atomic32_wrapper.h', + '../interface/atomic32.h', '../interface/compile_assert.h', '../interface/condition_variable_wrapper.h', '../interface/cpu_info.h', @@ -48,10 +48,9 @@ '../interface/tick_util.h', '../interface/trace.h', 'aligned_malloc.cc', - 'atomic32.cc', - 'atomic32_linux.h', - 'atomic32_mac.h', - 'atomic32_win.h', + 'atomic32_linux.cc', + 'atomic32_mac.cc', + 'atomic32_win.cc', 'condition_variable.cc', 'condition_variable_posix.cc', 'condition_variable_posix.h',