blob: a81fa0bbdc39087d6f6f120e707863072a5450d2 [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.
37#define USE_NATIVE_MUTEX_ON_MAC 0
38
39#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
40#include <dispatch/dispatch.h>
41#endif
42
43#define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
44
45#if CS_DEBUG_CHECKS
46#define CS_DEBUG_CODE(x) x
47#else // !CS_DEBUG_CHECKS
48#define CS_DEBUG_CODE(x)
49#endif // !CS_DEBUG_CHECKS
50
51namespace rtc {
52
53// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
54// members inside a const context without requiring mutable CriticalSections
Ruslan Burakov695af942019-02-26 15:19:20 +010055// everywhere. CriticalSection is reentrant lock.
danilchap3c6abd22017-09-06 05:46:29 -070056class RTC_LOCKABLE CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 public:
58 CriticalSection();
59 ~CriticalSection();
60
danilchap3c6abd22017-09-06 05:46:29 -070061 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
62 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
63 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064
65 private:
66 // Use only for RTC_DCHECKing.
67 bool CurrentThreadIsOwner() const;
68
69#if defined(WEBRTC_WIN)
70 mutable CRITICAL_SECTION crit_;
71#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +020072#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 // Number of times the lock has been locked + number of threads waiting.
74 // TODO(tommi): We could use this number and subtract the recursion count
75 // to find places where we have multiple threads contending on the same lock.
76 mutable volatile int lock_queue_;
77 // |recursion_| represents the recursion count + 1 for the thread that owns
78 // the lock. Only modified by the thread that owns the lock.
79 mutable int recursion_;
80 // Used to signal a single waiting thread when the lock becomes available.
81 mutable dispatch_semaphore_t semaphore_;
82 // The thread that currently holds the lock. Required to handle recursion.
83 mutable PlatformThreadRef owning_thread_;
Yves Gerey665174f2018-06-19 15:03:05 +020084#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 mutable pthread_mutex_t mutex_;
Yves Gerey665174f2018-06-19 15:03:05 +020086#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
88 mutable int recursion_count_; // Only used by RTC_DCHECKs.
89#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +020090#error Unsupported platform.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091#endif
92};
93
94// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070095class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096 public:
danilchap3c6abd22017-09-06 05:46:29 -070097 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
98 ~CritScope() RTC_UNLOCK_FUNCTION();
99
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 private:
101 const CriticalSection* const cs_;
102 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
103};
104
105// Tries to lock a critical section on construction via
106// CriticalSection::TryEnter, and unlocks on destruction if the
107// lock was taken. Never blocks.
108//
109// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
110// subsequent code. Users *must* check locked() to determine if the
111// lock was taken. If you're not calling locked(), you're doing it wrong!
112class TryCritScope {
113 public:
114 explicit TryCritScope(const CriticalSection* cs);
115 ~TryCritScope();
116#if defined(WEBRTC_WIN)
117 _Check_return_ bool locked() const;
118#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200119 bool locked() const __attribute__((__warn_unused_result__));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200121#error Unsupported platform.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122#endif
123 private:
124 const CriticalSection* const cs_;
125 const bool locked_;
126 mutable bool lock_was_called_; // Only used by RTC_DCHECKs.
127 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
128};
129
130// A POD lock used to protect global variables. Do NOT use for other purposes.
131// No custom constructor or private data member should be added.
danilchap3c6abd22017-09-06 05:46:29 -0700132class RTC_LOCKABLE GlobalLockPod {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133 public:
danilchap3c6abd22017-09-06 05:46:29 -0700134 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135
danilchap3c6abd22017-09-06 05:46:29 -0700136 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137
138 volatile int lock_acquired;
139};
140
141class GlobalLock : public GlobalLockPod {
142 public:
143 GlobalLock();
144};
145
146// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700147class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200148 public:
danilchap3c6abd22017-09-06 05:46:29 -0700149 explicit GlobalLockScope(GlobalLockPod* lock)
150 RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
151 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
152
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153 private:
154 GlobalLockPod* const lock_;
155 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
156};
157
Yves Gerey665174f2018-06-19 15:03:05 +0200158} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159
Steve Anton10542f22019-01-11 09:11:00 -0800160#endif // RTC_BASE_CRITICAL_SECTION_H_