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
This commit is contained in:
phoglund@webrtc.org
2012-11-30 10:44:49 +00:00
parent 219df91095
commit 99f7c917d2
8 changed files with 110 additions and 117 deletions

View File

@ -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_

View File

@ -72,7 +72,7 @@ ConditionVariablePosix::~ConditionVariablePosix() {
void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect) {
CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>(
&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;
}
}

View File

@ -9,19 +9,20 @@
*/
#if defined(_WIN32)
#include <windows.h>
#include "critical_section_win.h"
#include <windows.h>
#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

View File

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

View File

@ -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 <pthread.h>
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_

View File

@ -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 <windows.h>
@ -16,13 +18,11 @@
#include <time.h>
#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<ProtectedCount*> (obj);
ProtectedCount* the_count = static_cast<ProtectedCount*>(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<ProtectedCount*> (obj);
ProtectedCount* the_count = static_cast<ProtectedCount*>(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);

View File

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

View File

@ -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 <windows.h>
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_