git-svn-id: svn://db.shs.com.ru/pip@756 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2019-02-18 18:30:51 +00:00
parent 0af7eabf56
commit 217947cc89
16 changed files with 296 additions and 217 deletions

View File

@@ -18,7 +18,7 @@
*/
#include "pitimer.h"
#include <stdio.h>
#include "piincludes_p.h"
#ifdef PIP_TIMER_RT
# include <csignal>
#endif
@@ -73,6 +73,7 @@ bool _PITimerBase::start(double interval_ms) {
if (isRunning()) stop(true);
deferred_ = false;
setInterval(interval_ms);
piCout << "_PITimerBase::startTimer"<<interval_ms<<"...";
running_ = startTimer(interval_ms);
return running_;
}
@@ -99,15 +100,16 @@ void _PITimerBase::startDeferred(double interval_ms, double delay_ms) {
bool _PITimerBase::stop(bool wait) {
piCout << "_PITimerBase::stop" << isRunning();
//piCout << GetCurrentThreadId() << "_PITimerBase::stop" << wait << isRunning();
if (!isRunning()) return true;
piCout << "_PITimerBase::stopTimer";
piCout << "_PITimerBase::stopTimer ...";
running_ = !stopTimer(wait);
return !running_;
}
class _PITimerImp_Thread: public _PITimerBase {
public:
_PITimerImp_Thread();
@@ -147,7 +149,7 @@ private:
class _PITimerImp_Pool: public _PITimerImp_Thread {
public:
_PITimerImp_Pool();
virtual ~_PITimerImp_Pool() {}
virtual ~_PITimerImp_Pool() {stop(true);}
private:
class Pool: public PIThread {
public:
@@ -172,14 +174,13 @@ _PITimerImp_Thread::_PITimerImp_Thread() {
wait_dt = 100;
wait_dd = 200;
wait_tick = 10;
piCout << "new _PITimerImp_Thread" << &thread_ << this;
piCout << "_PITimerImp_Thread" << this << ", thread& =" << &thread_;
//piCout << "new _PITimerImp_Thread";
}
_PITimerImp_Thread::~_PITimerImp_Thread() {
piCout << "~_PITimerImp_Thread ..." << &thread_ << this;
thread_.stop(true);
piCout << "~_PITimerImp_Thread done" << &thread_ << this;
stop(true);
}
@@ -209,7 +210,6 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) {
bool _PITimerImp_Thread::stopTimer(bool wait) {
piCout << "stop timer..." << &thread_ << this;
#ifndef FREERTOS
thread_.stop(true);
#else
@@ -219,19 +219,12 @@ bool _PITimerImp_Thread::stopTimer(bool wait) {
if (thread_.isRunning())
thread_.terminate();
#endif
piCout << "stop timer done!" << this << st_wait;
return true;
}
bool _PITimerImp_Thread::threadFunc() {
//piCout << "threadFunc";
//printf("threadFunc\n");
if (!running_) {
//piCout << "threadFunc 1";
//printf("threadFunc 1");
return false;
}
if (!running_) return false;
if (deferred_) {
PISystemTime dwt;
int wth(wait_dt);
@@ -242,69 +235,46 @@ bool _PITimerImp_Thread::threadFunc() {
dwt = st_time - PISystemTime::current(true);
if (wth > 0) {
if (dwt.toMilliseconds() > wth + 1.) {
//printf("wait 2\n");
//piCout << "wait 2" << this << dwt;
msleep(wth);
//printf("threadFunc 2\n");
//piCout << "threadFunc 2";
piMSleep(wth);
return false;
} else {
//piCout << "wait 3" << this << dwt;
//printf("wait 3\n");
dwt.sleep();
deferred_ = false;
st_time = PISystemTime::current(true);
}
} else {
if (dwt.toMilliseconds() > 0.1) {
//piCout << "threadFunc 3";
//printf("threadFunc 3\n");
if (dwt.toMilliseconds() > 0.1)
return false;
}
}
}
st_wait = st_time - PISystemTime::current(true);
//piCout << "wait" << this << st_wait;
if (st_wait.abs() > st_odt || st_wait.seconds <= -5) {
piCout << &thread_ << "adjust" << "...";
adjustTimes();
//piCout << "threadFunc 4";
//printf("threadFunc 4\n");
piCout << &thread_ << "adjust" << "ok";
return true;
}
if (wait_tick > 0) {
if (st_wait.toMilliseconds() > wait_tick + 1.) {
//piCout << "wait 5" << this << wait_tick;
//printf("wait 5 %d\n", wait_tick);
//fflush(stdout);
msleep(wait_tick);
//piCout << "threadFunc 5";
//printf("threadFunc 5\n");
//fflush(stdout);
piMSleep(wait_tick);
return false;
} else {
//piCout << "wait 6" << this << st_wait;
//printf("wait 6 %f\n" , st_wait.toMicroseconds());
piCout << &thread_ << "sleep for" << st_wait;
st_wait.sleep();
}
} else {
if (st_wait.toMilliseconds() > 0.1) {
//piCout << "threadFunc 6";
//printf("threadFunc 6\n");
if (st_wait.toMilliseconds() > 0.1)
return false;
}
}
st_time += st_inc;
if (!parent->isPIObject()) {
piCout << "Achtung! PITimer \"parent\" is not PIObject!";
//piCout << "threadFunc 7";
printf("threadFunc 7\n");
return false;
}
//piCout << "timer tick";
//printf("timer tick\n");
piCout << &thread_ << "tfunc" << "...";
tfunc(parent);
//piCout << "threadFunc 8";
//printf("threadFunc 8\n");
piCout << &thread_ << "tfunc" << "ok";
return true;
}
@@ -529,18 +499,42 @@ PITimer::~PITimer() {
}
double PITimer::interval() const {
init();
return imp->interval_;
}
void PITimer::setInterval(double ms) {
init();
setProperty("interval", ms);
imp->setInterval(ms);
}
bool PITimer::isRunning() const {
init();
return imp->running_;
}
bool PITimer::isStopped() const {
init();
return !imp->running_;
}
void PITimer::initFirst() {
setProperty("interval", 0.);
lockRun = false;
data_t = 0;
ret_func = 0;
imp = 0;
init();
setProperty("interval", 0.);
}
void PITimer::init() {
destroy();
void PITimer::init() const {
if (imp) return;
switch (imp_mode) {
case PITimer::Pool: imp = new _PITimerImp_Pool(); break;
case PITimer::ThreadRT:
@@ -553,16 +547,17 @@ void PITimer::init() {
case PITimer::Thread: imp = new _PITimerImp_Thread(); break;
default: piCout << "Fatal: invalid implementation() of" << this << "!"; assert(0);
}
if (imp == 0) return;
if (!imp) return;
piCout << this << "init" << imp;
imp->tfunc = tickImpS;
imp->parent = this;
imp->parent = const_cast<PITimer*>(this);
}
void PITimer::destroy() {
//piCout << "destroy" << this << imp;
if (imp == 0) return;
imp->stop(true); ///BUG: WTF FreeRTOS segfault on this!
if (!imp) return;
piCout << this << "destroy" << imp;
imp->stop(false); ///BUG: WTF FreeRTOS segfault on this!
delete imp;
imp = 0;
}
@@ -571,15 +566,15 @@ void PITimer::destroy() {
void PITimer::tickImp() {
if (!isRunning()) return;
if (lockRun) lock();
if (ret_func != 0) ret_func(data_t, 1);
if (ret_func) ret_func(data_t, 1);
tick(data_t, 1);
tickEvent(data_t, 1);
maybeCallQueuedEvents();
piForeach (Delimiter & i, delims) {
if (i.delim > ++(i.tick)) continue;
i.tick = 0;
if (i.slot != 0) i.slot(data_t, i.delim);
else if (ret_func != 0) ret_func(data_t, i.delim);
if (i.slot) i.slot(data_t, i.delim);
else if (ret_func) ret_func(data_t, i.delim);
tick(data_t, i.delim);
tickEvent(data_t, i.delim);
}
@@ -587,6 +582,67 @@ void PITimer::tickImp() {
}
bool PITimer::start() {
piCout << this << "start" << imp;
return imp->start();
}
bool PITimer::start(double interval_ms_d) {
init();
piCout << this << "start" << imp << interval_ms_d;
setProperty("interval", interval_ms_d);
return imp->start(interval_ms_d);
}
bool PITimer::start(int interval_ms_i) {
return start((double)interval_ms_i);
}
void PITimer::startDeferred(double delay_ms) {
init();
imp->startDeferred(delay_ms);
}
void PITimer::startDeferred(double interval_ms, double delay_ms) {
init();
imp->startDeferred(interval_ms, delay_ms);
}
void PITimer::startDeferred(PIDateTime start_datetime) {
startDeferred(imp->interval_, start_datetime);
}
void PITimer::startDeferred(double interval_ms, PIDateTime start_datetime) {
init();
imp->startDeferred(interval_ms, start_datetime);
}
bool PITimer::restart() {
init();
imp->stop(true);
return imp->start();
}
bool PITimer::stop() {
return stop(true);
}
bool PITimer::stop(bool wait) {
init();
piCout << this << "stop" << imp << wait;
return imp->stop(wait);
}
bool PITimer::waitForFinish(int timeout_msecs) {
if (timeout_msecs < 0) {
while (isRunning())