Lock resources in event_posix.cc.

Fixes errors reported by Helgrind from event_posix.cc when running video_engine_tests.

BUG=
TEST=helgrind,trybots
R=henrike@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/2060004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4572 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
pbos@webrtc.org 2013-08-20 09:49:19 +00:00
parent 62e5af4425
commit e6dc38ea9b

View File

@ -50,7 +50,10 @@ int EventPosix::Construct() {
// Set start time to zero // Set start time to zero
memset(&created_at_, 0, sizeof(created_at_)); memset(&created_at_, 0, sizeof(created_at_));
int result = pthread_mutex_init(&mutex_, 0); pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
int result = pthread_mutex_init(&mutex_, &attr);
if (result != 0) { if (result != 0) {
return -1; return -1;
} }
@ -180,15 +183,18 @@ EventTypeWrapper EventPosix::Wait(timespec& wake_at) {
} }
bool EventPosix::StartTimer(bool periodic, unsigned long time) { bool EventPosix::StartTimer(bool periodic, unsigned long time) {
pthread_mutex_lock(&mutex_);
if (timer_thread_) { if (timer_thread_) {
if (periodic_) { if (periodic_) {
// Timer already started. // Timer already started.
pthread_mutex_unlock(&mutex_);
return false; return false;
} else { } else {
// New one shot timer // New one shot timer
time_ = time; time_ = time;
created_at_.tv_sec = 0; created_at_.tv_sec = 0;
timer_event_->Set(); timer_event_->Set();
pthread_mutex_unlock(&mutex_);
return true; return true;
} }
} }
@ -201,10 +207,10 @@ bool EventPosix::StartTimer(bool periodic, unsigned long time) {
periodic_ = periodic; periodic_ = periodic;
time_ = time; time_ = time;
unsigned int id = 0; unsigned int id = 0;
if (timer_thread_->Start(id)) { bool started = timer_thread_->Start(id);
return true; pthread_mutex_unlock(&mutex_);
}
return false; return started;
} }
bool EventPosix::Run(ThreadObj obj) { bool EventPosix::Run(ThreadObj obj) {
@ -212,6 +218,7 @@ bool EventPosix::Run(ThreadObj obj) {
} }
bool EventPosix::Process() { bool EventPosix::Process() {
pthread_mutex_lock(&mutex_);
if (created_at_.tv_sec == 0) { if (created_at_.tv_sec == 0) {
#ifndef WEBRTC_MAC #ifndef WEBRTC_MAC
#ifdef WEBRTC_CLOCK_TYPE_REALTIME #ifdef WEBRTC_CLOCK_TYPE_REALTIME
@ -240,6 +247,7 @@ bool EventPosix::Process() {
end_at.tv_nsec -= E9; end_at.tv_nsec -= E9;
} }
pthread_mutex_unlock(&mutex_);
switch (timer_event_->Wait(end_at)) { switch (timer_event_->Wait(end_at)) {
case kEventSignaled: case kEventSignaled:
return true; return true;
@ -248,9 +256,12 @@ bool EventPosix::Process() {
case kEventTimeout: case kEventTimeout:
break; break;
} }
if (periodic_ || count_ == 1) {
pthread_mutex_lock(&mutex_);
if (periodic_ || count_ == 1)
Set(); Set();
} pthread_mutex_unlock(&mutex_);
return true; return true;
} }