Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 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 "webrtc/base/criticalsection.h" |
| 12 | |
| 13 | #include "webrtc/base/checks.h" |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 14 | #include "webrtc/base/platform_thread.h" |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 15 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 16 | // TODO(tommi): Split this file up to per-platform implementation files. |
| 17 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 18 | namespace rtc { |
| 19 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 20 | CriticalSection::CriticalSection() { |
| 21 | #if defined(WEBRTC_WIN) |
| 22 | InitializeCriticalSection(&crit_); |
| 23 | #else |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 24 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 25 | lock_queue_ = 0; |
| 26 | owning_thread_ = 0; |
| 27 | recursion_ = 0; |
| 28 | semaphore_ = dispatch_semaphore_create(0); |
| 29 | #else |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 30 | pthread_mutexattr_t mutex_attribute; |
| 31 | pthread_mutexattr_init(&mutex_attribute); |
| 32 | pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| 33 | pthread_mutex_init(&mutex_, &mutex_attribute); |
| 34 | pthread_mutexattr_destroy(&mutex_attribute); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 35 | #endif |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 36 | CS_DEBUG_CODE(thread_ = 0); |
| 37 | CS_DEBUG_CODE(recursion_count_ = 0); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | CriticalSection::~CriticalSection() { |
| 42 | #if defined(WEBRTC_WIN) |
| 43 | DeleteCriticalSection(&crit_); |
| 44 | #else |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 45 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 46 | dispatch_release(semaphore_); |
| 47 | #else |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 48 | pthread_mutex_destroy(&mutex_); |
| 49 | #endif |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 50 | #endif |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 53 | void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 54 | #if defined(WEBRTC_WIN) |
| 55 | EnterCriticalSection(&crit_); |
| 56 | #else |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 57 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 58 | int spin = 3000; |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 59 | PlatformThreadRef self = CurrentThreadRef(); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 60 | bool have_lock = false; |
| 61 | do { |
| 62 | // 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 |
| 64 | // cache-line while spinning, in case another thread is using the lock. |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 65 | if (!IsThreadRefEqual(owning_thread_, self)) { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 66 | if (AtomicOps::AcquireLoad(&lock_queue_) == 0) { |
| 67 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) { |
| 68 | have_lock = true; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | AtomicOps::Increment(&lock_queue_); |
| 74 | have_lock = true; |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | sched_yield(); |
| 79 | } while (--spin); |
| 80 | |
| 81 | if (!have_lock && AtomicOps::Increment(&lock_queue_) > 1) { |
| 82 | // Owning thread cannot be the current thread since TryEnter() would |
| 83 | // have succeeded. |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 84 | RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self)); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 85 | // Wait for the lock to become available. |
| 86 | dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER); |
| 87 | RTC_DCHECK(owning_thread_ == 0); |
| 88 | RTC_DCHECK(!recursion_); |
| 89 | } |
| 90 | |
| 91 | owning_thread_ = self; |
| 92 | ++recursion_; |
| 93 | |
| 94 | #else |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 95 | pthread_mutex_lock(&mutex_); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 96 | #endif |
| 97 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 98 | #if CS_DEBUG_CHECKS |
| 99 | if (!recursion_count_) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 100 | RTC_DCHECK(!thread_); |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 101 | thread_ = CurrentThreadRef(); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 102 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 103 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 104 | } |
| 105 | ++recursion_count_; |
| 106 | #endif |
| 107 | #endif |
| 108 | } |
| 109 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 110 | bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 111 | #if defined(WEBRTC_WIN) |
| 112 | return TryEnterCriticalSection(&crit_) != FALSE; |
| 113 | #else |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 114 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 115 | if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 116 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) |
| 117 | return false; |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 118 | owning_thread_ = CurrentThreadRef(); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 119 | RTC_DCHECK(!recursion_); |
| 120 | } else { |
| 121 | AtomicOps::Increment(&lock_queue_); |
| 122 | } |
| 123 | ++recursion_; |
| 124 | #else |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 125 | if (pthread_mutex_trylock(&mutex_) != 0) |
| 126 | return false; |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 127 | #endif |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 128 | #if CS_DEBUG_CHECKS |
| 129 | if (!recursion_count_) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 130 | RTC_DCHECK(!thread_); |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 131 | thread_ = CurrentThreadRef(); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 132 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 133 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 134 | } |
| 135 | ++recursion_count_; |
| 136 | #endif |
| 137 | return true; |
| 138 | #endif |
| 139 | } |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 140 | void CriticalSection::Leave() const UNLOCK_FUNCTION() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 141 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 142 | #if defined(WEBRTC_WIN) |
| 143 | LeaveCriticalSection(&crit_); |
| 144 | #else |
| 145 | #if CS_DEBUG_CHECKS |
| 146 | --recursion_count_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 147 | RTC_DCHECK(recursion_count_ >= 0); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 148 | if (!recursion_count_) |
| 149 | thread_ = 0; |
| 150 | #endif |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 151 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 152 | RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 153 | RTC_DCHECK_GE(recursion_, 0); |
| 154 | --recursion_; |
| 155 | if (!recursion_) |
| 156 | owning_thread_ = 0; |
| 157 | |
| 158 | if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) |
| 159 | dispatch_semaphore_signal(semaphore_); |
| 160 | #else |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 161 | pthread_mutex_unlock(&mutex_); |
| 162 | #endif |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 163 | #endif |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool CriticalSection::CurrentThreadIsOwner() const { |
| 167 | #if defined(WEBRTC_WIN) |
brucedawson | a10492f | 2015-10-06 13:34:30 -0700 | [diff] [blame] | 168 | // 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 |
| 170 | // Converting through size_t avoids the VS 2015 warning C4312: conversion from |
| 171 | // 'type1' to 'type2' of greater size |
| 172 | return crit_.OwningThread == |
| 173 | reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 174 | #else |
| 175 | #if CS_DEBUG_CHECKS |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 176 | return IsThreadRefEqual(thread_, CurrentThreadRef()); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 177 | #else |
| 178 | return true; |
| 179 | #endif // CS_DEBUG_CHECKS |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | bool CriticalSection::IsLocked() const { |
| 184 | #if defined(WEBRTC_WIN) |
| 185 | return crit_.LockCount != -1; |
| 186 | #else |
| 187 | #if CS_DEBUG_CHECKS |
| 188 | return thread_ != 0; |
| 189 | #else |
| 190 | return true; |
| 191 | #endif |
| 192 | #endif |
| 193 | } |
| 194 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 195 | CritScope::CritScope(const CriticalSection* cs) : cs_(cs) { cs_->Enter(); } |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 196 | CritScope::~CritScope() { cs_->Leave(); } |
| 197 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 198 | TryCritScope::TryCritScope(const CriticalSection* cs) |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 199 | : cs_(cs), locked_(cs->TryEnter()) { |
| 200 | CS_DEBUG_CODE(lock_was_called_ = false); |
| 201 | } |
| 202 | |
| 203 | TryCritScope::~TryCritScope() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 204 | CS_DEBUG_CODE(RTC_DCHECK(lock_was_called_)); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 205 | if (locked_) |
| 206 | cs_->Leave(); |
| 207 | } |
| 208 | |
| 209 | bool TryCritScope::locked() const { |
| 210 | CS_DEBUG_CODE(lock_was_called_ = true); |
| 211 | return locked_; |
| 212 | } |
| 213 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 214 | void GlobalLockPod::Lock() { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 215 | #if !defined(WEBRTC_WIN) && (!defined(WEBRTC_MAC) || USE_NATIVE_MUTEX_ON_MAC) |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 216 | const struct timespec ts_null = {0}; |
| 217 | #endif |
| 218 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 219 | while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) { |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 220 | #if defined(WEBRTC_WIN) |
| 221 | ::Sleep(0); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 222 | #elif defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 223 | sched_yield(); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 224 | #else |
| 225 | nanosleep(&ts_null, nullptr); |
| 226 | #endif |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | void GlobalLockPod::Unlock() { |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 231 | int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 232 | RTC_DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first"; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 233 | } |
| 234 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 235 | GlobalLock::GlobalLock() { |
| 236 | lock_acquired = 0; |
| 237 | } |
pbos | 3c12f4d | 2015-11-17 03:21:02 -0800 | [diff] [blame] | 238 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 239 | GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) |
| 240 | : lock_(lock) { |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 241 | lock_->Lock(); |
| 242 | } |
| 243 | |
| 244 | GlobalLockScope::~GlobalLockScope() { |
| 245 | lock_->Unlock(); |
| 246 | } |
| 247 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 248 | } // namespace rtc |