PITimer thread imp changed from PIWaitEvent to PIConditionalVariable

This commit is contained in:
2022-11-10 13:47:57 +03:00
parent d34374d4e0
commit 702d1642e0
2 changed files with 19 additions and 14 deletions

View File

@@ -19,7 +19,7 @@
#include "pitimer.h" #include "pitimer.h"
#include "piincludes_p.h" #include "piincludes_p.h"
#include "piwaitevent_p.h" #include "piconditionvar.h"
#ifdef PIP_TIMER_RT #ifdef PIP_TIMER_RT
# include <csignal> # include <csignal>
#endif #endif
@@ -188,10 +188,11 @@ private:
virtual bool stopTimer(bool wait); virtual bool stopTimer(bool wait);
static void threadFuncS(void * d) {((_PITimerImp_Thread*)d)->threadFunc();} static void threadFuncS(void * d) {((_PITimerImp_Thread*)d)->threadFunc();}
void adjustTimes(); void adjustTimes();
bool smallWait(int ms);
PIThread thread_; PIThread thread_;
PISystemTime st_time, st_inc, st_wait, st_odt; PISystemTime st_time, st_inc, st_wait, st_odt;
PIWaitEvent event; PIConditionVariable event;
}; };
@@ -240,7 +241,6 @@ _PITimerImp_Thread::_PITimerImp_Thread() {
wait_dt = 1000; wait_dt = 1000;
wait_dd = 2000; wait_dd = 2000;
wait_tick = 1000; wait_tick = 1000;
event.create();
//piCout << "_PITimerImp_Thread" << this << ", thread& =" << &thread_; //piCout << "_PITimerImp_Thread" << this << ", thread& =" << &thread_;
//piCout << "new _PITimerImp_Thread"; //piCout << "new _PITimerImp_Thread";
} }
@@ -248,7 +248,6 @@ _PITimerImp_Thread::_PITimerImp_Thread() {
_PITimerImp_Thread::~_PITimerImp_Thread() { _PITimerImp_Thread::~_PITimerImp_Thread() {
stop(true); stop(true);
event.destroy();
} }
@@ -279,7 +278,9 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) {
bool _PITimerImp_Thread::stopTimer(bool wait) { bool _PITimerImp_Thread::stopTimer(bool wait) {
thread_.stop(); thread_.stop();
event.interrupt(); thread_.mutex().lock();
event.notifyAll();
thread_.mutex().unlock();
if (wait) return thread_.waitForFinish(); if (wait) return thread_.waitForFinish();
// if (wait) // if (wait)
// if (!thread_.waitForFinish(10)) // if (!thread_.waitForFinish(10))
@@ -301,12 +302,10 @@ bool _PITimerImp_Thread::threadFunc() {
dwt = st_time - PISystemTime::current(true); dwt = st_time - PISystemTime::current(true);
if (wth > 0) { if (wth > 0) {
if (dwt.toMilliseconds() > wth + 1.) { if (dwt.toMilliseconds() > wth + 1.) {
event.sleep(wth * 1000); smallWait(wth);
if (thread_.isStopping()) return false;
return false; return false;
} else { } else {
event.sleep(dwt.toMicroseconds()); if (!smallWait(dwt.toMilliseconds())) return false;
if (thread_.isStopping()) return false;
deferred_ = false; deferred_ = false;
st_time = PISystemTime::current(true); st_time = PISystemTime::current(true);
} }
@@ -325,13 +324,11 @@ bool _PITimerImp_Thread::threadFunc() {
} }
if (wait_tick > 0) { if (wait_tick > 0) {
if (st_wait.toMilliseconds() > wait_tick + 1.) { if (st_wait.toMilliseconds() > wait_tick + 1.) {
event.sleep(wait_tick * 1000); smallWait(wait_tick);
if (thread_.isStopping()) return false;
return false; return false;
} else { } else {
//piCout << &thread_ << "sleep for" << st_wait; //piCout << &thread_ << "sleep for" << st_wait;
event.sleep(st_wait.toMicroseconds()); if (!smallWait(st_wait.toMilliseconds())) return false;
if (thread_.isStopping()) return false;
} }
} else { } else {
if (st_wait.toMilliseconds() > 0.1) if (st_wait.toMilliseconds() > 0.1)
@@ -372,6 +369,14 @@ void _PITimerImp_Thread::adjustTimes() {
} }
bool _PITimerImp_Thread::smallWait(int ms) {
thread_.mutex().lock();
event.waitFor(thread_.mutex(), ms);
thread_.mutex().unlock();
return !thread_.isStopping();
}
#ifdef PIP_TIMER_RT #ifdef PIP_TIMER_RT

View File

@@ -96,7 +96,7 @@ int main(int argc, char * argv[]) {
}); });
timer.start(500); timer.start(500);
piSleep(2.12); piSleep(1.12);
piCout << "end"; piCout << "end";
PITimeMeasurer tm; PITimeMeasurer tm;
timer.stop(); timer.stop();