Moved insight worker and introduced worker tasks

This commit is contained in:
Nav
2021-08-30 22:17:59 +01:00
parent a52d2271b3
commit 3be8d90e09
7 changed files with 141 additions and 8 deletions

View File

@@ -123,7 +123,7 @@ add_executable(Bloom
# Insight
src/Insight/Insight.cpp
src/Insight/InsightWorker.cpp
src/Insight/InsightWorker/InsightWorker.cpp
src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp
src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp
src/Insight/UserInterfaces/InsightWindow/AboutWindow.cpp
@@ -135,6 +135,10 @@ add_executable(Bloom
src/Insight/UserInterfaces/InsightWindow/Widgets/ExpandingWidget.hpp
src/Insight/UserInterfaces/InsightWindow/Widgets/ExpandingHeightScrollAreaWidget.hpp
# Insight worker tasks
src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp
src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp
# Target package widgets
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPinWidget.hpp

View File

@@ -1,13 +1,11 @@
#include <thread>
#include "InsightWorker.hpp"
#include <filesystem>
#include <typeindex>
#include <QObject>
#include <QTimer>
#include "InsightWorker.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Helpers/Thread.hpp"
#include "src/Exceptions/InvalidConfig.hpp"
#include "src/Logger/Logger.hpp"
using namespace Bloom;
using namespace Bloom::Exceptions;
@@ -39,14 +37,46 @@ void InsightWorker::startup() {
);
this->eventDispatchTimer = new QTimer(this);
QTimer::connect(this->eventDispatchTimer, &QTimer::timeout, this, &InsightWorker::dispatchEvents);
this->connect(this->eventDispatchTimer, &QTimer::timeout, this, &InsightWorker::dispatchEvents);
this->eventDispatchTimer->start(5);
this->connect(this, &InsightWorker::taskQueued, this, &InsightWorker::executeTasks);
this->eventManager.triggerEvent(
std::make_shared<Events::InsightThreadStateChanged>(ThreadState::READY)
);
}
void InsightWorker::queueTask(InsightWorkerTask* task) {
auto taskQueueLock = this->queuedTasks.acquireLock();
task->moveToThread(this->thread());
task->setParent(this);
this->queuedTasks.getReference().push(task);
emit this->taskQueued();
}
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
auto task = std::optional<InsightWorkerTask*>();
auto& queuedTasks = this->queuedTasks.getReference();
auto taskQueueLock = this->queuedTasks.acquireLock();
if (!queuedTasks.empty()) {
task = queuedTasks.front();
queuedTasks.pop();
}
return task;
}
void InsightWorker::executeTasks() {
auto task = std::optional<InsightWorkerTask*>();
while ((task = this->getQueuedTask()).has_value()) {
task.value()->execute(this->targetControllerConsole);
}
}
void InsightWorker::requestPinStates(int variantId) {
this->targetControllerConsole.requestPinStates(variantId);
}

View File

@@ -4,11 +4,13 @@
#include <QApplication>
#include "src/Helpers/Thread.hpp"
#include "src/Helpers/SyncSafe.hpp"
#include "src/ApplicationConfig.hpp"
#include "src/EventManager/EventManager.hpp"
#include "src/EventManager/EventListener.hpp"
#include "src/TargetController/TargetControllerConsole.hpp"
#include "src/TargetController/TargetControllerState.hpp"
#include "Tasks/InsightWorkerTask.hpp"
namespace Bloom
{
@@ -27,17 +29,23 @@ namespace Bloom
this->eventManager,
*(this->eventListener)
);
TargetControllerState lastTargetControllerState = TargetControllerState::ACTIVE;
QTimer* eventDispatchTimer = nullptr;
SyncSafe<std::queue<InsightWorkerTask*>> queuedTasks;
std::optional<InsightWorkerTask*> getQueuedTask();
void onTargetStoppedEvent(const Events::TargetExecutionStopped& event);
void onTargetResumedEvent(const Events::TargetExecutionResumed& event);
void onTargetPinStatesRetrievedEvent(const Events::TargetPinStatesRetrieved& event);
void onTargetIoPortsUpdatedEvent(const Events::TargetIoPortsUpdated& event);
void onTargetControllerStateReported(const Events::TargetControllerStateReported& event);
private slots:
void executeTasks();
public:
explicit InsightWorker(EventManager& eventManager): eventManager(eventManager) {};
@@ -45,6 +53,8 @@ namespace Bloom
this->eventListener->dispatchCurrentEvents();
}
void queueTask(InsightWorkerTask* task);
public slots:
void startup();
void requestPinStates(int variantId);
@@ -55,6 +65,7 @@ namespace Bloom
);
signals:
void taskQueued();
void targetStateUpdated(Bloom::Targets::TargetState newState);
void targetProgramCounterUpdated(quint32 programCounter);
void targetPinStatesUpdated(int variantId, Bloom::Targets::TargetPinStateMappingType pinStatesByNumber);

View File

@@ -0,0 +1,17 @@
#include "InsightWorkerTask.hpp"
using namespace Bloom;
void InsightWorkerTask::execute(TargetControllerConsole& targetControllerConsole) {
try {
this->state = InsightWorkerTaskState::STARTED;
emit this->started();
this->run(targetControllerConsole);
this->state = InsightWorkerTaskState::COMPLETED;
emit this->completed();
} catch (std::exception& exception) {
this->state = InsightWorkerTaskState::FAILED;
emit this->failed(QString::fromStdString(exception.what()));
}
}

View File

@@ -0,0 +1,37 @@
#pragma once
#include <QObject>
#include <QString>
#include "src/TargetController/TargetControllerConsole.hpp"
namespace Bloom
{
enum class InsightWorkerTaskState: std::uint8_t
{
CREATED,
STARTED,
FAILED,
COMPLETED,
};
class InsightWorkerTask: public QObject
{
Q_OBJECT
protected:
virtual void run(TargetControllerConsole& targetControllerConsole) = 0;
public:
InsightWorkerTaskState state;
InsightWorkerTask() = default;
InsightWorkerTask(QObject* parent): QObject(parent) {};
void execute(TargetControllerConsole& targetControllerConsole);
signals:
void started();
void failed(QString errorMessage);
void completed();
};
}

View File

@@ -0,0 +1,7 @@
#include "ReadTargetRegisters.hpp"
using namespace Bloom;
void ReadTargetRegisters::run(TargetControllerConsole& targetControllerConsole) {
emit this->targetRegistersRead(targetControllerConsole.readRegisters(this->descriptors));
}

View File

@@ -0,0 +1,27 @@
#pragma once
#include <QObject>
#include <QString>
#include "InsightWorkerTask.hpp"
#include "src/Targets/TargetRegister.hpp"
namespace Bloom
{
class ReadTargetRegisters: public InsightWorkerTask
{
Q_OBJECT
private:
Targets::TargetRegisterDescriptors descriptors;
protected:
void run(TargetControllerConsole& targetControllerConsole) override;
public:
ReadTargetRegisters(const Targets::TargetRegisterDescriptors& descriptors, QObject* parent):
InsightWorkerTask(nullptr), descriptors(descriptors) {}
signals:
void targetRegistersRead(Targets::TargetRegisters registers);
};
}