From 57eaa989f549bdf6ed28614121186120b8611da3 Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 12 Mar 2023 23:31:19 +0000 Subject: [PATCH] Manage `InsightWorkerTask` pointers via `QSharedPointer`, in preparation for task indicator widget and task window. --- src/Insight/InsightSignals.hpp | 7 +++++-- src/Insight/InsightWorker/InsightWorker.cpp | 19 +++++++++---------- src/Insight/InsightWorker/InsightWorker.hpp | 6 ++---- .../InsightWorker/Tasks/InsightWorkerTask.cpp | 4 ++++ .../InsightWorker/Tasks/InsightWorkerTask.hpp | 9 ++++++++- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Insight/InsightSignals.hpp b/src/Insight/InsightSignals.hpp index 475c23ae..ada63890 100644 --- a/src/Insight/InsightSignals.hpp +++ b/src/Insight/InsightSignals.hpp @@ -2,11 +2,14 @@ #include #include +#include #include "src/Targets/TargetState.hpp" #include "src/Targets/TargetDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" +#include "InsightWorker/Tasks/InsightWorkerTask.hpp" + namespace Bloom { /** @@ -27,8 +30,8 @@ namespace Bloom void operator = (const InsightSignals&) = delete; signals: - void taskQueued(); - void taskProcessed(); + void taskQueued(QSharedPointer task); + void taskProcessed(QSharedPointer task); void targetStateUpdated(Bloom::Targets::TargetState newState); void targetReset(); diff --git a/src/Insight/InsightWorker/InsightWorker.cpp b/src/Insight/InsightWorker/InsightWorker.cpp index 47c38f61..975e5bee 100644 --- a/src/Insight/InsightWorker/InsightWorker.cpp +++ b/src/Insight/InsightWorker/InsightWorker.cpp @@ -34,19 +34,19 @@ namespace Bloom } void InsightWorker::queueTask(InsightWorkerTask* task) { - static std::atomic lastQueuedTaskId = 0; - task->moveToThread(nullptr); + const auto taskPtr = QSharedPointer(task, &QObject::deleteLater); + taskPtr->moveToThread(nullptr); { const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock(); - InsightWorker::queuedTasksById.getValue().insert(std::pair(++(lastQueuedTaskId), task)); + InsightWorker::queuedTasksById.getValue().emplace(taskPtr->id, taskPtr); } - emit InsightSignals::instance()->taskQueued(); + emit InsightSignals::instance()->taskQueued(taskPtr); } void InsightWorker::executeTasks() { - static const auto getQueuedTask = [] () -> std::optional { + static const auto getQueuedTask = [] () -> std::optional> { const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock(); auto& queuedTasks = InsightWorker::queuedTasksById.getValue(); @@ -54,7 +54,7 @@ namespace Bloom const auto taskGroupsLock = InsightWorker::taskGroupsInExecution.acquireLock(); auto& taskGroupsInExecution = InsightWorker::taskGroupsInExecution.getValue(); - const auto canExecuteTask = [&taskGroupsInExecution] (InsightWorkerTask* task) { + const auto canExecuteTask = [&taskGroupsInExecution] (const QSharedPointer& task) { for (const auto taskGroup : task->getTaskGroups()) { if (taskGroupsInExecution.contains(taskGroup)) { return false; @@ -77,10 +77,10 @@ namespace Bloom return std::nullopt; }; - auto queuedTask = std::optional(); + auto queuedTask = std::optional>(); while ((queuedTask = getQueuedTask())) { - auto* task = queuedTask.value(); + auto& task = *queuedTask; task->moveToThread(this->thread()); task->setParent(this); task->execute(this->targetControllerService); @@ -94,8 +94,7 @@ namespace Bloom } } - task->deleteLater(); - emit InsightSignals::instance()->taskProcessed(); + emit InsightSignals::instance()->taskProcessed(task); } } } diff --git a/src/Insight/InsightWorker/InsightWorker.hpp b/src/Insight/InsightWorker/InsightWorker.hpp index 124d9af8..1f9142c7 100644 --- a/src/Insight/InsightWorker/InsightWorker.hpp +++ b/src/Insight/InsightWorker/InsightWorker.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "Tasks/InsightWorkerTask.hpp" @@ -34,11 +35,8 @@ namespace Bloom void ready(); private: - using QueuedTaskId = std::uint64_t; - static_assert(std::atomic::is_always_lock_free); - static inline std::atomic lastWorkerId = 0; - static inline SyncSafe> queuedTasksById = {}; + static inline SyncSafe>> queuedTasksById = {}; static inline SyncSafe taskGroupsInExecution = {}; Services::TargetControllerService targetControllerService = Services::TargetControllerService(); diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp index 36ae306e..c5c9e5f8 100644 --- a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp @@ -6,6 +6,10 @@ namespace Bloom { using Services::TargetControllerService; + InsightWorkerTask::InsightWorkerTask() + : QObject(nullptr) + {} + void InsightWorkerTask::execute(TargetControllerService& targetControllerService) { try { this->state = InsightWorkerTaskState::STARTED; diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp index dba8c6c7..9063f6ed 100644 --- a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -21,9 +23,11 @@ namespace Bloom Q_OBJECT public: + using IdType = std::uint64_t; + const InsightWorkerTask::IdType id = ++(InsightWorkerTask::lastId); InsightWorkerTaskState state = InsightWorkerTaskState::CREATED; - InsightWorkerTask(): QObject(nullptr) {}; + InsightWorkerTask(); virtual TaskGroups getTaskGroups() const { return TaskGroups(); @@ -57,5 +61,8 @@ namespace Bloom protected: virtual void run(Services::TargetControllerService& targetControllerService) = 0; + + private: + static inline std::atomic lastId = 0; }; }