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