pithread, pitimer stop, stopAndWait

This commit is contained in:
Бычков Андрей
2022-11-09 17:17:21 +03:00
parent f9c1ef5ba4
commit d9eac06749
8 changed files with 25 additions and 20 deletions

View File

@@ -588,7 +588,7 @@ void PIScreen::waitForFinish() {
void PIScreen::stop(bool clear) { void PIScreen::stop(bool clear) {
PIThread::stop(true); PIThread::stopAndWait();
if (clear) console.clearScreen(); if (clear) console.clearScreen();
#ifndef WINDOWS #ifndef WINDOWS
fflush(0); fflush(0);

View File

@@ -192,7 +192,7 @@ PIPeer::~PIPeer() {
if (p._data) { if (p._data) {
p._data->dt_in.stop(); p._data->dt_in.stop();
p._data->dt_out.stop(); p._data->dt_out.stop();
p._data->t.stop(true); p._data->t.stopAndWait();
} }
destroyEths(); destroyEths();
piForeach (PIEthernet * i, eths_mcast) { piForeach (PIEthernet * i, eths_mcast) {

View File

@@ -620,10 +620,9 @@ void PIThread::interrupt() {
} }
void PIThread::stop(bool wait) { void PIThread::stop() {
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop ..." << running_ << wait; //PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop ..." << running_ << wait;
terminating = true; terminating = true;
if (wait) waitForFinish();
} }

View File

@@ -107,12 +107,11 @@ public:
bool start(std::function<void()> func, int timer_delay) {ret_func = [func](void*){func();}; return start(timer_delay);} bool start(std::function<void()> func, int timer_delay) {ret_func = [func](void*){func();}; return start(timer_delay);}
EVENT_HANDLER0(bool, startOnce); EVENT_HANDLER0(bool, startOnce);
EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();} EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();}
EVENT_HANDLER0(void, stop) {stop(false);} EVENT_HANDLER0(void, stop);
EVENT_HANDLER1(void, stop, bool, wait);
EVENT_HANDLER0(void, terminate); EVENT_HANDLER0(void, terminate);
//! \~english Stop thread and wait for finish. //! \~english Stop thread and wait for finish.
//! \~russian Останавливает потоков и ожидает завершения. //! \~russian Останавливает поток и ожидает завершения.
void stopAndWait(int timeout_ms = -1); void stopAndWait(int timeout_ms = -1);
void interrupt(); void interrupt();
@@ -201,7 +200,7 @@ public:
//! \~english Start thread without internal loop //! \~english Start thread without internal loop
//! \~russian Запускает поток без внутреннего цикла //! \~russian Запускает поток без внутреннего цикла
//! \fn void stop(bool wait = false) //! \fn void stop()
//! \brief //! \brief
//! \~english Stop thread //! \~english Stop thread
//! \~russian Останавливает поток //! \~russian Останавливает поток
@@ -209,7 +208,7 @@ public:
//! \fn void terminate() //! \fn void terminate()
//! \brief //! \brief
//! \~english Strongly stop thread //! \~english Strongly stop thread
//! \~russian Жестко останавливает поток //! \~russian Жёстко прерывает поток
//! \fn bool waitForStart(int timeout_msecs = -1) //! \fn bool waitForStart(int timeout_msecs = -1)
//! \brief //! \brief

View File

@@ -114,7 +114,7 @@ PIThreadPoolLoop::PIThreadPoolLoop(int thread_cnt) {
PIThreadPoolLoop::~PIThreadPoolLoop() { PIThreadPoolLoop::~PIThreadPoolLoop() {
for (auto * t: threads) { for (auto * t: threads) {
t->stop(false); t->stop();
if (!t->waitForFinish(100)) if (!t->waitForFinish(100))
t->terminate(); t->terminate();
delete t; delete t;
@@ -135,7 +135,7 @@ void PIThreadPoolLoop::start(int index_start, int index_count) {
while (1) { while (1) {
int cc = counter.fetch_add(1); int cc = counter.fetch_add(1);
if (cc >= end) { if (cc >= end) {
t->stop(false); t->stop();
return; return;
} }
func(cc); func(cc);

View File

@@ -274,15 +274,12 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) {
bool _PITimerImp_Thread::stopTimer(bool wait) { bool _PITimerImp_Thread::stopTimer(bool wait) {
#ifndef FREERTOS
thread_.stop(wait);
#else
thread_.stop(); thread_.stop();
if (wait) if (wait) return thread_.waitForFinish();
if (!thread_.waitForFinish(10)) // if (wait)
if (thread_.isRunning()) // if (!thread_.waitForFinish(10))
thread_.terminate(); // if (thread_.isRunning())
#endif // thread_.terminate();
return true; return true;
} }
@@ -739,3 +736,9 @@ bool PITimer::waitForFinish(int timeout_msecs) {
piMinSleep(); piMinSleep();
return tm.elapsed_m() < timeout_msecs; return tm.elapsed_m() < timeout_msecs;
} }
void PITimer::stopAndWait(int timeout_ms) {
init();
imp->stop(false);
waitForFinish(timeout_ms);
}

View File

@@ -164,6 +164,10 @@ public:
EVENT_HANDLER1(bool, stop, bool, wait); EVENT_HANDLER1(bool, stop, bool, wait);
bool waitForFinish() {return waitForFinish(-1);} bool waitForFinish() {return waitForFinish(-1);}
bool waitForFinish(int timeout_msecs); bool waitForFinish(int timeout_msecs);
//! \~english Stop timer and wait for finish.
//! \~russian Останавливает таймер и ожидает завершения.
void stopAndWait(int timeout_ms = -1);
//! \~english Set custom data //! \~english Set custom data
//! \~russian Установить данные, передаваемые в метод таймера //! \~russian Установить данные, передаваемые в метод таймера

View File

@@ -24,7 +24,7 @@ Daemon::Remote::Remote(const PIString & n): PIThread() {
Daemon::Remote::~Remote() { Daemon::Remote::~Remote() {
shellClose(); shellClose();
ft.stop(); ft.stop();
stop(true); stopAndWait();
} }