Created start/stop routines for the signal handler

This commit is contained in:
Nav
2022-01-09 14:25:52 +00:00
parent 4a5ddb1a4b
commit 023b655145
2 changed files with 37 additions and 22 deletions

View File

@@ -98,9 +98,8 @@ void Application::startup() {
this->loadProjectConfiguration(); this->loadProjectConfiguration();
Logger::configure(this->projectConfig.value()); Logger::configure(this->projectConfig.value());
// Start signal handler
this->blockAllSignalsOnCurrentThread(); this->blockAllSignalsOnCurrentThread();
this->signalHandlerThread = std::thread(&SignalHandler::run, std::ref(this->signalHandler)); this->startSignalHandler();
Logger::info("Selected environment: \"" + this->selectedEnvironmentName + "\""); Logger::info("Selected environment: \"" + this->selectedEnvironmentName + "\"");
Logger::debug("Number of environments extracted from config: " Logger::debug("Number of environments extracted from config: "
@@ -131,25 +130,7 @@ void Application::shutdown() {
this->stopDebugServer(); this->stopDebugServer();
this->stopTargetController(); this->stopTargetController();
this->stopSignalHandler();
if (this->signalHandler.getThreadState() != ThreadState::STOPPED
&& this->signalHandler.getThreadState() != ThreadState::UNINITIALISED
) {
// Signal handler is still running
this->signalHandler.triggerShutdown();
/*
* Send meaningless signal to the SignalHandler thread to have it shutdown. The signal will pull it out of a
* blocking state and allow it to action the shutdown. See SignalHandler::run() for more.
*/
pthread_kill(this->signalHandlerThread.native_handle(), SIGUSR1);
}
if (this->signalHandlerThread.joinable()) {
Logger::debug("Joining SignalHandler thread");
this->signalHandlerThread.join();
Logger::debug("SignalHandler thread joined");
}
Thread::setThreadState(ThreadState::STOPPED); Thread::setThreadState(ThreadState::STOPPED);
} }
@@ -303,6 +284,30 @@ int Application::initProject() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void Application::startSignalHandler() {
this->signalHandlerThread = std::thread(&SignalHandler::run, std::ref(this->signalHandler));
}
void Application::stopSignalHandler() {
if (this->signalHandler.getThreadState() != ThreadState::STOPPED
&& this->signalHandler.getThreadState() != ThreadState::UNINITIALISED
) {
this->signalHandler.triggerShutdown();
/*
* Send meaningless signal to the SignalHandler thread to have it shutdown. The signal will pull it out of a
* blocking state and allow it to action the shutdown. See SignalHandler::run() for more.
*/
pthread_kill(this->signalHandlerThread.native_handle(), SIGUSR1);
}
if (this->signalHandlerThread.joinable()) {
Logger::debug("Joining SignalHandler thread");
this->signalHandlerThread.join();
Logger::debug("SignalHandler thread joined");
}
}
void Application::startTargetController() { void Application::startTargetController() {
this->targetController = std::make_unique<TargetController>( this->targetController = std::make_unique<TargetController>(
this->eventManager, this->eventManager,

View File

@@ -246,7 +246,17 @@ namespace Bloom
int initProject(); int initProject();
/** /**
* Prepares a dedicated thread for the TargetController and kicks it off. * Prepares a dedicated thread for the SignalHandler and kicks it off with a call to SignalHandler::run().
*/
void startSignalHandler();
/**
* Sends a shutdown request to the SignalHandler and waits on the dedicated thread to exit.
*/
void stopSignalHandler();
/**
* Prepares a dedicated thread for the TargetController and kicks it off with a call to TargetController::run().
*/ */
void startTargetController(); void startTargetController();