diff --git a/libspinscale/include/spinscale/componentThread.h b/libspinscale/include/spinscale/componentThread.h index 494c28e..87a8935 100644 --- a/libspinscale/include/spinscale/componentThread.h +++ b/libspinscale/include/spinscale/componentThread.h @@ -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& selfPtr, - Callback callback); + void joltThreadReq(Callback callback); // CPU management methods void pinToCpu(int cpuId); @@ -163,8 +157,8 @@ extern std::shared_ptr 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 diff --git a/libspinscale/src/componentThread.cpp b/libspinscale/src/componentThread.cpp index 8625b38..f181301 100644 --- a/libspinscale/src/componentThread.cpp +++ b/libspinscale/src/componentThread.cpp @@ -66,7 +66,7 @@ class PuppetThread::ThreadLifetimeMgmtOp public: ThreadLifetimeMgmtOp( const std::shared_ptr &caller, - const std::shared_ptr &target, + PuppetThread& target, Callback callback) : PostedAsynchronousContinuation( caller, callback), @@ -74,18 +74,18 @@ public: {} public: - const std::shared_ptr target; + PuppetThread& target; public: void joltThreadReq1_posted( [[maybe_unused]] std::shared_ptr 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 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 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 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 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 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& selfPtr, - Callback callback) +void PuppetThread::joltThreadReq(Callback 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 mrntt = mrntt::thread; auto request = std::make_shared( - mrntt, selfPtr, callback); + mrntt, *this, callback); this->getIoService().post( STC(std::bind( @@ -202,8 +201,7 @@ void PuppetThread::startThreadReq(Callback callback) { std::shared_ptr caller = getSelf(); auto request = std::make_shared( - caller, std::static_pointer_cast(shared_from_this()), - callback); + caller, *this, callback); this->getIoService().post( STC(std::bind( @@ -215,8 +213,7 @@ void PuppetThread::exitThreadReq(Callback callback) { std::shared_ptr caller = getSelf(); auto request = std::make_shared( - caller, std::static_pointer_cast(shared_from_this()), - callback); + caller, *this, callback); this->getIoService().post( STC(std::bind( @@ -239,8 +236,7 @@ void PuppetThread::pauseThreadReq(Callback callback) std::shared_ptr caller = getSelf(); auto request = std::make_shared( - caller, std::static_pointer_cast(shared_from_this()), - callback); + caller, *this, callback); this->getIoService().post( STC(std::bind( @@ -259,8 +255,7 @@ void PuppetThread::resumeThreadReq(Callback callback) // Post to the pause_io_service to unblock the paused thread std::shared_ptr caller = getSelf(); auto request = std::make_shared( - caller, std::static_pointer_cast(shared_from_this()), - callback); + caller, *this, callback); pause_io_service.post( STC(std::bind( diff --git a/smocore/mind.cpp b/smocore/mind.cpp index 151e4d3..1fba835 100644 --- a/smocore/mind.cpp +++ b/smocore/mind.cpp @@ -339,11 +339,7 @@ void Mind::joltAllMindThreadsReq( for (auto& thread : componentThreads) { - std::shared_ptr puppetThread = - std::static_pointer_cast(thread); - thread->joltThreadReq( - puppetThread, {request, std::bind( &MindThreadLifetimeMgmtOp::joltAllMindThreadsReq1, request.get(), request)});