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