From d46f1a137a2f935703692eb997328d317806f9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Thu, 10 Nov 2022 15:26:19 +0300 Subject: [PATCH] PITimer remove wait in stop, waitForFinish --- libs/cloud/picloudserver.cpp | 6 ++-- libs/main/thread/pitimer.cpp | 62 ++++++++++-------------------------- libs/main/thread/pitimer.h | 11 ++----- 3 files changed, 22 insertions(+), 57 deletions(-) diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 35ac8f49..f5a54766 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -40,7 +40,7 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) delete c; } opened_ = false; - ping_timer.stop(false); + ping_timer.stop(); piMSleep(100); }); CONNECTL(&ping_timer, tickEvent, [this] (void *, int){ @@ -80,7 +80,7 @@ bool PICloudServer::openDevice() { ping_timer.start(5000); return true; } else { - ping_timer.stop(false); + ping_timer.stop(); eth.close(); return false; } @@ -90,7 +90,7 @@ bool PICloudServer::openDevice() { bool PICloudServer::closeDevice() { //piCoutObj << "closeDevice" << this; eth.stopAndWait(); - ping_timer.stop(false); + ping_timer.stop(); eth.close(); for (auto c : clients_) { delete c; diff --git a/libs/main/thread/pitimer.cpp b/libs/main/thread/pitimer.cpp index 71675174..c4cabed7 100644 --- a/libs/main/thread/pitimer.cpp +++ b/libs/main/thread/pitimer.cpp @@ -128,14 +128,14 @@ void _PITimerBase::setInterval(double i) { interval_ = i; if (isRunning()) { //piCout << "change interval runtime"; - stop(true); + stop(); start(); } } bool _PITimerBase::start(double interval_ms) { - if (isRunning()) stop(true); + if (isRunning()) stop(); deferred_ = false; setInterval(interval_ms); //piCout << "_PITimerBase::startTimer"<threadFunc();} void adjustTimes(); bool smallWait(int ms); @@ -216,7 +216,7 @@ private: class _PITimerImp_Pool: public _PITimerImp_Thread { public: _PITimerImp_Pool(); - virtual ~_PITimerImp_Pool() {stop(true);} + virtual ~_PITimerImp_Pool() {stop();} private: class Pool: public PIThread { public: @@ -229,8 +229,8 @@ private: explicit Pool(); virtual ~Pool(); }; - virtual bool startTimer(double interval_ms); - virtual bool stopTimer(bool wait); + bool startTimer(double interval_ms) override; + bool stopTimer() override; }; @@ -246,11 +246,6 @@ _PITimerImp_Thread::_PITimerImp_Thread() { } -_PITimerImp_Thread::~_PITimerImp_Thread() { - stop(true); -} - - void _PITimerImp_Thread::prepareStart(double interval_ms) { if (interval_ms <= 0.) { piCout << "Achtung! Start PITimer with interval <= 0!"; @@ -276,7 +271,7 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { } -bool _PITimerImp_Thread::stopTimer(bool wait) { +bool _PITimerImp_Thread::stopTimer() { thread_.stop(); event.notifyAll(); thread_.waitForFinish(); @@ -521,7 +516,7 @@ bool _PITimerImp_Pool::startTimer(double interval_ms) { } -bool _PITimerImp_Pool::stopTimer(bool wait) { +bool _PITimerImp_Pool::stopTimer() { Pool::instance()->remove(this); return true; } @@ -633,7 +628,7 @@ void PITimer::init() const { void PITimer::destroy() { if (!imp) return; //piCout << this << "destroy" << imp; - imp->stop(false); ///BUG: WTF FreeRTOS segfault on this! + imp->stop(); delete imp; imp = 0; } @@ -723,37 +718,14 @@ void PITimer::startDeferred(double interval_ms, PIDateTime start_datetime) { bool PITimer::restart() { init(); - imp->stop(true); + imp->stop(); return imp->start(); } bool PITimer::stop() { - return stop(true); -} - - -bool PITimer::stop(bool wait) { init(); //piCout << this << "stop" << imp << wait; - return imp->stop(wait); + return imp->stop(); } - -bool PITimer::waitForFinish(int timeout_msecs) { - if (timeout_msecs < 0) { - while (isRunning()) - piMinSleep(); - return true; - } - PITimeMeasurer tm; - while (isRunning() && tm.elapsed_m() < timeout_msecs) - piMinSleep(); - return tm.elapsed_m() < timeout_msecs; -} - -void PITimer::stopAndWait(int timeout_ms) { - init(); - imp->stop(false); - waitForFinish(timeout_ms); -} diff --git a/libs/main/thread/pitimer.h b/libs/main/thread/pitimer.h index d3f944c9..73dd5ac6 100644 --- a/libs/main/thread/pitimer.h +++ b/libs/main/thread/pitimer.h @@ -53,7 +53,7 @@ public: void startDeferred(PIDateTime start_datetime) {startDeferred(interval_, start_datetime);} void startDeferred(double interval_ms, PIDateTime start_datetime); - bool stop(bool wait); + bool stop(); typedef void(*TickFunc)(PITimer*); TickFunc tfunc; @@ -62,7 +62,7 @@ public: protected: virtual bool startTimer(double interval_ms) = 0; - virtual bool stopTimer(bool wait) = 0; + virtual bool stopTimer() = 0; double interval_, deferred_delay; bool deferred_, deferred_mode; // mode: true - date, false - delay @@ -161,14 +161,7 @@ public: void startDeferred(double interval_ms, PIDateTime start_datetime); EVENT_HANDLER0(bool, stop); - EVENT_HANDLER1(bool, stop, bool, wait); - bool waitForFinish() {return waitForFinish(-1);} - bool waitForFinish(int timeout_msecs); - //! \~english Stop timer and wait for finish. - //! \~russian Останавливает таймер и ожидает завершения. - void stopAndWait(int timeout_ms = -1); - //! \~english Set custom data //! \~russian Установить данные, передаваемые в метод таймера void setData(void * data_) {data_t = data_;}