blob: 4433529886b896ad31f2cfdf5ea2da55cf7be4db [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"
17#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020020// clang-format off
21// clang formating would change include order.
22
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020024// win32.h. To include win32.h directly, it must be broken out into its own
25// build target.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026#include <winsock2.h>
27#include <windows.h>
28#include <sal.h> // must come after windows headers.
Yves Gerey665174f2018-06-19 15:03:05 +020029// clang-format on
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030#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.
Oskar Sundbom13471a42019-03-01 16:11:49 +010037#define USE_NATIVE_MUTEX_ON_MAC 1
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
39#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
40#include <dispatch/dispatch.h>
41#endif
42
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043namespace rtc {
44
45// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
46// members inside a const context without requiring mutable CriticalSections
Ruslan Burakov695af942019-02-26 15:19:20 +010047// everywhere. CriticalSection is reentrant lock.
danilchap3c6abd22017-09-06 05:46:29 -070048class RTC_LOCKABLE CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 public:
50 CriticalSection();
51 ~CriticalSection();
52
danilchap3c6abd22017-09-06 05:46:29 -070053 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
54 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
55 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056
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)
Yves Gerey665174f2018-06-19 15:03:05 +020064#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065 // 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 Gerey665174f2018-06-19 15:03:05 +020076#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077 mutable pthread_mutex_t mutex_;
Yves Gerey665174f2018-06-19 15:03:05 +020078#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 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 Gerey665174f2018-06-19 15:03:05 +020082#error Unsupported platform.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083#endif
84};
85
86// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070087class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 public:
danilchap3c6abd22017-09-06 05:46:29 -070089 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
90 ~CritScope() RTC_UNLOCK_FUNCTION();
91
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 private:
93 const CriticalSection* const cs_;
94 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
95};
96
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097// A POD lock used to protect global variables. Do NOT use for other purposes.
98// No custom constructor or private data member should be added.
danilchap3c6abd22017-09-06 05:46:29 -070099class RTC_LOCKABLE GlobalLockPod {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 public:
danilchap3c6abd22017-09-06 05:46:29 -0700101 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102
danilchap3c6abd22017-09-06 05:46:29 -0700103 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104
105 volatile int lock_acquired;
106};
107
108class GlobalLock : public GlobalLockPod {
109 public:
110 GlobalLock();
111};
112
113// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700114class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 public:
danilchap3c6abd22017-09-06 05:46:29 -0700116 explicit GlobalLockScope(GlobalLockPod* lock)
117 RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
118 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
119
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 private:
121 GlobalLockPod* const lock_;
122 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
123};
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
Steve Anton10542f22019-01-11 09:11:00 -0800127#endif // RTC_BASE_CRITICAL_SECTION_H_