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