blob: 241d611a074c081eade2f58ab67be74740919b9c [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
Tommi494f2092015-04-27 17:39:23 +020044class LOCKABLE CriticalSection {
45 public:
46 CriticalSection();
47 ~CriticalSection();
48
49 void Enter() EXCLUSIVE_LOCK_FUNCTION();
50 bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true);
51 void Leave() UNLOCK_FUNCTION();
52
53 // Use only for DCHECKing.
54 bool CurrentThreadIsOwner() const;
55 // Use only for DCHECKing.
56 bool IsLocked() const;
57
58 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 CRITICAL_SECTION crit_;
Tommi494f2092015-04-27 17:39:23 +020061#elif defined(WEBRTC_POSIX)
tommi@webrtc.orga32f0642015-03-08 07:38:31 +000062 pthread_mutex_t mutex_;
Tommi494f2092015-04-27 17:39:23 +020063 CS_DEBUG_CODE(pthread_t thread_);
64 CS_DEBUG_CODE(int recursion_count_);
65#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
68// CritScope, for serializing execution through a scope.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000069class SCOPED_LOCKABLE CritScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 public:
Tommi494f2092015-04-27 17:39:23 +020071 explicit CritScope(CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs);
72 ~CritScope() UNLOCK_FUNCTION();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 private:
Tommi494f2092015-04-27 17:39:23 +020074 CriticalSection* const cs_;
henrikg3c089d72015-09-16 05:37:44 -070075 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076};
77
78// Tries to lock a critical section on construction via
79// CriticalSection::TryEnter, and unlocks on destruction if the
80// lock was taken. Never blocks.
81//
82// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
83// subsequent code. Users *must* check locked() to determine if the
84// lock was taken. If you're not calling locked(), you're doing it wrong!
85class TryCritScope {
86 public:
Tommi494f2092015-04-27 17:39:23 +020087 explicit TryCritScope(CriticalSection* cs);
88 ~TryCritScope();
89#if defined(WEBRTC_WIN)
90 _Check_return_ bool locked() const;
91#else
92 bool locked() const __attribute__((warn_unused_result));
93#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 private:
Tommi494f2092015-04-27 17:39:23 +020095 CriticalSection* const cs_;
96 const bool locked_;
97 CS_DEBUG_CODE(mutable bool lock_was_called_);
henrikg3c089d72015-09-16 05:37:44 -070098 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099};
100
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700101// A POD lock used to protect global variables. Do NOT use for other purposes.
102// No custom constructor or private data member should be added.
103class LOCKABLE GlobalLockPod {
104 public:
105 void Lock() EXCLUSIVE_LOCK_FUNCTION();
106
107 void Unlock() UNLOCK_FUNCTION();
108
109 volatile int lock_acquired;
110};
111
112class GlobalLock : public GlobalLockPod {
113 public:
114 GlobalLock();
115};
116
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200117// GlobalLockScope, for serializing execution through a scope.
118class SCOPED_LOCKABLE GlobalLockScope {
119 public:
120 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock);
121 ~GlobalLockScope() UNLOCK_FUNCTION();
122 private:
123 GlobalLockPod* const lock_;
henrikg3c089d72015-09-16 05:37:44 -0700124 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
Joachim Bauchfec2c6d2015-05-27 23:41:43 +0200125};
126
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127} // namespace rtc
128
Peter Boströmff019b02015-04-30 14:16:07 +0200129#endif // WEBRTC_BASE_CRITICALSECTION_H_