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
This commit is contained in:
		| @@ -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 <windows.h> | ||||
|  | ||||
| #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<class MemoryType> | ||||
|   | ||||
| @@ -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 <winsock2.h> | ||||
|  | ||||
| #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; | ||||
| }; | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -19,7 +19,7 @@ | ||||
| #include <Ntddndis.h> | ||||
| #include <traffic.h> | ||||
|  | ||||
| #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_ | ||||
|   | ||||
							
								
								
									
										65
									
								
								src/system_wrappers/interface/atomic32.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/system_wrappers/interface/atomic32.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <cstddef> | ||||
|  | ||||
| #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<ptrdiff_t>(&_value) & 3) == 0; | ||||
|     } | ||||
|  | ||||
|     DISALLOW_COPY_AND_ASSIGN(Atomic32); | ||||
|  | ||||
|     WebRtc_Word32 _value; | ||||
| }; | ||||
| }  // namespace webrtc | ||||
|  | ||||
| #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_H_ | ||||
| @@ -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_ | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
							
								
								
									
										64
									
								
								src/system_wrappers/source/atomic32_linux.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/system_wrappers/source/atomic32_linux.cc
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <assert.h> | ||||
| #include <inttypes.h> | ||||
| #include <malloc.h> | ||||
|  | ||||
| #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 | ||||
| @@ -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 <inttypes.h> | ||||
| #include <malloc.h> | ||||
|  | ||||
| #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_ | ||||
							
								
								
									
										60
									
								
								src/system_wrappers/source/atomic32_mac.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/system_wrappers/source/atomic32_mac.cc
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <assert.h> | ||||
| #include <libkern/OSAtomic.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #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 | ||||
| @@ -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 <stdlib.h> | ||||
| #include <libkern/OSAtomic.h> | ||||
|  | ||||
| #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<volatile int32_t*>(_value)); | ||||
| } | ||||
|  | ||||
| inline WebRtc_Word32 Atomic32Impl::operator--() | ||||
| { | ||||
|     return OSAtomicDecrement32Barrier( | ||||
|                reinterpret_cast<volatile int32_t*>(_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<volatile int32_t*>(_value)); | ||||
| } | ||||
|  | ||||
| inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs) | ||||
| { | ||||
|     return OSAtomicAdd32Barrier(-rhs, | ||||
|                                 reinterpret_cast<volatile int32_t*>(_value)); | ||||
| } | ||||
|  | ||||
| inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue, | ||||
|                                           WebRtc_Word32 compareValue) | ||||
| { | ||||
|     return OSAtomicCompareAndSwap32Barrier( | ||||
|                compareValue, | ||||
|                newValue, | ||||
|                reinterpret_cast<volatile int32_t*>(_value)); | ||||
| } | ||||
|  | ||||
| inline WebRtc_Word32 Atomic32Impl::Value() const | ||||
| { | ||||
|     return *_value; | ||||
| } | ||||
| } // namespace webrtc | ||||
| #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_MAC_H_ | ||||
							
								
								
									
										72
									
								
								src/system_wrappers/source/atomic32_win.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/system_wrappers/source/atomic32_win.cc
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <assert.h> | ||||
| #include <windows.h> | ||||
|  | ||||
| #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<WebRtc_Word32>(InterlockedIncrement( | ||||
|         reinterpret_cast<volatile LONG*>(&_value))); | ||||
| } | ||||
|  | ||||
| WebRtc_Word32 Atomic32::operator--() | ||||
| { | ||||
|     return static_cast<WebRtc_Word32>(InterlockedDecrement( | ||||
|         reinterpret_cast<volatile LONG*>(&_value))); | ||||
| } | ||||
|  | ||||
| WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) | ||||
| { | ||||
|     return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value), | ||||
|                                   value); | ||||
| } | ||||
|  | ||||
| WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) | ||||
| { | ||||
|     return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value), | ||||
|                                   -value); | ||||
| } | ||||
|  | ||||
| bool Atomic32::CompareExchange(WebRtc_Word32 newValue, | ||||
|                                WebRtc_Word32 compareValue) | ||||
| { | ||||
|     const LONG oldValue = InterlockedCompareExchange( | ||||
|           reinterpret_cast<volatile LONG*>(&_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 | ||||
| @@ -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 <malloc.h> | ||||
| #include <windows.h> | ||||
|  | ||||
| #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<LONG*> (((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_ | ||||
| @@ -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', | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 tommi@webrtc.org
					tommi@webrtc.org