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->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() {

View File

@@ -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<Events::DebugServerThreadStateChanged>(state)
);

View File

@@ -1,13 +1,13 @@
#pragma once
#include <cstdint>
#include <csignal>
#include <cassert>
#include "SyncSafe.hpp"
#include <atomic>
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 = ThreadState::UNINITIALISED;
/**
* Disables signal interrupts on current thread.
@@ -53,8 +50,5 @@ namespace Bloom
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 {
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<Events::TargetExecutionStopped>(
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() {

View File

@@ -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;
}
}

View File

@@ -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:

View File

@@ -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);

View File

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