blob: 1ef9634efe43424fe312ef8a4756f214be013c52 [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/atomicops.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/constructormagic.h"
17#include "rtc_base/platform_thread_types.h"
18#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21#if defined(WEBRTC_WIN)
22// Include winsock2.h before including <windows.h> to maintain consistency with
23// win32.h. We can't include win32.h directly here since it pulls in
24// headers such as basictypes.h which causes problems in Chromium where webrtc
25// exists as two separate projects, webrtc and libjingle.
26#include <winsock2.h>
27#include <windows.h>
28#include <sal.h> // must come after windows headers.
29#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.
36#define USE_NATIVE_MUTEX_ON_MAC 0
37
38#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
39#include <dispatch/dispatch.h>
40#endif
41
42#define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
43
44#if CS_DEBUG_CHECKS
45#define CS_DEBUG_CODE(x) x
46#else // !CS_DEBUG_CHECKS
47#define CS_DEBUG_CODE(x)
48#endif // !CS_DEBUG_CHECKS
49
50namespace rtc {
51
52// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
53// members inside a const context without requiring mutable CriticalSections
54// everywhere.
danilchap3c6abd22017-09-06 05:46:29 -070055class RTC_LOCKABLE CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 public:
57 CriticalSection();
58 ~CriticalSection();
59
danilchap3c6abd22017-09-06 05:46:29 -070060 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
61 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
62 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
64 private:
65 // Use only for RTC_DCHECKing.
66 bool CurrentThreadIsOwner() const;
67
68#if defined(WEBRTC_WIN)
69 mutable CRITICAL_SECTION crit_;
70#elif defined(WEBRTC_POSIX)
71# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
72 // Number of times the lock has been locked + number of threads waiting.
73 // TODO(tommi): We could use this number and subtract the recursion count
74 // to find places where we have multiple threads contending on the same lock.
75 mutable volatile int lock_queue_;
76 // |recursion_| represents the recursion count + 1 for the thread that owns
77 // the lock. Only modified by the thread that owns the lock.
78 mutable int recursion_;
79 // Used to signal a single waiting thread when the lock becomes available.
80 mutable dispatch_semaphore_t semaphore_;
81 // The thread that currently holds the lock. Required to handle recursion.
82 mutable PlatformThreadRef owning_thread_;
83# else
84 mutable pthread_mutex_t mutex_;
85# endif
86 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
87 mutable int recursion_count_; // Only used by RTC_DCHECKs.
88#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
89# error Unsupported platform.
90#endif
91};
92
93// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070094class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 public:
danilchap3c6abd22017-09-06 05:46:29 -070096 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
97 ~CritScope() RTC_UNLOCK_FUNCTION();
98
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099 private:
100 const CriticalSection* const cs_;
101 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
102};
103
104// Tries to lock a critical section on construction via
105// CriticalSection::TryEnter, and unlocks on destruction if the
106// lock was taken. Never blocks.
107//
108// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
109// subsequent code. Users *must* check locked() to determine if the
110// lock was taken. If you're not calling locked(), you're doing it wrong!
111class TryCritScope {
112 public:
113 explicit TryCritScope(const CriticalSection* cs);
114 ~TryCritScope();
115#if defined(WEBRTC_WIN)
116 _Check_return_ bool locked() const;
117#elif defined(WEBRTC_POSIX)
118 bool locked() const __attribute__ ((__warn_unused_result__));
119#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
120# error Unsupported platform.
121#endif
122 private:
123 const CriticalSection* const cs_;
124 const bool locked_;
125 mutable bool lock_was_called_; // Only used by RTC_DCHECKs.
126 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
127};
128
129// A POD lock used to protect global variables. Do NOT use for other purposes.
130// No custom constructor or private data member should be added.
danilchap3c6abd22017-09-06 05:46:29 -0700131class RTC_LOCKABLE GlobalLockPod {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 public:
danilchap3c6abd22017-09-06 05:46:29 -0700133 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134
danilchap3c6abd22017-09-06 05:46:29 -0700135 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 volatile int lock_acquired;
138};
139
140class GlobalLock : public GlobalLockPod {
141 public:
142 GlobalLock();
143};
144
145// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700146class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200147 public:
danilchap3c6abd22017-09-06 05:46:29 -0700148 explicit GlobalLockScope(GlobalLockPod* lock)
149 RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
150 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
151
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200152 private:
153 GlobalLockPod* const lock_;
154 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
155};
156
157} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200159#endif // RTC_BASE_CRITICALSECTION_H_