Manage InsightWorkerTask pointers via QSharedPointer, in preparation for task indicator widget and task window.
This commit is contained in:
@@ -2,11 +2,14 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
|
||||||
#include "src/Targets/TargetState.hpp"
|
#include "src/Targets/TargetState.hpp"
|
||||||
#include "src/Targets/TargetDescriptor.hpp"
|
#include "src/Targets/TargetDescriptor.hpp"
|
||||||
#include "src/Targets/TargetRegister.hpp"
|
#include "src/Targets/TargetRegister.hpp"
|
||||||
|
|
||||||
|
#include "InsightWorker/Tasks/InsightWorkerTask.hpp"
|
||||||
|
|
||||||
namespace Bloom
|
namespace Bloom
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -27,8 +30,8 @@ namespace Bloom
|
|||||||
void operator = (const InsightSignals&) = delete;
|
void operator = (const InsightSignals&) = delete;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void taskQueued();
|
void taskQueued(QSharedPointer<InsightWorkerTask> task);
|
||||||
void taskProcessed();
|
void taskProcessed(QSharedPointer<InsightWorkerTask> task);
|
||||||
|
|
||||||
void targetStateUpdated(Bloom::Targets::TargetState newState);
|
void targetStateUpdated(Bloom::Targets::TargetState newState);
|
||||||
void targetReset();
|
void targetReset();
|
||||||
|
|||||||
@@ -34,19 +34,19 @@ namespace Bloom
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
||||||
static std::atomic<QueuedTaskId> lastQueuedTaskId = 0;
|
const auto taskPtr = QSharedPointer<InsightWorkerTask>(task, &QObject::deleteLater);
|
||||||
task->moveToThread(nullptr);
|
taskPtr->moveToThread(nullptr);
|
||||||
|
|
||||||
{
|
{
|
||||||
const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock();
|
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() {
|
void InsightWorker::executeTasks() {
|
||||||
static const auto getQueuedTask = [] () -> std::optional<InsightWorkerTask*> {
|
static const auto getQueuedTask = [] () -> std::optional<QSharedPointer<InsightWorkerTask>> {
|
||||||
const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock();
|
const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock();
|
||||||
auto& queuedTasks = InsightWorker::queuedTasksById.getValue();
|
auto& queuedTasks = InsightWorker::queuedTasksById.getValue();
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ namespace Bloom
|
|||||||
const auto taskGroupsLock = InsightWorker::taskGroupsInExecution.acquireLock();
|
const auto taskGroupsLock = InsightWorker::taskGroupsInExecution.acquireLock();
|
||||||
auto& taskGroupsInExecution = InsightWorker::taskGroupsInExecution.getValue();
|
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()) {
|
for (const auto taskGroup : task->getTaskGroups()) {
|
||||||
if (taskGroupsInExecution.contains(taskGroup)) {
|
if (taskGroupsInExecution.contains(taskGroup)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -77,10 +77,10 @@ namespace Bloom
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto queuedTask = std::optional<InsightWorkerTask*>();
|
auto queuedTask = std::optional<QSharedPointer<InsightWorkerTask>>();
|
||||||
|
|
||||||
while ((queuedTask = getQueuedTask())) {
|
while ((queuedTask = getQueuedTask())) {
|
||||||
auto* task = queuedTask.value();
|
auto& task = *queuedTask;
|
||||||
task->moveToThread(this->thread());
|
task->moveToThread(this->thread());
|
||||||
task->setParent(this);
|
task->setParent(this);
|
||||||
task->execute(this->targetControllerService);
|
task->execute(this->targetControllerService);
|
||||||
@@ -94,8 +94,7 @@ namespace Bloom
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
task->deleteLater();
|
emit InsightSignals::instance()->taskProcessed(task);
|
||||||
emit InsightSignals::instance()->taskProcessed();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
|
||||||
#include "Tasks/InsightWorkerTask.hpp"
|
#include "Tasks/InsightWorkerTask.hpp"
|
||||||
|
|
||||||
@@ -34,11 +35,8 @@ namespace Bloom
|
|||||||
void ready();
|
void ready();
|
||||||
|
|
||||||
private:
|
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 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 = {};
|
static inline SyncSafe<TaskGroups> taskGroupsInExecution = {};
|
||||||
|
|
||||||
Services::TargetControllerService targetControllerService = Services::TargetControllerService();
|
Services::TargetControllerService targetControllerService = Services::TargetControllerService();
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ namespace Bloom
|
|||||||
{
|
{
|
||||||
using Services::TargetControllerService;
|
using Services::TargetControllerService;
|
||||||
|
|
||||||
|
InsightWorkerTask::InsightWorkerTask()
|
||||||
|
: QObject(nullptr)
|
||||||
|
{}
|
||||||
|
|
||||||
void InsightWorkerTask::execute(TargetControllerService& targetControllerService) {
|
void InsightWorkerTask::execute(TargetControllerService& targetControllerService) {
|
||||||
try {
|
try {
|
||||||
this->state = InsightWorkerTaskState::STARTED;
|
this->state = InsightWorkerTaskState::STARTED;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <atomic>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
@@ -21,9 +23,11 @@ namespace Bloom
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
using IdType = std::uint64_t;
|
||||||
|
const InsightWorkerTask::IdType id = ++(InsightWorkerTask::lastId);
|
||||||
InsightWorkerTaskState state = InsightWorkerTaskState::CREATED;
|
InsightWorkerTaskState state = InsightWorkerTaskState::CREATED;
|
||||||
|
|
||||||
InsightWorkerTask(): QObject(nullptr) {};
|
InsightWorkerTask();
|
||||||
|
|
||||||
virtual TaskGroups getTaskGroups() const {
|
virtual TaskGroups getTaskGroups() const {
|
||||||
return TaskGroups();
|
return TaskGroups();
|
||||||
@@ -57,5 +61,8 @@ namespace Bloom
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void run(Services::TargetControllerService& targetControllerService) = 0;
|
virtual void run(Services::TargetControllerService& targetControllerService) = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static inline std::atomic<InsightWorkerTask::IdType> lastId = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user