From 023b65514584955ed2b5f19869a1b1c6724d776a Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 9 Jan 2022 14:25:52 +0000 Subject: [PATCH] Created start/stop routines for the signal handler --- src/Application.cpp | 47 +++++++++++++++++++++++++-------------------- src/Application.hpp | 12 +++++++++++- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index f6f52a6a..ef6a89c1 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -98,9 +98,8 @@ void Application::startup() { this->loadProjectConfiguration(); Logger::configure(this->projectConfig.value()); - // Start signal handler this->blockAllSignalsOnCurrentThread(); - this->signalHandlerThread = std::thread(&SignalHandler::run, std::ref(this->signalHandler)); + this->startSignalHandler(); Logger::info("Selected environment: \"" + this->selectedEnvironmentName + "\""); Logger::debug("Number of environments extracted from config: " @@ -131,25 +130,7 @@ void Application::shutdown() { this->stopDebugServer(); this->stopTargetController(); - - 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"); - } + this->stopSignalHandler(); Thread::setThreadState(ThreadState::STOPPED); } @@ -303,6 +284,30 @@ int Application::initProject() { 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() { this->targetController = std::make_unique( this->eventManager, diff --git a/src/Application.hpp b/src/Application.hpp index c85a2e41..be7da76f 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -246,7 +246,17 @@ namespace Bloom 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();