From af9e6637c0fd154a6469a07913e233aad2b4d8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= Date: Thu, 21 Jan 2016 16:56:52 +0100 Subject: [PATCH] Make rtc::CriticalSection lockable from f() const. Removes the use of mutable rtc::CriticalSection across the code. BUG= R=tommi@webrtc.org Review URL: https://codereview.webrtc.org/1611223002 . Cr-Commit-Position: refs/heads/master@{#11342} --- webrtc/base/criticalsection.cc | 10 +++++----- webrtc/base/criticalsection.h | 25 ++++++++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/webrtc/base/criticalsection.cc b/webrtc/base/criticalsection.cc index 1f50c2355d..2c6b100a7d 100644 --- a/webrtc/base/criticalsection.cc +++ b/webrtc/base/criticalsection.cc @@ -36,7 +36,7 @@ CriticalSection::~CriticalSection() { #endif } -void CriticalSection::Enter() EXCLUSIVE_LOCK_FUNCTION() { +void CriticalSection::Enter() const EXCLUSIVE_LOCK_FUNCTION() { #if defined(WEBRTC_WIN) EnterCriticalSection(&crit_); #else @@ -53,7 +53,7 @@ void CriticalSection::Enter() EXCLUSIVE_LOCK_FUNCTION() { #endif } -bool CriticalSection::TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { +bool CriticalSection::TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true) { #if defined(WEBRTC_WIN) return TryEnterCriticalSection(&crit_) != FALSE; #else @@ -71,7 +71,7 @@ bool CriticalSection::TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { return true; #endif } -void CriticalSection::Leave() UNLOCK_FUNCTION() { +void CriticalSection::Leave() const UNLOCK_FUNCTION() { RTC_DCHECK(CurrentThreadIsOwner()); #if defined(WEBRTC_WIN) LeaveCriticalSection(&crit_); @@ -115,10 +115,10 @@ bool CriticalSection::IsLocked() const { #endif } -CritScope::CritScope(CriticalSection* cs) : cs_(cs) { cs_->Enter(); } +CritScope::CritScope(const CriticalSection* cs) : cs_(cs) { cs_->Enter(); } CritScope::~CritScope() { cs_->Leave(); } -TryCritScope::TryCritScope(CriticalSection* cs) +TryCritScope::TryCritScope(const CriticalSection* cs) : cs_(cs), locked_(cs->TryEnter()) { CS_DEBUG_CODE(lock_was_called_ = false); } diff --git a/webrtc/base/criticalsection.h b/webrtc/base/criticalsection.h index 5b3eaf5684..cc54eca1f0 100644 --- a/webrtc/base/criticalsection.h +++ b/webrtc/base/criticalsection.h @@ -41,14 +41,17 @@ namespace rtc { +// Locking methods (Enter, TryEnter, Leave)are const to permit protecting +// members inside a const context without requiring mutable CriticalSections +// everywhere. class LOCKABLE CriticalSection { public: CriticalSection(); ~CriticalSection(); - void Enter() EXCLUSIVE_LOCK_FUNCTION(); - bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true); - void Leave() UNLOCK_FUNCTION(); + void Enter() const EXCLUSIVE_LOCK_FUNCTION(); + bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); + void Leave() const UNLOCK_FUNCTION(); // Use only for RTC_DCHECKing. bool CurrentThreadIsOwner() const; @@ -57,21 +60,21 @@ class LOCKABLE CriticalSection { private: #if defined(WEBRTC_WIN) - CRITICAL_SECTION crit_; + mutable CRITICAL_SECTION crit_; #elif defined(WEBRTC_POSIX) - pthread_mutex_t mutex_; - CS_DEBUG_CODE(pthread_t thread_); - CS_DEBUG_CODE(int recursion_count_); + mutable pthread_mutex_t mutex_; + CS_DEBUG_CODE(mutable pthread_t thread_); + CS_DEBUG_CODE(mutable int recursion_count_); #endif }; // CritScope, for serializing execution through a scope. class SCOPED_LOCKABLE CritScope { public: - explicit CritScope(CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); + explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); ~CritScope() UNLOCK_FUNCTION(); private: - CriticalSection* const cs_; + const CriticalSection* const cs_; RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); }; @@ -84,7 +87,7 @@ class SCOPED_LOCKABLE CritScope { // lock was taken. If you're not calling locked(), you're doing it wrong! class TryCritScope { public: - explicit TryCritScope(CriticalSection* cs); + explicit TryCritScope(const CriticalSection* cs); ~TryCritScope(); #if defined(WEBRTC_WIN) _Check_return_ bool locked() const; @@ -92,7 +95,7 @@ class TryCritScope { bool locked() const __attribute__ ((__warn_unused_result__)); #endif private: - CriticalSection* const cs_; + const CriticalSection* const cs_; const bool locked_; CS_DEBUG_CODE(mutable bool lock_was_called_); RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);