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,11 +14,10 @@
// 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
{
class CriticalSectionWrapper {
public:
// Factory method, constructor disabled
static CriticalSectionWrapper* CreateCriticalSection();
@@ -35,31 +34,28 @@ public:
// RAII extension of the critical section. Prevents Enter/Leave mismatches and
// provides more compact critical section syntax.
class CriticalSectionScoped
{
class CriticalSectionScoped {
public:
explicit CriticalSectionScoped(CriticalSectionWrapper* critsec)
: _ptrCritSec(critsec)
{
_ptrCritSec->Enter();
: ptr_crit_sec_(critsec) {
ptr_crit_sec_->Enter();
}
~CriticalSectionScoped()
{
if (_ptrCritSec)
{
~CriticalSectionScoped() {
if (ptr_crit_sec_) {
Leave();
}
}
private:
void Leave()
{
_ptrCritSec->Leave();
_ptrCritSec = 0;
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_

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

@@ -10,18 +10,19 @@
#if defined(_WIN32)
#include <windows.h>
#include "critical_section_win.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();
#else
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()
{
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);
(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,13 +11,13 @@
#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
{
class CriticalSectionPosix : public CriticalSectionWrapper {
public:
CriticalSectionPosix();
@@ -27,9 +27,10 @@ public:
virtual void Leave();
private:
pthread_mutex_t _mutex;
pthread_mutex_t mutex_;
friend class ConditionVariablePosix;
};
} // namespace webrtc
#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 {
@@ -90,8 +90,8 @@ bool LockUnlockThenStopRunFunction(void* obj) {
}
TEST_F(CritSectTest, ThreadWakesOnce) {
CriticalSectionWrapper* crit_sect
= CriticalSectionWrapper::CreateCriticalSection();
CriticalSectionWrapper* crit_sect =
CriticalSectionWrapper::CreateCriticalSection();
ProtectedCount count(crit_sect);
ThreadWrapper* thread = ThreadWrapper::CreateThread(
&LockUnlockThenStopRunFunction, &count);
@@ -119,8 +119,8 @@ bool LockUnlockRunFunction(void* obj) {
}
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()
{
CriticalSectionWindows::CriticalSectionWindows() {
InitializeCriticalSection(&crit);
}
CriticalSectionWindows::~CriticalSectionWindows()
{
CriticalSectionWindows::~CriticalSectionWindows() {
DeleteCriticalSection(&crit);
}
void
CriticalSectionWindows::Enter()
{
CriticalSectionWindows::Enter() {
EnterCriticalSection(&crit);
}
void
CriticalSectionWindows::Leave()
{
CriticalSectionWindows::Leave() {
LeaveCriticalSection(&crit);
}
} // namespace webrtc

View File

@@ -8,16 +8,16 @@
* 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
{
class CriticalSectionWindows : public CriticalSectionWrapper {
public:
CriticalSectionWindows();
@@ -31,6 +31,7 @@ private:
friend class ConditionVariableWindows;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WINDOWS_H_
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CRITICAL_SECTION_WIN_H_