From 4485ee0961616af6f546214f100cf156d2fe9024 Mon Sep 17 00:00:00 2001 From: Nav Date: Thu, 25 May 2023 23:16:30 +0100 Subject: [PATCH] Used std::atomic for ThreadState --- src/Application.cpp | 6 +++--- src/DebugServer/DebugServerComponent.cpp | 10 ++++------ src/Helpers/Thread.hpp | 18 ++++++------------ src/Insight/Insight.cpp | 6 +++--- src/SignalHandler/SignalHandler.cpp | 8 ++++---- src/SignalHandler/SignalHandler.hpp | 2 +- .../TargetControllerComponent.cpp | 2 +- .../TargetControllerComponent.hpp | 2 +- 8 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index dbe8a4b5..c9184ede 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -153,7 +153,7 @@ namespace Bloom this->startTargetController(); this->startDebugServer(); - Thread::setThreadState(ThreadState::READY); + Thread::threadState = ThreadState::READY; } void Application::shutdown() { @@ -162,7 +162,7 @@ namespace Bloom return; } - Thread::setThreadState(ThreadState::SHUTDOWN_INITIATED); + Thread::threadState = ThreadState::SHUTDOWN_INITIATED; Logger::info("Shutting down Bloom"); #ifndef EXCLUDE_INSIGHT @@ -176,7 +176,7 @@ namespace Bloom this->stopSignalHandler(); this->saveProjectSettings(); - Thread::setThreadState(ThreadState::STOPPED); + Thread::threadState = ThreadState::STOPPED; } void Application::loadProjectSettings() { diff --git a/src/DebugServer/DebugServerComponent.cpp b/src/DebugServer/DebugServerComponent.cpp index 247ea5de..3867c3c8 100644 --- a/src/DebugServer/DebugServerComponent.cpp +++ b/src/DebugServer/DebugServerComponent.cpp @@ -80,14 +80,12 @@ namespace Bloom::DebugServer } void DebugServerComponent::shutdown() { - if ( - this->getThreadState() == ThreadState::STOPPED - || this->getThreadState() == ThreadState::SHUTDOWN_INITIATED - ) { + const auto threadState = this->getThreadState(); + if (threadState == ThreadState::STOPPED || threadState == ThreadState::SHUTDOWN_INITIATED) { return; } - this->setThreadState(ThreadState::SHUTDOWN_INITIATED); + this->threadState = ThreadState::SHUTDOWN_INITIATED; Logger::info("Shutting down DebugServer"); if (this->server) { @@ -101,7 +99,7 @@ namespace Bloom::DebugServer } void DebugServerComponent::setThreadStateAndEmitEvent(ThreadState state) { - Thread::setThreadState(state); + this->threadState = state; EventManager::triggerEvent( std::make_shared(state) ); diff --git a/src/Helpers/Thread.hpp b/src/Helpers/Thread.hpp index 5c1609ec..9fa2c10b 100644 --- a/src/Helpers/Thread.hpp +++ b/src/Helpers/Thread.hpp @@ -1,13 +1,13 @@ #pragma once +#include #include #include - -#include "SyncSafe.hpp" +#include namespace Bloom { - enum class ThreadState + enum class ThreadState: std::uint8_t { UNINITIALISED, READY, @@ -28,15 +28,12 @@ namespace Bloom Thread& operator = (const Thread& other) = delete; Thread& operator = (Thread&& other) = delete; - virtual ThreadState getThreadState() { - auto lock = this->state.acquireLock(); - return this->state.getValue(); + ThreadState getThreadState() { + return this->threadState; } protected: - virtual void setThreadState(ThreadState state) { - this->state.setValue(state); - } + std::atomic threadState = ThreadState::UNINITIALISED; /** * Disables signal interrupts on current thread. @@ -53,8 +50,5 @@ namespace Bloom pthread_setname_np(pthread_self(), name.c_str()); } - - private: - SyncSafe state = SyncSafe(ThreadState::UNINITIALISED); }; } diff --git a/src/Insight/Insight.cpp b/src/Insight/Insight.cpp index 6bcb2c7c..2c44ae7e 100644 --- a/src/Insight/Insight.cpp +++ b/src/Insight/Insight.cpp @@ -50,7 +50,7 @@ namespace Bloom try { this->startup(); - this->setThreadState(ThreadState::READY); + this->threadState = ThreadState::READY; Logger::info("Insight ready"); this->application.exec(); @@ -73,7 +73,7 @@ namespace Bloom void Insight::startup() { Logger::info("Starting Insight"); - this->setThreadState(ThreadState::STARTING); + this->threadState = ThreadState::STARTING; this->eventListener.registerCallbackForEventType( std::bind(&Insight::onTargetStoppedEvent, this, std::placeholders::_1) @@ -232,7 +232,7 @@ namespace Bloom } this->application.exit(0); - this->setThreadState(ThreadState::STOPPED); + this->threadState = ThreadState::STOPPED; } void Insight::checkBloomVersion() { diff --git a/src/SignalHandler/SignalHandler.cpp b/src/SignalHandler/SignalHandler.cpp index dd7fe5d5..1503108c 100644 --- a/src/SignalHandler/SignalHandler.cpp +++ b/src/SignalHandler/SignalHandler.cpp @@ -32,12 +32,12 @@ namespace Bloom } Logger::info("Shutting down SignalHandler"); - Thread::setThreadState(ThreadState::STOPPED); + this->threadState = ThreadState::STOPPED; } void SignalHandler::startup() { this->setName("SH"); - Thread::setThreadState(ThreadState::STARTING); + this->threadState = ThreadState::STARTING; Logger::debug("Starting SignalHandler"); // Block all signal interrupts auto signalSet = this->getRegisteredSignalSet(); @@ -54,9 +54,9 @@ namespace Bloom std::bind(&SignalHandler::triggerApplicationShutdown, this) )); - // It's possible that the SignalHandler has been instructed to shutdown, before it could finish starting up. + // It's possible that the SignalHandler has been instructed to shut down, before it could finish starting up. if (this->getThreadState() != ThreadState::SHUTDOWN_INITIATED) { - Thread::setThreadState(ThreadState::READY); + this->threadState = ThreadState::READY; } } diff --git a/src/SignalHandler/SignalHandler.hpp b/src/SignalHandler/SignalHandler.hpp index 47b01c07..79fbfbb4 100644 --- a/src/SignalHandler/SignalHandler.hpp +++ b/src/SignalHandler/SignalHandler.hpp @@ -22,7 +22,7 @@ namespace Bloom * Triggers the shutdown of the SignalHandler thread. */ void triggerShutdown() { - this->setThreadState(ThreadState::SHUTDOWN_INITIATED); + this->threadState = ThreadState::SHUTDOWN_INITIATED; }; private: diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index e6366c21..ec3e9be5 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -134,7 +134,7 @@ namespace Bloom::TargetController void TargetControllerComponent::startup() { this->setName("TC"); Logger::info("Starting TargetController"); - this->setThreadState(ThreadState::STARTING); + this->threadState = ThreadState::STARTING; this->blockAllSignals(); this->eventListener->setInterruptEventNotifier(&TargetControllerComponent::notifier); EventManager::registerListener(this->eventListener); diff --git a/src/TargetController/TargetControllerComponent.hpp b/src/TargetController/TargetControllerComponent.hpp index 70d4e60d..2ed6c2ae 100644 --- a/src/TargetController/TargetControllerComponent.hpp +++ b/src/TargetController/TargetControllerComponent.hpp @@ -189,7 +189,7 @@ namespace Bloom::TargetController * @param emitEvent */ void setThreadStateAndEmitEvent(ThreadState state) { - this->setThreadState(state); + this->threadState = state; EventManager::triggerEvent( std::make_shared(state) );