Manage InsightWorkerTask pointers via QSharedPointer, in preparation for task indicator widget and task window.

This commit is contained in:
Nav
2023-03-12 23:31:19 +00:00
parent db9cddf12c
commit 57eaa989f5
5 changed files with 28 additions and 17 deletions

View File

@@ -2,11 +2,14 @@
#include <QObject>
#include <QDateTime>
#include <QSharedPointer>
#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<InsightWorkerTask> task);
void taskProcessed(QSharedPointer<InsightWorkerTask> task);
void targetStateUpdated(Bloom::Targets::TargetState newState);
void targetReset();

View File

@@ -34,19 +34,19 @@ namespace Bloom
}
void InsightWorker::queueTask(InsightWorkerTask* task) {
static std::atomic<QueuedTaskId> lastQueuedTaskId = 0;
task->moveToThread(nullptr);
const auto taskPtr = QSharedPointer<InsightWorkerTask>(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<InsightWorkerTask*> {
static const auto getQueuedTask = [] () -> std::optional<QSharedPointer<InsightWorkerTask>> {
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<InsightWorkerTask>& 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<InsightWorkerTask*>();
auto queuedTask = std::optional<QSharedPointer<InsightWorkerTask>>();
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);
}
}
}

View File

@@ -5,6 +5,7 @@
#include <QtCore>
#include <queue>
#include <map>
#include <QSharedPointer>
#include "Tasks/InsightWorkerTask.hpp"
@@ -34,11 +35,8 @@ namespace Bloom
void ready();
private:
using QueuedTaskId = std::uint64_t;
static_assert(std::atomic<QueuedTaskId>::is_always_lock_free);
static inline std::atomic<std::uint8_t> lastWorkerId = 0;
static inline SyncSafe<std::map<QueuedTaskId, InsightWorkerTask*>> queuedTasksById = {};
static inline SyncSafe<std::map<InsightWorkerTask::IdType, QSharedPointer<InsightWorkerTask>>> queuedTasksById = {};
static inline SyncSafe<TaskGroups> taskGroupsInExecution = {};
Services::TargetControllerService targetControllerService = Services::TargetControllerService();

View File

@@ -6,6 +6,10 @@ namespace Bloom
{
using Services::TargetControllerService;
InsightWorkerTask::InsightWorkerTask()
: QObject(nullptr)
{}
void InsightWorkerTask::execute(TargetControllerService& targetControllerService) {
try {
this->state = InsightWorkerTaskState::STARTED;

View File

@@ -1,5 +1,7 @@
#pragma once
#include <cstdint>
#include <atomic>
#include <QObject>
#include <QString>
@@ -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<InsightWorkerTask::IdType> lastId = 0;
};
}