git-svn-id: svn://db.shs.com.ru/pip@755 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "pitimer.h"
|
||||
#include <stdio.h>
|
||||
#ifdef PIP_TIMER_RT
|
||||
# include <csignal>
|
||||
#endif
|
||||
@@ -98,14 +99,15 @@ void _PITimerBase::startDeferred(double interval_ms, double delay_ms) {
|
||||
|
||||
|
||||
bool _PITimerBase::stop(bool wait) {
|
||||
piCout << "_PITimerBase::stop" << isRunning();
|
||||
if (!isRunning()) return true;
|
||||
piCout << "_PITimerBase::stopTimer";
|
||||
running_ = !stopTimer(wait);
|
||||
return !running_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class _PITimerImp_Thread: public _PITimerBase {
|
||||
public:
|
||||
_PITimerImp_Thread();
|
||||
@@ -145,7 +147,7 @@ private:
|
||||
class _PITimerImp_Pool: public _PITimerImp_Thread {
|
||||
public:
|
||||
_PITimerImp_Pool();
|
||||
virtual ~_PITimerImp_Pool() {stop(true);}
|
||||
virtual ~_PITimerImp_Pool() {}
|
||||
private:
|
||||
class Pool: public PIThread {
|
||||
public:
|
||||
@@ -170,12 +172,14 @@ _PITimerImp_Thread::_PITimerImp_Thread() {
|
||||
wait_dt = 100;
|
||||
wait_dd = 200;
|
||||
wait_tick = 10;
|
||||
//piCout << "new _PITimerImp_Thread";
|
||||
piCout << "new _PITimerImp_Thread" << &thread_ << this;
|
||||
}
|
||||
|
||||
|
||||
_PITimerImp_Thread::~_PITimerImp_Thread() {
|
||||
stop(true);
|
||||
piCout << "~_PITimerImp_Thread ..." << &thread_ << this;
|
||||
thread_.stop(true);
|
||||
piCout << "~_PITimerImp_Thread done" << &thread_ << this;
|
||||
}
|
||||
|
||||
|
||||
@@ -205,6 +209,7 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) {
|
||||
|
||||
|
||||
bool _PITimerImp_Thread::stopTimer(bool wait) {
|
||||
piCout << "stop timer..." << &thread_ << this;
|
||||
#ifndef FREERTOS
|
||||
thread_.stop(true);
|
||||
#else
|
||||
@@ -214,12 +219,19 @@ 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() {
|
||||
if (!running_) return false;
|
||||
//piCout << "threadFunc";
|
||||
//printf("threadFunc\n");
|
||||
if (!running_) {
|
||||
//piCout << "threadFunc 1";
|
||||
//printf("threadFunc 1");
|
||||
return false;
|
||||
}
|
||||
if (deferred_) {
|
||||
PISystemTime dwt;
|
||||
int wth(wait_dt);
|
||||
@@ -230,41 +242,69 @@ bool _PITimerImp_Thread::threadFunc() {
|
||||
dwt = st_time - PISystemTime::current(true);
|
||||
if (wth > 0) {
|
||||
if (dwt.toMilliseconds() > wth + 1.) {
|
||||
piMSleep(wth);
|
||||
//printf("wait 2\n");
|
||||
//piCout << "wait 2" << this << dwt;
|
||||
msleep(wth);
|
||||
//printf("threadFunc 2\n");
|
||||
//piCout << "threadFunc 2";
|
||||
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)
|
||||
if (dwt.toMilliseconds() > 0.1) {
|
||||
//piCout << "threadFunc 3";
|
||||
//printf("threadFunc 3\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
st_wait = st_time - PISystemTime::current(true);
|
||||
//piCout << "wait" << this << st_wait;
|
||||
if (st_wait.abs() > st_odt || st_wait.seconds <= -5) {
|
||||
adjustTimes();
|
||||
//piCout << "threadFunc 4";
|
||||
//printf("threadFunc 4\n");
|
||||
return true;
|
||||
}
|
||||
if (wait_tick > 0) {
|
||||
if (st_wait.toMilliseconds() > wait_tick + 1.) {
|
||||
piMSleep(wait_tick);
|
||||
//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);
|
||||
return false;
|
||||
} else {
|
||||
//piCout << "wait 6" << this << st_wait;
|
||||
//printf("wait 6 %f\n" , st_wait.toMicroseconds());
|
||||
st_wait.sleep();
|
||||
}
|
||||
} else {
|
||||
if (st_wait.toMilliseconds() > 0.1)
|
||||
if (st_wait.toMilliseconds() > 0.1) {
|
||||
//piCout << "threadFunc 6";
|
||||
//printf("threadFunc 6\n");
|
||||
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");
|
||||
tfunc(parent);
|
||||
//piCout << "threadFunc 8";
|
||||
//printf("threadFunc 8\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -522,7 +562,7 @@ void PITimer::init() {
|
||||
void PITimer::destroy() {
|
||||
//piCout << "destroy" << this << imp;
|
||||
if (imp == 0) return;
|
||||
//imp->stop(true); ///BUG: WTF FreeRTOS segfault on this!
|
||||
imp->stop(true); ///BUG: WTF FreeRTOS segfault on this!
|
||||
delete imp;
|
||||
imp = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user