Used std::atomic for ThreadState

This commit is contained in:
Nav
2023-05-25 23:16:30 +01:00
parent 9475a80cd0
commit 4485ee0961
8 changed files with 23 additions and 31 deletions

View File

@@ -153,7 +153,7 @@ namespace Bloom
this->startTargetController(); this->startTargetController();
this->startDebugServer(); this->startDebugServer();
Thread::setThreadState(ThreadState::READY); Thread::threadState = ThreadState::READY;
} }
void Application::shutdown() { void Application::shutdown() {
@@ -162,7 +162,7 @@ namespace Bloom
return; return;
} }
Thread::setThreadState(ThreadState::SHUTDOWN_INITIATED); Thread::threadState = ThreadState::SHUTDOWN_INITIATED;
Logger::info("Shutting down Bloom"); Logger::info("Shutting down Bloom");
#ifndef EXCLUDE_INSIGHT #ifndef EXCLUDE_INSIGHT
@@ -176,7 +176,7 @@ namespace Bloom
this->stopSignalHandler(); this->stopSignalHandler();
this->saveProjectSettings(); this->saveProjectSettings();
Thread::setThreadState(ThreadState::STOPPED); Thread::threadState = ThreadState::STOPPED;
} }
void Application::loadProjectSettings() { void Application::loadProjectSettings() {

View File

@@ -80,14 +80,12 @@ namespace Bloom::DebugServer
} }
void DebugServerComponent::shutdown() { void DebugServerComponent::shutdown() {
if ( const auto threadState = this->getThreadState();
this->getThreadState() == ThreadState::STOPPED if (threadState == ThreadState::STOPPED || threadState == ThreadState::SHUTDOWN_INITIATED) {
|| this->getThreadState() == ThreadState::SHUTDOWN_INITIATED
) {
return; return;
} }
this->setThreadState(ThreadState::SHUTDOWN_INITIATED); this->threadState = ThreadState::SHUTDOWN_INITIATED;
Logger::info("Shutting down DebugServer"); Logger::info("Shutting down DebugServer");
if (this->server) { if (this->server) {
@@ -101,7 +99,7 @@ namespace Bloom::DebugServer
} }
void DebugServerComponent::setThreadStateAndEmitEvent(ThreadState state) { void DebugServerComponent::setThreadStateAndEmitEvent(ThreadState state) {
Thread::setThreadState(state); this->threadState = state;
EventManager::triggerEvent( EventManager::triggerEvent(
std::make_shared<Events::DebugServerThreadStateChanged>(state) std::make_shared<Events::DebugServerThreadStateChanged>(state)
); );

View File

@@ -1,13 +1,13 @@
#pragma once #pragma once
#include <cstdint>
#include <csignal> #include <csignal>
#include <cassert> #include <cassert>
#include <atomic>
#include "SyncSafe.hpp"
namespace Bloom namespace Bloom
{ {
enum class ThreadState enum class ThreadState: std::uint8_t
{ {
UNINITIALISED, UNINITIALISED,
READY, READY,
@@ -28,15 +28,12 @@ namespace Bloom
Thread& operator = (const Thread& other) = delete; Thread& operator = (const Thread& other) = delete;
Thread& operator = (Thread&& other) = delete; Thread& operator = (Thread&& other) = delete;
virtual ThreadState getThreadState() { ThreadState getThreadState() {
auto lock = this->state.acquireLock(); return this->threadState;
return this->state.getValue();
} }
protected: protected:
virtual void setThreadState(ThreadState state) { std::atomic<ThreadState> threadState = ThreadState::UNINITIALISED;
this->state.setValue(state);
}
/** /**
* Disables signal interrupts on current thread. * Disables signal interrupts on current thread.
@@ -53,8 +50,5 @@ namespace Bloom
pthread_setname_np(pthread_self(), name.c_str()); pthread_setname_np(pthread_self(), name.c_str());
} }
private:
SyncSafe<ThreadState> state = SyncSafe<ThreadState>(ThreadState::UNINITIALISED);
}; };
} }

View File

@@ -50,7 +50,7 @@ namespace Bloom
try { try {
this->startup(); this->startup();
this->setThreadState(ThreadState::READY); this->threadState = ThreadState::READY;
Logger::info("Insight ready"); Logger::info("Insight ready");
this->application.exec(); this->application.exec();
@@ -73,7 +73,7 @@ namespace Bloom
void Insight::startup() { void Insight::startup() {
Logger::info("Starting Insight"); Logger::info("Starting Insight");
this->setThreadState(ThreadState::STARTING); this->threadState = ThreadState::STARTING;
this->eventListener.registerCallbackForEventType<Events::TargetExecutionStopped>( this->eventListener.registerCallbackForEventType<Events::TargetExecutionStopped>(
std::bind(&Insight::onTargetStoppedEvent, this, std::placeholders::_1) std::bind(&Insight::onTargetStoppedEvent, this, std::placeholders::_1)
@@ -232,7 +232,7 @@ namespace Bloom
} }
this->application.exit(0); this->application.exit(0);
this->setThreadState(ThreadState::STOPPED); this->threadState = ThreadState::STOPPED;
} }
void Insight::checkBloomVersion() { void Insight::checkBloomVersion() {

View File

@@ -32,12 +32,12 @@ namespace Bloom
} }
Logger::info("Shutting down SignalHandler"); Logger::info("Shutting down SignalHandler");
Thread::setThreadState(ThreadState::STOPPED); this->threadState = ThreadState::STOPPED;
} }
void SignalHandler::startup() { void SignalHandler::startup() {
this->setName("SH"); this->setName("SH");
Thread::setThreadState(ThreadState::STARTING); this->threadState = ThreadState::STARTING;
Logger::debug("Starting SignalHandler"); Logger::debug("Starting SignalHandler");
// Block all signal interrupts // Block all signal interrupts
auto signalSet = this->getRegisteredSignalSet(); auto signalSet = this->getRegisteredSignalSet();
@@ -54,9 +54,9 @@ namespace Bloom
std::bind(&SignalHandler::triggerApplicationShutdown, this) 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) { if (this->getThreadState() != ThreadState::SHUTDOWN_INITIATED) {
Thread::setThreadState(ThreadState::READY); this->threadState = ThreadState::READY;
} }
} }

View File

@@ -22,7 +22,7 @@ namespace Bloom
* Triggers the shutdown of the SignalHandler thread. * Triggers the shutdown of the SignalHandler thread.
*/ */
void triggerShutdown() { void triggerShutdown() {
this->setThreadState(ThreadState::SHUTDOWN_INITIATED); this->threadState = ThreadState::SHUTDOWN_INITIATED;
}; };
private: private:

View File

@@ -134,7 +134,7 @@ namespace Bloom::TargetController
void TargetControllerComponent::startup() { void TargetControllerComponent::startup() {
this->setName("TC"); this->setName("TC");
Logger::info("Starting TargetController"); Logger::info("Starting TargetController");
this->setThreadState(ThreadState::STARTING); this->threadState = ThreadState::STARTING;
this->blockAllSignals(); this->blockAllSignals();
this->eventListener->setInterruptEventNotifier(&TargetControllerComponent::notifier); this->eventListener->setInterruptEventNotifier(&TargetControllerComponent::notifier);
EventManager::registerListener(this->eventListener); EventManager::registerListener(this->eventListener);

View File

@@ -189,7 +189,7 @@ namespace Bloom::TargetController
* @param emitEvent * @param emitEvent
*/ */
void setThreadStateAndEmitEvent(ThreadState state) { void setThreadStateAndEmitEvent(ThreadState state) {
this->setThreadState(state); this->threadState = state;
EventManager::triggerEvent( EventManager::triggerEvent(
std::make_shared<Events::TargetControllerThreadStateChanged>(state) std::make_shared<Events::TargetControllerThreadStateChanged>(state)
); );