blob: 6abdbe57a6e363c5fb81adf55b33a2d749c64c7b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_CRITICALSECTION_H_
12#define RTC_BASE_CRITICALSECTION_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
15#include "rtc_base/constructormagic.h"
16#include "rtc_base/platform_thread_types.h"
17#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20#if defined(WEBRTC_WIN)
21// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020022// win32.h. To include win32.h directly, it must be broken out into its own
23// build target.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#include <winsock2.h>
25#include <windows.h>
26#include <sal.h> // must come after windows headers.
27#endif // defined(WEBRTC_WIN)
28
29#if defined(WEBRTC_POSIX)
30#include <pthread.h>
31#endif
32
33// See notes in the 'Performance' unit test for the effects of this flag.
34#define USE_NATIVE_MUTEX_ON_MAC 0
35
36#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
37#include <dispatch/dispatch.h>
38#endif
39
40#define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
41
42#if CS_DEBUG_CHECKS
43#define CS_DEBUG_CODE(x) x
44#else // !CS_DEBUG_CHECKS
45#define CS_DEBUG_CODE(x)
46#endif // !CS_DEBUG_CHECKS
47
48namespace rtc {
49
50// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
51// members inside a const context without requiring mutable CriticalSections
52// everywhere.
danilchap3c6abd22017-09-06 05:46:29 -070053class RTC_LOCKABLE CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 public:
55 CriticalSection();
56 ~CriticalSection();
57
danilchap3c6abd22017-09-06 05:46:29 -070058 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
59 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
60 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061
62 private:
63 // Use only for RTC_DCHECKing.
64 bool CurrentThreadIsOwner() const;
65
66#if defined(WEBRTC_WIN)
67 mutable CRITICAL_SECTION crit_;
68#elif defined(WEBRTC_POSIX)
69# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
70 // Number of times the lock has been locked + number of threads waiting.
71 // TODO(tommi): We could use this number and subtract the recursion count
72 // to find places where we have multiple threads contending on the same lock.
73 mutable volatile int lock_queue_;
74 // |recursion_| represents the recursion count + 1 for the thread that owns
75 // the lock. Only modified by the thread that owns the lock.
76 mutable int recursion_;
77 // Used to signal a single waiting thread when the lock becomes available.
78 mutable dispatch_semaphore_t semaphore_;
79 // The thread that currently holds the lock. Required to handle recursion.
80 mutable PlatformThreadRef owning_thread_;
81# else
82 mutable pthread_mutex_t mutex_;
83# endif
84 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
85 mutable int recursion_count_; // Only used by RTC_DCHECKs.
86#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
87# error Unsupported platform.
88#endif
89};
90
91// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070092class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093 public:
danilchap3c6abd22017-09-06 05:46:29 -070094 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
95 ~CritScope() RTC_UNLOCK_FUNCTION();
96
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 private:
98 const CriticalSection* const cs_;
99 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
100};
101
102// Tries to lock a critical section on construction via
103// CriticalSection::TryEnter, and unlocks on destruction if the
104// lock was taken. Never blocks.
105//
106// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
107// subsequent code. Users *must* check locked() to determine if the
108// lock was taken. If you're not calling locked(), you're doing it wrong!
109class TryCritScope {
110 public:
111 explicit TryCritScope(const CriticalSection* cs);
112 ~TryCritScope();
113#if defined(WEBRTC_WIN)
114 _Check_return_ bool locked() const;
115#elif defined(WEBRTC_POSIX)
116 bool locked() const __attribute__ ((__warn_unused_result__));
117#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
118# error Unsupported platform.
119#endif
120 private:
121 const CriticalSection* const cs_;
122 const bool locked_;
123 mutable bool lock_was_called_; // Only used by RTC_DCHECKs.
124 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
125};
126
127// A POD lock used to protect global variables. Do NOT use for other purposes.
128// No custom constructor or private data member should be added.
danilchap3c6abd22017-09-06 05:46:29 -0700129class RTC_LOCKABLE GlobalLockPod {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200130 public:
danilchap3c6abd22017-09-06 05:46:29 -0700131 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132
danilchap3c6abd22017-09-06 05:46:29 -0700133 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134
135 volatile int lock_acquired;
136};
137
138class GlobalLock : public GlobalLockPod {
139 public:
140 GlobalLock();
141};
142
143// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700144class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145 public:
danilchap3c6abd22017-09-06 05:46:29 -0700146 explicit GlobalLockScope(GlobalLockPod* lock)
147 RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
148 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
149
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 private:
151 GlobalLockPod* const lock_;
152 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
153};
154
155} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200157#endif // RTC_BASE_CRITICALSECTION_H_