From ca81c65eaeafb7134bd1e8809a247d5116ab070b Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sat, 6 May 2017 23:46:25 +0100 Subject: [PATCH] T66 CContinuousWorker tracks whether it has been started, and when starting checks that it was not already started. --- src/blackmisc/worker.cpp | 8 +++++++- src/blackmisc/worker.h | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/blackmisc/worker.cpp b/src/blackmisc/worker.cpp index 9860d5114..13484882c 100644 --- a/src/blackmisc/worker.cpp +++ b/src/blackmisc/worker.cpp @@ -9,6 +9,7 @@ #include "blackmisc/threadutils.h" #include "blackmisc/worker.h" +#include "blackmisc/verify.h" #include @@ -54,8 +55,9 @@ namespace BlackMisc CWorker *CWorker::fromTaskImpl(QObject *owner, const QString &name, int typeId, std::function task) { - auto *thread = new CRegularThread(owner); auto *worker = new CWorker(task); + worker->setStarted(); + auto *thread = new CRegularThread(owner); if (typeId != QMetaType::Void) { worker->m_result = CVariant(typeId, nullptr); } @@ -115,8 +117,12 @@ namespace BlackMisc void CContinuousWorker::start(QThread::Priority priority) { + BLACK_VERIFY_X(!hasStarted(), Q_FUNC_INFO, "Tried to start a worker that was already started"); + if (hasStarted()) { return; } + if (m_name.isEmpty()) { m_name = metaObject()->className(); } + setStarted(); auto *thread = new CRegularThread(m_owner); Q_ASSERT(m_owner); // must not be null, see (9) https://dev.vatsim-germany.org/issues/402 diff --git a/src/blackmisc/worker.h b/src/blackmisc/worker.h index 8886a785d..9de5d58c3 100644 --- a/src/blackmisc/worker.h +++ b/src/blackmisc/worker.h @@ -166,6 +166,12 @@ namespace BlackMisc //! \threadsafe bool isAbandoned() const; + //! True if the worker has started. + bool hasStarted() const { return m_started; } + + //! Mark the task as started. + void setStarted() { m_started = true; } + //! Mark the task as finished. void setFinished() { @@ -178,6 +184,7 @@ namespace BlackMisc virtual void quit() noexcept {} virtual void quitAndWait() noexcept { waitForFinished(); } + bool m_started = false; bool m_finished = false; mutable QMutex m_finishedMutex { QMutex::Recursive }; }; @@ -285,6 +292,8 @@ namespace BlackMisc void ps_finish(); private: + using CWorkerBase::hasStarted; + using CWorkerBase::setStarted; using CWorkerBase::setFinished; QObject *m_owner = nullptr;