blob: f4822d96fa779cc8b36116c14b4ed1460450197e [file] [log] [blame]
Jiayang Liubef8d2d2015-03-26 14:38:46 -07001/*
2 * Copyright 2015 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#include "webrtc/base/criticalsection.h"
12
13#include "webrtc/base/checks.h"
Jiayang Liubef8d2d2015-03-26 14:38:46 -070014
15namespace rtc {
16
Tommi494f2092015-04-27 17:39:23 +020017CriticalSection::CriticalSection() {
18#if defined(WEBRTC_WIN)
19 InitializeCriticalSection(&crit_);
20#else
21 pthread_mutexattr_t mutex_attribute;
22 pthread_mutexattr_init(&mutex_attribute);
23 pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
24 pthread_mutex_init(&mutex_, &mutex_attribute);
25 pthread_mutexattr_destroy(&mutex_attribute);
26 CS_DEBUG_CODE(thread_ = 0);
27 CS_DEBUG_CODE(recursion_count_ = 0);
28#endif
29}
30
31CriticalSection::~CriticalSection() {
32#if defined(WEBRTC_WIN)
33 DeleteCriticalSection(&crit_);
34#else
35 pthread_mutex_destroy(&mutex_);
36#endif
37}
38
39void CriticalSection::Enter() EXCLUSIVE_LOCK_FUNCTION() {
40#if defined(WEBRTC_WIN)
41 EnterCriticalSection(&crit_);
42#else
43 pthread_mutex_lock(&mutex_);
44#if CS_DEBUG_CHECKS
45 if (!recursion_count_) {
46 DCHECK(!thread_);
47 thread_ = pthread_self();
48 } else {
49 DCHECK(CurrentThreadIsOwner());
50 }
51 ++recursion_count_;
52#endif
53#endif
54}
55
56bool CriticalSection::TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) {
57#if defined(WEBRTC_WIN)
58 return TryEnterCriticalSection(&crit_) != FALSE;
59#else
60 if (pthread_mutex_trylock(&mutex_) != 0)
61 return false;
62#if CS_DEBUG_CHECKS
63 if (!recursion_count_) {
64 DCHECK(!thread_);
65 thread_ = pthread_self();
66 } else {
67 DCHECK(CurrentThreadIsOwner());
68 }
69 ++recursion_count_;
70#endif
71 return true;
72#endif
73}
74void CriticalSection::Leave() UNLOCK_FUNCTION() {
75 DCHECK(CurrentThreadIsOwner());
76#if defined(WEBRTC_WIN)
77 LeaveCriticalSection(&crit_);
78#else
79#if CS_DEBUG_CHECKS
80 --recursion_count_;
81 DCHECK(recursion_count_ >= 0);
82 if (!recursion_count_)
83 thread_ = 0;
84#endif
85 pthread_mutex_unlock(&mutex_);
86#endif
87}
88
89bool CriticalSection::CurrentThreadIsOwner() const {
90#if defined(WEBRTC_WIN)
91 return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId());
92#else
93#if CS_DEBUG_CHECKS
94 return pthread_equal(thread_, pthread_self());
95#else
96 return true;
97#endif // CS_DEBUG_CHECKS
98#endif
99}
100
101bool CriticalSection::IsLocked() const {
102#if defined(WEBRTC_WIN)
103 return crit_.LockCount != -1;
104#else
105#if CS_DEBUG_CHECKS
106 return thread_ != 0;
107#else
108 return true;
109#endif
110#endif
111}
112
113CritScope::CritScope(CriticalSection* cs) : cs_(cs) { cs_->Enter(); }
114CritScope::~CritScope() { cs_->Leave(); }
115
116TryCritScope::TryCritScope(CriticalSection* cs)
117 : cs_(cs), locked_(cs->TryEnter()) {
118 CS_DEBUG_CODE(lock_was_called_ = false);
119}
120
121TryCritScope::~TryCritScope() {
122 CS_DEBUG_CODE(DCHECK(lock_was_called_));
123 if (locked_)
124 cs_->Leave();
125}
126
127bool TryCritScope::locked() const {
128 CS_DEBUG_CODE(lock_was_called_ = true);
129 return locked_;
130}
131
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700132void GlobalLockPod::Lock() {
Tommi494f2092015-04-27 17:39:23 +0200133#if !defined(WEBRTC_WIN)
134 const struct timespec ts_null = {0};
135#endif
136
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700137 while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) {
Tommi494f2092015-04-27 17:39:23 +0200138#if defined(WEBRTC_WIN)
139 ::Sleep(0);
140#else
141 nanosleep(&ts_null, nullptr);
142#endif
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700143 }
144}
145
146void GlobalLockPod::Unlock() {
147 int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0);
148 DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first";
149}
150
151GlobalLock::GlobalLock() {
152 lock_acquired = 0;
153}
154
155} // namespace rtc