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. |
Mirko Bonadei | a0eefc1 | 2019-07-08 14:11:02 +0200 | [diff] [blame] | 37 | #define RTC_USE_NATIVE_MUTEX_ON_MAC 1 |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 38 | |
Mirko Bonadei | a0eefc1 | 2019-07-08 14:11:02 +0200 | [diff] [blame] | 39 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | #include <dispatch/dispatch.h> |
| 41 | #endif |
| 42 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | namespace rtc { |
| 44 | |
| 45 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 46 | // members inside a const context without requiring mutable CriticalSections |
Ruslan Burakov | 695af94 | 2019-02-26 15:19:20 +0100 | [diff] [blame] | 47 | // everywhere. CriticalSection is reentrant lock. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 48 | class RTC_LOCKABLE CriticalSection { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | public: |
| 50 | CriticalSection(); |
| 51 | ~CriticalSection(); |
| 52 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 53 | void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 54 | bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 55 | void Leave() const RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | // Use only for RTC_DCHECKing. |
| 59 | bool CurrentThreadIsOwner() const; |
| 60 | |
| 61 | #if defined(WEBRTC_WIN) |
| 62 | mutable CRITICAL_SECTION crit_; |
| 63 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 14:11:02 +0200 | [diff] [blame] | 64 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | // Number of times the lock has been locked + number of threads waiting. |
| 66 | // TODO(tommi): We could use this number and subtract the recursion count |
| 67 | // to find places where we have multiple threads contending on the same lock. |
| 68 | mutable volatile int lock_queue_; |
| 69 | // |recursion_| represents the recursion count + 1 for the thread that owns |
| 70 | // the lock. Only modified by the thread that owns the lock. |
| 71 | mutable int recursion_; |
| 72 | // Used to signal a single waiting thread when the lock becomes available. |
| 73 | mutable dispatch_semaphore_t semaphore_; |
| 74 | // The thread that currently holds the lock. Required to handle recursion. |
| 75 | mutable PlatformThreadRef owning_thread_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 77 | mutable pthread_mutex_t mutex_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 80 | mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 81 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 82 | #error Unsupported platform. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 83 | #endif |
| 84 | }; |
| 85 | |
| 86 | // CritScope, for serializing execution through a scope. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 87 | class RTC_SCOPED_LOCKABLE CritScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 88 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 89 | explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs); |
| 90 | ~CritScope() RTC_UNLOCK_FUNCTION(); |
| 91 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 92 | private: |
| 93 | const CriticalSection* const cs_; |
| 94 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 95 | }; |
| 96 | |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 97 | // A lock used to protect global variables. Do NOT use for other purposes. |
| 98 | class RTC_LOCKABLE GlobalLock { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | public: |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 100 | constexpr GlobalLock() : lock_acquired_(0) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 101 | |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 102 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 103 | void Unlock() RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 104 | |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 105 | private: |
| 106 | volatile int lock_acquired_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // GlobalLockScope, for serializing execution through a scope. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 110 | class RTC_SCOPED_LOCKABLE GlobalLockScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | public: |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 112 | explicit GlobalLockScope(GlobalLock* lock) RTC_EXCLUSIVE_LOCK_FUNCTION(lock); |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 113 | ~GlobalLockScope() RTC_UNLOCK_FUNCTION(); |
| 114 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 115 | private: |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame^] | 116 | GlobalLock* const lock_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 117 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 118 | }; |
| 119 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 122 | #endif // RTC_BASE_CRITICAL_SECTION_H_ |