05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
80
pitimer.cpp
80
pitimer.cpp
@@ -1,10 +1,16 @@
|
||||
#include "pitimer.h"
|
||||
|
||||
|
||||
PITimer::PITimer(TimerEvent slot, void * data_) {
|
||||
PITimer::PITimer(TimerEvent slot, void * data_)
|
||||
#ifdef WINDOWS
|
||||
: PIThread() {
|
||||
#else
|
||||
: PIObject() {
|
||||
#endif
|
||||
ret_func = slot;
|
||||
data = data_;
|
||||
#ifndef WINDOWS
|
||||
piMonitor.timers++;
|
||||
ti = -1;
|
||||
running = false;
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
@@ -14,11 +20,24 @@ PITimer::PITimer(TimerEvent slot, void * data_) {
|
||||
lockRun = false;
|
||||
#endif
|
||||
reset();
|
||||
/*addEvent<void*, int>("timeout");
|
||||
addEventHandler<double>(HANDLER(PITimer, start));
|
||||
addEventHandler(HANDLER(PITimer, stop));
|
||||
addEventHandler(HANDLER(PITimer, reset));*/
|
||||
}
|
||||
|
||||
|
||||
PITimer::~PITimer() {
|
||||
#ifndef WINDOWS
|
||||
piMonitor.timers--;
|
||||
#endif
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
#ifndef WINDOWS
|
||||
void PITimer::start(double msecs) {
|
||||
if (ti == 0) return;
|
||||
spec.it_interval.tv_nsec = ((int)(msecs * 1000) % 1000000) * 1000;
|
||||
spec.it_interval.tv_sec = (time_t)(msecs / 1000);
|
||||
spec.it_value = spec.it_interval;
|
||||
@@ -30,31 +49,50 @@ void PITimer::start(double msecs) {
|
||||
|
||||
void PITimer::timer_event(sigval e) {
|
||||
PITimer * ct = (PITimer * )e.sival_ptr;
|
||||
if (ct->ret_func != 0) {
|
||||
if (ct->lockRun) ct->lock();
|
||||
ct->ret_func(ct->data, 1);
|
||||
piForeach (TimerSlot & i, ct->ret_funcs) {
|
||||
if (i.delim > ++(i.tick)) continue;
|
||||
i.tick = 0;
|
||||
if (i.slot != 0) i.slot(ct->data, i.delim);
|
||||
else ct->ret_func(ct->data, i.delim);
|
||||
}
|
||||
if (ct->lockRun) ct->unlock();
|
||||
if (!ct->running) return;
|
||||
if (ct->lockRun) ct->lock();
|
||||
if (ct->ret_func != 0) ct->ret_func(ct->data, 1);
|
||||
raiseEvent<void*, int>(ct, "timeout", ct->data, 1);
|
||||
piForeach (TimerSlot & i, ct->ret_funcs) {
|
||||
if (i.delim > ++(i.tick)) continue;
|
||||
i.tick = 0;
|
||||
if (i.slot != 0) i.slot(ct->data, i.delim);
|
||||
else if (ct->ret_func != 0) ct->ret_func(ct->data, i.delim);
|
||||
raiseEvent<void*, int>(ct, "timeout", ct->data, i.delim);
|
||||
}
|
||||
if (ct->lockRun) ct->unlock();
|
||||
}
|
||||
|
||||
|
||||
bool PITimer::waitForFinish(int timeout_msecs) {
|
||||
if (timeout_msecs < 0) {
|
||||
while (running)
|
||||
msleep(1);
|
||||
return true;
|
||||
}
|
||||
int cnt = 0;
|
||||
while (running && cnt < timeout_msecs) {
|
||||
msleep(1);
|
||||
++cnt;
|
||||
}
|
||||
return cnt < timeout_msecs;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
void PITimer::run() {
|
||||
if (ret_func != 0) {
|
||||
if (lockRun) lock();
|
||||
ret_func(data, 1);
|
||||
piForeach (TimerSlot & i, ret_funcs) {
|
||||
if (i.delim > ++(i.tick)) continue;
|
||||
i.tick = 0;
|
||||
if (i.slot != 0) i.slot(data, i.delim);
|
||||
else ret_func(data, i.delim);
|
||||
}
|
||||
if (lockRun) unlock();
|
||||
if (!running) return;
|
||||
if (lockRun) lock();
|
||||
if (ret_func != 0) ret_func(data, 1);
|
||||
raiseEvent<void*, int>(this, "timeout", data, 1);
|
||||
piForeach (TimerSlot & i, ret_funcs) {
|
||||
if (i.delim > ++(i.tick)) continue;
|
||||
i.tick = 0;
|
||||
if (i.slot != 0) i.slot(data, i.delim);
|
||||
else if (ret_func != 0) ret_func(data, i.delim);
|
||||
raiseEvent<void*, int>(this, "timeout", data, i.delim);
|
||||
}
|
||||
if (lockRun) unlock();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user