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