From dd7a9d54c92fe6556a9e3afa40935e5066a64f11 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 26 Aug 2019 14:51:15 +0500 Subject: [PATCH] ControlManager: Fix worker's completion waiting. --- src/ui/control/controlmanager.cpp | 4 +++- src/ui/control/controlworker.cpp | 12 ++++++------ src/ui/control/controlworker.h | 1 - 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index a51bb4a5..b427029a 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -116,7 +116,8 @@ void ControlManager::processRequest(const QString &scriptPath, void ControlManager::setupWorker() { - m_worker = new ControlWorker(&m_semaphore, &m_sharedMemory); // autoDelete = true + m_worker = new ControlWorker(&m_semaphore, &m_sharedMemory, this); + m_worker->setAutoDelete(false); connect(m_worker, &ControlWorker::requestReady, this, &ControlManager::processRequest); @@ -131,5 +132,6 @@ void ControlManager::abort() m_worker->disconnect(this); m_worker->abort(); + m_worker->deleteLater(); m_worker = nullptr; } diff --git a/src/ui/control/controlworker.cpp b/src/ui/control/controlworker.cpp index fcf2745d..b8b1d951 100644 --- a/src/ui/control/controlworker.cpp +++ b/src/ui/control/controlworker.cpp @@ -9,7 +9,6 @@ ControlWorker::ControlWorker(QSystemSemaphore *semaphore, QObject *parent) : QObject(parent), m_aborted(false), - m_finished(false), m_semaphore(semaphore), m_sharedMemory(sharedMemory) { @@ -22,8 +21,6 @@ void ControlWorker::run() while (!m_aborted && m_semaphore->acquire()) { processRequest(); } - - m_finished = true; } void ControlWorker::abort() @@ -32,9 +29,12 @@ void ControlWorker::abort() m_semaphore->release(); - m_mutex.lock(); - if (!m_finished) { - m_mutex.unlock(); + // Wait thread finishing + { + QMutexLocker locker(&m_mutex); + + m_semaphore = nullptr; + m_sharedMemory = nullptr; } } diff --git a/src/ui/control/controlworker.h b/src/ui/control/controlworker.h index 1d64d7d4..06206ea7 100644 --- a/src/ui/control/controlworker.h +++ b/src/ui/control/controlworker.h @@ -42,7 +42,6 @@ private: private: volatile bool m_aborted; - volatile bool m_finished; QSystemSemaphore *m_semaphore; QSharedMemory *m_sharedMemory;