diff --git a/libs/console/piscreen.cpp b/libs/console/piscreen.cpp index abbbf657..fba57853 100644 --- a/libs/console/piscreen.cpp +++ b/libs/console/piscreen.cpp @@ -588,7 +588,7 @@ void PIScreen::waitForFinish() { void PIScreen::stop(bool clear) { - PIThread::stop(true); + PIThread::stopAndWait(); if (clear) console.clearScreen(); #ifndef WINDOWS fflush(0); diff --git a/libs/main/io_devices/pipeer.cpp b/libs/main/io_devices/pipeer.cpp index 34d04004..92177ed5 100644 --- a/libs/main/io_devices/pipeer.cpp +++ b/libs/main/io_devices/pipeer.cpp @@ -192,7 +192,7 @@ PIPeer::~PIPeer() { if (p._data) { p._data->dt_in.stop(); p._data->dt_out.stop(); - p._data->t.stop(true); + p._data->t.stopAndWait(); } destroyEths(); piForeach (PIEthernet * i, eths_mcast) { diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index 7c984c56..0276cd25 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -620,10 +620,9 @@ void PIThread::interrupt() { } -void PIThread::stop(bool wait) { +void PIThread::stop() { //PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop ..." << running_ << wait; terminating = true; - if (wait) waitForFinish(); } diff --git a/libs/main/thread/pithread.h b/libs/main/thread/pithread.h index 914a29f5..948cbefd 100644 --- a/libs/main/thread/pithread.h +++ b/libs/main/thread/pithread.h @@ -107,12 +107,11 @@ public: bool start(std::function func, int timer_delay) {ret_func = [func](void*){func();}; return start(timer_delay);} EVENT_HANDLER0(bool, startOnce); EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();} - EVENT_HANDLER0(void, stop) {stop(false);} - EVENT_HANDLER1(void, stop, bool, wait); + EVENT_HANDLER0(void, stop); EVENT_HANDLER0(void, terminate); //! \~english Stop thread and wait for finish. - //! \~russian Останавливает потоков и ожидает завершения. + //! \~russian Останавливает поток и ожидает завершения. void stopAndWait(int timeout_ms = -1); void interrupt(); @@ -201,7 +200,7 @@ public: //! \~english Start thread without internal loop //! \~russian Запускает поток без внутреннего цикла - //! \fn void stop(bool wait = false) + //! \fn void stop() //! \brief //! \~english Stop thread //! \~russian Останавливает поток @@ -209,7 +208,7 @@ public: //! \fn void terminate() //! \brief //! \~english Strongly stop thread - //! \~russian Жестко останавливает поток + //! \~russian Жёстко прерывает поток //! \fn bool waitForStart(int timeout_msecs = -1) //! \brief diff --git a/libs/main/thread/pithreadpoolloop.cpp b/libs/main/thread/pithreadpoolloop.cpp index 47b9cbb2..18548c98 100644 --- a/libs/main/thread/pithreadpoolloop.cpp +++ b/libs/main/thread/pithreadpoolloop.cpp @@ -114,7 +114,7 @@ PIThreadPoolLoop::PIThreadPoolLoop(int thread_cnt) { PIThreadPoolLoop::~PIThreadPoolLoop() { for (auto * t: threads) { - t->stop(false); + t->stop(); if (!t->waitForFinish(100)) t->terminate(); delete t; @@ -135,7 +135,7 @@ void PIThreadPoolLoop::start(int index_start, int index_count) { while (1) { int cc = counter.fetch_add(1); if (cc >= end) { - t->stop(false); + t->stop(); return; } func(cc); diff --git a/libs/main/thread/pitimer.cpp b/libs/main/thread/pitimer.cpp index f629bb1e..271fcddc 100644 --- a/libs/main/thread/pitimer.cpp +++ b/libs/main/thread/pitimer.cpp @@ -274,15 +274,12 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { bool _PITimerImp_Thread::stopTimer(bool wait) { -#ifndef FREERTOS - thread_.stop(wait); -#else thread_.stop(); - if (wait) - if (!thread_.waitForFinish(10)) - if (thread_.isRunning()) - thread_.terminate(); -#endif + if (wait) return thread_.waitForFinish(); +// if (wait) +// if (!thread_.waitForFinish(10)) +// if (thread_.isRunning()) +// thread_.terminate(); return true; } @@ -739,3 +736,9 @@ bool PITimer::waitForFinish(int 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 6a330005..d3f944c9 100644 --- a/libs/main/thread/pitimer.h +++ b/libs/main/thread/pitimer.h @@ -164,6 +164,10 @@ public: 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 Установить данные, передаваемые в метод таймера diff --git a/utils/system_daemon/daemon.cpp b/utils/system_daemon/daemon.cpp index 8d827d24..cfec7e4a 100644 --- a/utils/system_daemon/daemon.cpp +++ b/utils/system_daemon/daemon.cpp @@ -24,7 +24,7 @@ Daemon::Remote::Remote(const PIString & n): PIThread() { Daemon::Remote::~Remote() { shellClose(); ft.stop(); - stop(true); + stopAndWait(); }