From 7f3bfec835c4c3e13f6162ecb1614a00e0fee484 Mon Sep 17 00:00:00 2001 From: Hayodea Hakol Date: Mon, 15 Sep 2025 15:01:26 -0400 Subject: [PATCH] Mind:init/finiReq: now posted to mrntt; callbacks std:bind We now have mind::initialize/finalizeReq post their requests to Mrntt instead of executing on the caller's thread context. We also fixed the way that we invoke callbacks by properly wrapping it in a std::bind. --- smocore/mind.cpp | 71 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/smocore/mind.cpp b/smocore/mind.cpp index 3241c5f..e687b1a 100644 --- a/smocore/mind.cpp +++ b/smocore/mind.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace smo { @@ -83,19 +84,21 @@ Mind::getMindThreads() const } class Mind::MindLifetimeMgmtOp -: public AsynchronousContinuation +: public TargetedAsynchronousContinuation { public: MindLifetimeMgmtOp( - Mind &parent, mindLifetimeMgmtOpCbFn callback) - : AsynchronousContinuation(callback), + Mind &parent, const std::shared_ptr &caller, + mindLifetimeMgmtOpCbFn callback) + : TargetedAsynchronousContinuation( + caller, callback), parent(parent) {} void callOriginalCbFn(bool success) { if (originalCbFn) { - originalCbFn(success); + caller->getIoService().post(std::bind(originalCbFn, success)); } } @@ -103,13 +106,12 @@ public: Mind &parent; public: - void initializeReq1( + void initializeReq1_posted( [[maybe_unused]] std::shared_ptr context ) { - std::cout << "Mrntt: All mind threads JOLTed." << "\n"; - - parent.startAllMindThreadsReq( + /* Jolt the threads, then start them */ + parent.joltAllMindThreadsReq( std::bind( &MindLifetimeMgmtOp::initializeReq2, context.get(), context)); @@ -118,16 +120,28 @@ public: void initializeReq2( [[maybe_unused]] std::shared_ptr context ) + { + std::cout << "Mrntt: All mind threads JOLTed." << "\n"; + + parent.startAllMindThreadsReq( + std::bind( + &MindLifetimeMgmtOp::initializeReq3, + context.get(), context)); + } + + void initializeReq3( + [[maybe_unused]] std::shared_ptr context + ) { std::cout << "Mrntt: All mind threads started." << "\n"; parent.body.initializeReq( std::bind( - &MindLifetimeMgmtOp::initializeReq3, + &MindLifetimeMgmtOp::initializeReq4, context.get(), context, std::placeholders::_1)); } - void initializeReq3( + void initializeReq4( [[maybe_unused]] std::shared_ptr context, bool success ) @@ -136,7 +150,17 @@ public: callOriginalCbFn(success); } - void finalizeReq1( + void finalizeReq1_posted( + [[maybe_unused]] std::shared_ptr context + ) + { + parent.body.finalizeReq( + std::bind( + &MindLifetimeMgmtOp::finalizeReq2, + context.get(), context, std::placeholders::_1)); + } + + void finalizeReq2( [[maybe_unused]] std::shared_ptr context, bool success ) @@ -153,11 +177,11 @@ public: */ parent.joltAllMindThreadsReq( std::bind( - &MindLifetimeMgmtOp::finalizeReq2, + &MindLifetimeMgmtOp::finalizeReq3, context.get(), context)); } - void finalizeReq2( + void finalizeReq3( [[maybe_unused]] std::shared_ptr context ) { @@ -165,11 +189,11 @@ public: parent.exitAllMindThreadsReq( std::bind( - &MindLifetimeMgmtOp::finalizeReq3, + &MindLifetimeMgmtOp::finalizeReq4, context.get(), context)); } - void finalizeReq3( + void finalizeReq4( [[maybe_unused]] std::shared_ptr context ) { @@ -192,25 +216,26 @@ void Mind::initializeReq(mindLifetimeMgmtOpCbFn callback) "Error: " << e.what() << "\n"; } + const auto& caller = ComponentThread::getSelf(); auto request = std::make_shared( - *this, callback); + *this, caller, callback); - /* Jolt the threads, then start them */ - joltAllMindThreadsReq( + mrntt::mrntt.thread->getIoService().post( std::bind( - &MindLifetimeMgmtOp::initializeReq1, + &MindLifetimeMgmtOp::initializeReq1_posted, request.get(), request)); } void Mind::finalizeReq(mindLifetimeMgmtOpCbFn callback) { + const auto& caller = ComponentThread::getSelf(); auto request = std::make_shared( - *this, callback); + *this, caller, callback); - body.finalizeReq( + mrntt::mrntt.thread->getIoService().post( std::bind( - &MindLifetimeMgmtOp::finalizeReq1, - request.get(), request, std::placeholders::_1)); + &MindLifetimeMgmtOp::finalizeReq1_posted, + request.get(), request)); } void Mind::distributeAndPinThreadsAcrossCpus()