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
* event loops and set up TLS vars after all global constructors have
* 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(
const std::shared_ptr<PuppetThread>& selfPtr,
Callback<threadLifetimeMgmtOpCbFn> callback);
void joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
// CPU management methods
void pinToCpu(int cpuId);
@@ -163,8 +157,8 @@ extern std::shared_ptr<MarionetteThread> thread;
// Forward declaration for spinscale namespace functions and variables
// Must be after smo namespace so ThreadId is defined
namespace spinscale {
extern smo::ThreadId marionetteThreadId;
void setMarionetteThreadId(smo::ThreadId id);
extern smo::ThreadId marionetteThreadId;
void setMarionetteThreadId(smo::ThreadId id);
}
#endif // COMPONENT_THREAD_H
+26 -31
View File
@@ -66,7 +66,7 @@ class PuppetThread::ThreadLifetimeMgmtOp
public:
ThreadLifetimeMgmtOp(
const std::shared_ptr<ComponentThread> &caller,
const std::shared_ptr<PuppetThread> &target,
PuppetThread& target,
Callback<threadLifetimeMgmtOpCbFn> callback)
: PostedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>(
caller, callback),
@@ -74,18 +74,18 @@ public:
{}
public:
const std::shared_ptr<PuppetThread> target;
PuppetThread& target;
public:
void joltThreadReq1_posted(
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
)
{
std::cout << __func__ << ": Thread '" << target->name << "': handling "
std::cout << __func__ << ": Thread '" << target.name << "': handling "
"JOLT request."
<< "\n";
target->io_service.stop();
target.io_service.stop();
callOriginalCb();
}
@@ -93,7 +93,7 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
)
{
std::cout << __func__ << ": Thread '" << target->name << "': handling "
std::cout << __func__ << ": Thread '" << target.name << "': handling "
"startThread."
<< "\n";
@@ -107,11 +107,11 @@ public:
[[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";
target->cleanup();
target->io_service.stop();
target.cleanup();
target.io_service.stop();
callOriginalCb();
}
@@ -119,12 +119,12 @@ public:
[[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";
target->cleanup();
target->pause_io_service.stop();
target->io_service.stop();
target.cleanup();
target.pause_io_service.stop();
target.io_service.stop();
callOriginalCb();
}
@@ -132,7 +132,7 @@ public:
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
)
{
std::cout << __func__ << ": Thread '" << target->name << "': handling "
std::cout << __func__ << ": Thread '" << target.name << "': handling "
"pauseThread." << "\n";
/* 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.
*/
callOriginalCb();
target->pause_io_service.reset();
target->pause_io_service.run();
target.pause_io_service.reset();
target.pause_io_service.run();
}
void resumeThreadReq1_posted(
[[maybe_unused]] std::shared_ptr<ThreadLifetimeMgmtOp> context
)
{
std::cout << __func__ << ": Thread '" << target->name << "': handling "
std::cout << __func__ << ": Thread '" << target.name << "': handling "
"resumeThread." << "\n";
target->pause_io_service.stop();
target.pause_io_service.stop();
callOriginalCb();
}
};
@@ -161,9 +161,7 @@ void ComponentThread::cleanup(void)
this->keepLooping = false;
}
void PuppetThread::joltThreadReq(
const std::shared_ptr<PuppetThread>& selfPtr,
Callback<threadLifetimeMgmtOpCbFn> callback)
void PuppetThread::joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{
/** EXPLANATION:
* 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
* supplies cmdline args and envp.
*
* To obtain a sh_ptr to the target thread, we use the selfPtr parameter
* passed in by the caller.
* To obtain a ref to the target thread, we just use 'this'. Since
* 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)
{
@@ -189,7 +188,7 @@ void PuppetThread::joltThreadReq(
std::shared_ptr<MarionetteThread> mrntt = mrntt::thread;
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
mrntt, selfPtr, callback);
mrntt, *this, callback);
this->getIoService().post(
STC(std::bind(
@@ -202,8 +201,7 @@ void PuppetThread::startThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()),
callback);
caller, *this, callback);
this->getIoService().post(
STC(std::bind(
@@ -215,8 +213,7 @@ void PuppetThread::exitThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()),
callback);
caller, *this, callback);
this->getIoService().post(
STC(std::bind(
@@ -239,8 +236,7 @@ void PuppetThread::pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()),
callback);
caller, *this, callback);
this->getIoService().post(
STC(std::bind(
@@ -259,8 +255,7 @@ void PuppetThread::resumeThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
// Post to the pause_io_service to unblock the paused thread
std::shared_ptr<ComponentThread> caller = getSelf();
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
caller, std::static_pointer_cast<PuppetThread>(shared_from_this()),
callback);
caller, *this, callback);
pause_io_service.post(
STC(std::bind(
-4
View File
@@ -339,11 +339,7 @@ void Mind::joltAllMindThreadsReq(
for (auto& thread : componentThreads)
{
std::shared_ptr<PuppetThread> puppetThread =
std::static_pointer_cast<PuppetThread>(thread);
thread->joltThreadReq(
puppetThread,
{request, std::bind(
&MindThreadLifetimeMgmtOp::joltAllMindThreadsReq1,
request.get(), request)});