2021-08-30 22:17:59 +01:00
|
|
|
#include "InsightWorker.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <QObject>
|
2022-04-27 21:27:59 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
#include "src/Insight/InsightSignals.hpp"
|
2021-08-30 22:17:59 +01:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom
|
|
|
|
|
{
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-05-24 20:58:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
using Bloom::Targets::TargetState;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::startup() {
|
2022-09-07 22:25:28 +01:00
|
|
|
QObject::connect(
|
|
|
|
|
InsightSignals::instance(),
|
|
|
|
|
&InsightSignals::taskQueued,
|
|
|
|
|
this,
|
|
|
|
|
&InsightWorker::executeTasks
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
2021-10-18 01:03:22 +01:00
|
|
|
|
2022-09-08 15:29:54 +01:00
|
|
|
Logger::debug("InsightWorker" + std::to_string(this->id) + " thread ready");
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->ready();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
|
|
|
|
task->moveToThread(nullptr);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
{
|
|
|
|
|
const auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
|
|
|
|
|
InsightWorker::queuedTasks.getValue().push(task);
|
2022-04-23 17:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
emit InsightSignals::instance()->taskQueued();
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
|
|
|
|
|
auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
|
|
|
|
|
auto& queuedTasks = InsightWorker::queuedTasks.getValue();
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
if (!queuedTasks.empty()) {
|
|
|
|
|
auto* task = queuedTasks.front();
|
|
|
|
|
queuedTasks.pop();
|
|
|
|
|
return task;
|
2021-05-30 16:53:24 +01:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
return std::nullopt;
|
2022-06-05 17:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::executeTasks() {
|
2022-09-07 22:25:28 +01:00
|
|
|
auto queuedTask = std::optional<InsightWorkerTask*>();
|
|
|
|
|
|
|
|
|
|
while ((queuedTask = InsightWorker::getQueuedTask()).has_value()) {
|
|
|
|
|
auto* task = queuedTask.value();
|
|
|
|
|
task->moveToThread(this->thread());
|
|
|
|
|
task->setParent(this);
|
|
|
|
|
task->execute(this->targetControllerConsole);
|
|
|
|
|
task->deleteLater();
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|