blob: cf10463bdffee0e1bff639194db8f919a3b4a099 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_CRITICAL_SECTION_H_
12#define RTC_BASE_CRITICAL_SECTION_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/platform_thread_types.h"
Mirko Bonadeic66e0042019-10-18 09:52:22 +020017#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020021// clang-format off
22// clang formating would change include order.
23
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020025// win32.h. To include win32.h directly, it must be broken out into its own
26// build target.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027#include <winsock2.h>
28#include <windows.h>
29#include <sal.h> // must come after windows headers.
Yves Gerey665174f2018-06-19 15:03:05 +020030// clang-format on
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031#endif // defined(WEBRTC_WIN)
32
33#if defined(WEBRTC_POSIX)
34#include <pthread.h>
35#endif
36
37// See notes in the 'Performance' unit test for the effects of this flag.
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020038#define RTC_USE_NATIVE_MUTEX_ON_MAC 1
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020040#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041#include <dispatch/dispatch.h>
42#endif
43
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044namespace rtc {
45
46// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
47// members inside a const context without requiring mutable CriticalSections
Ruslan Burakov695af942019-02-26 15:19:20 +010048// everywhere. CriticalSection is reentrant lock.
Mirko Bonadeic66e0042019-10-18 09:52:22 +020049class RTC_LOCKABLE RTC_EXPORT CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050 public:
51 CriticalSection();
52 ~CriticalSection();
53
danilchap3c6abd22017-09-06 05:46:29 -070054 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
55 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
56 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58 private:
59 // Use only for RTC_DCHECKing.
60 bool CurrentThreadIsOwner() const;
61
62#if defined(WEBRTC_WIN)
63 mutable CRITICAL_SECTION crit_;
64#elif defined(WEBRTC_POSIX)
Mirko Bonadeia0eefc12019-07-08 14:11:02 +020065#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 // Number of times the lock has been locked + number of threads waiting.
67 // TODO(tommi): We could use this number and subtract the recursion count
68 // to find places where we have multiple threads contending on the same lock.
69 mutable volatile int lock_queue_;
70 // |recursion_| represents the recursion count + 1 for the thread that owns
71 // the lock. Only modified by the thread that owns the lock.
72 mutable int recursion_;
73 // Used to signal a single waiting thread when the lock becomes available.
74 mutable dispatch_semaphore_t semaphore_;
75 // The thread that currently holds the lock. Required to handle recursion.
76 mutable PlatformThreadRef owning_thread_;
Yves Gerey665174f2018-06-19 15:03:05 +020077#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 mutable pthread_mutex_t mutex_;
Yves Gerey665174f2018-06-19 15:03:05 +020079#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
81 mutable int recursion_count_; // Only used by RTC_DCHECKs.
82#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +020083#error Unsupported platform.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084#endif
85};
86
87// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070088class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089 public:
danilchap3c6abd22017-09-06 05:46:29 -070090 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
91 ~CritScope() RTC_UNLOCK_FUNCTION();
92
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093 private:
94 const CriticalSection* const cs_;
95 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
96};
97
Danil Chapovalov5740f3e2019-10-10 11:12:15 +020098// A lock used to protect global variables. Do NOT use for other purposes.
99class RTC_LOCKABLE GlobalLock {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 public:
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200101 constexpr GlobalLock() : lock_acquired_(0) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200103 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
danilchap3c6abd22017-09-06 05:46:29 -0700104 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200106 private:
107 volatile int lock_acquired_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108};
109
110// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700111class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 public:
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200113 explicit GlobalLockScope(GlobalLock* lock) RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
danilchap3c6abd22017-09-06 05:46:29 -0700114 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
115
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 private:
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200117 GlobalLock* const lock_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
119};
120
Yves Gerey665174f2018-06-19 15:03:05 +0200121} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122
Steve Anton10542f22019-01-11 09:11:00 -0800123#endif // RTC_BASE_CRITICAL_SECTION_H_