diff --git a/smocore/componentThread.cpp b/smocore/componentThread.cpp index a6e1a66..070802b 100644 --- a/smocore/componentThread.cpp +++ b/smocore/componentThread.cpp @@ -94,7 +94,11 @@ void MindThread::main(MindThread& self) } if (sendExceptionInd) - { mrntt::thread->exceptionInd(self.shared_from_this()); } + { + mrntt::mrntt.finalizeReq( + std::bind( + &mrntt::marionetteFinalizeReqCb, std::placeholders::_1)); + } } std::cout << self.name << ":" << __func__ << ": Exited event loop" << "\n"; @@ -304,119 +308,7 @@ void MindThread::resumeThreadReq(threadLifetimeMgmtOpCbFn callback) pause_io_service.post( std::bind( - &ThreadLifetimeMgmtOp::resumeThreadReq1, - request.get(), request)); -} - -class MindThread::MindShutdownIndOp -: public TargetedAsynchronousContinuation -{ -public: - MindShutdownIndOp( - const std::shared_ptr &caller, - mindShutdownIndOpCbFn callback) - : TargetedAsynchronousContinuation( - caller, callback) - {} - -public: - void mindShutdownInd1_exception( - [[maybe_unused]] std::shared_ptr context - ) - { - std::cerr << "Mrntt: Exception occurred: in thread " - << context->caller->name << ". Killing Salmanoff." << "\n"; - - /** EXPLANATION: - * An exception has occurred in one of a mind's threads. We need to - * shut down all of that particular mind's threads. - */ - smo::mind::globalMind->finalizeReq( - std::bind( - &MindShutdownIndOp::mindShutdownInd2, - context.get(), context)); - } - - void mindShutdownInd1_userShutdown( - [[maybe_unused]] std::shared_ptr context - ) - { - std::cerr << "Mrntt: User requested shutdown (SIGINT)." - << " Killing Salmanoff." << "\n"; - - /** EXPLANATION: - * A user has requested a shutdown. We need to shut down all of the - * threads in all running Minds. - * - * FIXME: - * So this should ideally be a loop - * through all running Minds, calling finalizeReq on each one. - */ - smo::mind::globalMind->finalizeReq( - std::bind( - &MindShutdownIndOp::mindShutdownInd2, - context.get(), context)); - } - - void mindShutdownInd2( - [[maybe_unused]] std::shared_ptr context - ) - { - std::cout << "Mrntt: About to exit marionette loop." << "\n"; - /** FIXME: - * When we eventually support multiple minds, we should remove this - * since it causes marionette to exit, even if there are other minds - * that are still running. - */ - smo::mrntt::exitMarionetteLoop(); - } - -}; - -/* This shouldn't take a callback because the caller shouldn't expect to - * Mrntt to send a reply signal to it. Sending this Indication means that - * Mrntt will send the calling thread an exitThreadReq. When the caller - * processes that exitThreadReq(), the caller will exit its event loop and then - * terminate. - * - * Even if Mrntt sent a RDY response, the caller shouldn't actually be executing - * any longer to receive it anyway. - */ -void ComponentThread::exceptionInd( - const std::shared_ptr &faultyThread -) -{ - if (this->id != ComponentThread::MRNTT) - { - throw std::runtime_error(std::string(__func__) - + ": invoked on non-mrntt thread " + faultyThread->name); - } - - auto request = std::make_shared( - faultyThread, nullptr); - - // Post the exception to the mrntt thread. - this->getIoService().post( - std::bind( - &MindThread::MindShutdownIndOp::mindShutdownInd1_exception, - request.get(), request)); -} - -void ComponentThread::userShutdownInd() -{ - if (this->id != ComponentThread::MRNTT) - { - throw std::runtime_error(std::string(__func__) - + ": invoked on non-mrntt thread " + this->name); - } - - auto request = std::make_shared( - ComponentThread::getMrntt(), nullptr); - - // Post the user shutdown to the mrntt thread. - this->getIoService().post( - std::bind( - &MindThread::MindShutdownIndOp::mindShutdownInd1_userShutdown, + &ThreadLifetimeMgmtOp::resumeThreadReq1_posted, request.get(), request)); } diff --git a/smocore/include/marionette/marionette.h b/smocore/include/marionette/marionette.h index a2ed092..70c21a0 100644 --- a/smocore/include/marionette/marionette.h +++ b/smocore/include/marionette/marionette.h @@ -15,6 +15,7 @@ namespace mrntt { extern std::atomic exitCode; void exitMarionetteLoop(); +void marionetteFinalizeReqCb(bool success); extern mrntt::MarionetteComponent mrntt; } // namespace mrntt