You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.6 KiB

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