Use ref in ComponentThread::joltThreadReq

This commit is contained in:
2025-12-26 13:46:28 -04:00
parent bfe5eb12af
commit 2222491c21
3 changed files with 29 additions and 44 deletions
@@ -125,14 +125,8 @@ public:
* JOLTing is the mechanism that allows threads to enter their main * JOLTing is the mechanism that allows threads to enter their main
* event loops and set up TLS vars after all global constructors have * event loops and set up TLS vars after all global constructors have
* completed. This prevents race conditions during system startup. * completed. This prevents race conditions during system startup.
*
* @param selfPtr Shared pointer to this thread (required because TLS
* isn't set up yet, so shared_from_this() can't be used)
* @param callback Callback to invoke when JOLT completes
*/ */
void joltThreadReq( void joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
const std::shared_ptr<PuppetThread>& selfPtr,
Callback<threadLifetimeMgmtOpCbFn> callback);
// CPU management methods // CPU management methods
void pinToCpu(int cpuId); void pinToCpu(int cpuId);
+26 -31
View File
@@ -66,7 +66,7 @@ class PuppetThread::ThreadLifetimeMgmtOp
public: public:
ThreadLifetimeMgmtOp( ThreadLifetimeMgmtOp(
const std::shared_ptr<ComponentThread> &caller, const std::shared_ptr<ComponentThread> &caller,
const std::shared_ptr<PuppetThread> &target, PuppetThread& target,
Callback<threadLifetimeMgmtOpCbFn> callback) Callback<threadLifetimeMgmtOpCbFn> callback)
: PostedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>( : PostedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>(
caller, callback), caller, callback),
@@ -74,18 +74,18 @@ public:
{} {}
public: public:
const std::shared_ptr<PuppetThread> target; PuppetThread& target;
public: public:
void joltThreadReq1_posted( void joltThreadReq1_posted(
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"JOLT request." "JOLT request."
<< "\n"; << "\n";
target->io_service.stop(); target.io_service.stop();
callOriginalCb(); callOriginalCb();
} }
@@ -93,7 +93,7 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"startThread." "startThread."
<< "\n"; << "\n";
@@ -107,11 +107,11 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"exitThread (main queue)." << "\n"; "exitThread (main queue)." << "\n";
target->cleanup(); target.cleanup();
target->io_service.stop(); target.io_service.stop();
callOriginalCb(); callOriginalCb();
} }
@@ -119,12 +119,12 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"exitThread (pause queue)."<< "\n"; "exitThread (pause queue)."<< "\n";
target->cleanup(); target.cleanup();
target->pause_io_service.stop(); target.pause_io_service.stop();
target->io_service.stop(); target.io_service.stop();
callOriginalCb(); callOriginalCb();
} }
@@ -132,7 +132,7 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"pauseThread." << "\n"; "pauseThread." << "\n";
/* We have to invoke the callback here before moving on because /* We have to invoke the callback here before moving on because
@@ -140,18 +140,18 @@ public:
* have a chance to invoke the callback until it's unblocked. * have a chance to invoke the callback until it's unblocked.
*/ */
callOriginalCb(); callOriginalCb();
target->pause_io_service.reset(); target.pause_io_service.reset();
target->pause_io_service.run(); target.pause_io_service.run();
} }
void resumeThreadReq1_posted( void resumeThreadReq1_posted(
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context [[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
) )
{ {
std::cout << __func__ << ": Thread '" << target->name << "': handling " std::cout << __func__ << ": Thread '" << target.name << "': handling "
"resumeThread." << "\n"; "resumeThread." << "\n";
target->pause_io_service.stop(); target.pause_io_service.stop();
callOriginalCb(); callOriginalCb();
} }
}; };
@@ -161,9 +161,7 @@ void ComponentThread::cleanup(void)
this->keepLooping = false; this->keepLooping = false;
} }
void PuppetThread::joltThreadReq( void PuppetThread::joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
const std::shared_ptr<PuppetThread>& selfPtr,
Callback<threadLifetimeMgmtOpCbFn> callback)
{ {
/** EXPLANATION: /** EXPLANATION:
* We can't use shared_from_this() here because JOLTing occurs prior to * We can't use shared_from_this() here because JOLTing occurs prior to
@@ -177,8 +175,9 @@ void PuppetThread::joltThreadReq(
* CRT main() function invokes on the mrntt thread is special since it * CRT main() function invokes on the mrntt thread is special since it
* supplies cmdline args and envp. * supplies cmdline args and envp.
* *
* To obtain a sh_ptr to the target thread, we use the selfPtr parameter * To obtain a ref to the target thread, we just use 'this'. Since
* passed in by the caller. * the operation is posted to this thread's io_service (which is a member
* of this object), the object must be alive when the operation executes.
*/ */
if (id == spinscale::marionetteThreadId) if (id == spinscale::marionetteThreadId)
{ {
@@ -189,7 +188,7 @@ void PuppetThread::joltThreadReq(
std::shared_ptr<MarionetteThread> mrntt = mrntt::thread; std::shared_ptr<MarionetteThread> mrntt = mrntt::thread;
auto request = std::make_shared<ThreadLifetimeMgmtOp>( auto request = std::make_shared<ThreadLifetimeMgmtOp>(
mrntt, selfPtr, callback); mrntt, *this, callback);
this->getIoService().post( this->getIoService().post(
STC(std::bind( STC(std::bind(
@@ -202,8 +201,7 @@ void PuppetThread::startThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{ {
std::shared_ptr<ComponentThread> caller = getSelf(); std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>( auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()), caller, *this, callback);
callback);
this->getIoService().post( this->getIoService().post(
STC(std::bind( STC(std::bind(
@@ -215,8 +213,7 @@ void PuppetThread::exitThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{ {
std::shared_ptr<ComponentThread> caller = getSelf(); std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>( auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()), caller, *this, callback);
callback);
this->getIoService().post( this->getIoService().post(
STC(std::bind( STC(std::bind(
@@ -239,8 +236,7 @@ void PuppetThread::pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
std::shared_ptr<ComponentThread> caller = getSelf(); std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>( auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()), caller, *this, callback);
callback);
this->getIoService().post( this->getIoService().post(
STC(std::bind( STC(std::bind(
@@ -259,8 +255,7 @@ void PuppetThread::resumeThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
// Post to the pause_io_service to unblock the paused thread // Post to the pause_io_service to unblock the paused thread
std::shared_ptr<ComponentThread> caller = getSelf(); std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>( auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()), caller, *this, callback);
callback);
pause_io_service.post( pause_io_service.post(
STC(std::bind( STC(std::bind(
-4
View File
@@ -339,11 +339,7 @@ void Mind::joltAllMindThreadsReq(
for (auto& thread : componentThreads) for (auto& thread : componentThreads)
{ {
std::shared_ptr<PuppetThread> puppetThread =
std::static_pointer_cast<PuppetThread>(thread);
thread->joltThreadReq( thread->joltThreadReq(
puppetThread,
{request, std::bind( {request, std::bind(
&MindThreadLifetimeMgmtOp::joltAllMindThreadsReq1, &MindThreadLifetimeMgmtOp::joltAllMindThreadsReq1,
request.get(), request)}); request.get(), request)});