From 82cda42e75c3228199d95bbfea21ec12bc1d3ae7 Mon Sep 17 00:00:00 2001 From: peri4 Date: Wed, 15 Jan 2025 18:38:50 +0300 Subject: [PATCH] change last logic to PIThreadNotifier --- libs/main/thread/pithread.cpp | 8 ++------ libs/main/thread/pithread.h | 6 +++--- libs/main/thread/pithreadnotifier.cpp | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index eaff0ea2..a7445d35 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -908,9 +908,7 @@ void PIThread::_endThread() { PIScopeExitCall ec([this] { terminating = running_ = false; tid_ = -1; - state_mutex.lock(); - state_var.notifyAll(); - state_mutex.unlock(); + state_notifier.notify(); }); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "..."; stopped(); @@ -1078,9 +1076,7 @@ void PIThread::setThreadName() { bool PIThread::_waitForFinish(PISystemTime max_tm) { if (!running_) return true; - state_mutex.lock(); - state_var.waitFor(state_mutex, max_tm); - state_mutex.unlock(); + state_notifier.waitFor(max_tm); if (!running_) return true; #ifdef WINDOWS if (!isExists(PRIVATE->thread)) { diff --git a/libs/main/thread/pithread.h b/libs/main/thread/pithread.h index e9d1dc4f..0e29754e 100644 --- a/libs/main/thread/pithread.h +++ b/libs/main/thread/pithread.h @@ -26,10 +26,10 @@ #ifndef PITHREAD_H #define PITHREAD_H -#include "piconditionvar.h" #include "piinit.h" #include "pimutex.h" #include "piobject.h" +#include "pithreadnotifier.h" class PIThread; @@ -287,11 +287,11 @@ protected: PISystemTime delay_; llong tid_ = -1; void * data_ = nullptr; - mutable PIMutex thread_mutex, state_mutex; + mutable PIMutex thread_mutex; PITimeMeasurer tmf_, tms_, tmr_; PIThread::Priority priority_ = piNormal; ThreadFunc ret_func = nullptr; - PIConditionVariable state_var; + PIThreadNotifier state_notifier; PRIVATE_DECLARATION(PIP_EXPORT) private: diff --git a/libs/main/thread/pithreadnotifier.cpp b/libs/main/thread/pithreadnotifier.cpp index 46326988..67c33b1d 100644 --- a/libs/main/thread/pithreadnotifier.cpp +++ b/libs/main/thread/pithreadnotifier.cpp @@ -118,7 +118,7 @@ void PIThreadNotifier::wait() { bool PIThreadNotifier::waitFor(PISystemTime timeout) { bool ret = false; m.lock(); - v.waitFor(m, timeout); + if (cnt == 0) v.waitFor(m, timeout); if (cnt > 0) { cnt--; ret = true;