| 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 | #include "rtc_base/criticalsection.h" |
| 12 |
| 13 | #include <time.h> |
| 14 |
| 15 | #include "rtc_base/atomicops.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/platform_thread_types.h" |
| 18 | #include "rtc_base/system/unused.h" |
| 19 |
| 20 | // TODO(tommi): Split this file up to per-platform implementation files. |
| 21 |
| 22 | namespace rtc { |
| 23 |
| 24 | CriticalSection::CriticalSection() { |
| 25 | #if defined(WEBRTC_WIN) |
| 26 | InitializeCriticalSection(&crit_); |
| 27 | #elif defined(WEBRTC_POSIX) |
| 28 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 29 | lock_queue_ = 0; |
| 30 | owning_thread_ = 0; |
| 31 | recursion_ = 0; |
| 32 | semaphore_ = dispatch_semaphore_create(0); |
| 33 | #else |
| 34 | pthread_mutexattr_t mutex_attribute; |
| 35 | pthread_mutexattr_init(&mutex_attribute); |
| 36 | pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| 37 | pthread_mutex_init(&mutex_, &mutex_attribute); |
| 38 | pthread_mutexattr_destroy(&mutex_attribute); |
| 39 | #endif |
| 40 | CS_DEBUG_CODE(thread_ = 0); |
| 41 | CS_DEBUG_CODE(recursion_count_ = 0); |
| 42 | RTC_UNUSED(thread_); |
| 43 | RTC_UNUSED(recursion_count_); |
| 44 | #else |
| 45 | #error Unsupported platform. |
| 46 | #endif |
| 47 | } |
| 48 |
| 49 | CriticalSection::~CriticalSection() { |
| 50 | #if defined(WEBRTC_WIN) |
| 51 | DeleteCriticalSection(&crit_); |
| 52 | #elif defined(WEBRTC_POSIX) |
| 53 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 54 | dispatch_release(semaphore_); |
| 55 | #else |
| 56 | pthread_mutex_destroy(&mutex_); |
| 57 | #endif |
| 58 | #else |
| 59 | #error Unsupported platform. |
| 60 | #endif |
| 61 | } |
| 62 |
| 63 | void CriticalSection::Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION() { |
| 64 | #if defined(WEBRTC_WIN) |
| 65 | EnterCriticalSection(&crit_); |
| 66 | #elif defined(WEBRTC_POSIX) |
| 67 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 68 | int spin = 3000; |
| 69 | PlatformThreadRef self = CurrentThreadRef(); |
| 70 | bool have_lock = false; |
| 71 | do { |
| 72 | // Instead of calling TryEnter() in this loop, we do two interlocked |
| 73 | // operations, first a read-only one in order to avoid affecting the lock |
| 74 | // cache-line while spinning, in case another thread is using the lock. |
| 75 | if (!IsThreadRefEqual(owning_thread_, self)) { |
| 76 | if (AtomicOps::AcquireLoad(&lock_queue_) == 0) { |
| 77 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) { |
| 78 | have_lock = true; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | AtomicOps::Increment(&lock_queue_); |
| 84 | have_lock = true; |
| 85 | break; |
| 86 | } |
| 87 |
| 88 | sched_yield(); |
| 89 | } while (--spin); |
| 90 |
| 91 | if (!have_lock && AtomicOps::Increment(&lock_queue_) > 1) { |
| 92 | // Owning thread cannot be the current thread since TryEnter() would |
| 93 | // have succeeded. |
| 94 | RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self)); |
| 95 | // Wait for the lock to become available. |
| 96 | dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER); |
| 97 | RTC_DCHECK(owning_thread_ == 0); |
| 98 | RTC_DCHECK(!recursion_); |
| 99 | } |
| 100 |
| 101 | owning_thread_ = self; |
| 102 | ++recursion_; |
| 103 |
| 104 | #else |
| 105 | pthread_mutex_lock(&mutex_); |
| 106 | #endif |
| 107 |
| 108 | #if CS_DEBUG_CHECKS |
| 109 | if (!recursion_count_) { |
| 110 | RTC_DCHECK(!thread_); |
| 111 | thread_ = CurrentThreadRef(); |
| 112 | } else { |
| 113 | RTC_DCHECK(CurrentThreadIsOwner()); |
| 114 | } |
| 115 | ++recursion_count_; |
| 116 | #endif |
| 117 | #else |
| 118 | #error Unsupported platform. |
| 119 | #endif |
| 120 | } |
| 121 |
| 122 | bool CriticalSection::TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 123 | #if defined(WEBRTC_WIN) |
| 124 | return TryEnterCriticalSection(&crit_) != FALSE; |
| 125 | #elif defined(WEBRTC_POSIX) |
| 126 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 127 | if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { |
| 128 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) |
| 129 | return false; |
| 130 | owning_thread_ = CurrentThreadRef(); |
| 131 | RTC_DCHECK(!recursion_); |
| 132 | } else { |
| 133 | AtomicOps::Increment(&lock_queue_); |
| 134 | } |
| 135 | ++recursion_; |
| 136 | #else |
| 137 | if (pthread_mutex_trylock(&mutex_) != 0) |
| 138 | return false; |
| 139 | #endif |
| 140 | #if CS_DEBUG_CHECKS |
| 141 | if (!recursion_count_) { |
| 142 | RTC_DCHECK(!thread_); |
| 143 | thread_ = CurrentThreadRef(); |
| 144 | } else { |
| 145 | RTC_DCHECK(CurrentThreadIsOwner()); |
| 146 | } |
| 147 | ++recursion_count_; |
| 148 | #endif |
| 149 | return true; |
| 150 | #else |
| 151 | #error Unsupported platform. |
| 152 | #endif |
| 153 | } |
| 154 |
| 155 | void CriticalSection::Leave() const RTC_UNLOCK_FUNCTION() { |
| 156 | RTC_DCHECK(CurrentThreadIsOwner()); |
| 157 | #if defined(WEBRTC_WIN) |
| 158 | LeaveCriticalSection(&crit_); |
| 159 | #elif defined(WEBRTC_POSIX) |
| 160 | #if CS_DEBUG_CHECKS |
| 161 | --recursion_count_; |
| 162 | RTC_DCHECK(recursion_count_ >= 0); |
| 163 | if (!recursion_count_) |
| 164 | thread_ = 0; |
| 165 | #endif |
| 166 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 167 | RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); |
| 168 | RTC_DCHECK_GE(recursion_, 0); |
| 169 | --recursion_; |
| 170 | if (!recursion_) |
| 171 | owning_thread_ = 0; |
| 172 |
| 173 | if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) |
| 174 | dispatch_semaphore_signal(semaphore_); |
| 175 | #else |
| 176 | pthread_mutex_unlock(&mutex_); |
| 177 | #endif |
| 178 | #else |
| 179 | #error Unsupported platform. |
| 180 | #endif |
| 181 | } |
| 182 |
| 183 | bool CriticalSection::CurrentThreadIsOwner() const { |
| 184 | #if defined(WEBRTC_WIN) |
| 185 | // OwningThread has type HANDLE but actually contains the Thread ID: |
| 186 | // http://stackoverflow.com/questions/12675301/why-is-the-owningthread-member-of-critical-section-of-type-handle-when-it-is-de |
| 187 | // Converting through size_t avoids the VS 2015 warning C4312: conversion from |
| 188 | // 'type1' to 'type2' of greater size |
| 189 | return crit_.OwningThread == |
| 190 | reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); |
| 191 | #elif defined(WEBRTC_POSIX) |
| 192 | #if CS_DEBUG_CHECKS |
| 193 | return IsThreadRefEqual(thread_, CurrentThreadRef()); |
| 194 | #else |
| 195 | return true; |
| 196 | #endif // CS_DEBUG_CHECKS |
| 197 | #else |
| 198 | #error Unsupported platform. |
| 199 | #endif |
| 200 | } |
| 201 |
| 202 | CritScope::CritScope(const CriticalSection* cs) : cs_(cs) { |
| 203 | cs_->Enter(); |
| 204 | } |
| 205 | CritScope::~CritScope() { |
| 206 | cs_->Leave(); |
| 207 | } |
| 208 |
| 209 | TryCritScope::TryCritScope(const CriticalSection* cs) |
| 210 | : cs_(cs), locked_(cs->TryEnter()) { |
| 211 | CS_DEBUG_CODE(lock_was_called_ = false); |
| 212 | RTC_UNUSED(lock_was_called_); |
| 213 | } |
| 214 |
| 215 | TryCritScope::~TryCritScope() { |
| 216 | CS_DEBUG_CODE(RTC_DCHECK(lock_was_called_)); |
| 217 | if (locked_) |
| 218 | cs_->Leave(); |
| 219 | } |
| 220 |
| 221 | bool TryCritScope::locked() const { |
| 222 | CS_DEBUG_CODE(lock_was_called_ = true); |
| 223 | return locked_; |
| 224 | } |
| 225 |
| 226 | void GlobalLockPod::Lock() { |
| 227 | #if !defined(WEBRTC_WIN) && (!defined(WEBRTC_MAC) || USE_NATIVE_MUTEX_ON_MAC) |
| 228 | const struct timespec ts_null = {0}; |
| 229 | #endif |
| 230 |
| 231 | while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) { |
| 232 | #if defined(WEBRTC_WIN) |
| 233 | ::Sleep(0); |
| 234 | #elif defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 235 | sched_yield(); |
| 236 | #else |
| 237 | nanosleep(&ts_null, nullptr); |
| 238 | #endif |
| 239 | } |
| 240 | } |
| 241 |
| 242 | void GlobalLockPod::Unlock() { |
| 243 | int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0); |
| 244 | RTC_DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first"; |
| 245 | } |
| 246 |
| 247 | GlobalLock::GlobalLock() { |
| 248 | lock_acquired = 0; |
| 249 | } |
| 250 |
| 251 | GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) : lock_(lock) { |
| 252 | lock_->Lock(); |
| 253 | } |
| 254 |
| 255 | GlobalLockScope::~GlobalLockScope() { |
| 256 | lock_->Unlock(); |
| 257 | } |
| 258 |
| 259 | } // namespace rtc |
| 260 |