| 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 |
| 11 | #ifndef RTC_BASE_CRITICALSECTION_H_ |
| 12 | #define RTC_BASE_CRITICALSECTION_H_ |
| 13 |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/constructormagic.h" |
| 16 | #include "rtc_base/platform_thread_types.h" |
| 17 | #include "rtc_base/thread_annotations.h" |
| 18 |
| 19 | #if defined(WEBRTC_WIN) |
| 20 | // clang-format off |
| 21 | // clang formating would change include order. |
| 22 |
| 23 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 24 | // win32.h. To include win32.h directly, it must be broken out into its own |
| 25 | // build target. |
| 26 | #include <winsock2.h> |
| 27 | #include <windows.h> |
| 28 | #include <sal.h> // must come after windows headers. |
| 29 | // clang-format on |
| 30 | #endif // defined(WEBRTC_WIN) |
| 31 |
| 32 | #if defined(WEBRTC_POSIX) |
| 33 | #include <pthread.h> |
| 34 | #endif |
| 35 |
| 36 | // See notes in the 'Performance' unit test for the effects of this flag. |
| 37 | #define USE_NATIVE_MUTEX_ON_MAC 0 |
| 38 |
| 39 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 40 | #include <dispatch/dispatch.h> |
| 41 | #endif |
| 42 |
| 43 | #define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON |
| 44 |
| 45 | #if CS_DEBUG_CHECKS |
| 46 | #define CS_DEBUG_CODE(x) x |
| 47 | #else // !CS_DEBUG_CHECKS |
| 48 | #define CS_DEBUG_CODE(x) |
| 49 | #endif // !CS_DEBUG_CHECKS |
| 50 |
| 51 | namespace rtc |
| 52 | { |
| 53 |
| 54 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 55 | // members inside a const context without requiring mutable CriticalSections |
| 56 | // everywhere. |
| 57 | class RTC_LOCKABLE CriticalSection |
| 58 | { |
| 59 | public: |
| 60 | CriticalSection(); |
| 61 | ~CriticalSection(); |
| 62 |
| 63 | void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 64 | bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 65 | void Leave() const RTC_UNLOCK_FUNCTION(); |
| 66 |
| 67 | private: |
| 68 | // Use only for RTC_DCHECKing. |
| 69 | bool CurrentThreadIsOwner() const; |
| 70 |
| 71 | #if defined(WEBRTC_WIN) |
| 72 | mutable CRITICAL_SECTION crit_; |
| 73 | #elif defined(WEBRTC_POSIX) |
| 74 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 75 | // Number of times the lock has been locked + number of threads waiting. |
| 76 | // TODO(tommi): We could use this number and subtract the recursion count |
| 77 | // to find places where we have multiple threads contending on the same lock. |
| 78 | mutable volatile int lock_queue_; |
| 79 | // |recursion_| represents the recursion count + 1 for the thread that owns |
| 80 | // the lock. Only modified by the thread that owns the lock. |
| 81 | mutable int recursion_; |
| 82 | // Used to signal a single waiting thread when the lock becomes available. |
| 83 | mutable dispatch_semaphore_t semaphore_; |
| 84 | // The thread that currently holds the lock. Required to handle recursion. |
| 85 | mutable PlatformThreadRef owning_thread_; |
| 86 | #else |
| 87 | mutable pthread_mutex_t mutex_; |
| 88 | #endif |
| 89 | mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 90 | mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 91 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 92 | #error Unsupported platform. |
| 93 | #endif |
| 94 | }; |
| 95 |
| 96 | // CritScope, for serializing execution through a scope. |
| 97 | class RTC_SCOPED_LOCKABLE CritScope |
| 98 | { |
| 99 | public: |
| 100 | explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs); |
| 101 | ~CritScope() RTC_UNLOCK_FUNCTION(); |
| 102 |
| 103 | private: |
| 104 | const CriticalSection* const cs_; |
| 105 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 106 | }; |
| 107 |
| 108 | // Tries to lock a critical section on construction via |
| 109 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 110 | // lock was taken. Never blocks. |
| 111 | // |
| 112 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 113 | // subsequent code. Users *must* check locked() to determine if the |
| 114 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 115 | class TryCritScope |
| 116 | { |
| 117 | public: |
| 118 | explicit TryCritScope(const CriticalSection* cs); |
| 119 | ~TryCritScope(); |
| 120 | #if defined(WEBRTC_WIN) |
| 121 | _Check_return_ bool locked() const; |
| 122 | #elif defined(WEBRTC_POSIX) |
| 123 | bool locked() const __attribute__((__warn_unused_result__)); |
| 124 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 125 | #error Unsupported platform. |
| 126 | #endif |
| 127 | private: |
| 128 | const CriticalSection* const cs_; |
| 129 | const bool locked_; |
| 130 | mutable bool lock_was_called_; // Only used by RTC_DCHECKs. |
| 131 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 132 | }; |
| 133 |
| 134 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 135 | // No custom constructor or private data member should be added. |
| 136 | class RTC_LOCKABLE GlobalLockPod |
| 137 | { |
| 138 | public: |
| 139 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 140 |
| 141 | void Unlock() RTC_UNLOCK_FUNCTION(); |
| 142 |
| 143 | volatile int lock_acquired; |
| 144 | }; |
| 145 |
| 146 | class GlobalLock : public GlobalLockPod |
| 147 | { |
| 148 | public: |
| 149 | GlobalLock(); |
| 150 | }; |
| 151 |
| 152 | // GlobalLockScope, for serializing execution through a scope. |
| 153 | class RTC_SCOPED_LOCKABLE GlobalLockScope |
| 154 | { |
| 155 | public: |
| 156 | explicit GlobalLockScope(GlobalLockPod* lock) |
| 157 | RTC_EXCLUSIVE_LOCK_FUNCTION(lock); |
| 158 | ~GlobalLockScope() RTC_UNLOCK_FUNCTION(); |
| 159 |
| 160 | private: |
| 161 | GlobalLockPod* const lock_; |
| 162 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 163 | }; |
| 164 |
| 165 | } // namespace rtc |
| 166 |
| 167 | #endif // RTC_BASE_CRITICALSECTION_H_ |
| 168 |