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

This commit is contained in:
2019-02-15 14:45:34 +00:00
parent e1d1853235
commit 0af7eabf56
10 changed files with 140 additions and 63 deletions

View File

@@ -168,6 +168,7 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay)
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = timer_delay;
piCout << "PIThread" << this;
if (startNow) start(timer_delay);
}
@@ -180,6 +181,7 @@ PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = timer_delay;
piCout << "PIThread" << this;
if (startNow) start(timer_delay);
}
@@ -202,6 +204,8 @@ PIThread::~PIThread() {
pthread_cancel(PRIVATE->thread);
# endif
# else
piCout << "terminate by ~PIThread" << this;
while(1) msleep(10);
TerminateThread(PRIVATE->thread, 0);
CloseHandle(PRIVATE->thread);
# endif
@@ -321,6 +325,10 @@ void PIThread::terminate() {
//pthread_join(PRIVATE->thread, &ret);
# endif
#else
piCout << "terminate by terminate";
while (1) {
msleep(10);
}
TerminateThread(PRIVATE->thread, 0);
CloseHandle(PRIVATE->thread);
#endif
@@ -438,12 +446,12 @@ void PIThread::__thread_func__(void * t) {
while (!ct.terminating) {
ct.maybeCallQueuedEvents();
if (ct.lockRun) ct.mutex_.lock();
//piCout << "thread" << ct.name() << "...";
// piCout << "thread" << ct.name() << "..." << ct.lockRun;
ct.run();
//piCout << "thread" << ct.name() << "done";
//printf("thread %p tick\n", &ct);
if (ct.ret_func != 0) ct.ret_func(ct.data_);
if (ct.lockRun) ct.mutex_.unlock();
// piCout << "thread" << ct.name() << "done";
if (ct.delay_ > 0) {
ct.tmr_.reset();
double sl(0.);
@@ -472,6 +480,7 @@ void PIThread::__thread_func__(void * t) {
//piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread;
UNREGISTER_THREAD(&ct);
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
piCout << "pthread_exit" << &ct;
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
(ct.__privateinitializer__.p)->thread = 0;

View File

@@ -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;
}

View File

@@ -35,7 +35,7 @@ class PIP_EXPORT _PITimerBase {
friend class PITimer;
public:
_PITimerBase();
virtual ~_PITimerBase() {stop(true);}
virtual ~_PITimerBase() {}
double interval() const {return interval_;}
void setInterval(double i);