blob: 0b4bb15057682f0fab26857d25ec97c8072e0d61 [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
11#ifndef WEBRTC_BASE_CRITICALSECTION_H__
12#define WEBRTC_BASE_CRITICALSECTION_H__
13
14#include "webrtc/base/constructormagic.h"
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000015#include "webrtc/base/thread_annotations.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17#if defined(WEBRTC_WIN)
tommi@webrtc.org4a4e6882015-03-04 20:09:37 +000018// Include winsock2.h before including <windows.h> to maintain consistency with
19// win32.h. We can't include win32.h directly here since it pulls in
20// headers such as basictypes.h which causes problems in Chromium where webrtc
21// exists as two separate projects, webrtc and libjingle.
22#include <winsock2.h>
23#include <windows.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#endif
25
26#if defined(WEBRTC_POSIX)
27#include <pthread.h>
28#endif
29
Magnus Jedvert6bf10842015-04-23 11:37:55 +020030#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031#define CS_TRACK_OWNER 1
Magnus Jedvert6bf10842015-04-23 11:37:55 +020032#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
34#if CS_TRACK_OWNER
35#define TRACK_OWNER(x) x
36#else // !CS_TRACK_OWNER
37#define TRACK_OWNER(x)
38#endif // !CS_TRACK_OWNER
39
40namespace rtc {
41
42#if defined(WEBRTC_WIN)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000043class LOCKABLE CriticalSection {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 public:
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000045 CriticalSection() { InitializeCriticalSection(&crit_); }
46 ~CriticalSection() { DeleteCriticalSection(&crit_); }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000047 void Enter() EXCLUSIVE_LOCK_FUNCTION() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 EnterCriticalSection(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000050 bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) {
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000051 return TryEnterCriticalSection(&crit_) != FALSE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000053 void Leave() UNLOCK_FUNCTION() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 LeaveCriticalSection(&crit_);
55 }
56
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000057 // Used for debugging.
58 bool CurrentThreadIsOwner() const {
59 return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId());
60 }
Magnus Jedvert6bf10842015-04-23 11:37:55 +020061 // Use only for DCHECKing.
62 bool IsLocked() { return crit_.LockCount != -1; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64 private:
65 CRITICAL_SECTION crit_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066};
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000067#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69#if defined(WEBRTC_POSIX)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000070class LOCKABLE CriticalSection {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 public:
72 CriticalSection() {
73 pthread_mutexattr_t mutex_attribute;
74 pthread_mutexattr_init(&mutex_attribute);
75 pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
76 pthread_mutex_init(&mutex_, &mutex_attribute);
77 pthread_mutexattr_destroy(&mutex_attribute);
78 TRACK_OWNER(thread_ = 0);
79 }
80 ~CriticalSection() {
81 pthread_mutex_destroy(&mutex_);
82 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000083 void Enter() EXCLUSIVE_LOCK_FUNCTION() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 pthread_mutex_lock(&mutex_);
85 TRACK_OWNER(thread_ = pthread_self());
86 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000087 bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 if (pthread_mutex_trylock(&mutex_) == 0) {
89 TRACK_OWNER(thread_ = pthread_self());
90 return true;
91 }
92 return false;
93 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000094 void Leave() UNLOCK_FUNCTION() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 TRACK_OWNER(thread_ = 0);
96 pthread_mutex_unlock(&mutex_);
97 }
98
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000099 // Used for debugging.
100 bool CurrentThreadIsOwner() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101#if CS_TRACK_OWNER
tommi@webrtc.org04cd4662015-01-26 15:27:29 +0000102 return pthread_equal(thread_, pthread_self());
103#else
104 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105#endif // CS_TRACK_OWNER
tommi@webrtc.org04cd4662015-01-26 15:27:29 +0000106 }
Magnus Jedvert6bf10842015-04-23 11:37:55 +0200107 // Use only for DCHECKing.
108#if CS_TRACK_OWNER
109 bool IsLocked() { return thread_ != 0; }
110#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112 private:
tommi@webrtc.orga32f0642015-03-08 07:38:31 +0000113 pthread_mutex_t mutex_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 TRACK_OWNER(pthread_t thread_);
115};
116#endif // WEBRTC_POSIX
117
118// CritScope, for serializing execution through a scope.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000119class SCOPED_LOCKABLE CritScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 public:
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000121 explicit CritScope(CriticalSection *pcrit) EXCLUSIVE_LOCK_FUNCTION(pcrit) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 pcrit_ = pcrit;
123 pcrit_->Enter();
124 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000125 ~CritScope() UNLOCK_FUNCTION() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 pcrit_->Leave();
127 }
128 private:
129 CriticalSection *pcrit_;
130 DISALLOW_COPY_AND_ASSIGN(CritScope);
131};
132
133// Tries to lock a critical section on construction via
134// CriticalSection::TryEnter, and unlocks on destruction if the
135// lock was taken. Never blocks.
136//
137// IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
138// subsequent code. Users *must* check locked() to determine if the
139// lock was taken. If you're not calling locked(), you're doing it wrong!
140class TryCritScope {
141 public:
142 explicit TryCritScope(CriticalSection *pcrit) {
143 pcrit_ = pcrit;
144 locked_ = pcrit_->TryEnter();
145 }
146 ~TryCritScope() {
147 if (locked_) {
148 pcrit_->Leave();
149 }
150 }
151 bool locked() const {
152 return locked_;
153 }
154 private:
155 CriticalSection *pcrit_;
156 bool locked_;
157 DISALLOW_COPY_AND_ASSIGN(TryCritScope);
158};
159
160// TODO: Move this to atomicops.h, which can't be done easily because of
161// complex compile rules.
162class AtomicOps {
163 public:
164#if defined(WEBRTC_WIN)
165 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000166 static int Increment(volatile int* i) {
167 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 }
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000169 static int Decrement(volatile int* i) {
170 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
171 }
172 static int Load(volatile const int* i) {
173 return *i;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 }
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000175 static void Store(volatile int* i, int value) {
176 *i = value;
177 }
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700178 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
179 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),
180 new_value,
181 old_value);
182 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183#else
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000184 static int Increment(volatile int* i) {
tommi@webrtc.org25819b82015-03-17 15:35:11 +0000185 return __sync_add_and_fetch(i, 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186 }
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000187 static int Decrement(volatile int* i) {
tommi@webrtc.org25819b82015-03-17 15:35:11 +0000188 return __sync_sub_and_fetch(i, 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189 }
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000190 static int Load(volatile const int* i) {
191 // Adding 0 is a no-op, so const_cast is fine.
tommi@webrtc.org25819b82015-03-17 15:35:11 +0000192 return __sync_add_and_fetch(const_cast<volatile int*>(i), 0);
magjed@webrtc.orga43fce62015-02-21 13:23:27 +0000193 }
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000194 static void Store(volatile int* i, int value) {
195 __sync_synchronize();
196 *i = value;
197 }
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700198 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
199 return __sync_val_compare_and_swap(i, old_value, new_value);
200 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201#endif
202};
203
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700204
205// A POD lock used to protect global variables. Do NOT use for other purposes.
206// No custom constructor or private data member should be added.
207class LOCKABLE GlobalLockPod {
208 public:
209 void Lock() EXCLUSIVE_LOCK_FUNCTION();
210
211 void Unlock() UNLOCK_FUNCTION();
212
213 volatile int lock_acquired;
214};
215
216class GlobalLock : public GlobalLockPod {
217 public:
218 GlobalLock();
219};
220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221} // namespace rtc
222
223#endif // WEBRTC_BASE_CRITICALSECTION_H__