ControlManager: Fix worker's completion waiting.

This commit is contained in:
Nodir Temirkhodjaev 2019-08-26 14:51:15 +05:00
parent 33b642655b
commit dd7a9d54c9
3 changed files with 9 additions and 8 deletions

View File

@ -116,7 +116,8 @@ void ControlManager::processRequest(const QString &scriptPath,
void ControlManager::setupWorker() 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, connect(m_worker, &ControlWorker::requestReady,
this, &ControlManager::processRequest); this, &ControlManager::processRequest);
@ -131,5 +132,6 @@ void ControlManager::abort()
m_worker->disconnect(this); m_worker->disconnect(this);
m_worker->abort(); m_worker->abort();
m_worker->deleteLater();
m_worker = nullptr; m_worker = nullptr;
} }

View File

@ -9,7 +9,6 @@ ControlWorker::ControlWorker(QSystemSemaphore *semaphore,
QObject *parent) : QObject *parent) :
QObject(parent), QObject(parent),
m_aborted(false), m_aborted(false),
m_finished(false),
m_semaphore(semaphore), m_semaphore(semaphore),
m_sharedMemory(sharedMemory) m_sharedMemory(sharedMemory)
{ {
@ -22,8 +21,6 @@ void ControlWorker::run()
while (!m_aborted && m_semaphore->acquire()) { while (!m_aborted && m_semaphore->acquire()) {
processRequest(); processRequest();
} }
m_finished = true;
} }
void ControlWorker::abort() void ControlWorker::abort()
@ -32,9 +29,12 @@ void ControlWorker::abort()
m_semaphore->release(); m_semaphore->release();
m_mutex.lock(); // Wait thread finishing
if (!m_finished) { {
m_mutex.unlock(); QMutexLocker locker(&m_mutex);
m_semaphore = nullptr;
m_sharedMemory = nullptr;
} }
} }

View File

@ -42,7 +42,6 @@ private:
private: private:
volatile bool m_aborted; volatile bool m_aborted;
volatile bool m_finished;
QSystemSemaphore *m_semaphore; QSystemSemaphore *m_semaphore;
QSharedMemory *m_sharedMemory; QSharedMemory *m_sharedMemory;