Mrntt: use keepLooping after exception; Mind: split code from mrntt

Mrntt now has the event loop structure required to orderly shut itself
down when it itself generates an exception. We can now post a message
within the catch{} blocks for Mrntt's event loop, telling Mrntt
to shut down the Mind threads and then shut itself down.

We also split the code to initialize threads etc out of mrntt and
put it into the Mind:: namespace.
This commit is contained in:
2025-07-30 09:09:38 -04:00
parent 36c79f3a2e
commit e276fcbdce
6 changed files with 79 additions and 31 deletions
+10 -1
View File
@@ -96,6 +96,13 @@ void ComponentThread::main(ComponentThread& self)
std::cout << self.name << ":" << __func__ << ": Entering event loop" <<"\n";
/* We loop here because when an exception is caught, we need to first catch
* it in the catch blocks. We bubble the exception to mrntt in the catch
* blocks, and then we loop here to await control messages from mrntt.
*
* We can't just exit on our own. Rather, we must wait for mrntt to tell us
* to exit. When we wish to finally exit, we set keepLooping to false.
*/
for (self.keepLooping = true; self.keepLooping;)
{
try {
@@ -118,7 +125,7 @@ void ComponentThread::main(ComponentThread& self)
}
}
std::cout << self.name << ":" << __func__ << ": Exiting event loop" << "\n";
std::cout << self.name << ":" << __func__ << ": Exited event loop" << "\n";
}
// Thread management method implementations
@@ -245,6 +252,8 @@ void ComponentThread::exceptionInd(ComponentThread& thread)
currThreadJ->thread.join();
}
mrntt::mrntt->keepLooping = false;
mrntt::mrntt->getIoService().stop();
}
);
+1 -1
View File
@@ -28,7 +28,7 @@ struct CrtCommandLineArgs
static void set(int argc, char *argv[], char *envp[]);
};
int initializeSalmanoff(void);
void initializeSalmanoff(void);
} // namespace smo
+1
View File
@@ -12,6 +12,7 @@ namespace smo {
class Mind
{
public:
void initialize(void);
void execute(void);
public:
+43 -27
View File
@@ -3,6 +3,7 @@
#include <iostream>
#include <exception>
#include <opts.h>
#include <mind.h>
#include <componentThread.h>
#include <marionette/marionette.h>
@@ -57,37 +58,52 @@ void ComponentThread::marionetteMain(ComponentThread& self)
return;
}
int ret = smo::initializeSalmanoff();
if (ret != 0)
smo::Mind mind;
self.getIoService().post([&mind]()
{
std::cerr << __func__ << ": Initialization failed with code: "
<< ret << std::endl;
mrntt::exitCode = ret;
return;
}
try {
initializeSalmanoff();
mind.initialize();
}
catch (const std::exception& e) {
mrntt::exitCode = EXIT_FAILURE;
throw;
}
/* Start the threads */
for (auto& componentThread : smo::ComponentThread::componentThreads)
{
// Post startThread() to the event loop of all threads except MRNTT.
if (componentThread->id == ComponentThread::MRNTT) { continue; }
// JOLT the thread.
componentThread->getIoService().post([componentThread]()
{ componentThread->getIoService().stop(); }
);
// Now tell it to execute its initialization sequence.
componentThread->startThreadReq();
}
body::body->getIoService().post([]{
throw std::runtime_error("test exception");
});
body::body->getIoService().post([]{
throw std::runtime_error("test exception");
});
});
std::cout << __func__ << ": Entering event loop" << "\n";
self.getIoService().reset();
self.getIoService().run();
/* We loop here because when an exception occurs in mrntt, we need to
* both direct the mind threads to exit gracefully, and then we also
* need to post messages to our own event loop to initiate our own
* orderly exit. So we loop here to re-enter the event loop, both
* to receive the ACK messages from the mind threads, and to post
* messages to our own event loop to initiate our own orderly exit.
*/
for (self.keepLooping = true; self.keepLooping;)
{
try {
self.getIoService().reset();
self.getIoService().run();
}
catch (const std::exception& e)
{
std::cerr << self.name << ":" << __func__
<< ": Exception occurred: " << e.what() << "\n";
}
catch (...)
{
std::cerr << self.name << ":" << __func__
<< ": Unknown exception occurred" << "\n";
}
}
std::cout << __func__ << ": Exited event loop" << "\n";
}
catch (const std::exception& e)
{
+1 -2
View File
@@ -4,7 +4,7 @@
namespace smo {
int initializeSalmanoff(void)
void initializeSalmanoff(void)
{
std::cout << __func__ << ": Entered." << std::endl;
@@ -21,7 +21,6 @@ std::cerr << "About to attachAllSenseDevicesFromSpecs" << std::endl;
std::cerr << "Done attachAllSenseDevicesFromSpecs" << std::endl;
std::cout << __func__ << ": Done." << std::endl;
return 0;
}
} // namespace smo
+23
View File
@@ -1,2 +1,25 @@
#include <mind.h>
#include <componentThread.h>
namespace smo {
void Mind::initialize()
{
/* Start the threads */
for (auto& componentThread : smo::ComponentThread::componentThreads)
{
// Post startThread() to the event loop of all threads except MRNTT.
if (componentThread->id == ComponentThread::MRNTT) { continue; }
// JOLT the thread.
componentThread->getIoService().post([componentThread]()
{ componentThread->getIoService().stop(); }
);
// Now tell it to execute its initialization sequence.
componentThread->startThreadReq();
}
}
} // namespace smo