critical_section.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2004 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. #ifndef RTC_BASE_CRITICAL_SECTION_H_
  11. #define RTC_BASE_CRITICAL_SECTION_H_
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/constructor_magic.h"
  14. #include "rtc_base/platform_thread_types.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. #include "rtc_base/thread_annotations.h"
  17. #if defined(WEBRTC_WIN)
  18. // clang-format off
  19. // clang formating would change include order.
  20. // Include winsock2.h before including <windows.h> to maintain consistency with
  21. // win32.h. To include win32.h directly, it must be broken out into its own
  22. // build target.
  23. #include <winsock2.h>
  24. #include <windows.h>
  25. #include <sal.h> // must come after windows headers.
  26. // clang-format on
  27. #endif // defined(WEBRTC_WIN)
  28. #if defined(WEBRTC_POSIX)
  29. #include <pthread.h>
  30. #endif
  31. // See notes in the 'Performance' unit test for the effects of this flag.
  32. #define RTC_USE_NATIVE_MUTEX_ON_MAC 1
  33. #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
  34. #include <dispatch/dispatch.h>
  35. #endif
  36. namespace rtc {
  37. // Locking methods (Enter, TryEnter, Leave)are const to permit protecting
  38. // members inside a const context without requiring mutable CriticalSections
  39. // everywhere. CriticalSection is reentrant lock.
  40. class RTC_LOCKABLE RTC_EXPORT CriticalSection {
  41. public:
  42. CriticalSection();
  43. ~CriticalSection();
  44. void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
  45. bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
  46. void Leave() const RTC_UNLOCK_FUNCTION();
  47. private:
  48. // Use only for RTC_DCHECKing.
  49. bool CurrentThreadIsOwner() const;
  50. #if defined(WEBRTC_WIN)
  51. mutable CRITICAL_SECTION crit_;
  52. #elif defined(WEBRTC_POSIX)
  53. #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
  54. // Number of times the lock has been locked + number of threads waiting.
  55. // TODO(tommi): We could use this number and subtract the recursion count
  56. // to find places where we have multiple threads contending on the same lock.
  57. mutable volatile int lock_queue_;
  58. // |recursion_| represents the recursion count + 1 for the thread that owns
  59. // the lock. Only modified by the thread that owns the lock.
  60. mutable int recursion_;
  61. // Used to signal a single waiting thread when the lock becomes available.
  62. mutable dispatch_semaphore_t semaphore_;
  63. // The thread that currently holds the lock. Required to handle recursion.
  64. mutable PlatformThreadRef owning_thread_;
  65. #else
  66. mutable pthread_mutex_t mutex_;
  67. #endif
  68. mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
  69. mutable int recursion_count_; // Only used by RTC_DCHECKs.
  70. #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
  71. #error Unsupported platform.
  72. #endif
  73. };
  74. // CritScope, for serializing execution through a scope.
  75. class RTC_SCOPED_LOCKABLE CritScope {
  76. public:
  77. explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
  78. ~CritScope() RTC_UNLOCK_FUNCTION();
  79. private:
  80. const CriticalSection* const cs_;
  81. RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
  82. };
  83. // A lock used to protect global variables. Do NOT use for other purposes.
  84. class RTC_LOCKABLE GlobalLock {
  85. public:
  86. constexpr GlobalLock() : lock_acquired_(0) {}
  87. void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
  88. void Unlock() RTC_UNLOCK_FUNCTION();
  89. private:
  90. volatile int lock_acquired_;
  91. };
  92. // GlobalLockScope, for serializing execution through a scope.
  93. class RTC_SCOPED_LOCKABLE GlobalLockScope {
  94. public:
  95. explicit GlobalLockScope(GlobalLock* lock) RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
  96. ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
  97. private:
  98. GlobalLock* const lock_;
  99. RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
  100. };
  101. } // namespace rtc
  102. #endif // RTC_BASE_CRITICAL_SECTION_H_