diff --git a/smocore/componentThread.cpp b/smocore/componentThread.cpp index 0542294..d8763b8 100644 --- a/smocore/componentThread.cpp +++ b/smocore/componentThread.cpp @@ -106,9 +106,7 @@ void MindThread::main(MindThread& self) if (sendExceptionInd) { - mrntt::mrntt.finalizeReq( - {nullptr, std::bind( - &mrntt::marionetteFinalizeReqCb, std::placeholders::_1)}); + mrntt::mrntt.exceptionInd(); } } diff --git a/smocore/include/marionette/marionette.h b/smocore/include/marionette/marionette.h index 79a55a5..5610ccc 100644 --- a/smocore/include/marionette/marionette.h +++ b/smocore/include/marionette/marionette.h @@ -23,9 +23,12 @@ public: typedef std::function mrnttLifetimeMgmtOpCbFn; void initializeReq(Callback callback); void finalizeReq(Callback callback); + // Intentionally doesn't take a callback. + void exceptionInd(); private: class MrnttLifetimeMgmtOp; + class TerminationEvent; }; extern std::atomic exitCode; diff --git a/smocore/marionette/lifetime.cpp b/smocore/marionette/lifetime.cpp index b3ab6b6..4f544c2 100644 --- a/smocore/marionette/lifetime.cpp +++ b/smocore/marionette/lifetime.cpp @@ -117,6 +117,34 @@ public: } }; +class MarionetteComponent::TerminationEvent +: public PostedAsynchronousContinuation +{ +public: + TerminationEvent( + const std::shared_ptr &caller) + : PostedAsynchronousContinuation( + caller, {nullptr, nullptr}) + {} + +public: + void exceptionInd1_posted( + [[maybe_unused]] std::shared_ptr context + ) + { + auto self = ComponentThread::getSelf(); + if (self->id != ComponentThread::MRNTT) + { + throw std::runtime_error(std::string(__func__) + + ": Must be executed on Marionette thread"); + } + + mrntt::mrntt.finalizeReq({nullptr, std::bind( + &mrntt::marionetteFinalizeReqCb, + std::placeholders::_1)}); + } +}; + void MarionetteComponent::initializeReq( Callback callback) { @@ -157,5 +185,19 @@ void MarionetteComponent::finalizeReq( request.get(), request))); } +void MarionetteComponent::exceptionInd() +{ + auto faultyThread = ComponentThread::getSelf(); + auto mrntt = ComponentThread::getMrntt(); + + auto request = std::make_shared( + faultyThread); + + mrntt->getIoService().post( + STC(std::bind( + &TerminationEvent::exceptionInd1_posted, + request.get(), request))); +} + } // namespace mrntt } // namespace smo diff --git a/smocore/marionette/main.cpp b/smocore/marionette/main.cpp index 9a024c0..0aba7fa 100644 --- a/smocore/marionette/main.cpp +++ b/smocore/marionette/main.cpp @@ -184,9 +184,7 @@ void MarionetteThread::main(MarionetteThread& self) if (sendExceptionInd) { mrntt::exitCode = EXIT_FAILURE; - mrntt::mrntt.finalizeReq({nullptr, std::bind( - &mrntt::marionetteFinalizeReqCb, - std::placeholders::_1)}); + mrntt::mrntt.exceptionInd(); } }