Chromium Code Reviews| Index: webrtc/base/criticalsection.cc |
| diff --git a/webrtc/base/criticalsection.cc b/webrtc/base/criticalsection.cc |
| index b15bb82ef4fb40da3f96a476fe09fec23e5c1fb2..b642514817e0e2c8a00d738404c7dfb99fd65eed 100644 |
| --- a/webrtc/base/criticalsection.cc |
| +++ b/webrtc/base/criticalsection.cc |
| @@ -20,41 +20,47 @@ namespace rtc { |
| CriticalSection::CriticalSection() { |
| #if defined(WEBRTC_WIN) |
| InitializeCriticalSection(&crit_); |
| -#else |
| -#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| +#elif defined(WEBRTC_POSIX) |
| +# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| lock_queue_ = 0; |
| owning_thread_ = 0; |
| recursion_ = 0; |
| semaphore_ = dispatch_semaphore_create(0); |
| -#else |
| +# else |
| pthread_mutexattr_t mutex_attribute; |
| pthread_mutexattr_init(&mutex_attribute); |
| pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| pthread_mutex_init(&mutex_, &mutex_attribute); |
| pthread_mutexattr_destroy(&mutex_attribute); |
| -#endif |
| +# endif |
| CS_DEBUG_CODE(thread_ = 0); |
| CS_DEBUG_CODE(recursion_count_ = 0); |
| + RTC_UNUSED(thread_); |
| + RTC_UNUSED(recursion_count_); |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
|
nisse-webrtc
2017/06/26 13:49:46
I'd prefer dropping this comment, it adds litte in
eladalon
2017/06/26 14:22:32
I've removed, though my own preference would have
|
| +#error Unsupported platform. |
| #endif |
| } |
| CriticalSection::~CriticalSection() { |
| #if defined(WEBRTC_WIN) |
| DeleteCriticalSection(&crit_); |
| -#else |
| -#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| +#elif defined(WEBRTC_POSIX) |
| +# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| dispatch_release(semaphore_); |
| -#else |
| +# else |
| pthread_mutex_destroy(&mutex_); |
| -#endif |
| +# endif |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| +#error Unsupported platform. |
| #endif |
| } |
| void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { |
| #if defined(WEBRTC_WIN) |
| EnterCriticalSection(&crit_); |
| -#else |
| -#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| +#elif defined(WEBRTC_POSIX) |
| +# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| int spin = 3000; |
| PlatformThreadRef self = CurrentThreadRef(); |
| bool have_lock = false; |
| @@ -91,11 +97,11 @@ void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { |
| owning_thread_ = self; |
| ++recursion_; |
| -#else |
| +# else |
| pthread_mutex_lock(&mutex_); |
| -#endif |
| +# endif |
| -#if CS_DEBUG_CHECKS |
| +# if CS_DEBUG_CHECKS |
| if (!recursion_count_) { |
| RTC_DCHECK(!thread_); |
| thread_ = CurrentThreadRef(); |
| @@ -103,15 +109,17 @@ void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { |
| RTC_DCHECK(CurrentThreadIsOwner()); |
| } |
| ++recursion_count_; |
| -#endif |
| +# endif |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| +#error Unsupported platform. |
| #endif |
| } |
| bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| #if defined(WEBRTC_WIN) |
| return TryEnterCriticalSection(&crit_) != FALSE; |
| -#else |
| -#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| +#elif defined(WEBRTC_POSIX) |
| +# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { |
| if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) |
| return false; |
| @@ -121,11 +129,11 @@ bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| AtomicOps::Increment(&lock_queue_); |
| } |
| ++recursion_; |
| -#else |
| +# else |
| if (pthread_mutex_trylock(&mutex_) != 0) |
| return false; |
| -#endif |
| -#if CS_DEBUG_CHECKS |
| +# endif |
| +# if CS_DEBUG_CHECKS |
| if (!recursion_count_) { |
| RTC_DCHECK(!thread_); |
| thread_ = CurrentThreadRef(); |
| @@ -133,22 +141,25 @@ bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| RTC_DCHECK(CurrentThreadIsOwner()); |
| } |
| ++recursion_count_; |
| -#endif |
| +# endif |
| return true; |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| +#error Unsupported platform. |
| #endif |
| } |
| + |
| void CriticalSection::Leave() const UNLOCK_FUNCTION() { |
| RTC_DCHECK(CurrentThreadIsOwner()); |
| #if defined(WEBRTC_WIN) |
| LeaveCriticalSection(&crit_); |
| -#else |
| -#if CS_DEBUG_CHECKS |
| +#elif defined(WEBRTC_POSIX) |
| +# if CS_DEBUG_CHECKS |
| --recursion_count_; |
| RTC_DCHECK(recursion_count_ >= 0); |
| if (!recursion_count_) |
| thread_ = 0; |
| -#endif |
| -#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| +# endif |
| +# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); |
| RTC_DCHECK_GE(recursion_, 0); |
| --recursion_; |
| @@ -157,9 +168,11 @@ void CriticalSection::Leave() const UNLOCK_FUNCTION() { |
| if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) |
| dispatch_semaphore_signal(semaphore_); |
| -#else |
| +# else |
| pthread_mutex_unlock(&mutex_); |
| -#endif |
| +# endif |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| +#error Unsupported platform. |
| #endif |
| } |
| @@ -171,12 +184,14 @@ bool CriticalSection::CurrentThreadIsOwner() const { |
| // 'type1' to 'type2' of greater size |
| return crit_.OwningThread == |
| reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); |
| -#else |
| -#if CS_DEBUG_CHECKS |
| +#elif defined(WEBRTC_POSIX) |
| +# if CS_DEBUG_CHECKS |
| return IsThreadRefEqual(thread_, CurrentThreadRef()); |
| -#else |
| +# else |
| return true; |
| -#endif // CS_DEBUG_CHECKS |
| +# endif // CS_DEBUG_CHECKS |
| +#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| +#error Unsupported platform. |
| #endif |
| } |
| @@ -186,6 +201,7 @@ CritScope::~CritScope() { cs_->Leave(); } |
| TryCritScope::TryCritScope(const CriticalSection* cs) |
| : cs_(cs), locked_(cs->TryEnter()) { |
| CS_DEBUG_CODE(lock_was_called_ = false); |
| + RTC_UNUSED(lock_was_called_); |
| } |
| TryCritScope::~TryCritScope() { |