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 | |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_ |
| 12 | #define RTC_BASE_DEPRECATED_RECURSIVE_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/platform_thread_types.h" |
| 15 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | |
| 17 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 18 | // clang-format off |
| 19 | // clang formating would change include order. |
| 20 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | // clang-format on |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 28 | #endif // defined(WEBRTC_WIN) |
| 29 | |
| 30 | #if defined(WEBRTC_POSIX) |
| 31 | #include <pthread.h> |
| 32 | #endif |
| 33 | |
| 34 | // 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] | 35 | #define RTC_USE_NATIVE_MUTEX_ON_MAC 1 |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | |
Mirko Bonadei | a0eefc1 | 2019-07-08 14:11:02 +0200 | [diff] [blame] | 37 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 38 | #include <dispatch/dispatch.h> |
| 39 | #endif |
| 40 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 41 | namespace rtc { |
| 42 | |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 43 | // NOTE: This class is deprecated. Please use webrtc::Mutex instead! |
| 44 | // Search using https://www.google.com/?q=recursive+lock+considered+harmful |
| 45 | // to find the reasons. |
| 46 | // |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 47 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 48 | // members inside a const context without requiring mutable |
| 49 | // RecursiveCriticalSections everywhere. RecursiveCriticalSection is |
| 50 | // reentrant lock. |
| 51 | class RTC_LOCKABLE RecursiveCriticalSection { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | public: |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 53 | RecursiveCriticalSection(); |
| 54 | ~RecursiveCriticalSection(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 56 | void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 57 | bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 58 | void Leave() const RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | // Use only for RTC_DCHECKing. |
| 62 | bool CurrentThreadIsOwner() const; |
| 63 | |
| 64 | #if defined(WEBRTC_WIN) |
| 65 | mutable CRITICAL_SECTION crit_; |
| 66 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | a0eefc1 | 2019-07-08 14:11:02 +0200 | [diff] [blame] | 67 | #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | // Number of times the lock has been locked + number of threads waiting. |
| 69 | // TODO(tommi): We could use this number and subtract the recursion count |
| 70 | // to find places where we have multiple threads contending on the same lock. |
| 71 | mutable volatile int lock_queue_; |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 72 | // `recursion_` represents the recursion count + 1 for the thread that owns |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | // the lock. Only modified by the thread that owns the lock. |
| 74 | mutable int recursion_; |
| 75 | // Used to signal a single waiting thread when the lock becomes available. |
| 76 | mutable dispatch_semaphore_t semaphore_; |
| 77 | // The thread that currently holds the lock. Required to handle recursion. |
| 78 | mutable PlatformThreadRef owning_thread_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 79 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 80 | mutable pthread_mutex_t mutex_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 81 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 83 | mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 84 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | #error Unsupported platform. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | #endif |
| 87 | }; |
| 88 | |
| 89 | // CritScope, for serializing execution through a scope. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 90 | class RTC_SCOPED_LOCKABLE CritScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | public: |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 92 | explicit CritScope(const RecursiveCriticalSection* cs) |
| 93 | RTC_EXCLUSIVE_LOCK_FUNCTION(cs); |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 94 | ~CritScope() RTC_UNLOCK_FUNCTION(); |
| 95 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 96 | CritScope(const CritScope&) = delete; |
| 97 | CritScope& operator=(const CritScope&) = delete; |
| 98 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | private: |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 100 | const RecursiveCriticalSection* const cs_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 105 | #endif // RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_ |