Reformatted event* classes.
TEST=Ran trybots. Review URL: https://webrtc-codereview.appspot.com/972012 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3253 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
3bb42ef0d6
commit
5bbe069f28
@ -12,57 +12,55 @@
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_
|
||||
|
||||
namespace webrtc {
|
||||
enum EventTypeWrapper
|
||||
{
|
||||
kEventSignaled = 1,
|
||||
kEventError = 2,
|
||||
kEventTimeout = 3
|
||||
enum EventTypeWrapper {
|
||||
kEventSignaled = 1,
|
||||
kEventError = 2,
|
||||
kEventTimeout = 3
|
||||
};
|
||||
|
||||
#define WEBRTC_EVENT_10_SEC 10000
|
||||
#define WEBRTC_EVENT_INFINITE 0xffffffff
|
||||
|
||||
class EventWrapper
|
||||
{
|
||||
public:
|
||||
// Factory method. Constructor disabled.
|
||||
static EventWrapper* Create();
|
||||
virtual ~EventWrapper() {}
|
||||
class EventWrapper {
|
||||
public:
|
||||
// Factory method. Constructor disabled.
|
||||
static EventWrapper* Create();
|
||||
virtual ~EventWrapper() {}
|
||||
|
||||
// Releases threads who are calling Wait() and has started waiting. Please
|
||||
// note that a thread calling Wait() will not start waiting immediately.
|
||||
// assumptions to the contrary is a very common source of issues in
|
||||
// multithreaded programming.
|
||||
// Set is sticky in the sense that it will release at least one thread
|
||||
// either immediately or some time in the future.
|
||||
virtual bool Set() = 0;
|
||||
// Releases threads who are calling Wait() and has started waiting. Please
|
||||
// note that a thread calling Wait() will not start waiting immediately.
|
||||
// assumptions to the contrary is a very common source of issues in
|
||||
// multithreaded programming.
|
||||
// Set is sticky in the sense that it will release at least one thread
|
||||
// either immediately or some time in the future.
|
||||
virtual bool Set() = 0;
|
||||
|
||||
// Prevents future Wait() calls from finishing without a new Set() call.
|
||||
virtual bool Reset() = 0;
|
||||
// Prevents future Wait() calls from finishing without a new Set() call.
|
||||
virtual bool Reset() = 0;
|
||||
|
||||
// Puts the calling thread into a wait state. The thread may be released
|
||||
// by a Set() call depending on if other threads are waiting and if so on
|
||||
// timing. The thread that was released will call Reset() before leaving
|
||||
// preventing more threads from being released. If multiple threads
|
||||
// are waiting for the same Set(), only one (random) thread is guaranteed to
|
||||
// be released. It is possible that multiple (random) threads are released
|
||||
// Depending on timing.
|
||||
virtual EventTypeWrapper Wait(unsigned long maxTime) = 0;
|
||||
// Puts the calling thread into a wait state. The thread may be released
|
||||
// by a Set() call depending on if other threads are waiting and if so on
|
||||
// timing. The thread that was released will call Reset() before leaving
|
||||
// preventing more threads from being released. If multiple threads
|
||||
// are waiting for the same Set(), only one (random) thread is guaranteed to
|
||||
// be released. It is possible that multiple (random) threads are released
|
||||
// Depending on timing.
|
||||
virtual EventTypeWrapper Wait(unsigned long max_time) = 0;
|
||||
|
||||
// Starts a timer that will call a non-sticky version of Set() either once
|
||||
// or periodically. If the timer is periodic it ensures that there is no
|
||||
// drift over time relative to the system clock.
|
||||
virtual bool StartTimer(bool periodic, unsigned long time) = 0;
|
||||
// Starts a timer that will call a non-sticky version of Set() either once
|
||||
// or periodically. If the timer is periodic it ensures that there is no
|
||||
// drift over time relative to the system clock.
|
||||
virtual bool StartTimer(bool periodic, unsigned long time) = 0;
|
||||
|
||||
virtual bool StopTimer() = 0;
|
||||
virtual bool StopTimer() = 0;
|
||||
|
||||
// Only implemented on Windows
|
||||
// Returns 1 if a key has been pressed since last call to this function.
|
||||
// -1 indicates failure
|
||||
// 0 indicates no key has been pressed since last call
|
||||
// TODO(hellner) this function does not seem to belong here
|
||||
static int KeyPressed();
|
||||
// Only implemented on Windows
|
||||
// Returns 1 if a key has been pressed since last call to this function.
|
||||
// -1 indicates failure
|
||||
// 0 indicates no key has been pressed since last call
|
||||
// TODO(hellner) this function does not seem to belong here
|
||||
static int KeyPressed();
|
||||
};
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_
|
||||
|
@ -8,64 +8,55 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "event_wrapper.h"
|
||||
#include "webrtc/system_wrappers/interface/event_wrapper.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#include "event_win.h"
|
||||
#include <windows.h>
|
||||
#include "webrtc/system_wrappers/source/event_win.h"
|
||||
#elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#include <pthread.h>
|
||||
#include "event_posix.h"
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#include <pthread.h>
|
||||
#include "webrtc/system_wrappers/source/event_posix.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include "event_posix.h"
|
||||
#include <pthread.h>
|
||||
#include "webrtc/system_wrappers/source/event_posix.h"
|
||||
#endif
|
||||
|
||||
namespace webrtc {
|
||||
EventWrapper* EventWrapper::Create()
|
||||
{
|
||||
EventWrapper* EventWrapper::Create() {
|
||||
#if defined(_WIN32)
|
||||
return new EventWindows();
|
||||
return new EventWindows();
|
||||
#else
|
||||
return EventPosix::Create();
|
||||
return EventPosix::Create();
|
||||
#endif
|
||||
}
|
||||
|
||||
int EventWrapper::KeyPressed()
|
||||
{
|
||||
int EventWrapper::KeyPressed() {
|
||||
#if defined(_WIN32)
|
||||
int keyDown = 0;
|
||||
for(int key = 0x20; key < 0x90; key++)
|
||||
{
|
||||
short res = GetAsyncKeyState(key);
|
||||
keyDown |= res%2; // Get the LSB
|
||||
}
|
||||
if(keyDown)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int key_down = 0;
|
||||
for (int key = 0x20; key < 0x90; key++) {
|
||||
short res = GetAsyncKeyState(key);
|
||||
key_down |= res % 2; // Get the LSB
|
||||
}
|
||||
if (key_down) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
||||
bool keyDown = false;
|
||||
// loop through all Mac virtual key constant values
|
||||
for(int keyIndex = 0; keyIndex <= 0x5C; keyIndex++)
|
||||
{
|
||||
keyDown |= CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, keyIndex);
|
||||
}
|
||||
if(keyDown)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
bool key_down = false;
|
||||
// loop through all Mac virtual key constant values
|
||||
for (int key_index = 0; key_index <= 0x5C; key_index++) {
|
||||
key_down |= CGEventSourceKeyState(kCGEventSourceStateHIDSystemState,
|
||||
key_index);
|
||||
}
|
||||
if (key_down) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
return -1;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
} // namespace webrtc
|
||||
|
@ -8,7 +8,7 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "event_posix.h"
|
||||
#include "webrtc/system_wrappers/source/event_posix.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
@ -19,306 +19,265 @@
|
||||
#include <unistd.h>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
const long int E6 = 1000000;
|
||||
const long int E9 = 1000 * E6;
|
||||
|
||||
EventWrapper* EventPosix::Create()
|
||||
{
|
||||
EventPosix* ptr = new EventPosix;
|
||||
if (!ptr)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
EventWrapper* EventPosix::Create() {
|
||||
EventPosix* ptr = new EventPosix;
|
||||
if (!ptr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int error = ptr->Construct();
|
||||
if (error)
|
||||
{
|
||||
delete ptr;
|
||||
return NULL;
|
||||
}
|
||||
return ptr;
|
||||
const int error = ptr->Construct();
|
||||
if (error) {
|
||||
delete ptr;
|
||||
return NULL;
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
EventPosix::EventPosix()
|
||||
: _timerThread(0),
|
||||
_timerEvent(0),
|
||||
_periodic(false),
|
||||
_time(0),
|
||||
_count(0),
|
||||
_state(kDown)
|
||||
{
|
||||
: timer_thread_(0),
|
||||
timer_event_(0),
|
||||
periodic_(false),
|
||||
time_(0),
|
||||
count_(0),
|
||||
state_(kDown) {
|
||||
}
|
||||
|
||||
int EventPosix::Construct()
|
||||
{
|
||||
// Set start time to zero
|
||||
memset(&_tCreate, 0, sizeof(_tCreate));
|
||||
int EventPosix::Construct() {
|
||||
// Set start time to zero
|
||||
memset(&created_at_, 0, sizeof(created_at_));
|
||||
|
||||
int result = pthread_mutex_init(&mutex, 0);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int result = pthread_mutex_init(&mutex_, 0);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef WEBRTC_CLOCK_TYPE_REALTIME
|
||||
result = pthread_cond_init(&cond, 0);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
result = pthread_cond_init(&cond_, 0);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
pthread_condattr_t condAttr;
|
||||
result = pthread_condattr_init(&condAttr);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
result = pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
result = pthread_cond_init(&cond, &condAttr);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
result = pthread_condattr_destroy(&condAttr);
|
||||
if (result != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pthread_condattr_t cond_attr;
|
||||
result = pthread_condattr_init(&cond_attr);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
result = pthread_cond_init(&cond_, &cond_attr);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
result = pthread_condattr_destroy(&cond_attr);
|
||||
if (result != 0) {
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
EventPosix::~EventPosix()
|
||||
{
|
||||
StopTimer();
|
||||
pthread_cond_destroy(&cond);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
EventPosix::~EventPosix() {
|
||||
StopTimer();
|
||||
pthread_cond_destroy(&cond_);
|
||||
pthread_mutex_destroy(&mutex_);
|
||||
}
|
||||
|
||||
bool EventPosix::Reset()
|
||||
{
|
||||
if (0 != pthread_mutex_lock(&mutex))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_state = kDown;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventPosix::Set()
|
||||
{
|
||||
if (0 != pthread_mutex_lock(&mutex))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_state = kUp;
|
||||
// Release all waiting threads
|
||||
pthread_cond_broadcast(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return true;
|
||||
}
|
||||
|
||||
EventTypeWrapper EventPosix::Wait(unsigned long timeout)
|
||||
{
|
||||
int retVal = 0;
|
||||
if (0 != pthread_mutex_lock(&mutex))
|
||||
{
|
||||
return kEventError;
|
||||
}
|
||||
|
||||
if (kDown == _state)
|
||||
{
|
||||
if (WEBRTC_EVENT_INFINITE != timeout)
|
||||
{
|
||||
timespec tEnd;
|
||||
#ifndef WEBRTC_MAC
|
||||
#ifdef WEBRTC_CLOCK_TYPE_REALTIME
|
||||
clock_gettime(CLOCK_REALTIME, &tEnd);
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC, &tEnd);
|
||||
#endif
|
||||
#else
|
||||
timeval tVal;
|
||||
struct timezone tZone;
|
||||
tZone.tz_minuteswest = 0;
|
||||
tZone.tz_dsttime = 0;
|
||||
gettimeofday(&tVal,&tZone);
|
||||
TIMEVAL_TO_TIMESPEC(&tVal,&tEnd);
|
||||
#endif
|
||||
tEnd.tv_sec += timeout / 1000;
|
||||
tEnd.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6;
|
||||
|
||||
if (tEnd.tv_nsec >= E9)
|
||||
{
|
||||
tEnd.tv_sec++;
|
||||
tEnd.tv_nsec -= E9;
|
||||
}
|
||||
retVal = pthread_cond_timedwait(&cond, &mutex, &tEnd);
|
||||
} else {
|
||||
retVal = pthread_cond_wait(&cond, &mutex);
|
||||
}
|
||||
}
|
||||
|
||||
_state = kDown;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
switch(retVal)
|
||||
{
|
||||
case 0:
|
||||
return kEventSignaled;
|
||||
case ETIMEDOUT:
|
||||
return kEventTimeout;
|
||||
default:
|
||||
return kEventError;
|
||||
}
|
||||
}
|
||||
|
||||
EventTypeWrapper EventPosix::Wait(timespec& tPulse)
|
||||
{
|
||||
int retVal = 0;
|
||||
if (0 != pthread_mutex_lock(&mutex))
|
||||
{
|
||||
return kEventError;
|
||||
}
|
||||
|
||||
if (kUp != _state)
|
||||
{
|
||||
retVal = pthread_cond_timedwait(&cond, &mutex, &tPulse);
|
||||
}
|
||||
_state = kDown;
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
switch(retVal)
|
||||
{
|
||||
case 0:
|
||||
return kEventSignaled;
|
||||
case ETIMEDOUT:
|
||||
return kEventTimeout;
|
||||
default:
|
||||
return kEventError;
|
||||
}
|
||||
}
|
||||
|
||||
bool EventPosix::StartTimer(bool periodic, unsigned long time)
|
||||
{
|
||||
if (_timerThread)
|
||||
{
|
||||
if(_periodic)
|
||||
{
|
||||
// Timer already started.
|
||||
return false;
|
||||
} else {
|
||||
// New one shot timer
|
||||
_time = time;
|
||||
_tCreate.tv_sec = 0;
|
||||
_timerEvent->Set();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Start the timer thread
|
||||
_timerEvent = static_cast<EventPosix*>(EventWrapper::Create());
|
||||
const char* threadName = "WebRtc_event_timer_thread";
|
||||
_timerThread = ThreadWrapper::CreateThread(Run, this, kRealtimePriority,
|
||||
threadName);
|
||||
_periodic = periodic;
|
||||
_time = time;
|
||||
unsigned int id = 0;
|
||||
if (_timerThread->Start(id))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
bool EventPosix::Reset() {
|
||||
if (0 != pthread_mutex_lock(&mutex_)) {
|
||||
return false;
|
||||
}
|
||||
state_ = kDown;
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventPosix::Run(ThreadObj obj)
|
||||
{
|
||||
return static_cast<EventPosix*>(obj)->Process();
|
||||
bool EventPosix::Set() {
|
||||
if (0 != pthread_mutex_lock(&mutex_)) {
|
||||
return false;
|
||||
}
|
||||
state_ = kUp;
|
||||
// Release all waiting threads
|
||||
pthread_cond_broadcast(&cond_);
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventPosix::Process()
|
||||
{
|
||||
if (_tCreate.tv_sec == 0)
|
||||
{
|
||||
EventTypeWrapper EventPosix::Wait(unsigned long timeout) {
|
||||
int ret_val = 0;
|
||||
if (0 != pthread_mutex_lock(&mutex_)) {
|
||||
return kEventError;
|
||||
}
|
||||
|
||||
if (kDown == state_) {
|
||||
if (WEBRTC_EVENT_INFINITE != timeout) {
|
||||
timespec end_at;
|
||||
#ifndef WEBRTC_MAC
|
||||
#ifdef WEBRTC_CLOCK_TYPE_REALTIME
|
||||
clock_gettime(CLOCK_REALTIME, &_tCreate);
|
||||
clock_gettime(CLOCK_REALTIME, &end_at);
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC, &_tCreate);
|
||||
clock_gettime(CLOCK_MONOTONIC, &end_at);
|
||||
#endif
|
||||
#else
|
||||
timeval tVal;
|
||||
struct timezone tZone;
|
||||
tZone.tz_minuteswest = 0;
|
||||
tZone.tz_dsttime = 0;
|
||||
gettimeofday(&tVal,&tZone);
|
||||
TIMEVAL_TO_TIMESPEC(&tVal,&_tCreate);
|
||||
timeval value;
|
||||
struct timezone time_zone;
|
||||
time_zone.tz_minuteswest = 0;
|
||||
time_zone.tz_dsttime = 0;
|
||||
gettimeofday(&value, &time_zone);
|
||||
TIMEVAL_TO_TIMESPEC(&value, &end_at);
|
||||
#endif
|
||||
_count=0;
|
||||
end_at.tv_sec += timeout / 1000;
|
||||
end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6;
|
||||
|
||||
if (end_at.tv_nsec >= E9) {
|
||||
end_at.tv_sec++;
|
||||
end_at.tv_nsec -= E9;
|
||||
}
|
||||
ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at);
|
||||
} else {
|
||||
ret_val = pthread_cond_wait(&cond_, &mutex_);
|
||||
}
|
||||
}
|
||||
|
||||
timespec tEnd;
|
||||
unsigned long long time = _time * ++_count;
|
||||
tEnd.tv_sec = _tCreate.tv_sec + time/1000;
|
||||
tEnd.tv_nsec = _tCreate.tv_nsec + (time - (time/1000)*1000)*E6;
|
||||
state_ = kDown;
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
|
||||
if ( tEnd.tv_nsec >= E9 )
|
||||
{
|
||||
tEnd.tv_sec++;
|
||||
tEnd.tv_nsec -= E9;
|
||||
switch (ret_val) {
|
||||
case 0:
|
||||
return kEventSignaled;
|
||||
case ETIMEDOUT:
|
||||
return kEventTimeout;
|
||||
default:
|
||||
return kEventError;
|
||||
}
|
||||
}
|
||||
|
||||
EventTypeWrapper EventPosix::Wait(timespec& wake_at) {
|
||||
int ret_val = 0;
|
||||
if (0 != pthread_mutex_lock(&mutex_)) {
|
||||
return kEventError;
|
||||
}
|
||||
|
||||
if (kUp != state_) {
|
||||
ret_val = pthread_cond_timedwait(&cond_, &mutex_, &wake_at);
|
||||
}
|
||||
state_ = kDown;
|
||||
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
|
||||
switch (ret_val) {
|
||||
case 0:
|
||||
return kEventSignaled;
|
||||
case ETIMEDOUT:
|
||||
return kEventTimeout;
|
||||
default:
|
||||
return kEventError;
|
||||
}
|
||||
}
|
||||
|
||||
bool EventPosix::StartTimer(bool periodic, unsigned long time) {
|
||||
if (timer_thread_) {
|
||||
if (periodic_) {
|
||||
// Timer already started.
|
||||
return false;
|
||||
} else {
|
||||
// New one shot timer
|
||||
time_ = time;
|
||||
created_at_.tv_sec = 0;
|
||||
timer_event_->Set();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
switch(_timerEvent->Wait(tEnd))
|
||||
{
|
||||
// Start the timer thread
|
||||
timer_event_ = static_cast<EventPosix*>(EventWrapper::Create());
|
||||
const char* thread_name = "WebRtc_event_timer_thread";
|
||||
timer_thread_ = ThreadWrapper::CreateThread(Run, this, kRealtimePriority,
|
||||
thread_name);
|
||||
periodic_ = periodic;
|
||||
time_ = time;
|
||||
unsigned int id = 0;
|
||||
if (timer_thread_->Start(id)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EventPosix::Run(ThreadObj obj) {
|
||||
return static_cast<EventPosix*>(obj)->Process();
|
||||
}
|
||||
|
||||
bool EventPosix::Process() {
|
||||
if (created_at_.tv_sec == 0) {
|
||||
#ifndef WEBRTC_MAC
|
||||
#ifdef WEBRTC_CLOCK_TYPE_REALTIME
|
||||
clock_gettime(CLOCK_REALTIME, &created_at_);
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC, &created_at_);
|
||||
#endif
|
||||
#else
|
||||
timeval value;
|
||||
struct timezone time_zone;
|
||||
time_zone.tz_minuteswest = 0;
|
||||
time_zone.tz_dsttime = 0;
|
||||
gettimeofday(&value, &time_zone);
|
||||
TIMEVAL_TO_TIMESPEC(&value, &created_at_);
|
||||
#endif
|
||||
count_ = 0;
|
||||
}
|
||||
|
||||
timespec end_at;
|
||||
unsigned long long time = time_ * ++count_;
|
||||
end_at.tv_sec = created_at_.tv_sec + time / 1000;
|
||||
end_at.tv_nsec = created_at_.tv_nsec + (time - (time / 1000) * 1000) * E6;
|
||||
|
||||
if (end_at.tv_nsec >= E9) {
|
||||
end_at.tv_sec++;
|
||||
end_at.tv_nsec -= E9;
|
||||
}
|
||||
|
||||
switch (timer_event_->Wait(end_at)) {
|
||||
case kEventSignaled:
|
||||
return true;
|
||||
return true;
|
||||
case kEventError:
|
||||
return false;
|
||||
return false;
|
||||
case kEventTimeout:
|
||||
break;
|
||||
}
|
||||
if(_periodic || _count==1)
|
||||
{
|
||||
Set();
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
if (periodic_ || count_ == 1) {
|
||||
Set();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventPosix::StopTimer()
|
||||
{
|
||||
if(_timerThread)
|
||||
{
|
||||
_timerThread->SetNotAlive();
|
||||
}
|
||||
if (_timerEvent)
|
||||
{
|
||||
_timerEvent->Set();
|
||||
}
|
||||
if (_timerThread)
|
||||
{
|
||||
if(!_timerThread->Stop())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
delete _timerThread;
|
||||
_timerThread = 0;
|
||||
}
|
||||
if (_timerEvent)
|
||||
{
|
||||
delete _timerEvent;
|
||||
_timerEvent = 0;
|
||||
bool EventPosix::StopTimer() {
|
||||
if (timer_thread_) {
|
||||
timer_thread_->SetNotAlive();
|
||||
}
|
||||
if (timer_event_) {
|
||||
timer_event_->Set();
|
||||
}
|
||||
if (timer_thread_) {
|
||||
if (!timer_thread_->Stop()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set time to zero to force new reference time for the timer.
|
||||
memset(&_tCreate, 0, sizeof(_tCreate));
|
||||
_count=0;
|
||||
return true;
|
||||
delete timer_thread_;
|
||||
timer_thread_ = 0;
|
||||
}
|
||||
if (timer_event_) {
|
||||
delete timer_event_;
|
||||
timer_event_ = 0;
|
||||
}
|
||||
|
||||
// Set time to zero to force new reference time for the timer.
|
||||
memset(&created_at_, 0, sizeof(created_at_));
|
||||
count_ = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -11,56 +11,55 @@
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
|
||||
|
||||
#include "event_wrapper.h"
|
||||
#include "webrtc/system_wrappers/interface/event_wrapper.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "thread_wrapper.h"
|
||||
#include "webrtc/system_wrappers/interface/thread_wrapper.h"
|
||||
|
||||
namespace webrtc {
|
||||
enum State
|
||||
{
|
||||
kUp = 1,
|
||||
kDown = 2
|
||||
|
||||
enum State {
|
||||
kUp = 1,
|
||||
kDown = 2
|
||||
};
|
||||
|
||||
class EventPosix : public EventWrapper
|
||||
{
|
||||
public:
|
||||
static EventWrapper* Create();
|
||||
class EventPosix : public EventWrapper {
|
||||
public:
|
||||
static EventWrapper* Create();
|
||||
|
||||
virtual ~EventPosix();
|
||||
virtual ~EventPosix();
|
||||
|
||||
virtual EventTypeWrapper Wait(unsigned long maxTime);
|
||||
virtual bool Set();
|
||||
virtual bool Reset();
|
||||
virtual EventTypeWrapper Wait(unsigned long max_time);
|
||||
virtual bool Set();
|
||||
virtual bool Reset();
|
||||
|
||||
virtual bool StartTimer(bool periodic, unsigned long time);
|
||||
virtual bool StopTimer();
|
||||
virtual bool StartTimer(bool periodic, unsigned long time);
|
||||
virtual bool StopTimer();
|
||||
|
||||
private:
|
||||
EventPosix();
|
||||
int Construct();
|
||||
private:
|
||||
EventPosix();
|
||||
int Construct();
|
||||
|
||||
static bool Run(ThreadObj obj);
|
||||
bool Process();
|
||||
EventTypeWrapper Wait(timespec& tPulse);
|
||||
static bool Run(ThreadObj obj);
|
||||
bool Process();
|
||||
EventTypeWrapper Wait(timespec& wake_at);
|
||||
|
||||
private:
|
||||
pthread_cond_t cond_;
|
||||
pthread_mutex_t mutex_;
|
||||
|
||||
private:
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
ThreadWrapper* timer_thread_;
|
||||
EventPosix* timer_event_;
|
||||
timespec created_at_;
|
||||
|
||||
ThreadWrapper* _timerThread;
|
||||
EventPosix* _timerEvent;
|
||||
timespec _tCreate;
|
||||
|
||||
bool _periodic;
|
||||
unsigned long _time; // In ms
|
||||
unsigned long _count;
|
||||
State _state;
|
||||
bool periodic_;
|
||||
unsigned long time_; // In ms
|
||||
unsigned long count_;
|
||||
State state_;
|
||||
};
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
|
||||
|
@ -8,77 +8,68 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "event_win.h"
|
||||
#include "webrtc/system_wrappers/source/event_win.h"
|
||||
|
||||
#include "Mmsystem.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
EventWindows::EventWindows()
|
||||
: _event(::CreateEvent(NULL /* security attributes */,
|
||||
FALSE /* manual reset */,
|
||||
FALSE /* initial state */,
|
||||
NULL /* name of event */)),
|
||||
_timerID(NULL)
|
||||
{
|
||||
: event_(::CreateEvent(NULL, // security attributes
|
||||
FALSE, // manual reset
|
||||
FALSE, // initial state
|
||||
NULL)), // name of event
|
||||
timerID_(NULL) {
|
||||
}
|
||||
|
||||
EventWindows::~EventWindows()
|
||||
{
|
||||
CloseHandle(_event);
|
||||
EventWindows::~EventWindows() {
|
||||
CloseHandle(event_);
|
||||
}
|
||||
|
||||
bool EventWindows::Set()
|
||||
{
|
||||
// Note: setting an event that is already set has no effect.
|
||||
return SetEvent(_event) == 1 ? true : false;
|
||||
bool EventWindows::Set() {
|
||||
// Note: setting an event that is already set has no effect.
|
||||
return SetEvent(event_) == 1 ? true : false;
|
||||
}
|
||||
|
||||
bool EventWindows::Reset()
|
||||
{
|
||||
return ResetEvent(_event) == 1 ? true : false;
|
||||
bool EventWindows::Reset() {
|
||||
return ResetEvent(event_) == 1 ? true : false;
|
||||
}
|
||||
|
||||
EventTypeWrapper EventWindows::Wait(unsigned long maxTime)
|
||||
{
|
||||
unsigned long res = WaitForSingleObject(_event, maxTime);
|
||||
switch(res)
|
||||
{
|
||||
EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
|
||||
unsigned long res = WaitForSingleObject(event_, max_time);
|
||||
switch (res) {
|
||||
case WAIT_OBJECT_0:
|
||||
return kEventSignaled;
|
||||
return kEventSignaled;
|
||||
case WAIT_TIMEOUT:
|
||||
return kEventTimeout;
|
||||
return kEventTimeout;
|
||||
default:
|
||||
return kEventError;
|
||||
}
|
||||
return kEventError;
|
||||
}
|
||||
}
|
||||
|
||||
bool EventWindows::StartTimer(bool periodic, unsigned long time)
|
||||
{
|
||||
if (_timerID != NULL)
|
||||
{
|
||||
timeKillEvent(_timerID);
|
||||
_timerID=NULL;
|
||||
}
|
||||
if (periodic)
|
||||
{
|
||||
_timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
|
||||
TIME_PERIODIC|TIME_CALLBACK_EVENT_PULSE);
|
||||
} else {
|
||||
_timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
|
||||
TIME_ONESHOT|TIME_CALLBACK_EVENT_SET);
|
||||
}
|
||||
bool EventWindows::StartTimer(bool periodic, unsigned long time) {
|
||||
if (timerID_ != NULL) {
|
||||
timeKillEvent(timerID_);
|
||||
timerID_ = NULL;
|
||||
}
|
||||
if (periodic) {
|
||||
timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
|
||||
TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
|
||||
} else {
|
||||
timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
|
||||
TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
|
||||
}
|
||||
|
||||
if (_timerID == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
if (timerID_ == NULL) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventWindows::StopTimer()
|
||||
{
|
||||
timeKillEvent(_timerID);
|
||||
_timerID = NULL;
|
||||
return true;
|
||||
bool EventWindows::StopTimer() {
|
||||
timeKillEvent(timerID_);
|
||||
timerID_ = NULL;
|
||||
return true;
|
||||
}
|
||||
} // namespace webrtc
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -8,33 +8,34 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WINDOWS_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WINDOWS_H_
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "event_wrapper.h"
|
||||
#include "webrtc/system_wrappers/interface/event_wrapper.h"
|
||||
|
||||
#include "typedefs.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
namespace webrtc {
|
||||
class EventWindows : public EventWrapper
|
||||
{
|
||||
public:
|
||||
EventWindows();
|
||||
virtual ~EventWindows();
|
||||
|
||||
virtual EventTypeWrapper Wait(unsigned long maxTime);
|
||||
virtual bool Set();
|
||||
virtual bool Reset();
|
||||
class EventWindows : public EventWrapper {
|
||||
public:
|
||||
EventWindows();
|
||||
virtual ~EventWindows();
|
||||
|
||||
virtual bool StartTimer(bool periodic, unsigned long time);
|
||||
virtual bool StopTimer();
|
||||
virtual EventTypeWrapper Wait(unsigned long max_time);
|
||||
virtual bool Set();
|
||||
virtual bool Reset();
|
||||
|
||||
private:
|
||||
HANDLE _event;
|
||||
WebRtc_UWord32 _timerID;
|
||||
virtual bool StartTimer(bool periodic, unsigned long time);
|
||||
virtual bool StopTimer();
|
||||
|
||||
private:
|
||||
HANDLE event_;
|
||||
WebRtc_UWord32 timerID_;
|
||||
};
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WINDOWS_H_
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
|
||||
|
Loading…
x
Reference in New Issue
Block a user