Handle SIGINT (CTL+C) by gracefully shutting down
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <mind.h>
|
||||
#include <componentThread.h>
|
||||
#include <marionette/marionette.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
@@ -381,13 +382,28 @@ void ComponentThread::exceptionInd(ComponentThread& thread)
|
||||
std::cerr << "Mrntt: Exception occurred: in thread "
|
||||
<< thread.name << ". Killing Salmanoff." << "\n";
|
||||
|
||||
smo::mind.finalizeReq(
|
||||
// Delegate to common shutdown request
|
||||
mind.finalizeReq(smo::mrntt::exitMarionetteLoop);
|
||||
});
|
||||
}
|
||||
|
||||
void ComponentThread::userShutdownInd()
|
||||
{
|
||||
if (this->id != ComponentThread::MRNTT)
|
||||
{
|
||||
throw std::runtime_error(std::string(__func__)
|
||||
+ ": invoked on non-mrntt thread " + this->name);
|
||||
}
|
||||
|
||||
// Post the user shutdown to the mrntt thread.
|
||||
this->getIoService().post(
|
||||
[]()
|
||||
{
|
||||
mrntt::mrntt->keepLooping = false;
|
||||
mrntt::mrntt->getIoService().stop();
|
||||
std::cout << "Mrntt: Signaled main loop to exit." << "\n";
|
||||
});
|
||||
std::cerr << "Mrntt: User requested shutdown (SIGINT)."
|
||||
<< " Killing Salmanoff." << "\n";
|
||||
|
||||
// Delegate to common shutdown request
|
||||
mind.finalizeReq(smo::mrntt::exitMarionetteLoop);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -110,6 +110,8 @@ public:
|
||||
|
||||
// Intentionally doesn't take a callback.
|
||||
void exceptionInd(ComponentThread& thread);
|
||||
// Intentionally doesn't take a callback.
|
||||
void userShutdownInd();
|
||||
|
||||
public:
|
||||
ThreadId id;
|
||||
|
||||
@@ -13,6 +13,8 @@ class Marionette
|
||||
{
|
||||
};
|
||||
|
||||
void exitMarionetteLoop();
|
||||
|
||||
} // namespace mrntt
|
||||
|
||||
struct CrtCommandLineArgs
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <componentThread.h>
|
||||
#include <marionette/marionette.h>
|
||||
#include <salmanoff.h>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
|
||||
namespace smo {
|
||||
|
||||
@@ -19,8 +20,16 @@ void CrtCommandLineArgs::set(int argc, char *argv[], char *envp[])
|
||||
|
||||
namespace mrntt {
|
||||
std::atomic<int> exitCode;
|
||||
|
||||
void exitMarionetteLoop()
|
||||
{
|
||||
mrntt::mrntt->keepLooping = false;
|
||||
mrntt::mrntt->getIoService().stop();
|
||||
std::cout << "Mrntt: Signaled main loop to exit." << "\n";
|
||||
}
|
||||
|
||||
} // namespace mrntt
|
||||
|
||||
void ComponentThread::marionetteMain(ComponentThread& self)
|
||||
{
|
||||
// Wait for CRT's main() to post us the command line args.
|
||||
@@ -28,8 +37,19 @@ void ComponentThread::marionetteMain(ComponentThread& self)
|
||||
self.getIoService().run();
|
||||
self.initializeTls();
|
||||
mrntt::exitCode = EXIT_SUCCESS;
|
||||
static boost::asio::signal_set signals(self.getIoService(), SIGINT);
|
||||
|
||||
try {
|
||||
// Register SIGINT (Ctrl+C) handler to request user shutdown
|
||||
signals.async_wait(
|
||||
[&self](const boost::system::error_code& ec, int /*signal*/)
|
||||
{
|
||||
if (ec) { return; }
|
||||
// Post user shutdown indication
|
||||
self.userShutdownInd();
|
||||
}
|
||||
);
|
||||
|
||||
OptionParser &options = OptionParser::getOptions();
|
||||
|
||||
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION
|
||||
|
||||
Reference in New Issue
Block a user