diff --git a/libs/main/thread/pitimer.cpp b/libs/main/thread/pitimer.cpp index f0de29f4..45aa73bc 100644 --- a/libs/main/thread/pitimer.cpp +++ b/libs/main/thread/pitimer.cpp @@ -19,7 +19,7 @@ #include "pitimer.h" #include "piincludes_p.h" -#include "piwaitevent_p.h" +#include "piconditionvar.h" #ifdef PIP_TIMER_RT # include #endif @@ -188,10 +188,11 @@ private: virtual bool stopTimer(bool wait); static void threadFuncS(void * d) {((_PITimerImp_Thread*)d)->threadFunc();} void adjustTimes(); + bool smallWait(int ms); PIThread thread_; 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_dd = 2000; wait_tick = 1000; - event.create(); //piCout << "_PITimerImp_Thread" << this << ", thread& =" << &thread_; //piCout << "new _PITimerImp_Thread"; } @@ -248,7 +248,6 @@ _PITimerImp_Thread::_PITimerImp_Thread() { _PITimerImp_Thread::~_PITimerImp_Thread() { stop(true); - event.destroy(); } @@ -279,7 +278,9 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { bool _PITimerImp_Thread::stopTimer(bool wait) { thread_.stop(); - event.interrupt(); + thread_.mutex().lock(); + event.notifyAll(); + thread_.mutex().unlock(); if (wait) return thread_.waitForFinish(); // if (wait) // if (!thread_.waitForFinish(10)) @@ -301,12 +302,10 @@ bool _PITimerImp_Thread::threadFunc() { dwt = st_time - PISystemTime::current(true); if (wth > 0) { if (dwt.toMilliseconds() > wth + 1.) { - event.sleep(wth * 1000); - if (thread_.isStopping()) return false; + smallWait(wth); return false; } else { - event.sleep(dwt.toMicroseconds()); - if (thread_.isStopping()) return false; + if (!smallWait(dwt.toMilliseconds())) return false; deferred_ = false; st_time = PISystemTime::current(true); } @@ -325,13 +324,11 @@ bool _PITimerImp_Thread::threadFunc() { } if (wait_tick > 0) { if (st_wait.toMilliseconds() > wait_tick + 1.) { - event.sleep(wait_tick * 1000); - if (thread_.isStopping()) return false; + smallWait(wait_tick); return false; } else { //piCout << &thread_ << "sleep for" << st_wait; - event.sleep(st_wait.toMicroseconds()); - if (thread_.isStopping()) return false; + if (!smallWait(st_wait.toMilliseconds())) return false; } } else { 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 diff --git a/main.cpp b/main.cpp index 56fda705..5ace916b 100644 --- a/main.cpp +++ b/main.cpp @@ -96,7 +96,7 @@ int main(int argc, char * argv[]) { }); timer.start(500); - piSleep(2.12); + piSleep(1.12); piCout << "end"; PITimeMeasurer tm; timer.stop();