henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_CRITICAL_SECTION_H_ |
| 12 | #define RTC_BASE_CRITICAL_SECTION_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/platform_thread_types.h" |
| 17 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | |
| 19 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 20 | // clang-format off |
| 21 | // clang formating would change include order. |
| 22 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | // Include winsock2.h before including <windows.h> to maintain consistency with |
Niels Möller | b06b0a6 | 2018-05-25 10:05:34 +0200 | [diff] [blame] | 24 | // win32.h. To include win32.h directly, it must be broken out into its own |
| 25 | // build target. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | #include <winsock2.h> |
| 27 | #include <windows.h> |
| 28 | #include <sal.h> // must come after windows headers. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | // clang-format on |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 54 | // members inside a const context without requiring mutable CriticalSections |
Ruslan Burakov | 695af94 | 2019-02-26 15:19:20 +0100 | [diff] [blame] | 55 | // everywhere. CriticalSection is reentrant lock. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 56 | class RTC_LOCKABLE CriticalSection { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 57 | public: |
| 58 | CriticalSection(); |
| 59 | ~CriticalSection(); |
| 60 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 61 | void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 62 | bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 63 | void Leave() const RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | // Use only for RTC_DCHECKing. |
| 67 | bool CurrentThreadIsOwner() const; |
| 68 | |
| 69 | #if defined(WEBRTC_WIN) |
| 70 | mutable CRITICAL_SECTION crit_; |
| 71 | #elif defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | // Number of times the lock has been locked + number of threads waiting. |
| 74 | // TODO(tommi): We could use this number and subtract the recursion count |
| 75 | // to find places where we have multiple threads contending on the same lock. |
| 76 | mutable volatile int lock_queue_; |
| 77 | // |recursion_| represents the recursion count + 1 for the thread that owns |
| 78 | // the lock. Only modified by the thread that owns the lock. |
| 79 | mutable int recursion_; |
| 80 | // Used to signal a single waiting thread when the lock becomes available. |
| 81 | mutable dispatch_semaphore_t semaphore_; |
| 82 | // The thread that currently holds the lock. Required to handle recursion. |
| 83 | mutable PlatformThreadRef owning_thread_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 84 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | mutable pthread_mutex_t mutex_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 86 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 88 | mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 89 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | #error Unsupported platform. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | // CritScope, for serializing execution through a scope. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 95 | class RTC_SCOPED_LOCKABLE CritScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 96 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 97 | explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs); |
| 98 | ~CritScope() RTC_UNLOCK_FUNCTION(); |
| 99 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 100 | private: |
| 101 | const CriticalSection* const cs_; |
| 102 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 103 | }; |
| 104 | |
| 105 | // Tries to lock a critical section on construction via |
| 106 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 107 | // lock was taken. Never blocks. |
| 108 | // |
| 109 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 110 | // subsequent code. Users *must* check locked() to determine if the |
| 111 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 112 | class TryCritScope { |
| 113 | public: |
| 114 | explicit TryCritScope(const CriticalSection* cs); |
| 115 | ~TryCritScope(); |
| 116 | #if defined(WEBRTC_WIN) |
| 117 | _Check_return_ bool locked() const; |
| 118 | #elif defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | bool locked() const __attribute__((__warn_unused_result__)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 121 | #error Unsupported platform. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | #endif |
| 123 | private: |
| 124 | const CriticalSection* const cs_; |
| 125 | const bool locked_; |
| 126 | mutable bool lock_was_called_; // Only used by RTC_DCHECKs. |
| 127 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 128 | }; |
| 129 | |
| 130 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 131 | // No custom constructor or private data member should be added. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 132 | class RTC_LOCKABLE GlobalLockPod { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 133 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 134 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 136 | void Unlock() RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | |
| 138 | volatile int lock_acquired; |
| 139 | }; |
| 140 | |
| 141 | class GlobalLock : public GlobalLockPod { |
| 142 | public: |
| 143 | GlobalLock(); |
| 144 | }; |
| 145 | |
| 146 | // GlobalLockScope, for serializing execution through a scope. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 147 | class RTC_SCOPED_LOCKABLE GlobalLockScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 148 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 149 | explicit GlobalLockScope(GlobalLockPod* lock) |
| 150 | RTC_EXCLUSIVE_LOCK_FUNCTION(lock); |
| 151 | ~GlobalLockScope() RTC_UNLOCK_FUNCTION(); |
| 152 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 153 | private: |
| 154 | GlobalLockPod* const lock_; |
| 155 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 156 | }; |
| 157 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 158 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 160 | #endif // RTC_BASE_CRITICAL_SECTION_H_ |