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}
This commit is contained in:
Peter Boström 2016-01-21 16:56:52 +01:00
parent 3c16978c84
commit af9e6637c0
2 changed files with 19 additions and 16 deletions

View File

@ -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);
}

View File

@ -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);