Jiayang Liu | bef8d2d | 2015-03-26 21:38:46 | [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 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 11 | #include "rtc_base/deprecated/recursive_critical_section.h" |
Jiayang Liu | bef8d2d | 2015-03-26 21:38:46 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <time.h> |
| 14 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 15 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Tommi | 1f3f3c2 | 2018-02-17 10:46:14 | [diff] [blame] | 17 | #include "rtc_base/platform_thread_types.h" |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 18 | #include "rtc_base/synchronization/yield.h" |
Niels Möller | a12c42a | 2018-07-25 14:05:48 | [diff] [blame] | 19 | #include "rtc_base/system/unused.h" |
Jiayang Liu | bef8d2d | 2015-03-26 21:38:46 | [diff] [blame] | 20 | |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 21 | #if RTC_DCHECK_IS_ON |
| 22 | #define RTC_CS_DEBUG_CODE(x) x |
| 23 | #else // !RTC_DCHECK_IS_ON |
| 24 | #define RTC_CS_DEBUG_CODE(x) |
| 25 | #endif // !RTC_DCHECK_IS_ON |
| 26 | |
Jiayang Liu | bef8d2d | 2015-03-26 21:38:46 | [diff] [blame] | 27 | namespace rtc { |
| 28 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 29 | RecursiveCriticalSection::RecursiveCriticalSection() { |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 30 | #if defined(WEBRTC_WIN) |
| 31 | InitializeCriticalSection(&crit_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 32 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 12:11:02 | [diff] [blame] | 33 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 34 | lock_queue_ = 0; |
| 35 | owning_thread_ = 0; |
| 36 | recursion_ = 0; |
| 37 | semaphore_ = dispatch_semaphore_create(0); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 38 | #else |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 39 | pthread_mutexattr_t mutex_attribute; |
| 40 | pthread_mutexattr_init(&mutex_attribute); |
| 41 | pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
Oskar Sundbom | 13471a4 | 2019-03-01 15:11:49 | [diff] [blame] | 42 | #if defined(WEBRTC_MAC) |
| 43 | pthread_mutexattr_setpolicy_np(&mutex_attribute, |
Markus Handell | 4a4f162 | 2020-06-05 09:21:10 | [diff] [blame] | 44 | _PTHREAD_MUTEX_POLICY_FIRSTFIT); |
Oskar Sundbom | 13471a4 | 2019-03-01 15:11:49 | [diff] [blame] | 45 | #endif |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 46 | pthread_mutex_init(&mutex_, &mutex_attribute); |
| 47 | pthread_mutexattr_destroy(&mutex_attribute); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 48 | #endif |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 49 | RTC_CS_DEBUG_CODE(thread_ = 0); |
| 50 | RTC_CS_DEBUG_CODE(recursion_count_ = 0); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 51 | RTC_UNUSED(thread_); |
| 52 | RTC_UNUSED(recursion_count_); |
| 53 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 54 | #error Unsupported platform. |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 55 | #endif |
| 56 | } |
| 57 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 58 | RecursiveCriticalSection::~RecursiveCriticalSection() { |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 59 | #if defined(WEBRTC_WIN) |
| 60 | DeleteCriticalSection(&crit_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 61 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 12:11:02 | [diff] [blame] | 62 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 63 | dispatch_release(semaphore_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 64 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 65 | pthread_mutex_destroy(&mutex_); |
| 66 | #endif |
| 67 | #else |
| 68 | #error Unsupported platform. |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 69 | #endif |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 70 | } |
| 71 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 72 | void RecursiveCriticalSection::Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION() { |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 73 | #if defined(WEBRTC_WIN) |
| 74 | EnterCriticalSection(&crit_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 75 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 12:11:02 | [diff] [blame] | 76 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 77 | int spin = 3000; |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 78 | PlatformThreadRef self = CurrentThreadRef(); |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 79 | bool have_lock = false; |
| 80 | do { |
| 81 | // Instead of calling TryEnter() in this loop, we do two interlocked |
| 82 | // operations, first a read-only one in order to avoid affecting the lock |
| 83 | // cache-line while spinning, in case another thread is using the lock. |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 84 | if (!IsThreadRefEqual(owning_thread_, self)) { |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 85 | if (AtomicOps::AcquireLoad(&lock_queue_) == 0) { |
| 86 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) { |
| 87 | have_lock = true; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } else { |
| 92 | AtomicOps::Increment(&lock_queue_); |
| 93 | have_lock = true; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | sched_yield(); |
| 98 | } while (--spin); |
| 99 | |
| 100 | if (!have_lock && AtomicOps::Increment(&lock_queue_) > 1) { |
| 101 | // Owning thread cannot be the current thread since TryEnter() would |
| 102 | // have succeeded. |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 103 | RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self)); |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 104 | // Wait for the lock to become available. |
| 105 | dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER); |
| 106 | RTC_DCHECK(owning_thread_ == 0); |
| 107 | RTC_DCHECK(!recursion_); |
| 108 | } |
| 109 | |
| 110 | owning_thread_ = self; |
| 111 | ++recursion_; |
| 112 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 113 | #else |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 114 | pthread_mutex_lock(&mutex_); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 115 | #endif |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 116 | |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 117 | #if RTC_DCHECK_IS_ON |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 118 | if (!recursion_count_) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 119 | RTC_DCHECK(!thread_); |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 120 | thread_ = CurrentThreadRef(); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 121 | } else { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 122 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 123 | } |
| 124 | ++recursion_count_; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 125 | #endif |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 126 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 127 | #error Unsupported platform. |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 128 | #endif |
| 129 | } |
| 130 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 131 | bool RecursiveCriticalSection::TryEnter() const |
| 132 | RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 133 | #if defined(WEBRTC_WIN) |
| 134 | return TryEnterCriticalSection(&crit_) != FALSE; |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 135 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 12:11:02 | [diff] [blame] | 136 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 137 | if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) { |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 138 | if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0) |
| 139 | return false; |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 140 | owning_thread_ = CurrentThreadRef(); |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 141 | RTC_DCHECK(!recursion_); |
| 142 | } else { |
| 143 | AtomicOps::Increment(&lock_queue_); |
| 144 | } |
| 145 | ++recursion_; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 146 | #else |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 147 | if (pthread_mutex_trylock(&mutex_) != 0) |
| 148 | return false; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 149 | #endif |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 150 | #if RTC_DCHECK_IS_ON |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 151 | if (!recursion_count_) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 152 | RTC_DCHECK(!thread_); |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 153 | thread_ = CurrentThreadRef(); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 154 | } else { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 155 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 156 | } |
| 157 | ++recursion_count_; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 158 | #endif |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 159 | return true; |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 160 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 161 | #error Unsupported platform. |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 162 | #endif |
| 163 | } |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 164 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 165 | void RecursiveCriticalSection::Leave() const RTC_UNLOCK_FUNCTION() { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 166 | RTC_DCHECK(CurrentThreadIsOwner()); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 167 | #if defined(WEBRTC_WIN) |
| 168 | LeaveCriticalSection(&crit_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 169 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 170 | #if RTC_DCHECK_IS_ON |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 171 | --recursion_count_; |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 172 | RTC_DCHECK(recursion_count_ >= 0); |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 173 | if (!recursion_count_) |
| 174 | thread_ = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 175 | #endif |
Mirko Bonadei | a0eefc1 | 2019-07-08 12:11:02 | [diff] [blame] | 176 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 177 | RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef())); |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 178 | RTC_DCHECK_GE(recursion_, 0); |
| 179 | --recursion_; |
| 180 | if (!recursion_) |
| 181 | owning_thread_ = 0; |
| 182 | |
| 183 | if (AtomicOps::Decrement(&lock_queue_) > 0 && !recursion_) |
| 184 | dispatch_semaphore_signal(semaphore_); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 185 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 186 | pthread_mutex_unlock(&mutex_); |
| 187 | #endif |
| 188 | #else |
| 189 | #error Unsupported platform. |
tommi | ed281e9 | 2016-01-22 07:47:25 | [diff] [blame] | 190 | #endif |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 191 | } |
| 192 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 193 | bool RecursiveCriticalSection::CurrentThreadIsOwner() const { |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 194 | #if defined(WEBRTC_WIN) |
brucedawson | a10492f | 2015-10-06 20:34:30 | [diff] [blame] | 195 | // OwningThread has type HANDLE but actually contains the Thread ID: |
| 196 | // http://stackoverflow.com/questions/12675301/why-is-the-owningthread-member-of-critical-section-of-type-handle-when-it-is-de |
| 197 | // Converting through size_t avoids the VS 2015 warning C4312: conversion from |
| 198 | // 'type1' to 'type2' of greater size |
| 199 | return crit_.OwningThread == |
| 200 | reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId())); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 201 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 202 | #if RTC_DCHECK_IS_ON |
tommi | 7406b96 | 2016-01-22 13:13:33 | [diff] [blame] | 203 | return IsThreadRefEqual(thread_, CurrentThreadRef()); |
eladalon | d6e9466 | 2017-06-28 14:31:30 | [diff] [blame] | 204 | #else |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 205 | return true; |
Mirko Bonadei | 84ce3c0 | 2019-07-03 12:45:54 | [diff] [blame] | 206 | #endif // RTC_DCHECK_IS_ON |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 207 | #else |
| 208 | #error Unsupported platform. |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 209 | #endif |
| 210 | } |
| 211 | |
Markus Handell | 3cb525b | 2020-07-16 14:16:09 | [diff] [blame] | 212 | CritScope::CritScope(const RecursiveCriticalSection* cs) : cs_(cs) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 213 | cs_->Enter(); |
| 214 | } |
| 215 | CritScope::~CritScope() { |
| 216 | cs_->Leave(); |
| 217 | } |
Tommi | 494f209 | 2015-04-27 15:39:23 | [diff] [blame] | 218 | |
Jiayang Liu | bef8d2d | 2015-03-26 21:38:46 | [diff] [blame] | 219 | } // namespace rtc |