CriticalSection: Use types+methods from base/platform_thread*.*.

Use PlatformThreadRef, CurrentThreadRef and IsThreadRefEqual instead of pthread_t, pthread_self and operator== (or !=).

BUG=

Review URL: https://codereview.webrtc.org/1619153003

Cr-Commit-Position: refs/heads/master@{#11355}
This commit is contained in:
tommi
2016-01-22 05:13:33 -08:00
committed by Commit bot
parent 32e590ec13
commit 7406b96abc
2 changed files with 13 additions and 11 deletions

View File

@ -11,6 +11,7 @@
#include "webrtc/base/criticalsection.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/platform_thread.h"
// TODO(tommi): Split this file up to per-platform implementation files.
@ -55,13 +56,13 @@ void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() {
#else
#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
int spin = 3000;
pthread_t self = pthread_self();
PlatformThreadRef self = CurrentThreadRef();
bool have_lock = false;
do {
// Instead of calling TryEnter() in this loop, we do two interlocked
// operations, first a read-only one in order to avoid affecting the lock
// cache-line while spinning, in case another thread is using the lock.
if (owning_thread_ != self) {
if (!IsThreadRefEqual(owning_thread_, self)) {
if (AtomicOps::AcquireLoad(&lock_queue_) == 0) {
if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) == 0) {
have_lock = true;
@ -80,7 +81,7 @@ void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() {
if (!have_lock && AtomicOps::Increment(&lock_queue_) > 1) {
// Owning thread cannot be the current thread since TryEnter() would
// have succeeded.
RTC_DCHECK(owning_thread_ != self);
RTC_DCHECK(!IsThreadRefEqual(owning_thread_, self));
// Wait for the lock to become available.
dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER);
RTC_DCHECK(owning_thread_ == 0);
@ -97,7 +98,7 @@ void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() {
#if CS_DEBUG_CHECKS
if (!recursion_count_) {
RTC_DCHECK(!thread_);
thread_ = pthread_self();
thread_ = CurrentThreadRef();
} else {
RTC_DCHECK(CurrentThreadIsOwner());
}
@ -111,10 +112,10 @@ bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) {
return TryEnterCriticalSection(&crit_) != FALSE;
#else
#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
if (owning_thread_ != pthread_self()) {
if (!IsThreadRefEqual(owning_thread_, CurrentThreadRef())) {
if (AtomicOps::CompareAndSwap(&lock_queue_, 0, 1) != 0)
return false;
owning_thread_ = pthread_self();
owning_thread_ = CurrentThreadRef();
RTC_DCHECK(!recursion_);
} else {
AtomicOps::Increment(&lock_queue_);
@ -127,7 +128,7 @@ bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) {
#if CS_DEBUG_CHECKS
if (!recursion_count_) {
RTC_DCHECK(!thread_);
thread_ = pthread_self();
thread_ = CurrentThreadRef();
} else {
RTC_DCHECK(CurrentThreadIsOwner());
}
@ -148,7 +149,7 @@ void CriticalSection::Leave() const UNLOCK_FUNCTION() {
thread_ = 0;
#endif
#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
RTC_DCHECK_EQ(owning_thread_, pthread_self());
RTC_DCHECK(IsThreadRefEqual(owning_thread_, CurrentThreadRef()));
RTC_DCHECK_GE(recursion_, 0);
--recursion_;
if (!recursion_)
@ -172,7 +173,7 @@ bool CriticalSection::CurrentThreadIsOwner() const {
reinterpret_cast<HANDLE>(static_cast<size_t>(GetCurrentThreadId()));
#else
#if CS_DEBUG_CHECKS
return pthread_equal(thread_, pthread_self());
return IsThreadRefEqual(thread_, CurrentThreadRef());
#else
return true;
#endif // CS_DEBUG_CHECKS

View File

@ -14,6 +14,7 @@
#include "webrtc/base/atomicops.h"
#include "webrtc/base/constructormagic.h"
#include "webrtc/base/thread_annotations.h"
#include "webrtc/base/platform_thread_types.h"
#if defined(WEBRTC_WIN)
// Include winsock2.h before including <windows.h> to maintain consistency with
@ -80,11 +81,11 @@ class LOCKABLE CriticalSection {
// Used to signal a single waiting thread when the lock becomes available.
mutable dispatch_semaphore_t semaphore_;
// The thread that currently holds the lock. Required to handle recursion.
mutable pthread_t owning_thread_;
mutable PlatformThreadRef owning_thread_;
#else
mutable pthread_mutex_t mutex_;
#endif
CS_DEBUG_CODE(mutable pthread_t thread_);
CS_DEBUG_CODE(mutable PlatformThreadRef thread_);
CS_DEBUG_CODE(mutable int recursion_count_);
#endif
};