From 99f7c917d2933005a7d41f6d85cc9231d0395669 Mon Sep 17 00:00:00 2001 From: "phoglund@webrtc.org" Date: Fri, 30 Nov 2012 10:44:49 +0000 Subject: [PATCH] Reformatted critical_section wrappers. BUG= TEST=ran trybots Review URL: https://webrtc-codereview.appspot.com/971012 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3210 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../interface/critical_section_wrapper.h | 64 +++++++++---------- .../source/condition_variable_posix.cc | 6 +- .../source/critical_section.cc | 15 +++-- .../source/critical_section_posix.cc | 28 ++++---- .../source/critical_section_posix.h | 25 ++++---- .../source/critical_section_unittest.cc | 34 +++++----- .../source/critical_section_win.cc | 24 ++++--- .../source/critical_section_win.h | 31 ++++----- 8 files changed, 110 insertions(+), 117 deletions(-) diff --git a/webrtc/system_wrappers/interface/critical_section_wrapper.h b/webrtc/system_wrappers/interface/critical_section_wrapper.h index a3a1134c83..167951d91f 100644 --- a/webrtc/system_wrappers/interface/critical_section_wrapper.h +++ b/webrtc/system_wrappers/interface/critical_section_wrapper.h @@ -14,52 +14,48 @@ // If the critical section is heavily contended it may be beneficial to use // read/write locks instead. -#include "common_types.h" +#include "webrtc/common_types.h" namespace webrtc { -class CriticalSectionWrapper -{ -public: - // Factory method, constructor disabled - static CriticalSectionWrapper* CreateCriticalSection(); +class CriticalSectionWrapper { + public: + // Factory method, constructor disabled + static CriticalSectionWrapper* CreateCriticalSection(); - virtual ~CriticalSectionWrapper() {} + virtual ~CriticalSectionWrapper() {} - // Tries to grab lock, beginning of a critical section. Will wait for the - // lock to become available if the grab failed. - virtual void Enter() = 0; + // Tries to grab lock, beginning of a critical section. Will wait for the + // lock to become available if the grab failed. + virtual void Enter() = 0; - // Returns a grabbed lock, end of critical section. - virtual void Leave() = 0; + // Returns a grabbed lock, end of critical section. + virtual void Leave() = 0; }; // RAII extension of the critical section. Prevents Enter/Leave mismatches and // provides more compact critical section syntax. -class CriticalSectionScoped -{ -public: - explicit CriticalSectionScoped(CriticalSectionWrapper* critsec) - : _ptrCritSec(critsec) - { - _ptrCritSec->Enter(); - } +class CriticalSectionScoped { + public: + explicit CriticalSectionScoped(CriticalSectionWrapper* critsec) + : ptr_crit_sec_(critsec) { + ptr_crit_sec_->Enter(); + } - ~CriticalSectionScoped() - { - if (_ptrCritSec) - { - Leave(); - } + ~CriticalSectionScoped() { + if (ptr_crit_sec_) { + Leave(); } + } -private: - void Leave() - { - _ptrCritSec->Leave(); - _ptrCritSec = 0; - } + private: + void Leave() { + ptr_crit_sec_->Leave(); + ptr_crit_sec_ = 0; + } - CriticalSectionWrapper* _ptrCritSec; + CriticalSectionWrapper* ptr_crit_sec_; }; + } // namespace webrtc -#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_ + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_ diff --git a/webrtc/system_wrappers/source/condition_variable_posix.cc b/webrtc/system_wrappers/source/condition_variable_posix.cc index 49ff32f271..a5df728ba6 100644 --- a/webrtc/system_wrappers/source/condition_variable_posix.cc +++ b/webrtc/system_wrappers/source/condition_variable_posix.cc @@ -72,7 +72,7 @@ ConditionVariablePosix::~ConditionVariablePosix() { void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect) { CriticalSectionPosix* cs = reinterpret_cast( &crit_sect); - pthread_cond_wait(&cond_, &cs->_mutex); + pthread_cond_wait(&cond_, &cs->mutex_); } bool ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect, @@ -113,10 +113,10 @@ bool ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect, ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; ts.tv_nsec %= NANOSECONDS_PER_SECOND; } - const int res = pthread_cond_timedwait(&cond_, &cs->_mutex, &ts); + const int res = pthread_cond_timedwait(&cond_, &cs->mutex_, &ts); return (res == ETIMEDOUT) ? false : true; } else { - pthread_cond_wait(&cond_, &cs->_mutex); + pthread_cond_wait(&cond_, &cs->mutex_); return true; } } diff --git a/webrtc/system_wrappers/source/critical_section.cc b/webrtc/system_wrappers/source/critical_section.cc index d3f3f01615..1b4511d85b 100644 --- a/webrtc/system_wrappers/source/critical_section.cc +++ b/webrtc/system_wrappers/source/critical_section.cc @@ -9,19 +9,20 @@ */ #if defined(_WIN32) - #include - #include "critical_section_win.h" +#include +#include "webrtc/system_wrappers/source/critical_section_win.h" #else - #include "critical_section_posix.h" +#include "webrtc/system_wrappers/source/critical_section_posix.h" #endif namespace webrtc { -CriticalSectionWrapper* CriticalSectionWrapper::CreateCriticalSection() -{ + +CriticalSectionWrapper* CriticalSectionWrapper::CreateCriticalSection() { #ifdef _WIN32 - return new CriticalSectionWindows(); + return new CriticalSectionWindows(); #else - return new CriticalSectionPosix(); + return new CriticalSectionPosix(); #endif } + } // namespace webrtc diff --git a/webrtc/system_wrappers/source/critical_section_posix.cc b/webrtc/system_wrappers/source/critical_section_posix.cc index 70f85f9a68..ac659400a1 100644 --- a/webrtc/system_wrappers/source/critical_section_posix.cc +++ b/webrtc/system_wrappers/source/critical_section_posix.cc @@ -14,33 +14,29 @@ // no equivalent to DCHECK_EQ in WebRTC code so this is the best we can do here. // TODO(henrike): add logging when pthread synchronization APIs are failing. -#include "critical_section_posix.h" +#include "webrtc/system_wrappers/source/critical_section_posix.h" namespace webrtc { -CriticalSectionPosix::CriticalSectionPosix() -{ - pthread_mutexattr_t attr; - (void) pthread_mutexattr_init(&attr); - (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - (void) pthread_mutex_init(&_mutex, &attr); +CriticalSectionPosix::CriticalSectionPosix() { + pthread_mutexattr_t attr; + (void) pthread_mutexattr_init(&attr); + (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + (void) pthread_mutex_init(&mutex_, &attr); } -CriticalSectionPosix::~CriticalSectionPosix() -{ - (void) pthread_mutex_destroy(&_mutex); +CriticalSectionPosix::~CriticalSectionPosix() { + (void) pthread_mutex_destroy(&mutex_); } void -CriticalSectionPosix::Enter() -{ - (void) pthread_mutex_lock(&_mutex); +CriticalSectionPosix::Enter() { + (void) pthread_mutex_lock(&mutex_); } void -CriticalSectionPosix::Leave() -{ - (void) pthread_mutex_unlock(&_mutex); +CriticalSectionPosix::Leave() { + (void) pthread_mutex_unlock(&mutex_); } } // namespace webrtc diff --git a/webrtc/system_wrappers/source/critical_section_posix.h b/webrtc/system_wrappers/source/critical_section_posix.h index 40b7dc9247..f09aba0782 100644 --- a/webrtc/system_wrappers/source/critical_section_posix.h +++ b/webrtc/system_wrappers/source/critical_section_posix.h @@ -11,25 +11,26 @@ #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_POSIX_H_ #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_POSIX_H_ -#include "critical_section_wrapper.h" +#include "webrtc/system_wrappers/interface/critical_section_wrapper.h" #include namespace webrtc { -class CriticalSectionPosix : public CriticalSectionWrapper -{ -public: - CriticalSectionPosix(); - virtual ~CriticalSectionPosix(); +class CriticalSectionPosix : public CriticalSectionWrapper { + public: + CriticalSectionPosix(); - virtual void Enter(); - virtual void Leave(); + virtual ~CriticalSectionPosix(); -private: - pthread_mutex_t _mutex; - friend class ConditionVariablePosix; + virtual void Enter(); + virtual void Leave(); + + private: + pthread_mutex_t mutex_; + friend class ConditionVariablePosix; }; + } // namespace webrtc -#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_POSIX_H_ +#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_POSIX_H_ diff --git a/webrtc/system_wrappers/source/critical_section_unittest.cc b/webrtc/system_wrappers/source/critical_section_unittest.cc index 23b61a2fc2..f375236913 100644 --- a/webrtc/system_wrappers/source/critical_section_unittest.cc +++ b/webrtc/system_wrappers/source/critical_section_unittest.cc @@ -8,6 +8,8 @@ * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/system_wrappers/interface/critical_section_wrapper.h" + #ifdef _WIN32 // For Sleep() #include @@ -16,13 +18,11 @@ #include #endif -#include "system_wrappers/interface/critical_section_wrapper.h" - #include "gtest/gtest.h" -#include "system_wrappers/interface/sleep.h" -#include "system_wrappers/interface/thread_wrapper.h" -#include "system_wrappers/interface/trace.h" -#include "system_wrappers/source/unittest_utilities.h" +#include "webrtc/system_wrappers/interface/sleep.h" +#include "webrtc/system_wrappers/interface/thread_wrapper.h" +#include "webrtc/system_wrappers/interface/trace.h" +#include "webrtc/system_wrappers/source/unittest_utilities.h" namespace webrtc { @@ -39,7 +39,7 @@ static void SwitchProcess() { } class ProtectedCount { - public: +public: explicit ProtectedCount(CriticalSectionWrapper* crit_sect) : crit_sect_(crit_sect), count_(0) { @@ -55,13 +55,13 @@ class ProtectedCount { return count_; } - private: +private: CriticalSectionWrapper* crit_sect_; int count_; }; class CritSectTest : public ::testing::Test { - public: +public: CritSectTest() : trace_(kLogTrace) { } @@ -72,26 +72,26 @@ class CritSectTest : public ::testing::Test { // On Posix, this SwitchProcess() needs to be in a loop to make the // test both fast and non-flaky. // With 1 us wait as the switch, up to 7 rounds have been observed. - while (count->Count() < target && loop_counter < 100*target) { + while (count->Count() < target && loop_counter < 100 * target) { ++loop_counter; SwitchProcess(); } return (count->Count() >= target); } - private: +private: ScopedTracing trace_; }; bool LockUnlockThenStopRunFunction(void* obj) { - ProtectedCount* the_count = static_cast (obj); + ProtectedCount* the_count = static_cast(obj); the_count->Increment(); return false; } TEST_F(CritSectTest, ThreadWakesOnce) { - CriticalSectionWrapper* crit_sect - = CriticalSectionWrapper::CreateCriticalSection(); + CriticalSectionWrapper* crit_sect = + CriticalSectionWrapper::CreateCriticalSection(); ProtectedCount count(crit_sect); ThreadWrapper* thread = ThreadWrapper::CreateThread( &LockUnlockThenStopRunFunction, &count); @@ -112,15 +112,15 @@ TEST_F(CritSectTest, ThreadWakesOnce) { } bool LockUnlockRunFunction(void* obj) { - ProtectedCount* the_count = static_cast (obj); + ProtectedCount* the_count = static_cast(obj); the_count->Increment(); SwitchProcess(); return true; } TEST_F(CritSectTest, ThreadWakesTwice) { - CriticalSectionWrapper* crit_sect - = CriticalSectionWrapper::CreateCriticalSection(); + CriticalSectionWrapper* crit_sect = + CriticalSectionWrapper::CreateCriticalSection(); ProtectedCount count(crit_sect); ThreadWrapper* thread = ThreadWrapper::CreateThread(&LockUnlockRunFunction, &count); diff --git a/webrtc/system_wrappers/source/critical_section_win.cc b/webrtc/system_wrappers/source/critical_section_win.cc index bbc66e5c7d..7acb007601 100644 --- a/webrtc/system_wrappers/source/critical_section_win.cc +++ b/webrtc/system_wrappers/source/critical_section_win.cc @@ -8,28 +8,26 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "critical_section_win.h" +#include "webrtc/system_wrappers/source/critical_section_win.h" namespace webrtc { -CriticalSectionWindows::CriticalSectionWindows() -{ - InitializeCriticalSection(&crit); + +CriticalSectionWindows::CriticalSectionWindows() { + InitializeCriticalSection(&crit); } -CriticalSectionWindows::~CriticalSectionWindows() -{ - DeleteCriticalSection(&crit); +CriticalSectionWindows::~CriticalSectionWindows() { + DeleteCriticalSection(&crit); } void -CriticalSectionWindows::Enter() -{ - EnterCriticalSection(&crit); +CriticalSectionWindows::Enter() { + EnterCriticalSection(&crit); } void -CriticalSectionWindows::Leave() -{ - LeaveCriticalSection(&crit); +CriticalSectionWindows::Leave() { + LeaveCriticalSection(&crit); } + } // namespace webrtc diff --git a/webrtc/system_wrappers/source/critical_section_win.h b/webrtc/system_wrappers/source/critical_section_win.h index 9556fa9556..01bb52c4a2 100644 --- a/webrtc/system_wrappers/source/critical_section_win.h +++ b/webrtc/system_wrappers/source/critical_section_win.h @@ -8,29 +8,30 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WINDOWS_H_ -#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WINDOWS_H_ +#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WIN_H_ +#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WIN_H_ -#include "typedefs.h" -#include "critical_section_wrapper.h" +#include "webrtc/typedefs.h" +#include "webrtc/system_wrappers/interface/critical_section_wrapper.h" #include namespace webrtc { -class CriticalSectionWindows : public CriticalSectionWrapper -{ -public: - CriticalSectionWindows(); - virtual ~CriticalSectionWindows(); +class CriticalSectionWindows : public CriticalSectionWrapper { + public: + CriticalSectionWindows(); - virtual void Enter(); - virtual void Leave(); + virtual ~CriticalSectionWindows(); -private: - CRITICAL_SECTION crit; + virtual void Enter(); + virtual void Leave(); - friend class ConditionVariableWindows; + private: + CRITICAL_SECTION crit; + + friend class ConditionVariableWindows; }; + } // namespace webrtc -#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WINDOWS_H_ +#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WIN_H_