Files
BloomPatched/src/Insight/InsightWorker/InsightWorker.cpp

62 lines
1.7 KiB
C++
Raw Normal View History

#include "InsightWorker.hpp"
2021-04-04 21:04:12 +01:00
#include <QObject>
#include "src/Insight/InsightSignals.hpp"
#include "src/Logger/Logger.hpp"
2021-04-04 21:04:12 +01:00
namespace Bloom
{
using namespace Bloom::Exceptions;
using Bloom::Targets::TargetState;
2021-04-04 21:04:12 +01:00
void InsightWorker::startup() {
QObject::connect(
InsightSignals::instance(),
&InsightSignals::taskQueued,
this,
&InsightWorker::executeTasks
);
2022-09-08 15:29:54 +01:00
Logger::debug("InsightWorker" + std::to_string(this->id) + " thread ready");
emit this->ready();
}
void InsightWorker::queueTask(InsightWorkerTask* task) {
task->moveToThread(nullptr);
2021-04-04 21:04:12 +01:00
{
const auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
InsightWorker::queuedTasks.getValue().push(task);
}
emit InsightSignals::instance()->taskQueued();
}
2021-05-30 16:53:24 +01:00
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
auto& queuedTasks = InsightWorker::queuedTasks.getValue();
if (!queuedTasks.empty()) {
auto* task = queuedTasks.front();
queuedTasks.pop();
return task;
2021-05-30 16:53:24 +01:00
}
return std::nullopt;
}
void InsightWorker::executeTasks() {
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();
}
}
}