blob: a0f9a6be814355532b4ea2652caca199832c41a1 [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
Peter Boströmff019b02015-04-30 14:16:07 +020011#ifndef WEBRTC_BASE_CRITICALSECTION_H_
12#define WEBRTC_BASE_CRITICALSECTION_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Peter Boströmff019b02015-04-30 14:16:07 +020014#include "webrtc/base/atomicops.h"
kwibergab0b9292016-10-03 05:04:21 -070015#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/constructormagic.h"
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000017#include "webrtc/base/thread_annotations.h"
tommi7406b962016-01-22 05:13:33 -080018#include "webrtc/base/platform_thread_types.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20#if defined(WEBRTC_WIN)
tommi@webrtc.org4a4e6882015-03-04 20:09:37 +000021// 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>
Tommi494f2092015-04-27 17:39:23 +020027#include <sal.h> // must come after windows headers.
28#endif // defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029
30#if defined(WEBRTC_POSIX)
31#include <pthread.h>
32#endif
33
tommied281e92016-01-21 23:47:25 -080034// 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
kwibergab0b9292016-10-03 05:04:21 -070041#define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
Tommi494f2092015-04-27 17:39:23 +020043#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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49namespace rtc {
50
Peter Boströmaf9e6632016-01-21 16:56:52 +010051// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
52// members inside a const context without requiring mutable CriticalSections
53// everywhere.
Tommi494f2092015-04-27 17:39:23 +020054class LOCKABLE CriticalSection {
55 public:
56 CriticalSection();
57 ~CriticalSection();
58
Peter Boströmaf9e6632016-01-21 16:56:52 +010059 void Enter() const EXCLUSIVE_LOCK_FUNCTION();
60 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true);
61 void Leave() const UNLOCK_FUNCTION();
Tommi494f2092015-04-27 17:39:23 +020062
andrespcdf61722016-07-08 02:45:40 -070063 private:
henrikg91d6ede2015-09-17 00:24:34 -070064 // Use only for RTC_DCHECKing.
Tommi494f2092015-04-27 17:39:23 +020065 bool CurrentThreadIsOwner() const;
Tommi494f2092015-04-27 17:39:23 +020066
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067#if defined(WEBRTC_WIN)
Peter Boströmaf9e6632016-01-21 16:56:52 +010068 mutable CRITICAL_SECTION crit_;
Tommi494f2092015-04-27 17:39:23 +020069#elif defined(WEBRTC_POSIX)
tommied281e92016-01-21 23:47:25 -080070#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.
tommi7406b962016-01-22 05:13:33 -080081 mutable PlatformThreadRef owning_thread_;
tommied281e92016-01-21 23:47:25 -080082#else
Peter Boströmaf9e6632016-01-21 16:56:52 +010083 mutable pthread_mutex_t mutex_;
tommied281e92016-01-21 23:47:25 -080084#endif
tommi7406b962016-01-22 05:13:33 -080085 CS_DEBUG_CODE(mutable PlatformThreadRef thread_);
Peter Boströmaf9e6632016-01-21 16:56:52 +010086 CS_DEBUG_CODE(mutable int recursion_count_);
Tommi494f2092015-04-27 17:39:23 +020087#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
90// CritScope, for serializing execution through a scope.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000091class SCOPED_LOCKABLE CritScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +010093 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs);
Tommi494f2092015-04-27 17:39:23 +020094 ~CritScope() UNLOCK_FUNCTION();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +010096 const CriticalSection* const cs_;
henrikg3c089d72015-09-16 05:37:44 -070097 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098};
99
100// Tries to lock a critical section on construction via
101// CriticalSection::TryEnter, and unlocks on destruction if the
102// lock was taken. Never blocks.
103//
104// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
105// subsequent code. Users *must* check locked() to determine if the
106// lock was taken. If you're not calling locked(), you're doing it wrong!
107class TryCritScope {
108 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +0100109 explicit TryCritScope(const CriticalSection* cs);
Tommi494f2092015-04-27 17:39:23 +0200110 ~TryCritScope();
111#if defined(WEBRTC_WIN)
112 _Check_return_ bool locked() const;
113#else
kjellanderb71b4f02016-01-08 04:51:38 -0800114 bool locked() const __attribute__ ((__warn_unused_result__));
Tommi494f2092015-04-27 17:39:23 +0200115#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +0100117 const CriticalSection* const cs_;
Tommi494f2092015-04-27 17:39:23 +0200118 const bool locked_;
119 CS_DEBUG_CODE(mutable bool lock_was_called_);
henrikg3c089d72015-09-16 05:37:44 -0700120 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121};
122
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700123// A POD lock used to protect global variables. Do NOT use for other purposes.
124// No custom constructor or private data member should be added.
125class LOCKABLE GlobalLockPod {
126 public:
127 void Lock() EXCLUSIVE_LOCK_FUNCTION();
128
129 void Unlock() UNLOCK_FUNCTION();
130
pbos46ad5422015-12-07 14:29:14 -0800131 volatile int lock_acquired;
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700132};
133
pbos3c12f4d2015-11-17 03:21:02 -0800134class GlobalLock : public GlobalLockPod {
135 public:
136 GlobalLock();
137};
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700138
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200139// GlobalLockScope, for serializing execution through a scope.
140class SCOPED_LOCKABLE GlobalLockScope {
141 public:
142 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock);
143 ~GlobalLockScope() UNLOCK_FUNCTION();
144 private:
145 GlobalLockPod* const lock_;
henrikg3c089d72015-09-16 05:37:44 -0700146 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200147};
148
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149} // namespace rtc
150
Peter Boströmff019b02015-04-30 14:16:07 +0200151#endif // WEBRTC_BASE_CRITICALSECTION_H_