Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/criticalsection.h" | 11 #include "webrtc/base/criticalsection.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/platform_thread.h" | 14 #include "webrtc/base/platform_thread.h" |
| 15 | 15 |
| 16 // TODO(tommi): Split this file up to per-platform implementation files. | 16 // TODO(tommi): Split this file up to per-platform implementation files. |
| 17 | 17 |
| 18 namespace rtc { | 18 namespace rtc { |
| 19 | 19 |
| 20 CriticalSection::CriticalSection() { | 20 CriticalSection::CriticalSection() { |
| 21 #if defined(WEBRTC_WIN) | 21 #if defined(WEBRTC_WIN) |
| 22 InitializeCriticalSection(&crit_); | 22 InitializeCriticalSection(&crit_); |
| 23 #else | 23 #elif defined(WEBRTC_POSIX) |
| 24 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 24 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 25 lock_queue_ = 0; | 25 lock_queue_ = 0; |
| 26 owning_thread_ = 0; | 26 owning_thread_ = 0; |
| 27 recursion_ = 0; | 27 recursion_ = 0; |
| 28 semaphore_ = dispatch_semaphore_create(0); | 28 semaphore_ = dispatch_semaphore_create(0); |
| 29 #else | 29 # else |
| 30 pthread_mutexattr_t mutex_attribute; | 30 pthread_mutexattr_t mutex_attribute; |
| 31 pthread_mutexattr_init(&mutex_attribute); | 31 pthread_mutexattr_init(&mutex_attribute); |
| 32 pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); | 32 pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| 33 pthread_mutex_init(&mutex_, &mutex_attribute); | 33 pthread_mutex_init(&mutex_, &mutex_attribute); |
| 34 pthread_mutexattr_destroy(&mutex_attribute); | 34 pthread_mutexattr_destroy(&mutex_attribute); |
| 35 #endif | 35 # endif |
| 36 CS_DEBUG_CODE(thread_ = 0); | 36 CS_DEBUG_CODE(thread_ = 0); |
| 37 CS_DEBUG_CODE(recursion_count_ = 0); | 37 CS_DEBUG_CODE(recursion_count_ = 0); |
| 38 RTC_UNUSED(thread_); | |
| 39 RTC_UNUSED(recursion_count_); | |
| 40 #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
| |
| 41 #error Unsupported platform. | |
| 38 #endif | 42 #endif |
| 39 } | 43 } |
| 40 | 44 |
| 41 CriticalSection::~CriticalSection() { | 45 CriticalSection::~CriticalSection() { |
| 42 #if defined(WEBRTC_WIN) | 46 #if defined(WEBRTC_WIN) |
| 43 DeleteCriticalSection(&crit_); | 47 DeleteCriticalSection(&crit_); |
| 44 #else | 48 #elif defined(WEBRTC_POSIX) |
| 45 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 49 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 46 dispatch_release(semaphore_); | 50 dispatch_release(semaphore_); |
| 47 #else | 51 # else |
| 48 pthread_mutex_destroy(&mutex_); | 52 pthread_mutex_destroy(&mutex_); |
| 49 #endif | 53 # endif |
| 54 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) | |
| 55 #error Unsupported platform. | |
| 50 #endif | 56 #endif |
| 51 } | 57 } |
| 52 | 58 |
| 53 void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { | 59 void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { |
| 54 #if defined(WEBRTC_WIN) | 60 #if defined(WEBRTC_WIN) |
| 55 EnterCriticalSection(&crit_); | 61 EnterCriticalSection(&crit_); |
| 56 #else | 62 #elif defined(WEBRTC_POSIX) |
| 57 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 63 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 58 int spin = 3000; | 64 int spin = 3000; |
| 59 PlatformThreadRef self = CurrentThreadRef(); | 65 PlatformThreadRef self = CurrentThreadRef(); |
| 60 bool have_lock = false; | 66 bool have_lock = false; |
| 61 do { | 67 do { |
| 62 // Instead of calling TryEnter() in this loop, we do two interlocked | 68 // Instead of calling TryEnter() in this loop, we do two interlocked |
| 63 // operations, first a read-only one in order to avoid affecting the lock | 69 // operations, first a read-only one in order to avoid affecting the lock |
| 64 // cache-line while spinning, in case another thread is using the lock. | 70 // cache-line while spinning, in case another thread is using the lock. |
| 65 if (!IsThreadRefEqual(owning_thread_, self)) { | 71 if (!IsThreadRefEqual(owning_thread_, self)) { |
| 66 if (AtomicOps::AcquireLoad(&lock_queue_) == 0) { | 72 if (AtomicOps::AcquireLoad(&lock_queue_) == 0) { |
| 67 if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) { | 73 if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 84 RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self)); | 90 RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self)); |
| 85 // Wait for the lock to become available. | 91 // Wait for the lock to become available. |
| 86 dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER); | 92 dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER); |
| 87 RTC_DCHECK(owning_thread_ == 0); | 93 RTC_DCHECK(owning_thread_ == 0); |
| 88 RTC_DCHECK(!recursion_); | 94 RTC_DCHECK(!recursion_); |
| 89 } | 95 } |
| 90 | 96 |
| 91 owning_thread_ = self; | 97 owning_thread_ = self; |
| 92 ++recursion_; | 98 ++recursion_; |
| 93 | 99 |
| 94 #else | 100 # else |
| 95 pthread_mutex_lock(&mutex_); | 101 pthread_mutex_lock(&mutex_); |
| 96 #endif | 102 # endif |
| 97 | 103 |
| 98 #if CS_DEBUG_CHECKS | 104 # if CS_DEBUG_CHECKS |
| 99 if (!recursion_count_) { | 105 if (!recursion_count_) { |
| 100 RTC_DCHECK(!thread_); | 106 RTC_DCHECK(!thread_); |
| 101 thread_ = CurrentThreadRef(); | 107 thread_ = CurrentThreadRef(); |
| 102 } else { | 108 } else { |
| 103 RTC_DCHECK(CurrentThreadIsOwner()); | 109 RTC_DCHECK(CurrentThreadIsOwner()); |
| 104 } | 110 } |
| 105 ++recursion_count_; | 111 ++recursion_count_; |
| 106 #endif | 112 # endif |
| 113 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) | |
| 114 #error Unsupported platform. | |
| 107 #endif | 115 #endif |
| 108 } | 116 } |
| 109 | 117 |
| 110 bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { | 118 bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 111 #if defined(WEBRTC_WIN) | 119 #if defined(WEBRTC_WIN) |
| 112 return TryEnterCriticalSection(&crit_) != FALSE; | 120 return TryEnterCriticalSection(&crit_) != FALSE; |
| 113 #else | 121 #elif defined(WEBRTC_POSIX) |
| 114 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 122 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 115 if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { | 123 if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { |
| 116 if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) | 124 if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) |
| 117 return false; | 125 return false; |
| 118 owning_thread_ = CurrentThreadRef(); | 126 owning_thread_ = CurrentThreadRef(); |
| 119 RTC_DCHECK(!recursion_); | 127 RTC_DCHECK(!recursion_); |
| 120 } else { | 128 } else { |
| 121 AtomicOps::Increment(&lock_queue_); | 129 AtomicOps::Increment(&lock_queue_); |
| 122 } | 130 } |
| 123 ++recursion_; | 131 ++recursion_; |
| 124 #else | 132 # else |
| 125 if (pthread_mutex_trylock(&mutex_) != 0) | 133 if (pthread_mutex_trylock(&mutex_) != 0) |
| 126 return false; | 134 return false; |
| 127 #endif | 135 # endif |
| 128 #if CS_DEBUG_CHECKS | 136 # if CS_DEBUG_CHECKS |
| 129 if (!recursion_count_) { | 137 if (!recursion_count_) { |
| 130 RTC_DCHECK(!thread_); | 138 RTC_DCHECK(!thread_); |
| 131 thread_ = CurrentThreadRef(); | 139 thread_ = CurrentThreadRef(); |
| 132 } else { | 140 } else { |
| 133 RTC_DCHECK(CurrentThreadIsOwner()); | 141 RTC_DCHECK(CurrentThreadIsOwner()); |
| 134 } | 142 } |
| 135 ++recursion_count_; | 143 ++recursion_count_; |
| 136 #endif | 144 # endif |
| 137 return true; | 145 return true; |
| 146 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) | |
| 147 #error Unsupported platform. | |
| 138 #endif | 148 #endif |
| 139 } | 149 } |
| 150 | |
| 140 void CriticalSection::Leave() const UNLOCK_FUNCTION() { | 151 void CriticalSection::Leave() const UNLOCK_FUNCTION() { |
| 141 RTC_DCHECK(CurrentThreadIsOwner()); | 152 RTC_DCHECK(CurrentThreadIsOwner()); |
| 142 #if defined(WEBRTC_WIN) | 153 #if defined(WEBRTC_WIN) |
| 143 LeaveCriticalSection(&crit_); | 154 LeaveCriticalSection(&crit_); |
| 144 #else | 155 #elif defined(WEBRTC_POSIX) |
| 145 #if CS_DEBUG_CHECKS | 156 # if CS_DEBUG_CHECKS |
| 146 --recursion_count_; | 157 --recursion_count_; |
| 147 RTC_DCHECK(recursion_count_ >= 0); | 158 RTC_DCHECK(recursion_count_ >= 0); |
| 148 if (!recursion_count_) | 159 if (!recursion_count_) |
| 149 thread_ = 0; | 160 thread_ = 0; |
| 150 #endif | 161 # endif |
| 151 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 162 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 152 RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); | 163 RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); |
| 153 RTC_DCHECK_GE(recursion_, 0); | 164 RTC_DCHECK_GE(recursion_, 0); |
| 154 --recursion_; | 165 --recursion_; |
| 155 if (!recursion_) | 166 if (!recursion_) |
| 156 owning_thread_ = 0; | 167 owning_thread_ = 0; |
| 157 | 168 |
| 158 if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) | 169 if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) |
| 159 dispatch_semaphore_signal(semaphore_); | 170 dispatch_semaphore_signal(semaphore_); |
| 160 #else | 171 # else |
| 161 pthread_mutex_unlock(&mutex_); | 172 pthread_mutex_unlock(&mutex_); |
| 162 #endif | 173 # endif |
| 174 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) | |
| 175 #error Unsupported platform. | |
| 163 #endif | 176 #endif |
| 164 } | 177 } |
| 165 | 178 |
| 166 bool CriticalSection::CurrentThreadIsOwner() const { | 179 bool CriticalSection::CurrentThreadIsOwner() const { |
| 167 #if defined(WEBRTC_WIN) | 180 #if defined(WEBRTC_WIN) |
| 168 // OwningThread has type HANDLE but actually contains the Thread ID: | 181 // OwningThread has type HANDLE but actually contains the Thread ID: |
| 169 // http://stackoverflow.com/questions/12675301/why-is-the-owningthread-member- of-critical-section-of-type-handle-when-it-is-de | 182 // http://stackoverflow.com/questions/12675301/why-is-the-owningthread-member- of-critical-section-of-type-handle-when-it-is-de |
| 170 // Converting through size_t avoids the VS 2015 warning C4312: conversion from | 183 // Converting through size_t avoids the VS 2015 warning C4312: conversion from |
| 171 // 'type1' to 'type2' of greater size | 184 // 'type1' to 'type2' of greater size |
| 172 return crit_.OwningThread == | 185 return crit_.OwningThread == |
| 173 reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); | 186 reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); |
| 174 #else | 187 #elif defined(WEBRTC_POSIX) |
| 175 #if CS_DEBUG_CHECKS | 188 # if CS_DEBUG_CHECKS |
| 176 return IsThreadRefEqual(thread_, CurrentThreadRef()); | 189 return IsThreadRefEqual(thread_, CurrentThreadRef()); |
| 177 #else | 190 # else |
| 178 return true; | 191 return true; |
| 179 #endif // CS_DEBUG_CHECKS | 192 # endif // CS_DEBUG_CHECKS |
| 193 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) | |
| 194 #error Unsupported platform. | |
| 180 #endif | 195 #endif |
| 181 } | 196 } |
| 182 | 197 |
| 183 CritScope::CritScope(const CriticalSection* cs) : cs_(cs) { cs_->Enter(); } | 198 CritScope::CritScope(const CriticalSection* cs) : cs_(cs) { cs_->Enter(); } |
| 184 CritScope::~CritScope() { cs_->Leave(); } | 199 CritScope::~CritScope() { cs_->Leave(); } |
| 185 | 200 |
| 186 TryCritScope::TryCritScope(const CriticalSection* cs) | 201 TryCritScope::TryCritScope(const CriticalSection* cs) |
| 187 : cs_(cs), locked_(cs->TryEnter()) { | 202 : cs_(cs), locked_(cs->TryEnter()) { |
| 188 CS_DEBUG_CODE(lock_was_called_ = false); | 203 CS_DEBUG_CODE(lock_was_called_ = false); |
| 204 RTC_UNUSED(lock_was_called_); | |
| 189 } | 205 } |
| 190 | 206 |
| 191 TryCritScope::~TryCritScope() { | 207 TryCritScope::~TryCritScope() { |
| 192 CS_DEBUG_CODE(RTC_DCHECK(lock_was_called_)); | 208 CS_DEBUG_CODE(RTC_DCHECK(lock_was_called_)); |
| 193 if (locked_) | 209 if (locked_) |
| 194 cs_->Leave(); | 210 cs_->Leave(); |
| 195 } | 211 } |
| 196 | 212 |
| 197 bool TryCritScope::locked() const { | 213 bool TryCritScope::locked() const { |
| 198 CS_DEBUG_CODE(lock_was_called_ = true); | 214 CS_DEBUG_CODE(lock_was_called_ = true); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 227 GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) | 243 GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) |
| 228 : lock_(lock) { | 244 : lock_(lock) { |
| 229 lock_->Lock(); | 245 lock_->Lock(); |
| 230 } | 246 } |
| 231 | 247 |
| 232 GlobalLockScope::~GlobalLockScope() { | 248 GlobalLockScope::~GlobalLockScope() { |
| 233 lock_->Unlock(); | 249 lock_->Unlock(); |
| 234 } | 250 } |
| 235 | 251 |
| 236 } // namespace rtc | 252 } // namespace rtc |
| OLD | NEW |