blob: 068b9aa80893ff066caf726919b963bf44175ef8 [file] [log] [blame]
Jiayang Liubef8d2d2015-03-26 14:38:46 -07001/*
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 Handell3cb525b2020-07-16 16:16:09 +020011#include "rtc_base/deprecated/recursive_critical_section.h"
Jiayang Liubef8d2d2015-03-26 14:38:46 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <time.h>
14
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Tommi1f3f3c22018-02-17 11:46:14 +010017#include "rtc_base/platform_thread_types.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020018#include "rtc_base/synchronization/yield.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020019#include "rtc_base/system/unused.h"
Jiayang Liubef8d2d2015-03-26 14:38:46 -070020
Mirko Bonadei84ce3c02019-07-03 14:45:54 +020021#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 Liubef8d2d2015-03-26 14:38:46 -070027namespace rtc {
28
Markus Handell3cb525b2020-07-16 16:16:09 +020029RecursiveCriticalSection::RecursiveCriticalSection() {
Tommi494f2092015-04-27 17:39:23 +020030#if defined(WEBRTC_WIN)
31 InitializeCriticalSection(&crit_);
eladalond6e94662017-06-28 07:31:30 -070032#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020033#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
tommied281e92016-01-21 23:47:25 -080034 lock_queue_ = 0;
35 owning_thread_ = 0;
36 recursion_ = 0;
37 semaphore_ = dispatch_semaphore_create(0);
Yves Gerey665174f2018-06-19 15:03:05 +020038#else
Tommi494f2092015-04-27 17:39:23 +020039 pthread_mutexattr_t mutex_attribute;
40 pthread_mutexattr_init(&mutex_attribute);
41 pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
Oskar Sundbom13471a42019-03-01 16:11:49 +010042#if defined(WEBRTC_MAC)
43 pthread_mutexattr_setpolicy_np(&mutex_attribute,
Markus Handell4a4f1622020-06-05 11:21:10 +020044 _PTHREAD_MUTEX_POLICY_FIRSTFIT);
Oskar Sundbom13471a42019-03-01 16:11:49 +010045#endif
Tommi494f2092015-04-27 17:39:23 +020046 pthread_mutex_init(&mutex_, &mutex_attribute);
47 pthread_mutexattr_destroy(&mutex_attribute);
Yves Gerey665174f2018-06-19 15:03:05 +020048#endif
Mirko Bonadei84ce3c02019-07-03 14:45:54 +020049 RTC_CS_DEBUG_CODE(thread_ = 0);
50 RTC_CS_DEBUG_CODE(recursion_count_ = 0);
eladalond6e94662017-06-28 07:31:30 -070051 RTC_UNUSED(thread_);
52 RTC_UNUSED(recursion_count_);
53#else
Yves Gerey665174f2018-06-19 15:03:05 +020054#error Unsupported platform.
Tommi494f2092015-04-27 17:39:23 +020055#endif
56}
57
Markus Handell3cb525b2020-07-16 16:16:09 +020058RecursiveCriticalSection::~RecursiveCriticalSection() {
Tommi494f2092015-04-27 17:39:23 +020059#if defined(WEBRTC_WIN)
60 DeleteCriticalSection(&crit_);
eladalond6e94662017-06-28 07:31:30 -070061#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020062#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
tommied281e92016-01-21 23:47:25 -080063 dispatch_release(semaphore_);
eladalond6e94662017-06-28 07:31:30 -070064#else
Yves Gerey665174f2018-06-19 15:03:05 +020065 pthread_mutex_destroy(&mutex_);
66#endif
67#else
68#error Unsupported platform.
tommied281e92016-01-21 23:47:25 -080069#endif
Tommi494f2092015-04-27 17:39:23 +020070}
71
Markus Handell3cb525b2020-07-16 16:16:09 +020072void RecursiveCriticalSection::Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION() {
Tommi494f2092015-04-27 17:39:23 +020073#if defined(WEBRTC_WIN)
74 EnterCriticalSection(&crit_);
eladalond6e94662017-06-28 07:31:30 -070075#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020076#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
tommied281e92016-01-21 23:47:25 -080077 int spin = 3000;
tommi7406b962016-01-22 05:13:33 -080078 PlatformThreadRef self = CurrentThreadRef();
tommied281e92016-01-21 23:47:25 -080079 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.
tommi7406b962016-01-22 05:13:33 -080084 if (!IsThreadRefEqual(owning_thread_, self)) {
tommied281e92016-01-21 23:47:25 -080085 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.
tommi7406b962016-01-22 05:13:33 -0800103 RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self));
tommied281e92016-01-21 23:47:25 -0800104 // 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 Gerey665174f2018-06-19 15:03:05 +0200113#else
Tommi494f2092015-04-27 17:39:23 +0200114 pthread_mutex_lock(&mutex_);
Yves Gerey665174f2018-06-19 15:03:05 +0200115#endif
tommied281e92016-01-21 23:47:25 -0800116
Mirko Bonadei84ce3c02019-07-03 14:45:54 +0200117#if RTC_DCHECK_IS_ON
Tommi494f2092015-04-27 17:39:23 +0200118 if (!recursion_count_) {
henrikg91d6ede2015-09-17 00:24:34 -0700119 RTC_DCHECK(!thread_);
tommi7406b962016-01-22 05:13:33 -0800120 thread_ = CurrentThreadRef();
Tommi494f2092015-04-27 17:39:23 +0200121 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700122 RTC_DCHECK(CurrentThreadIsOwner());
Tommi494f2092015-04-27 17:39:23 +0200123 }
124 ++recursion_count_;
Yves Gerey665174f2018-06-19 15:03:05 +0200125#endif
eladalond6e94662017-06-28 07:31:30 -0700126#else
Yves Gerey665174f2018-06-19 15:03:05 +0200127#error Unsupported platform.
Tommi494f2092015-04-27 17:39:23 +0200128#endif
129}
130
Markus Handell3cb525b2020-07-16 16:16:09 +0200131bool RecursiveCriticalSection::TryEnter() const
132 RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
Tommi494f2092015-04-27 17:39:23 +0200133#if defined(WEBRTC_WIN)
134 return TryEnterCriticalSection(&crit_) != FALSE;
eladalond6e94662017-06-28 07:31:30 -0700135#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +0200136#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
tommi7406b962016-01-22 05:13:33 -0800137 if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) {
tommied281e92016-01-21 23:47:25 -0800138 if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0)
139 return false;
tommi7406b962016-01-22 05:13:33 -0800140 owning_thread_ = CurrentThreadRef();
tommied281e92016-01-21 23:47:25 -0800141 RTC_DCHECK(!recursion_);
142 } else {
143 AtomicOps::Increment(&lock_queue_);
144 }
145 ++recursion_;
Yves Gerey665174f2018-06-19 15:03:05 +0200146#else
Tommi494f2092015-04-27 17:39:23 +0200147 if (pthread_mutex_trylock(&mutex_) != 0)
148 return false;
Yves Gerey665174f2018-06-19 15:03:05 +0200149#endif
Mirko Bonadei84ce3c02019-07-03 14:45:54 +0200150#if RTC_DCHECK_IS_ON
Tommi494f2092015-04-27 17:39:23 +0200151 if (!recursion_count_) {
henrikg91d6ede2015-09-17 00:24:34 -0700152 RTC_DCHECK(!thread_);
tommi7406b962016-01-22 05:13:33 -0800153 thread_ = CurrentThreadRef();
Tommi494f2092015-04-27 17:39:23 +0200154 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700155 RTC_DCHECK(CurrentThreadIsOwner());
Tommi494f2092015-04-27 17:39:23 +0200156 }
157 ++recursion_count_;
Yves Gerey665174f2018-06-19 15:03:05 +0200158#endif
Tommi494f2092015-04-27 17:39:23 +0200159 return true;
eladalond6e94662017-06-28 07:31:30 -0700160#else
Yves Gerey665174f2018-06-19 15:03:05 +0200161#error Unsupported platform.
Tommi494f2092015-04-27 17:39:23 +0200162#endif
163}
eladalond6e94662017-06-28 07:31:30 -0700164
Markus Handell3cb525b2020-07-16 16:16:09 +0200165void RecursiveCriticalSection::Leave() const RTC_UNLOCK_FUNCTION() {
henrikg91d6ede2015-09-17 00:24:34 -0700166 RTC_DCHECK(CurrentThreadIsOwner());
Tommi494f2092015-04-27 17:39:23 +0200167#if defined(WEBRTC_WIN)
168 LeaveCriticalSection(&crit_);
eladalond6e94662017-06-28 07:31:30 -0700169#elif defined(WEBRTC_POSIX)
Mirko Bonadei84ce3c02019-07-03 14:45:54 +0200170#if RTC_DCHECK_IS_ON
Tommi494f2092015-04-27 17:39:23 +0200171 --recursion_count_;
henrikg91d6ede2015-09-17 00:24:34 -0700172 RTC_DCHECK(recursion_count_ >= 0);
Tommi494f2092015-04-27 17:39:23 +0200173 if (!recursion_count_)
174 thread_ = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200175#endif
Mirko Bonadeia0eefc12019-07-08 14:11:02 +0200176#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
tommi7406b962016-01-22 05:13:33 -0800177 RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef()));
tommied281e92016-01-21 23:47:25 -0800178 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_);
eladalond6e94662017-06-28 07:31:30 -0700185#else
Yves Gerey665174f2018-06-19 15:03:05 +0200186 pthread_mutex_unlock(&mutex_);
187#endif
188#else
189#error Unsupported platform.
tommied281e92016-01-21 23:47:25 -0800190#endif
Tommi494f2092015-04-27 17:39:23 +0200191}
192
Markus Handell3cb525b2020-07-16 16:16:09 +0200193bool RecursiveCriticalSection::CurrentThreadIsOwner() const {
Tommi494f2092015-04-27 17:39:23 +0200194#if defined(WEBRTC_WIN)
brucedawsona10492f2015-10-06 13:34:30 -0700195 // 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()));
eladalond6e94662017-06-28 07:31:30 -0700201#elif defined(WEBRTC_POSIX)
Mirko Bonadei84ce3c02019-07-03 14:45:54 +0200202#if RTC_DCHECK_IS_ON
tommi7406b962016-01-22 05:13:33 -0800203 return IsThreadRefEqual(thread_, CurrentThreadRef());
eladalond6e94662017-06-28 07:31:30 -0700204#else
Yves Gerey665174f2018-06-19 15:03:05 +0200205 return true;
Mirko Bonadei84ce3c02019-07-03 14:45:54 +0200206#endif // RTC_DCHECK_IS_ON
Yves Gerey665174f2018-06-19 15:03:05 +0200207#else
208#error Unsupported platform.
Tommi494f2092015-04-27 17:39:23 +0200209#endif
210}
211
Markus Handell3cb525b2020-07-16 16:16:09 +0200212CritScope::CritScope(const RecursiveCriticalSection* cs) : cs_(cs) {
Yves Gerey665174f2018-06-19 15:03:05 +0200213 cs_->Enter();
214}
215CritScope::~CritScope() {
216 cs_->Leave();
217}
Tommi494f2092015-04-27 17:39:23 +0200218
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700219} // namespace rtc