blob: c044c732b922eecd38df42e5d1c917d1bea46305 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Handell3cb525b2020-07-16 16:16:09 +020011#ifndef RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
12#define RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/platform_thread_types.h"
16#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017
18#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020019// clang-format off
20// clang formating would change include order.
21
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020023// win32.h. To include win32.h directly, it must be broken out into its own
24// build target.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025#include <winsock2.h>
26#include <windows.h>
27#include <sal.h> // must come after windows headers.
Yves Gerey665174f2018-06-19 15:03:05 +020028// clang-format on
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029#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.
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020036#define RTC_USE_NATIVE_MUTEX_ON_MAC 1
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020038#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039#include <dispatch/dispatch.h>
40#endif
41
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042namespace rtc {
43
Markus Handell3cb525b2020-07-16 16:16:09 +020044// NOTE: This class is deprecated. Please use webrtc::Mutex instead!
45// Search using https://www.google.com/?q=recursive+lock+considered+harmful
46// to find the reasons.
47//
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
Markus Handell3cb525b2020-07-16 16:16:09 +020049// members inside a const context without requiring mutable
50// RecursiveCriticalSections everywhere. RecursiveCriticalSection is
51// reentrant lock.
52class RTC_LOCKABLE RecursiveCriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 public:
Markus Handell3cb525b2020-07-16 16:16:09 +020054 RecursiveCriticalSection();
55 ~RecursiveCriticalSection();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056
danilchap3c6abd22017-09-06 05:46:29 -070057 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
58 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
59 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
61 private:
62 // Use only for RTC_DCHECKing.
63 bool CurrentThreadIsOwner() const;
64
65#if defined(WEBRTC_WIN)
66 mutable CRITICAL_SECTION crit_;
67#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020068#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 // Number of times the lock has been locked + number of threads waiting.
70 // TODO(tommi): We could use this number and subtract the recursion count
71 // to find places where we have multiple threads contending on the same lock.
72 mutable volatile int lock_queue_;
73 // |recursion_| represents the recursion count + 1 for the thread that owns
74 // the lock. Only modified by the thread that owns the lock.
75 mutable int recursion_;
76 // Used to signal a single waiting thread when the lock becomes available.
77 mutable dispatch_semaphore_t semaphore_;
78 // The thread that currently holds the lock. Required to handle recursion.
79 mutable PlatformThreadRef owning_thread_;
Yves Gerey665174f2018-06-19 15:03:05 +020080#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 mutable pthread_mutex_t mutex_;
Yves Gerey665174f2018-06-19 15:03:05 +020082#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
84 mutable int recursion_count_; // Only used by RTC_DCHECKs.
85#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +020086#error Unsupported platform.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087#endif
88};
89
90// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070091class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 public:
Markus Handell3cb525b2020-07-16 16:16:09 +020093 explicit CritScope(const RecursiveCriticalSection* cs)
94 RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
danilchap3c6abd22017-09-06 05:46:29 -070095 ~CritScope() RTC_UNLOCK_FUNCTION();
96
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 private:
Markus Handell3cb525b2020-07-16 16:16:09 +020098 const RecursiveCriticalSection* const cs_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
100};
101
Yves Gerey665174f2018-06-19 15:03:05 +0200102} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103
Markus Handell3cb525b2020-07-16 16:16:09 +0200104#endif // RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_