blob: 569e1479bdb0e2390b0c0ee3e705f8d2e61775e5 [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
22// win32.h. We can't include win32.h directly here since it pulls in
23// headers such as basictypes.h which causes problems in Chromium where webrtc
24// exists as two separate projects, webrtc and libjingle.
25#include <winsock2.h>
26#include <windows.h>
27#include <sal.h> // must come after windows headers.
28#endif // defined(WEBRTC_WIN)
29
30#if defined(WEBRTC_POSIX)
31#include <pthread.h>
32#endif
33
34// See notes in the 'Performance' unit test for the effects of this flag.
35#define USE_NATIVE_MUTEX_ON_MAC 0
36
37#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
38#include <dispatch/dispatch.h>
39#endif
40
41#define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
42
43#if CS_DEBUG_CHECKS
44#define CS_DEBUG_CODE(x) x
45#else // !CS_DEBUG_CHECKS
46#define CS_DEBUG_CODE(x)
47#endif // !CS_DEBUG_CHECKS
48
49namespace rtc {
50
51// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
52// members inside a const context without requiring mutable CriticalSections
53// everywhere.
danilchap3c6abd22017-09-06 05:46:29 -070054class RTC_LOCKABLE CriticalSection {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 public:
56 CriticalSection();
57 ~CriticalSection();
58
danilchap3c6abd22017-09-06 05:46:29 -070059 void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
60 bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
61 void Leave() const RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 private:
64 // Use only for RTC_DCHECKing.
65 bool CurrentThreadIsOwner() const;
66
67#if defined(WEBRTC_WIN)
68 mutable CRITICAL_SECTION crit_;
69#elif defined(WEBRTC_POSIX)
70# if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
71 // Number of times the lock has been locked + number of threads waiting.
72 // TODO(tommi): We could use this number and subtract the recursion count
73 // to find places where we have multiple threads contending on the same lock.
74 mutable volatile int lock_queue_;
75 // |recursion_| represents the recursion count + 1 for the thread that owns
76 // the lock. Only modified by the thread that owns the lock.
77 mutable int recursion_;
78 // Used to signal a single waiting thread when the lock becomes available.
79 mutable dispatch_semaphore_t semaphore_;
80 // The thread that currently holds the lock. Required to handle recursion.
81 mutable PlatformThreadRef owning_thread_;
82# else
83 mutable pthread_mutex_t mutex_;
84# endif
85 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
86 mutable int recursion_count_; // Only used by RTC_DCHECKs.
87#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
88# error Unsupported platform.
89#endif
90};
91
92// CritScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -070093class RTC_SCOPED_LOCKABLE CritScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094 public:
danilchap3c6abd22017-09-06 05:46:29 -070095 explicit CritScope(const CriticalSection* cs) RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
96 ~CritScope() RTC_UNLOCK_FUNCTION();
97
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 private:
99 const CriticalSection* const cs_;
100 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
101};
102
103// Tries to lock a critical section on construction via
104// CriticalSection::TryEnter, and unlocks on destruction if the
105// lock was taken. Never blocks.
106//
107// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
108// subsequent code. Users *must* check locked() to determine if the
109// lock was taken. If you're not calling locked(), you're doing it wrong!
110class TryCritScope {
111 public:
112 explicit TryCritScope(const CriticalSection* cs);
113 ~TryCritScope();
114#if defined(WEBRTC_WIN)
115 _Check_return_ bool locked() const;
116#elif defined(WEBRTC_POSIX)
117 bool locked() const __attribute__ ((__warn_unused_result__));
118#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
119# error Unsupported platform.
120#endif
121 private:
122 const CriticalSection* const cs_;
123 const bool locked_;
124 mutable bool lock_was_called_; // Only used by RTC_DCHECKs.
125 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
126};
127
128// A POD lock used to protect global variables. Do NOT use for other purposes.
129// No custom constructor or private data member should be added.
danilchap3c6abd22017-09-06 05:46:29 -0700130class RTC_LOCKABLE GlobalLockPod {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131 public:
danilchap3c6abd22017-09-06 05:46:29 -0700132 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133
danilchap3c6abd22017-09-06 05:46:29 -0700134 void Unlock() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135
136 volatile int lock_acquired;
137};
138
139class GlobalLock : public GlobalLockPod {
140 public:
141 GlobalLock();
142};
143
144// GlobalLockScope, for serializing execution through a scope.
danilchap3c6abd22017-09-06 05:46:29 -0700145class RTC_SCOPED_LOCKABLE GlobalLockScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200146 public:
danilchap3c6abd22017-09-06 05:46:29 -0700147 explicit GlobalLockScope(GlobalLockPod* lock)
148 RTC_EXCLUSIVE_LOCK_FUNCTION(lock);
149 ~GlobalLockScope() RTC_UNLOCK_FUNCTION();
150
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 private:
152 GlobalLockPod* const lock_;
153 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
154};
155
156} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200158#endif // RTC_BASE_CRITICALSECTION_H_