blob: cc54eca1f0d4e7ddbbf9d5d201fdb83575950480 [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18#if defined(WEBRTC_WIN)
tommi@webrtc.org4a4e6882015-03-04 20:09:37 +000019// Include winsock2.h before including <windows.h> to maintain consistency with
20// win32.h. We can't include win32.h directly here since it pulls in
21// headers such as basictypes.h which causes problems in Chromium where webrtc
22// exists as two separate projects, webrtc and libjingle.
23#include <winsock2.h>
24#include <windows.h>
Tommi494f2092015-04-27 17:39:23 +020025#include <sal.h> // must come after windows headers.
26#endif // defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28#if defined(WEBRTC_POSIX)
29#include <pthread.h>
30#endif
31
Magnus Jedvert6bf10842015-04-23 11:37:55 +020032#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
Tommi494f2092015-04-27 17:39:23 +020033#define CS_DEBUG_CHECKS 1
Magnus Jedvert6bf10842015-04-23 11:37:55 +020034#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
Tommi494f2092015-04-27 17:39:23 +020036#if CS_DEBUG_CHECKS
37#define CS_DEBUG_CODE(x) x
38#else // !CS_DEBUG_CHECKS
39#define CS_DEBUG_CODE(x)
40#endif // !CS_DEBUG_CHECKS
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
42namespace rtc {
43
Peter Boströmaf9e6632016-01-21 16:56:52 +010044// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
45// members inside a const context without requiring mutable CriticalSections
46// everywhere.
Tommi494f2092015-04-27 17:39:23 +020047class LOCKABLE CriticalSection {
48 public:
49 CriticalSection();
50 ~CriticalSection();
51
Peter Boströmaf9e6632016-01-21 16:56:52 +010052 void Enter() const EXCLUSIVE_LOCK_FUNCTION();
53 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true);
54 void Leave() const UNLOCK_FUNCTION();
Tommi494f2092015-04-27 17:39:23 +020055
henrikg91d6ede2015-09-17 00:24:34 -070056 // Use only for RTC_DCHECKing.
Tommi494f2092015-04-27 17:39:23 +020057 bool CurrentThreadIsOwner() const;
henrikg91d6ede2015-09-17 00:24:34 -070058 // Use only for RTC_DCHECKing.
Tommi494f2092015-04-27 17:39:23 +020059 bool IsLocked() const;
60
61 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062#if defined(WEBRTC_WIN)
Peter Boströmaf9e6632016-01-21 16:56:52 +010063 mutable CRITICAL_SECTION crit_;
Tommi494f2092015-04-27 17:39:23 +020064#elif defined(WEBRTC_POSIX)
Peter Boströmaf9e6632016-01-21 16:56:52 +010065 mutable pthread_mutex_t mutex_;
66 CS_DEBUG_CODE(mutable pthread_t thread_);
67 CS_DEBUG_CODE(mutable int recursion_count_);
Tommi494f2092015-04-27 17:39:23 +020068#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070
71// CritScope, for serializing execution through a scope.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000072class SCOPED_LOCKABLE CritScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +010074 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs);
Tommi494f2092015-04-27 17:39:23 +020075 ~CritScope() UNLOCK_FUNCTION();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +010077 const CriticalSection* const cs_;
henrikg3c089d72015-09-16 05:37:44 -070078 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079};
80
81// Tries to lock a critical section on construction via
82// CriticalSection::TryEnter, and unlocks on destruction if the
83// lock was taken. Never blocks.
84//
85// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
86// subsequent code. Users *must* check locked() to determine if the
87// lock was taken. If you're not calling locked(), you're doing it wrong!
88class TryCritScope {
89 public:
Peter Boströmaf9e6632016-01-21 16:56:52 +010090 explicit TryCritScope(const CriticalSection* cs);
Tommi494f2092015-04-27 17:39:23 +020091 ~TryCritScope();
92#if defined(WEBRTC_WIN)
93 _Check_return_ bool locked() const;
94#else
kjellanderb71b4f02016-01-08 04:51:38 -080095 bool locked() const __attribute__ ((__warn_unused_result__));
Tommi494f2092015-04-27 17:39:23 +020096#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 private:
Peter Boströmaf9e6632016-01-21 16:56:52 +010098 const CriticalSection* const cs_;
Tommi494f2092015-04-27 17:39:23 +020099 const bool locked_;
100 CS_DEBUG_CODE(mutable bool lock_was_called_);
henrikg3c089d72015-09-16 05:37:44 -0700101 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102};
103
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700104// A POD lock used to protect global variables. Do NOT use for other purposes.
105// No custom constructor or private data member should be added.
106class LOCKABLE GlobalLockPod {
107 public:
108 void Lock() EXCLUSIVE_LOCK_FUNCTION();
109
110 void Unlock() UNLOCK_FUNCTION();
111
pbos46ad5422015-12-07 14:29:14 -0800112 volatile int lock_acquired;
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700113};
114
pbos3c12f4d2015-11-17 03:21:02 -0800115class GlobalLock : public GlobalLockPod {
116 public:
117 GlobalLock();
118};
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700119
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200120// GlobalLockScope, for serializing execution through a scope.
121class SCOPED_LOCKABLE GlobalLockScope {
122 public:
123 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock);
124 ~GlobalLockScope() UNLOCK_FUNCTION();
125 private:
126 GlobalLockPod* const lock_;
henrikg3c089d72015-09-16 05:37:44 -0700127 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200128};
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130} // namespace rtc
131
Peter Boströmff019b02015-04-30 14:16:07 +0200132#endif // WEBRTC_BASE_CRITICALSECTION_H_