blob: 217627eefd08029cf29a6de6eb0c56c77fc881d5 [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include "webrtc/base/constructormagic.h"
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000016#include "webrtc/base/thread_annotations.h"
tommi7406b962016-01-22 05:13:33 -080017#include "webrtc/base/platform_thread_types.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19#if defined(WEBRTC_WIN)
tommi@webrtc.org4a4e6882015-03-04 20:09:37 +000020// Include winsock2.h before including <windows.h> to maintain consistency with
21// win32.h. We can't include win32.h directly here since it pulls in
22// headers such as basictypes.h which causes problems in Chromium where webrtc
23// exists as two separate projects, webrtc and libjingle.
24#include <winsock2.h>
25#include <windows.h>
Tommi494f2092015-04-27 17:39:23 +020026#include <sal.h> // must come after windows headers.
27#endif // defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
29#if defined(WEBRTC_POSIX)
30#include <pthread.h>
31#endif
32
tommied281e92016-01-21 23:47:25 -080033// 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
Magnus Jedvert6bf10842015-04-23 11:37:55 +020040#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
Tommi494f2092015-04-27 17:39:23 +020041#define CS_DEBUG_CHECKS 1
Magnus Jedvert6bf10842015-04-23 11:37:55 +020042#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043
Tommi494f2092015-04-27 17:39:23 +020044#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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50namespace rtc {
51
Peter Boströmaf9e6632016-01-21 16:56:52 +010052// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
53// members inside a const context without requiring mutable CriticalSections
54// everywhere.
Tommi494f2092015-04-27 17:39:23 +020055class LOCKABLE CriticalSection {
56 public:
57 CriticalSection();
58 ~CriticalSection();
59
Peter Boströmaf9e6632016-01-21 16:56:52 +010060 void Enter() const EXCLUSIVE_LOCK_FUNCTION();
61 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true);
62 void Leave() const UNLOCK_FUNCTION();
Tommi494f2092015-04-27 17:39:23 +020063
andrespcdf61722016-07-08 02:45:40 -070064 private:
henrikg91d6ede2015-09-17 00:24:34 -070065 // Use only for RTC_DCHECKing.
Tommi494f2092015-04-27 17:39:23 +020066 bool CurrentThreadIsOwner() const;
Tommi494f2092015-04-27 17:39:23 +020067
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068#if defined(WEBRTC_WIN)
Peter Boströmaf9e6632016-01-21 16:56:52 +010069 mutable CRITICAL_SECTION crit_;
Tommi494f2092015-04-27 17:39:23 +020070#elif defined(WEBRTC_POSIX)
tommied281e92016-01-21 23:47:25 -080071#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.
tommi7406b962016-01-22 05:13:33 -080082 mutable PlatformThreadRef owning_thread_;
tommied281e92016-01-21 23:47:25 -080083#else
Peter Boströmaf9e6632016-01-21 16:56:52 +010084 mutable pthread_mutex_t mutex_;
tommied281e92016-01-21 23:47:25 -080085#endif
tommi7406b962016-01-22 05:13:33 -080086 CS_DEBUG_CODE(mutable PlatformThreadRef thread_);
Peter Boströmaf9e6632016-01-21 16:56:52 +010087 CS_DEBUG_CODE(mutable int recursion_count_);
Tommi494f2092015-04-27 17:39:23 +020088#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090
91// CritScope, for serializing execution through a scope.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000092class SCOPED_LOCKABLE CritScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +010094 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs);
Tommi494f2092015-04-27 17:39:23 +020095 ~CritScope() UNLOCK_FUNCTION();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +010097 const CriticalSection* const cs_;
henrikg3c089d72015-09-16 05:37:44 -070098 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099};
100
101// Tries to lock a critical section on construction via
102// CriticalSection::TryEnter, and unlocks on destruction if the
103// lock was taken. Never blocks.
104//
105// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
106// subsequent code. Users *must* check locked() to determine if the
107// lock was taken. If you're not calling locked(), you're doing it wrong!
108class TryCritScope {
109 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +0100110 explicit TryCritScope(const CriticalSection* cs);
Tommi494f2092015-04-27 17:39:23 +0200111 ~TryCritScope();
112#if defined(WEBRTC_WIN)
113 _Check_return_ bool locked() const;
114#else
kjellanderb71b4f02016-01-08 04:51:38 -0800115 bool locked() const __attribute__ ((__warn_unused_result__));
Tommi494f2092015-04-27 17:39:23 +0200116#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +0100118 const CriticalSection* const cs_;
Tommi494f2092015-04-27 17:39:23 +0200119 const bool locked_;
120 CS_DEBUG_CODE(mutable bool lock_was_called_);
henrikg3c089d72015-09-16 05:37:44 -0700121 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122};
123
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700124// A POD lock used to protect global variables. Do NOT use for other purposes.
125// No custom constructor or private data member should be added.
126class LOCKABLE GlobalLockPod {
127 public:
128 void Lock() EXCLUSIVE_LOCK_FUNCTION();
129
130 void Unlock() UNLOCK_FUNCTION();
131
pbos46ad5422015-12-07 14:29:14 -0800132 volatile int lock_acquired;
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700133};
134
pbos3c12f4d2015-11-17 03:21:02 -0800135class GlobalLock : public GlobalLockPod {
136 public:
137 GlobalLock();
138};
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700139
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200140// GlobalLockScope, for serializing execution through a scope.
141class SCOPED_LOCKABLE GlobalLockScope {
142 public:
143 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock);
144 ~GlobalLockScope() UNLOCK_FUNCTION();
145 private:
146 GlobalLockPod* const lock_;
henrikg3c089d72015-09-16 05:37:44 -0700147 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200148};
149
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150} // namespace rtc
151
Peter Boströmff019b02015-04-30 14:16:07 +0200152#endif // WEBRTC_BASE_CRITICALSECTION_H_