From 3be8d90e09f300fdbcf02de19cc0ffaceb8240e1 Mon Sep 17 00:00:00 2001 From: Nav Date: Mon, 30 Aug 2021 22:17:59 +0100 Subject: [PATCH] Moved insight worker and introduced worker tasks --- CMakeLists.txt | 6 ++- .../{ => InsightWorker}/InsightWorker.cpp | 42 ++++++++++++++++--- .../{ => InsightWorker}/InsightWorker.hpp | 13 +++++- .../InsightWorker/Tasks/InsightWorkerTask.cpp | 17 ++++++++ .../InsightWorker/Tasks/InsightWorkerTask.hpp | 37 ++++++++++++++++ .../Tasks/ReadTargetRegisters.cpp | 7 ++++ .../Tasks/ReadTargetRegisters.hpp | 27 ++++++++++++ 7 files changed, 141 insertions(+), 8 deletions(-) rename src/Insight/{ => InsightWorker}/InsightWorker.cpp (83%) rename src/Insight/{ => InsightWorker}/InsightWorker.hpp (88%) create mode 100644 src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp create mode 100644 src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp create mode 100644 src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp create mode 100644 src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9846e559..4cf7ec75 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/Insight/InsightWorker.cpp b/src/Insight/InsightWorker/InsightWorker.cpp similarity index 83% rename from src/Insight/InsightWorker.cpp rename to src/Insight/InsightWorker/InsightWorker.cpp index 083a4f6d..9aebd2db 100644 --- a/src/Insight/InsightWorker.cpp +++ b/src/Insight/InsightWorker/InsightWorker.cpp @@ -1,13 +1,11 @@ -#include +#include "InsightWorker.hpp" + #include -#include #include #include -#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(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 InsightWorker::getQueuedTask() { + auto task = std::optional(); + + 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(); + + while ((task = this->getQueuedTask()).has_value()) { + task.value()->execute(this->targetControllerConsole); + } +} + void InsightWorker::requestPinStates(int variantId) { this->targetControllerConsole.requestPinStates(variantId); } diff --git a/src/Insight/InsightWorker.hpp b/src/Insight/InsightWorker/InsightWorker.hpp similarity index 88% rename from src/Insight/InsightWorker.hpp rename to src/Insight/InsightWorker/InsightWorker.hpp index c08cc0cd..a92e3ed5 100644 --- a/src/Insight/InsightWorker.hpp +++ b/src/Insight/InsightWorker/InsightWorker.hpp @@ -4,11 +4,13 @@ #include #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> queuedTasks; + + std::optional 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); diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp new file mode 100644 index 00000000..f6dae1bd --- /dev/null +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp @@ -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())); + } +} diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp new file mode 100644 index 00000000..5e69174e --- /dev/null +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include + +#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(); + }; +} diff --git a/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp new file mode 100644 index 00000000..bf3a8572 --- /dev/null +++ b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp @@ -0,0 +1,7 @@ +#include "ReadTargetRegisters.hpp" + +using namespace Bloom; + +void ReadTargetRegisters::run(TargetControllerConsole& targetControllerConsole) { + emit this->targetRegistersRead(targetControllerConsole.readRegisters(this->descriptors)); +} diff --git a/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp new file mode 100644 index 00000000..8d954297 --- /dev/null +++ b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#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); + }; +}