diff --git a/smocore/body/body.cpp b/smocore/body/body.cpp index d9ed455..6c7e2f4 100644 --- a/smocore/body/body.cpp +++ b/smocore/body/body.cpp @@ -16,7 +16,7 @@ Body::Body(Mind &parent, const std::shared_ptr &thread) { } -BodyViralPostingInvoker Body::initializeCReq() +BodyViralPostingInvoker Body::initializeCReq() { auto self = sscl::ComponentThread::getSelf(); if (self->id != SmoThreadId::BODY) @@ -71,10 +71,19 @@ BodyViralPostingInvoker Body::initializeCReq() << attachResults.nSucceeded << " of " << attachResults.nTotal << " sense devices." << "\n"; - co_return attachResults.nSucceeded > 0; + if (attachResults.nTotal > 0 && attachResults.nSucceeded == 0) + { + throw std::runtime_error( + std::string(__func__) + + ": Failed to attach any of " + + std::to_string(attachResults.nTotal) + + " requested sense devices"); + } + + co_return; } -BodyViralPostingInvoker Body::finalizeCReq() +BodyViralPostingInvoker Body::finalizeCReq() { auto self = sscl::ComponentThread::getSelf(); if (self->id != SmoThreadId::BODY) @@ -89,15 +98,25 @@ BodyViralPostingInvoker Body::finalizeCReq() { std::cout << "Mrntt: Body component not initialized. " << "Skipping finalization." << "\n"; - co_return true; + co_return; } std::cout << "Mrntt: About to detach all sense devices." << "\n"; sscl::MultiOperationResultSet detachResults = co_await device::DeviceManager::getInstance().detachAllAttachedDeviceRolesCReq(); - std::cout << "Mrntt: Successfully detached " - << detachResults.nSucceeded << " of " << detachResults.nTotal - << " sense devices." << "\n"; + + if (detachResults.nFailed > 0) + { + std::cerr << "Mrntt: Failed to detach " + << detachResults.nFailed << " of " << detachResults.nTotal + << " sense devices." << "\n"; + } + else + { + std::cout << "Mrntt: Successfully detached " + << detachResults.nSucceeded << " of " << detachResults.nTotal + << " sense devices." << "\n"; + } std::cout << "Mrntt: About to finalize all stim buff api libs." << "\n"; co_await stim_buff::StimBuffApiManager::getInstance() @@ -106,7 +125,7 @@ BodyViralPostingInvoker Body::finalizeCReq() std::cout << "Mrntt: About to unload all stim buff api libs." << "\n"; stim_buff::StimBuffApiManager::getInstance().unloadAllStimBuffApiLibs(); - co_return detachResults.nSucceeded == detachResults.nTotal; + co_return; } } // namespace body diff --git a/smocore/include/body/body.h b/smocore/include/body/body.h index add63e6..4ce721c 100644 --- a/smocore/include/body/body.h +++ b/smocore/include/body/body.h @@ -19,8 +19,8 @@ public: Body(Mind &parent, const std::shared_ptr &thread); ~Body() = default; - BodyViralPostingInvoker initializeCReq(); - BodyViralPostingInvoker finalizeCReq(); + BodyViralPostingInvoker initializeCReq(); + BodyViralPostingInvoker finalizeCReq(); }; } // namespace body diff --git a/smocore/include/marionette/marionette.h b/smocore/include/marionette/marionette.h index b10fef7..5d98292 100644 --- a/smocore/include/marionette/marionette.h +++ b/smocore/include/marionette/marionette.h @@ -64,8 +64,6 @@ private: public: std::exception_ptr initializeLifetimeExceptionPtr; std::exception_ptr finalizeLifetimeExceptionPtr; - /** Set true only when initializeCReq completes without failure. */ - bool initializeLifetimeSucceeded = false; }; extern std::shared_ptr thread; diff --git a/smocore/include/mind.h b/smocore/include/mind.h index 25bdab6..ad01aa9 100644 --- a/smocore/include/mind.h +++ b/smocore/include/mind.h @@ -24,8 +24,8 @@ public: Mind(void); ~Mind(void) = default; - mrntt::MrnttViralNonPostingInvokerT initializeCReq(); - mrntt::MrnttViralNonPostingInvokerT finalizeCReq(); + mrntt::MrnttViralNonPostingInvokerT initializeCReq(); + mrntt::MrnttViralNonPostingInvokerT finalizeCReq(); // ComponentThread access methods std::shared_ptr getComponentThread(sscl::ThreadId id) const; diff --git a/smocore/marionette/lifetime.cpp b/smocore/marionette/lifetime.cpp index 8f6befe..d3a2078 100644 --- a/smocore/marionette/lifetime.cpp +++ b/smocore/marionette/lifetime.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace smo { @@ -31,17 +32,31 @@ void assertMarionetteThread() void MarionetteComponent::holdInitializeCReq( std::function completion) { - initializeLifetimeSucceeded = false; initializeLifetimeExceptionPtr = nullptr; initializeCReqInvoker.emplace(initializeCReq( - initializeLifetimeExceptionPtr, std::move(completion))); + initializeLifetimeExceptionPtr, + [completion = std::move(completion)]() + { + sscl::co::NonViralCompletion nvc( + mrntt.initializeLifetimeExceptionPtr); + nvc.checkAndRethrowException(); + completion(); + })); } void MarionetteComponent::holdFinalizeCReq( std::function completion) { + finalizeLifetimeExceptionPtr = nullptr; finalizeCReqInvoker.emplace(finalizeCReq( - finalizeLifetimeExceptionPtr, std::move(completion))); + finalizeLifetimeExceptionPtr, + [completion = std::move(completion)]() + { + sscl::co::NonViralCompletion nvc( + mrntt.finalizeLifetimeExceptionPtr); + nvc.checkAndRethrowException(); + completion(); + })); } MrnttNonViralPostingInvoker MarionetteComponent::initializeCReq( @@ -52,15 +67,8 @@ MrnttNonViralPostingInvoker MarionetteComponent::initializeCReq( smo::mind::globalMind = std::make_shared(); - bool mindInitialized = co_await smo::mind::globalMind->initializeCReq(); - if (!mindInitialized) - { - std::cerr << __func__ << ": Failed to initialize globalMind" - << std::endl; - co_return; - } + co_await smo::mind::globalMind->initializeCReq(); - initializeLifetimeSucceeded = true; smo::device::DeviceManager::getInstance().initializeDeviceReattacher(); // Call negtrinEventInd on the Director in the final callback @@ -77,17 +85,9 @@ MrnttNonViralPostingInvoker MarionetteComponent::finalizeCReq( smo::device::DeviceManager::getInstance().finalizeDeviceReattacher(); - if (!smo::mind::globalMind) - { - co_return; - } + if (!smo::mind::globalMind) { co_return; } - bool mindFinalized = co_await smo::mind::globalMind->finalizeCReq(); - if (!mindFinalized) - { - std::cerr << __func__ << ": globalMind finalization failed" - << std::endl; - } + co_await smo::mind::globalMind->finalizeCReq(); co_return; } diff --git a/smocore/marionette/main.cpp b/smocore/marionette/main.cpp index 6c22572..c664fcf 100644 --- a/smocore/marionette/main.cpp +++ b/smocore/marionette/main.cpp @@ -129,12 +129,7 @@ void MarionetteComponent::preLoopHook() callShutdownSalmanoff = true; holdInitializeCReq( - [] - { - marionetteInitializeReqCb( - mrntt.initializeLifetimeSucceeded - && !mrntt.initializeLifetimeExceptionPtr); - }); + [] { marionetteInitializeReqCb(true); }); std::cout << "PuppeteerThread::main: Entering event loop" << "\n"; } diff --git a/smocore/mind.cpp b/smocore/mind.cpp index f629b7e..bfabde3 100644 --- a/smocore/mind.cpp +++ b/smocore/mind.cpp @@ -106,7 +106,7 @@ Mind::getMindThreads() const return mindThreads; } -mrntt::MrnttViralNonPostingInvokerT Mind::initializeCReq() +mrntt::MrnttViralNonPostingInvokerT Mind::initializeCReq() { try { @@ -125,20 +125,16 @@ mrntt::MrnttViralNonPostingInvokerT Mind::initializeCReq() co_await startAllPuppetThreadsCReq(); std::cout << "Mrntt: All mind threads started." << "\n"; - bool bodyInitialized = co_await body.initializeCReq(); + co_await body.initializeCReq(); std::cout << "Mrntt: Body component initialized." << "\n"; - co_return bodyInitialized; + co_return; } -mrntt::MrnttViralNonPostingInvokerT Mind::finalizeCReq() +mrntt::MrnttViralNonPostingInvokerT Mind::finalizeCReq() { - bool bodyFinalized = co_await body.finalizeCReq(); - if (!bodyFinalized) { - std::cerr << "Mrntt: Body component failed to finalize." << "\n"; - } else { - std::cout << "Mrntt: Body component finalized." << "\n"; - } + co_await body.finalizeCReq(); + std::cout << "Mrntt: Body component finalized." << "\n"; co_await joltAllPuppetThreadsCReq(); std::cout << "Mrntt: All mind threads JOLTed for finalization." << "\n"; @@ -146,7 +142,7 @@ mrntt::MrnttViralNonPostingInvokerT Mind::finalizeCReq() co_await exitAllPuppetThreadsCReq(); std::cout << "Mrntt: All mind threads exited." << "\n"; - co_return bodyFinalized; + co_return; } } // namespace smo