Split CompThread=>MindThr+MrnttThr; alloc globalMind in mrnttMain

We now allocate globalMind locally inside of marionetteMain. Why?

Before now, we had an asymmetric threading situation where the
globalMind's threads were initialized at during global constructor
invocation and not on demand. This meant that we had to shut down
those threads even if we had never got to the point of calling
Mind::initializeReq.

This significantly complicated our shutdown sequence since we had
to factor in the lifetime of the std::thread objects inside of the
ComponentThreads which were inside of the globalMind object.

Now, if we hadn't called Mind::initializeReq, we don't have to
perform any Mind::finalizeReq or adjacent operations. Shutdown is
symmetrically mirrored against the operations we actually performed
during execution.

We introduced some complexity by splitting ComponentThreads into
two derivative types (MindThread and MarionetteThread) but I think
in the long term we'll be able to massage this split into a much
cleaner situation overall.
This commit is contained in:
2025-09-14 11:07:05 -04:00
parent 83af74f4be
commit da0ef64f62
6 changed files with 152 additions and 107 deletions
+26 -26
View File
@@ -13,17 +13,18 @@ namespace smo {
thread_local std::shared_ptr<ComponentThread> thisComponentThread;
namespace mrntt {
extern std::shared_ptr<ComponentThread> mrntt;
}
// Implementation of static method
std::shared_ptr<ComponentThread> ComponentThread::getMrntt()
std::shared_ptr<MarionetteThread> ComponentThread::getMrntt()
{
return mrntt::mrntt;
}
void ComponentThread::initializeTls(void)
void MarionetteThread::initializeTls(void)
{
thisComponentThread = shared_from_this();
}
void MindThread::initializeTls(void)
{
thisComponentThread = shared_from_this();
}
@@ -39,7 +40,7 @@ const std::shared_ptr<ComponentThread> ComponentThread::getSelf(void)
return thisComponentThread;
}
void ComponentThread::main(ComponentThread& self)
void MindThread::main(MindThread& self)
{
if (OptionParser::getOptions().verbose)
@@ -98,13 +99,13 @@ void ComponentThread::main(ComponentThread& self)
std::cout << self.name << ":" << __func__ << ": Exited event loop" << "\n";
}
class ComponentThread::ThreadLifetimeMgmtOp
class MindThread::ThreadLifetimeMgmtOp
: public TargetedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>
{
public:
ThreadLifetimeMgmtOp(
const std::shared_ptr<ComponentThread> &caller,
const std::shared_ptr<ComponentThread> &target,
const std::shared_ptr<MindThread> &target,
threadLifetimeMgmtOpCbFn callback)
: TargetedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>(
caller, callback),
@@ -119,7 +120,7 @@ public:
}
public:
const std::shared_ptr<ComponentThread> target;
const std::shared_ptr<MindThread> target;
public:
void joltThreadReq1(
@@ -204,7 +205,7 @@ void ComponentThread::cleanup(void)
this->keepLooping = false;
}
void ComponentThread::joltThreadReq(threadLifetimeMgmtOpCbFn callback)
void MindThread::joltThreadReq(threadLifetimeMgmtOpCbFn callback)
{
/** EXPLANATION:
* We can't use shared_from_this() here because JOLTing occurs prior to
@@ -227,9 +228,8 @@ void ComponentThread::joltThreadReq(threadLifetimeMgmtOpCbFn callback)
+ ": invoked on mrntt thread");
}
std::shared_ptr<ComponentThread>
mrntt = mrntt::mrntt,
target = parent.getComponentThread(id);
std::shared_ptr<MarionetteThread> mrntt = mrntt::mrntt;
std::shared_ptr<MindThread> target = getParent().getComponentThread(id);
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
mrntt, target, callback);
@@ -241,7 +241,7 @@ void ComponentThread::joltThreadReq(threadLifetimeMgmtOpCbFn callback)
}
// Thread management method implementations
void ComponentThread::startThreadReq(threadLifetimeMgmtOpCbFn callback)
void MindThread::startThreadReq(threadLifetimeMgmtOpCbFn callback)
{
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
@@ -253,7 +253,7 @@ void ComponentThread::startThreadReq(threadLifetimeMgmtOpCbFn callback)
request.get(), request));
}
void ComponentThread::exitThreadReq(threadLifetimeMgmtOpCbFn callback)
void MindThread::exitThreadReq(threadLifetimeMgmtOpCbFn callback)
{
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
@@ -264,13 +264,13 @@ void ComponentThread::exitThreadReq(threadLifetimeMgmtOpCbFn callback)
&ThreadLifetimeMgmtOp::exitThreadReq1_mainQueue,
request.get(), request));
this->pause_io_service.post(
pause_io_service.post(
std::bind(
&ThreadLifetimeMgmtOp::exitThreadReq1_pauseQueue,
request.get(), request));
}
void ComponentThread::pauseThreadReq(threadLifetimeMgmtOpCbFn callback)
void MindThread::pauseThreadReq(threadLifetimeMgmtOpCbFn callback)
{
if (id == ComponentThread::MRNTT)
{
@@ -288,7 +288,7 @@ void ComponentThread::pauseThreadReq(threadLifetimeMgmtOpCbFn callback)
request.get(), request));
}
void ComponentThread::resumeThreadReq(threadLifetimeMgmtOpCbFn callback)
void MindThread::resumeThreadReq(threadLifetimeMgmtOpCbFn callback)
{
if (id == ComponentThread::MRNTT)
{
@@ -301,13 +301,13 @@ void ComponentThread::resumeThreadReq(threadLifetimeMgmtOpCbFn callback)
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, shared_from_this(), callback);
this->pause_io_service.post(
pause_io_service.post(
std::bind(
&ThreadLifetimeMgmtOp::resumeThreadReq1,
request.get(), request));
}
class ComponentThread::MindShutdownIndOp
class MindThread::MindShutdownIndOp
: public TargetedAsynchronousContinuation<mindShutdownIndOpCbFn>
{
public:
@@ -391,13 +391,13 @@ void ComponentThread::exceptionInd(
+ ": invoked on non-mrntt thread " + faultyThread->name);
}
auto request = std::make_shared<MindShutdownIndOp>(
auto request = std::make_shared<MindThread::MindShutdownIndOp>(
faultyThread, nullptr);
// Post the exception to the mrntt thread.
this->getIoService().post(
std::bind(
&MindShutdownIndOp::mindShutdownInd1_exception,
&MindThread::MindShutdownIndOp::mindShutdownInd1_exception,
request.get(), request));
}
@@ -409,13 +409,13 @@ void ComponentThread::userShutdownInd()
+ ": invoked on non-mrntt thread " + this->name);
}
auto request = std::make_shared<MindShutdownIndOp>(
auto request = std::make_shared<MindThread::MindShutdownIndOp>(
ComponentThread::getMrntt(), nullptr);
// Post the user shutdown to the mrntt thread.
this->getIoService().post(
std::bind(
&MindShutdownIndOp::mindShutdownInd1_userShutdown,
&MindThread::MindShutdownIndOp::mindShutdownInd1_userShutdown,
request.get(), request));
}
@@ -442,7 +442,7 @@ int ComponentThread::getAvailableCpuCount()
return cpuCount;
}
void ComponentThread::pinToCpu(int cpuId)
void MindThread::pinToCpu(int cpuId)
{
if (cpuId < 0)
{