diff --git a/libspinscale b/libspinscale index e6a924a..a7521f3 160000 --- a/libspinscale +++ b/libspinscale @@ -1 +1 @@ -Subproject commit e6a924a3f7f447d8b0da4dbbbd813797149da2c6 +Subproject commit a7521f376037dd9f5aaae2b9dfc98d793a5f1a54 diff --git a/main.cpp b/main.cpp index 1bbe385..c9055c1 100644 --- a/main.cpp +++ b/main.cpp @@ -2,29 +2,33 @@ #include #include #include -#include +#include +#include int main(int argc, char *argv[], char *envp[]) { - // Set the pptr thread ID before using any ComponentThread functionality - sscl::pptr::setPuppeteerThreadId(smo::SmoThreadId::MRNTT); + /* Set the pptr thread ID and thread before using any ComponentThread + * functionality. + */ + sscl::ComponentThread::setPuppeteerThreadId(smo::SmoThreadId::MRNTT); + sscl::ComponentThread::setPuppeteerThread(smo::mrntt::thread); pthread_setname_np(pthread_self(), "smo:CRT:main"); /* We don't do anything inside of main() * Main merely waits for the marionette thread to exit. */ std::cout << "CRT:" << __func__ << ": about to JOLT Mrntt with cmdline args" << '\n'; - sscl::pptr::thread->getIoService().post( + smo::mrntt::thread->getIoService().post( [argc, argv, envp]() { std::cout << "Mrntt:" << __func__ << ":JOLTED: setting cmdline args" << '\n'; sscl::CrtCommandLineArgs::set(argc, argv, envp); - sscl::pptr::thread->getIoService().stop(); + smo::mrntt::thread->getIoService().stop(); } ); - sscl::pptr::thread->thread.join(); + smo::mrntt::thread->thread.join(); std::cout << "CRT:" << __func__ << ": Mrntt exited with code '" << sscl::pptr::exitCode << "'\n"; return sscl::pptr::exitCode; diff --git a/smocore/CMakeLists.txt b/smocore/CMakeLists.txt index 8579cc5..a1f3018 100644 --- a/smocore/CMakeLists.txt +++ b/smocore/CMakeLists.txt @@ -5,7 +5,7 @@ include(${CMAKE_SOURCE_DIR}/cmake/flexYacc.cmake) add_library(smocore STATIC # Core files mind.cpp - mindThread.cpp + mindComponent.cpp componentThread.cpp opts.cpp diff --git a/smocore/body/body.cpp b/smocore/body/body.cpp index d9266c8..a00935d 100644 --- a/smocore/body/body.cpp +++ b/smocore/body/body.cpp @@ -15,7 +15,7 @@ namespace smo { namespace body { Body::Body(Mind &parent, const std::shared_ptr &thread) -: sscl::PuppetComponent(static_cast(parent), thread) +: MindComponent(static_cast(parent), thread) { } diff --git a/smocore/componentThread.cpp b/smocore/componentThread.cpp index 2f3f9f9..dd3122b 100644 --- a/smocore/componentThread.cpp +++ b/smocore/componentThread.cpp @@ -30,61 +30,4 @@ std::string ComponentThread::getThreadName(sscl::ThreadId id) return threadNames[static_cast(smoId)]; } -void sscl::PuppetThread::main(sscl::PuppetThread& self) -{ - std::string threadName = "smo:" + self.name; - pthread_setname_np(pthread_self(), threadName.c_str()); - - self.getIoService().run(); - self.initializeTls(); - - std::cout << self.name << ":" << __func__ << ": Entering event loop" <<"\n"; - - /* We loop here because when an exception is caught, we need to first catch - * it in the catch blocks. We bubble the exception to mrntt in the catch - * blocks, and then we loop here to await control messages from mrntt. - * - * We can't just exit on our own. Rather, we must wait for mrntt to tell us - * to exit. When we wish to finally exit, we set keepLooping to false. - */ - for (self.keepLooping = true; self.keepLooping;) - { - bool sendExceptionInd = false; - - try { - /** EXPLANATION: - * This reset() call is crucial for async bridging patterns - * to work. - * When the outermost thread's io_service is stop()ped (e.g., - * from JOLT sequence), it won't process any new work until - * reset() is called, even if nested async operations try to - * post work to it. This means async bridges invoked from - * the outermost thread main sequence won't work until this - * reset() call. - */ - self.getIoService().reset(); - self.getIoService().run(); - } - catch (const std::exception& e) - { - sendExceptionInd = true; - std::cerr << self.name << ":" << __func__ - << ": Exception occurred: " << e.what() << "\n"; - } - catch (...) - { - sendExceptionInd = true; - std::cerr << self.name << ":" << __func__ - << ": Unknown exception occurred" << "\n"; - } - - if (sendExceptionInd) - { - self.handleException(); - } - } - - std::cout << self.name << ":" << __func__ << ": Exited event loop" << "\n"; -} - } // namespace sscl diff --git a/smocore/deviceManager/deviceManager.cpp b/smocore/deviceManager/deviceManager.cpp index 098e73e..6906c95 100644 --- a/smocore/deviceManager/deviceManager.cpp +++ b/smocore/deviceManager/deviceManager.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include namespace smo { @@ -304,7 +304,7 @@ void DeviceManager::newDeviceAttachmentSpecInd( }); NewDeviceAttachmentSpecInd::LockerAndInvoker lockvoker( - *request, sscl::pptr::mrntt.thread, + *request, mrntt::mrntt.thread, std::bind( &NewDeviceAttachmentSpecInd::newDeviceAttachmentSpecInd1_posted, request.get(), request)); @@ -323,7 +323,7 @@ void DeviceManager::removeDeviceAttachmentSpecReq( }); RemoveDeviceAttachmentSpecReq::LockerAndInvoker lockvoker( - *request, sscl::pptr::mrntt.thread, + *request, mrntt::mrntt.thread, std::bind( &RemoveDeviceAttachmentSpecReq ::removeDeviceAttachmentSpecReq1_posted, @@ -500,7 +500,7 @@ void DeviceManager::attachStimBuffDeviceReq( }); AttachStimBuffDeviceReq::LockerAndInvoker lockvoker( - *request, sscl::pptr::mrntt.thread, + *request, mrntt::mrntt.thread, std::bind( &AttachStimBuffDeviceReq::attachStimBuffDeviceReq1_posted, request.get(), request)); @@ -535,7 +535,7 @@ void DeviceManager::detachStimBuffDeviceReq( }); DetachStimBuffDeviceReq::LockerAndInvoker lockvoker( - *request, sscl::pptr::mrntt.thread, + *request, mrntt::mrntt.thread, std::bind( &DetachStimBuffDeviceReq::detachStimBuffDeviceReq1_posted, request.get(), request)); @@ -626,7 +626,7 @@ void DeviceManager::attachAllUnattachedDevicesFromReq( auto request = std::make_shared( specs->size(), specs, caller, std::move(cb)); - sscl::pptr::mrntt.thread->getIoService().post( + mrntt::mrntt.thread->getIoService().post( STC(std::bind( &AttachAllUnattachedDevicesFromReq ::attachAllUnattachedDevicesFromReq1_posted, @@ -723,7 +723,7 @@ void DeviceManager::attachAllUnattachedDevicesFromKnownListReq( }); AttachAllUnattachedDevicesFromKnownListReq::LockerAndInvoker lockvoker( - *request, sscl::pptr::mrntt.thread, + *request, mrntt::mrntt.thread, std::bind( &AttachAllUnattachedDevicesFromKnownListReq ::attachAllUnattachedDevicesFromKnownListReq1_posted, @@ -807,7 +807,7 @@ void DeviceManager::detachAllAttachedDeviceRoles( DeviceManager::getInstance().attachedDeviceRoles.size(), caller, std::move(cb)); - sscl::pptr::mrntt.thread->getIoService().post( + mrntt::mrntt.thread->getIoService().post( STC(std::bind( &DetachAllAttachedDeviceRoles::detachAllAttachedDeviceRoles1_posted, request.get(), request))); @@ -816,7 +816,7 @@ void DeviceManager::detachAllAttachedDeviceRoles( void DeviceManager::initializeDeviceReattacher() { deviceReattacher = std::make_unique( - *this, sscl::pptr::mrntt.thread); + *this, mrntt::mrntt.thread); deviceReattacher->start(); } diff --git a/smocore/director/director.cpp b/smocore/director/director.cpp index c210716..316fff6 100644 --- a/smocore/director/director.cpp +++ b/smocore/director/director.cpp @@ -8,7 +8,7 @@ namespace director { Director::Director( Mind &parent, const std::shared_ptr &thread) -: sscl::PuppetComponent(static_cast(parent), thread) +: MindComponent(static_cast(parent), thread) { } diff --git a/smocore/include/body/body.h b/smocore/include/body/body.h index e6c2a7d..78fb6df 100644 --- a/smocore/include/body/body.h +++ b/smocore/include/body/body.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -13,7 +14,7 @@ class Mind; namespace body { class Body -: public sscl::PuppetComponent +: public MindComponent { public: Body(Mind &parent, const std::shared_ptr &thread); diff --git a/smocore/include/director/director.h b/smocore/include/director/director.h index ce488f5..77716bd 100644 --- a/smocore/include/director/director.h +++ b/smocore/include/director/director.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -14,7 +15,7 @@ class Mind; namespace director { class Director -: public sscl::PuppetComponent +: public MindComponent { public: Director(Mind &parent, const std::shared_ptr &thread); diff --git a/smocore/include/marionette/marionette.h b/smocore/include/marionette/marionette.h new file mode 100644 index 0000000..56098f4 --- /dev/null +++ b/smocore/include/marionette/marionette.h @@ -0,0 +1,66 @@ +#ifndef _MARIONETTE_H +#define _MARIONETTE_H + +#include +#include +#include +#include +#include +#include + +namespace sscl { + +class PuppeteerThread; + +} // namespace sscl + +namespace smo { +namespace mrntt { + +class MarionetteComponent +: public sscl::pptr::PuppeteerComponent +{ +public: + MarionetteComponent(const std::shared_ptr &thread) + : sscl::pptr::PuppeteerComponent(thread) + {} + ~MarionetteComponent() = default; + +public: + typedef std::function mrnttLifetimeMgmtOpCbFn; + void initializeReq(sscl::Callback callback); + void finalizeReq(sscl::Callback callback); + // Intentionally doesn't take a callback. + void exceptionInd(); + + void handleLoopExceptionHook() override; + + static void preJoltHook(sscl::PuppeteerThread &thr); + +protected: + void postJoltHook() override; + void tryBlock1Hook() override; + void preLoopHook() override; + void postLoopHook() override; + void postTryBlock1CatchHook() override; + void handleTryBlock1TypedException(const std::exception& e) override; + void handleTryBlock1UnknownException() override; + +private: + class MrnttLifetimeMgmtOp; + class TerminationEvent; + + std::unique_ptr signals; + bool callShutdownSalmanoff = false; +}; + +extern std::shared_ptr thread; +extern MarionetteComponent mrntt; + +void marionetteInitializeReqCb(bool success); +void marionetteFinalizeReqCb(bool success); + +} // namespace mrntt +} // namespace smo + +#endif diff --git a/smocore/include/mind.h b/smocore/include/mind.h index c158937..b3fdb7c 100644 --- a/smocore/include/mind.h +++ b/smocore/include/mind.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -38,9 +39,9 @@ public: public: director::Director director; simulator::Simulator canvas; - sscl::PuppetComponent subconscious; + MindComponent subconscious; body::Body body; - sscl::PuppetComponent world; + MindComponent world; private: friend class body::Body; diff --git a/smocore/include/mindComponent.h b/smocore/include/mindComponent.h new file mode 100644 index 0000000..3ae46b5 --- /dev/null +++ b/smocore/include/mindComponent.h @@ -0,0 +1,23 @@ +#ifndef MIND_COMPONENT_H +#define MIND_COMPONENT_H + +#include + +namespace smo { + +class MindComponent +: public sscl::PuppetComponent +{ +public: + using sscl::PuppetComponent::PuppetComponent; + + static void preJoltHook(sscl::PuppetThread &thr); + + void handleLoopExceptionHook() override; + void preLoopHook() override; + void postLoopHook() override; +}; + +} // namespace smo + +#endif // MIND_COMPONENT_H diff --git a/smocore/include/mindThread.h b/smocore/include/mindThread.h index 63f9436..6946a45 100644 --- a/smocore/include/mindThread.h +++ b/smocore/include/mindThread.h @@ -6,18 +6,16 @@ namespace smo { -class Mind; // Forward declaration - class MindThread : public sscl::PuppetThread { public: - MindThread(sscl::ThreadId _id) - : sscl::PuppetThread(_id) + MindThread( + sscl::ThreadId _id, sscl::PuppetThread::entryPointFn entryPoint, + sscl::PuppetComponent &component, + sscl::PuppetThread::preJoltHookFn preJoltFn = nullptr) + : sscl::PuppetThread(_id, std::move(entryPoint), component, preJoltFn) {} - -protected: - void handleException() override; }; } // namespace smo diff --git a/smocore/include/simulator/simulator.h b/smocore/include/simulator/simulator.h index 76ef3e8..6a5bb21 100644 --- a/smocore/include/simulator/simulator.h +++ b/smocore/include/simulator/simulator.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace smo { @@ -13,7 +14,7 @@ class Mind; namespace simulator { class Simulator -: public sscl::PuppetComponent +: public MindComponent { public: Simulator(Mind &parent, const std::shared_ptr &thread); diff --git a/smocore/marionette/lifetime.cpp b/smocore/marionette/lifetime.cpp index 6aa2650..89c84f8 100644 --- a/smocore/marionette/lifetime.cpp +++ b/smocore/marionette/lifetime.cpp @@ -1,23 +1,25 @@ +#include #include #include #include #include #include #include -#include +#include #include #include #include -namespace sscl { -namespace pptr { +namespace smo { +namespace mrntt { class MarionetteComponent::MrnttLifetimeMgmtOp : public sscl::PostedAsynchronousContinuation { public: MrnttLifetimeMgmtOp( - MarionetteComponent &parent, const std::shared_ptr &caller, + MarionetteComponent &parent, + const std::shared_ptr &caller, sscl::Callback callback) : sscl::PostedAsynchronousContinuation( caller, callback), @@ -139,13 +141,13 @@ public: + ": Must be executed on Marionette thread"); } - sscl::pptr::mrntt.finalizeReq({nullptr, std::bind( - &sscl::pptr::marionetteFinalizeReqCb, + smo::mrntt::mrntt.finalizeReq({nullptr, std::bind( + &smo::mrntt::marionetteFinalizeReqCb, std::placeholders::_1)}); } }; -void sscl::pptr::MarionetteComponent::initializeReq( +void MarionetteComponent::initializeReq( sscl::Callback callback) { auto mrntt = sscl::ComponentThread::getSelf(); @@ -165,7 +167,7 @@ void sscl::pptr::MarionetteComponent::initializeReq( request.get(), request))); } -void sscl::pptr::MarionetteComponent::finalizeReq( +void MarionetteComponent::finalizeReq( sscl::Callback callback) { auto mrntt = sscl::ComponentThread::getSelf(); @@ -185,10 +187,16 @@ void sscl::pptr::MarionetteComponent::finalizeReq( request.get(), request))); } -void sscl::pptr::MarionetteComponent::exceptionInd() +void MarionetteComponent::handleLoopExceptionHook() +{ + sscl::pptr::exitCode = EXIT_FAILURE; + exceptionInd(); +} + +void MarionetteComponent::exceptionInd() { auto faultyThread = sscl::ComponentThread::getSelf(); - auto mrntt = sscl::ComponentThread::getMrntt(); + auto mrntt = sscl::ComponentThread::getPptr(); auto request = std::make_shared( faultyThread); @@ -199,5 +207,5 @@ void sscl::pptr::MarionetteComponent::exceptionInd() request.get(), request))); } -} // namespace pptr -} // namespace sscl +} // namespace mrntt +} // namespace smo diff --git a/smocore/marionette/main.cpp b/smocore/marionette/main.cpp index f21e870..d06c246 100644 --- a/smocore/marionette/main.cpp +++ b/smocore/marionette/main.cpp @@ -1,49 +1,26 @@ #include #include -#include #include -#include -#include #include #include -#include -#include +#include #include -#include +#include +#include #include #include #include -// Define the global puppeteer thread instance (declared extern in libspinscale) -namespace sscl { -namespace pptr { - -std::shared_ptr thread = std::make_shared(); -std::atomic exitCode; -MarionetteComponent mrntt(std::static_pointer_cast(thread)); - -void exitMarionetteLoop() -{ - thread->keepLooping = false; - thread->getIoService().stop(); - std::cout << "Mrntt: Signaled main loop to exit." << "\n"; -} - -void marionetteFinalizeReqCb(bool success) -{ - if (!success) - { - std::cerr << __func__ << ": Failed to finalize Marionette." << '\n'; - // Fallthrough. - } - std::cout << __func__ << ": Marionette finalized." << '\n'; - exitMarionetteLoop(); -} - -} // namespace pptr -} // namespace sscl - namespace smo { +namespace mrntt { + +std::shared_ptr thread = std::make_shared< + sscl::PuppeteerThread>( + SmoThreadId::MRNTT, + sscl::pptr::PuppeteerComponent::defaultPuppeteerMain, + mrntt, &MarionetteComponent::preJoltHook); + +MarionetteComponent mrntt(thread); void marionetteInitializeReqCb(bool success) { @@ -56,169 +33,154 @@ void marionetteInitializeReqCb(bool success) std::cerr << __func__ << ": Failed to initialize Marionette. Shutting down." << '\n'; - sscl::pptr::mrntt.finalizeReq({nullptr, std::bind( - &sscl::pptr::marionetteFinalizeReqCb, + mrntt.finalizeReq({nullptr, std::bind( + &smo::mrntt::marionetteFinalizeReqCb, std::placeholders::_1)}); } -} // namespace smo - -namespace sscl { - -void PuppeteerThread::main(PuppeteerThread& self) +void marionetteFinalizeReqCb(bool success) { - std::string threadName = "smo:" + self.name; - pthread_setname_np(pthread_self(), threadName.c_str()); - // Wait for CRT's main() to post us the command line args. - std::cout << __func__ << ": Waiting for command line JOLT" << std::endl; - self.getIoService().run(); - self.initializeTls(); - sscl::pptr::exitCode = EXIT_SUCCESS; - static boost::asio::signal_set signals(self.getIoService(), SIGINT); - bool callShutdownSalmanoff = false; - - try { - // Register SIGINT (Ctrl+C) and SIGSEGV handlers - signals.async_wait([](const boost::system::error_code& ec, int signal) - { - if (ec) { return; } - - switch (signal) { - case SIGINT: - std::cerr << "SIGINT (Ctrl+C) received. Initiating " - "shutdown..." << '\n'; - break; - case SIGSEGV: - std::cerr << "FATAL: Segmentation fault detected. " - "Initiating shutdown..." << '\n'; - break; - default: - break; - } - sscl::pptr::mrntt.finalizeReq({nullptr, std::bind( - &sscl::pptr::marionetteFinalizeReqCb, - std::placeholders::_1)}); - } - ); - - OptionParser &options = OptionParser::getOptions(); - - std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION - << std::endl; - - options.parseArguments( - sscl::crtCommandLineArgs.argc, sscl::crtCommandLineArgs.argv, - sscl::crtCommandLineArgs.envp); - - std::cout << __func__ << ": " << options.stringifyOptions() - << std::endl; - - if (options.printUsage) { - throw JustPrintUsageNoError(options); - } - - /** EXPLANATION: - * Initialize Salmanoff's Manager classes first. - * Manager classes' initialization is synchronous in nature, so we - * don't need the minds to be running to initialize them. - * - * Then we initialize the Minds. Minds are asynchronous and they - * call upon the async methods of the Manager classes. Device - * attachment is actually Mind specific and not Smo-global - * - * It is arguable whether library loading is Mind specific or - * Smo-global. You could argue that libraries should be loaded and - * unloaded dynamically as-needed by the bodies and worlds of - * particular Minds. You could also argue that we should load all - * libraries at startup and unload them at shutdown. - * - * The latter is cleaner and more resource-respecting. The former is - * easier to implement. - */ - smo::initializeSalmanoff(); - callShutdownSalmanoff = true; - - // Create new Mind instance just before initializeReq - sscl::pptr::mrntt.initializeReq({nullptr, std::bind( - &smo::marionetteInitializeReqCb, std::placeholders::_1)}); - - std::cout << __func__ << ": Entering event loop" << "\n"; - - /* We loop here because when an exception occurs in mrntt, we need to - * both direct the mind threads to exit gracefully, and then we also - * need to post messages to our own event loop to initiate our own - * orderly exit. So we loop here to re-enter the event loop, both - * to receive the ACK messages from the mind threads, and to post - * messages to our own event loop to initiate our own orderly exit. - */ - for (self.keepLooping = true; self.keepLooping;) - { - bool sendExceptionInd = false; - try { - /** EXPLANATION: - * This reset() call is crucial for async bridging patterns - * to work. - * When the outermost thread's io_service is stop()ped (e.g., - * from JOLT sequence), it won't process any new work until - * reset() is called, even if nested async operations try to - * post work to it. This means async bridges invoked from - * the outermost thread main sequence won't work until this - * reset() call. - */ - self.getIoService().reset(); - self.getIoService().run(); - } - catch (const std::exception& e) - { - sendExceptionInd = true; - std::cerr << self.name << ":" << __func__ - << ": Exception occurred: " << e.what() << "\n"; - } - catch (...) - { - sendExceptionInd = true; - std::cerr << self.name << ":" << __func__ - << ": Unknown exception occurred" << "\n"; - } - - if (sendExceptionInd) - { - sscl::pptr::exitCode = EXIT_FAILURE; - sscl::pptr::mrntt.exceptionInd(); - } - } - - std::cout << __func__ << ": Exited event loop" << "\n"; - } - catch (const OptionParser::Exception& e) + if (!success) { - std::ostream *out = &std::cout; - std::string outUsageMsg; + std::cerr << __func__ << ": Failed to finalize Marionette." << '\n'; + // Fallthrough. + } + std::cout << __func__ << ": Marionette finalized." << '\n'; + thread->exitLoop(); +} - if (typeid(e) == typeid(OptionsParserError)) +void MarionetteComponent::preJoltHook(sscl::PuppeteerThread &self) +{ + sscl::pptr::exitCode = EXIT_SUCCESS; + pthread_setname_np(pthread_self(), self.name.c_str()); + std::cout << __func__ << ": Waiting for command line JOLT" << std::endl; +} + +void MarionetteComponent::postJoltHook() +{ + auto th = smo::mrntt::thread; + + callShutdownSalmanoff = false; + + // Register SIGINT (Ctrl+C) and SIGSEGV handlers + signals = std::make_unique( + th->getIoService(), SIGINT); + + signals->async_wait( + [](const boost::system::error_code& ec, int signal) { - sscl::pptr::exitCode = EXIT_FAILURE; - out = &std::cerr; - outUsageMsg = std::string(__func__) + ": "; - } + if (ec) { return; } - *out << outUsageMsg << e.what() << std::endl; - } - catch (const std::exception& e) - { - std::cerr << __func__ << ": Exception occurred: " << e.what() - << std::endl; - sscl::pptr::exitCode = EXIT_FAILURE; - } - catch (...) - { - std::cerr << __func__ << ": Unknown exception occurred" << std::endl; - sscl::pptr::exitCode = EXIT_FAILURE; - } + switch (signal) { + case SIGINT: + std::cerr << "SIGINT (Ctrl+C) received. Initiating " + "shutdown..." << '\n'; + break; + case SIGSEGV: + std::cerr << "FATAL: Segmentation fault detected. " + "Initiating shutdown..." << '\n'; + break; + default: + break; + } + mrntt.finalizeReq({nullptr, std::bind( + &marionetteFinalizeReqCb, + std::placeholders::_1)}); + }); +} +void MarionetteComponent::tryBlock1Hook() +{ + OptionParser &options = OptionParser::getOptions(); + + std::cout << "PuppeteerThread::main: " << PACKAGE_NAME << " " + << PACKAGE_VERSION << std::endl; + + options.parseArguments( + sscl::crtCommandLineArgs.argc, sscl::crtCommandLineArgs.argv, + sscl::crtCommandLineArgs.envp); + + std::cout << "PuppeteerThread::main: " << options.stringifyOptions() + << std::endl; + + if (options.printUsage) { + throw JustPrintUsageNoError(options); + } +} + +void MarionetteComponent::preLoopHook() +{ + /** EXPLANATION: + * Initialize Salmanoff's Manager classes first. + * Manager classes' initialization is synchronous in nature, so we + * don't need the minds to be running to initialize them. + * + * Then we initialize the Minds. Minds are asynchronous and they + * call upon the async methods of the Manager classes. Device + * attachment is actually Mind specific and not Smo-global + * + * It is arguable whether library loading is Mind specific or + * Smo-global. You could argue that libraries should be loaded and + * unloaded dynamically as-needed by the bodies and worlds of + * particular Minds. You could also argue that we should load all + * libraries at startup and unload them at shutdown. + * + * The latter is cleaner and more resource-respecting. The former is + * easier to implement. + */ + smo::initializeSalmanoff(); + callShutdownSalmanoff = true; + + initializeReq(sscl::Callback{ + nullptr, + std::bind(&marionetteInitializeReqCb, std::placeholders::_1)}); + + std::cout << "PuppeteerThread::main: Entering event loop" << "\n"; +} + +void MarionetteComponent::postLoopHook() +{ + std::cout << "PuppeteerThread::main: Exited event loop" << "\n"; +} + +void MarionetteComponent::postTryBlock1CatchHook() +{ if (callShutdownSalmanoff) { smo::shutdownSalmanoff(); } } -} // namespace sscl +void MarionetteComponent::handleTryBlock1TypedException(const std::exception& e) +{ + const OptionParser::Exception* optEx = + dynamic_cast(&e); + if (optEx) + { + std::ostream *out = &std::cout; + std::string outUsageMsg; + + if (typeid(*optEx) == typeid(OptionsParserError)) + { + sscl::pptr::exitCode = EXIT_FAILURE; + out = &std::cerr; + outUsageMsg = std::string("main: ") + ": "; + } + // Else it's a JustPrintUsageNoError. + + *out << outUsageMsg << optEx->what() << std::endl; + return; + } + + std::cerr << "main: Exception occurred: " << e.what() + << std::endl; + sscl::pptr::exitCode = EXIT_FAILURE; +} + +void MarionetteComponent::handleTryBlock1UnknownException() +{ + std::cerr << "main: Unknown exception occurred" << std::endl; + sscl::pptr::exitCode = EXIT_FAILURE; +} + +} // namespace mrntt +} // namespace smo diff --git a/smocore/marionette/negtrinEvent.cpp b/smocore/marionette/negtrinEvent.cpp index 6240aff..b55ac01 100644 --- a/smocore/marionette/negtrinEvent.cpp +++ b/smocore/marionette/negtrinEvent.cpp @@ -1,7 +1,6 @@ #include #include #include -#include namespace smo { diff --git a/smocore/mind.cpp b/smocore/mind.cpp index e236b8f..09ba075 100644 --- a/smocore/mind.cpp +++ b/smocore/mind.cpp @@ -5,24 +5,40 @@ #include #include #include +#include #include #include #include #include #include -#include +#include namespace smo { Mind::Mind(void) : sscl::PuppetApplication( std::vector>{ - std::make_shared(SmoThreadId::DIRECTOR), - std::make_shared(SmoThreadId::SIMULATOR), - std::make_shared(SmoThreadId::SUBCONSCIOUS), - std::make_shared(SmoThreadId::BODY) + std::make_shared( + SmoThreadId::DIRECTOR, + sscl::PuppetComponent::defaultPuppetMain, director, + &MindComponent::preJoltHook), + std::make_shared( + SmoThreadId::SIMULATOR, + sscl::PuppetComponent::defaultPuppetMain, canvas, + &MindComponent::preJoltHook), + std::make_shared( + SmoThreadId::SUBCONSCIOUS, + sscl::PuppetComponent::defaultPuppetMain, subconscious, + &MindComponent::preJoltHook), + std::make_shared( + SmoThreadId::BODY, + sscl::PuppetComponent::defaultPuppetMain, body, + &MindComponent::preJoltHook) #ifndef CONFIG_WORLD_USE_BODY_THREAD - , std::make_shared(SmoThreadId::WORLD) + , std::make_shared( + SmoThreadId::WORLD, + sscl::PuppetComponent::defaultPuppetMain, world, + &MindComponent::preJoltHook) #endif } ), @@ -223,7 +239,7 @@ void Mind::initializeReq(sscl::Callback callback) auto request = std::make_shared( *this, caller, callback); - sscl::pptr::mrntt.thread->getIoService().post( + mrntt::mrntt.thread->getIoService().post( STC(std::bind( &MindLifetimeMgmtOp::initializeReq1_posted, request.get(), request))); @@ -235,7 +251,7 @@ void Mind::finalizeReq(sscl::Callback callback) auto request = std::make_shared( *this, caller, callback); - sscl::pptr::mrntt.thread->getIoService().post( + mrntt::mrntt.thread->getIoService().post( STC(std::bind( &MindLifetimeMgmtOp::finalizeReq1_posted, request.get(), request))); diff --git a/smocore/mindComponent.cpp b/smocore/mindComponent.cpp new file mode 100644 index 0000000..5aa0c27 --- /dev/null +++ b/smocore/mindComponent.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +namespace smo { + +void MindComponent::preJoltHook(sscl::PuppetThread &thr) +{ + pthread_setname_np(pthread_self(), thr.name.c_str()); +} + +void MindComponent::handleLoopExceptionHook() +{ + mrntt::mrntt.exceptionInd(); +} + +void MindComponent::preLoopHook() +{ + std::cout << thread->name << ":defaultPuppetMain" + << ": Entering event loop" << "\n"; +} + +void MindComponent::postLoopHook() +{ + std::cout << thread->name << ":defaultPuppetMain" + << ": Exited event loop" << "\n"; +} + +} // namespace smo diff --git a/smocore/mindThread.cpp b/smocore/mindThread.cpp deleted file mode 100644 index 11e8f92..0000000 --- a/smocore/mindThread.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include - -namespace smo { - -void MindThread::handleException() -{ - sscl::pptr::mrntt.exceptionInd(); -} - -} // namespace smo diff --git a/smocore/simulator/simulator.cpp b/smocore/simulator/simulator.cpp index 0cc28c5..83e9650 100644 --- a/smocore/simulator/simulator.cpp +++ b/smocore/simulator/simulator.cpp @@ -6,7 +6,7 @@ namespace simulator { Simulator::Simulator( Mind &parent, const std::shared_ptr &thread) -: sscl::PuppetComponent(static_cast(parent), thread) +: MindComponent(static_cast(parent), thread) { } diff --git a/smocore/stimBuffApis/stimBuffApiManager.cpp b/smocore/stimBuffApis/stimBuffApiManager.cpp index 4966c46..2bcf991 100644 --- a/smocore/stimBuffApis/stimBuffApiManager.cpp +++ b/smocore/stimBuffApis/stimBuffApiManager.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include