| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #endif | 36 #endif |
| 37 } | 37 } |
| 38 | 38 |
| 39 void CriticalSection::Enter() EXCLUSIVE_LOCK_FUNCTION() { | 39 void CriticalSection::Enter() EXCLUSIVE_LOCK_FUNCTION() { |
| 40 #if defined(WEBRTC_WIN) | 40 #if defined(WEBRTC_WIN) |
| 41 EnterCriticalSection(&crit_); | 41 EnterCriticalSection(&crit_); |
| 42 #else | 42 #else |
| 43 pthread_mutex_lock(&mutex_); | 43 pthread_mutex_lock(&mutex_); |
| 44 #if CS_DEBUG_CHECKS | 44 #if CS_DEBUG_CHECKS |
| 45 if (!recursion_count_) { | 45 if (!recursion_count_) { |
| 46 DCHECK(!thread_); | 46 RTC_DCHECK(!thread_); |
| 47 thread_ = pthread_self(); | 47 thread_ = pthread_self(); |
| 48 } else { | 48 } else { |
| 49 DCHECK(CurrentThreadIsOwner()); | 49 RTC_DCHECK(CurrentThreadIsOwner()); |
| 50 } | 50 } |
| 51 ++recursion_count_; | 51 ++recursion_count_; |
| 52 #endif | 52 #endif |
| 53 #endif | 53 #endif |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool CriticalSection::TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { | 56 bool CriticalSection::TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 57 #if defined(WEBRTC_WIN) | 57 #if defined(WEBRTC_WIN) |
| 58 return TryEnterCriticalSection(&crit_) != FALSE; | 58 return TryEnterCriticalSection(&crit_) != FALSE; |
| 59 #else | 59 #else |
| 60 if (pthread_mutex_trylock(&mutex_) != 0) | 60 if (pthread_mutex_trylock(&mutex_) != 0) |
| 61 return false; | 61 return false; |
| 62 #if CS_DEBUG_CHECKS | 62 #if CS_DEBUG_CHECKS |
| 63 if (!recursion_count_) { | 63 if (!recursion_count_) { |
| 64 DCHECK(!thread_); | 64 RTC_DCHECK(!thread_); |
| 65 thread_ = pthread_self(); | 65 thread_ = pthread_self(); |
| 66 } else { | 66 } else { |
| 67 DCHECK(CurrentThreadIsOwner()); | 67 RTC_DCHECK(CurrentThreadIsOwner()); |
| 68 } | 68 } |
| 69 ++recursion_count_; | 69 ++recursion_count_; |
| 70 #endif | 70 #endif |
| 71 return true; | 71 return true; |
| 72 #endif | 72 #endif |
| 73 } | 73 } |
| 74 void CriticalSection::Leave() UNLOCK_FUNCTION() { | 74 void CriticalSection::Leave() UNLOCK_FUNCTION() { |
| 75 DCHECK(CurrentThreadIsOwner()); | 75 RTC_DCHECK(CurrentThreadIsOwner()); |
| 76 #if defined(WEBRTC_WIN) | 76 #if defined(WEBRTC_WIN) |
| 77 LeaveCriticalSection(&crit_); | 77 LeaveCriticalSection(&crit_); |
| 78 #else | 78 #else |
| 79 #if CS_DEBUG_CHECKS | 79 #if CS_DEBUG_CHECKS |
| 80 --recursion_count_; | 80 --recursion_count_; |
| 81 DCHECK(recursion_count_ >= 0); | 81 RTC_DCHECK(recursion_count_ >= 0); |
| 82 if (!recursion_count_) | 82 if (!recursion_count_) |
| 83 thread_ = 0; | 83 thread_ = 0; |
| 84 #endif | 84 #endif |
| 85 pthread_mutex_unlock(&mutex_); | 85 pthread_mutex_unlock(&mutex_); |
| 86 #endif | 86 #endif |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool CriticalSection::CurrentThreadIsOwner() const { | 89 bool CriticalSection::CurrentThreadIsOwner() const { |
| 90 #if defined(WEBRTC_WIN) | 90 #if defined(WEBRTC_WIN) |
| 91 return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId()); | 91 return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 112 | 112 |
| 113 CritScope::CritScope(CriticalSection* cs) : cs_(cs) { cs_->Enter(); } | 113 CritScope::CritScope(CriticalSection* cs) : cs_(cs) { cs_->Enter(); } |
| 114 CritScope::~CritScope() { cs_->Leave(); } | 114 CritScope::~CritScope() { cs_->Leave(); } |
| 115 | 115 |
| 116 TryCritScope::TryCritScope(CriticalSection* cs) | 116 TryCritScope::TryCritScope(CriticalSection* cs) |
| 117 : cs_(cs), locked_(cs->TryEnter()) { | 117 : cs_(cs), locked_(cs->TryEnter()) { |
| 118 CS_DEBUG_CODE(lock_was_called_ = false); | 118 CS_DEBUG_CODE(lock_was_called_ = false); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TryCritScope::~TryCritScope() { | 121 TryCritScope::~TryCritScope() { |
| 122 CS_DEBUG_CODE(DCHECK(lock_was_called_)); | 122 CS_DEBUG_CODE(RTC_DCHECK(lock_was_called_)); |
| 123 if (locked_) | 123 if (locked_) |
| 124 cs_->Leave(); | 124 cs_->Leave(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool TryCritScope::locked() const { | 127 bool TryCritScope::locked() const { |
| 128 CS_DEBUG_CODE(lock_was_called_ = true); | 128 CS_DEBUG_CODE(lock_was_called_ = true); |
| 129 return locked_; | 129 return locked_; |
| 130 } | 130 } |
| 131 | 131 |
| 132 void GlobalLockPod::Lock() { | 132 void GlobalLockPod::Lock() { |
| 133 #if !defined(WEBRTC_WIN) | 133 #if !defined(WEBRTC_WIN) |
| 134 const struct timespec ts_null = {0}; | 134 const struct timespec ts_null = {0}; |
| 135 #endif | 135 #endif |
| 136 | 136 |
| 137 while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) { | 137 while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) { |
| 138 #if defined(WEBRTC_WIN) | 138 #if defined(WEBRTC_WIN) |
| 139 ::Sleep(0); | 139 ::Sleep(0); |
| 140 #else | 140 #else |
| 141 nanosleep(&ts_null, nullptr); | 141 nanosleep(&ts_null, nullptr); |
| 142 #endif | 142 #endif |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void GlobalLockPod::Unlock() { | 146 void GlobalLockPod::Unlock() { |
| 147 int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0); | 147 int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0); |
| 148 DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first"; | 148 RTC_DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first"; |
| 149 } | 149 } |
| 150 | 150 |
| 151 GlobalLock::GlobalLock() { | 151 GlobalLock::GlobalLock() { |
| 152 lock_acquired = 0; | 152 lock_acquired = 0; |
| 153 } | 153 } |
| 154 | 154 |
| 155 GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) | 155 GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) |
| 156 : lock_(lock) { | 156 : lock_(lock) { |
| 157 lock_->Lock(); | 157 lock_->Lock(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 GlobalLockScope::~GlobalLockScope() { | 160 GlobalLockScope::~GlobalLockScope() { |
| 161 lock_->Unlock(); | 161 lock_->Unlock(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace rtc | 164 } // namespace rtc |
| OLD | NEW |