Split: Split libspinscale off from SMO.

Now we can probably begin using libspinscale in Couresilient
without worrying about excessive technical debt later on.
This commit is contained in:
2026-02-22 17:46:27 -04:00
parent 9361a43e40
commit 1c397dfeb5
22 changed files with 350 additions and 310 deletions
+10 -6
View File
@@ -2,29 +2,33 @@
#include <pthread.h> #include <pthread.h>
#include <componentThread.h> #include <componentThread.h>
#include <spinscale/componentThread.h> #include <spinscale/componentThread.h>
#include <spinscale/marionette.h> #include <spinscale/runtime.h>
#include <marionette/marionette.h>
int main(int argc, char *argv[], char *envp[]) int main(int argc, char *argv[], char *envp[])
{ {
// Set the pptr thread ID before using any ComponentThread functionality /* Set the pptr thread ID and thread before using any ComponentThread
sscl::pptr::setPuppeteerThreadId(smo::SmoThreadId::MRNTT); * functionality.
*/
sscl::ComponentThread::setPuppeteerThreadId(smo::SmoThreadId::MRNTT);
sscl::ComponentThread::setPuppeteerThread(smo::mrntt::thread);
pthread_setname_np(pthread_self(), "smo:CRT:main"); pthread_setname_np(pthread_self(), "smo:CRT:main");
/* We don't do anything inside of main() /* We don't do anything inside of main()
* Main merely waits for the marionette thread to exit. * Main merely waits for the marionette thread to exit.
*/ */
std::cout << "CRT:" << __func__ << ": about to JOLT Mrntt with cmdline args" std::cout << "CRT:" << __func__ << ": about to JOLT Mrntt with cmdline args"
<< '\n'; << '\n';
sscl::pptr::thread->getIoService().post( smo::mrntt::thread->getIoService().post(
[argc, argv, envp]() [argc, argv, envp]()
{ {
std::cout << "Mrntt:" << __func__ << ":JOLTED: setting cmdline args" std::cout << "Mrntt:" << __func__ << ":JOLTED: setting cmdline args"
<< '\n'; << '\n';
sscl::CrtCommandLineArgs::set(argc, argv, envp); 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 '" std::cout << "CRT:" << __func__ << ": Mrntt exited with code '"
<< sscl::pptr::exitCode << "'\n"; << sscl::pptr::exitCode << "'\n";
return sscl::pptr::exitCode; return sscl::pptr::exitCode;
+1 -1
View File
@@ -5,7 +5,7 @@ include(${CMAKE_SOURCE_DIR}/cmake/flexYacc.cmake)
add_library(smocore STATIC add_library(smocore STATIC
# Core files # Core files
mind.cpp mind.cpp
mindThread.cpp mindComponent.cpp
componentThread.cpp componentThread.cpp
opts.cpp opts.cpp
+1 -1
View File
@@ -15,7 +15,7 @@ namespace smo {
namespace body { namespace body {
Body::Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread) Body::Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
: sscl::PuppetComponent(static_cast<sscl::PuppetApplication&>(parent), thread) : MindComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
{ {
} }
-57
View File
@@ -30,61 +30,4 @@ std::string ComponentThread::getThreadName(sscl::ThreadId id)
return threadNames[static_cast<int>(smoId)]; return threadNames[static_cast<int>(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 } // namespace sscl
+9 -9
View File
@@ -14,7 +14,7 @@
#include <deviceManager/deviceManager.h> #include <deviceManager/deviceManager.h>
#include <deviceManager/deviceReattacher.h> #include <deviceManager/deviceReattacher.h>
#include <stimBuffApis/stimBuffApiManager.h> #include <stimBuffApis/stimBuffApiManager.h>
#include <spinscale/marionette.h> #include <marionette/marionette.h>
#include <mind.h> #include <mind.h>
namespace smo { namespace smo {
@@ -304,7 +304,7 @@ void DeviceManager::newDeviceAttachmentSpecInd(
}); });
NewDeviceAttachmentSpecInd::LockerAndInvoker lockvoker( NewDeviceAttachmentSpecInd::LockerAndInvoker lockvoker(
*request, sscl::pptr::mrntt.thread, *request, mrntt::mrntt.thread,
std::bind( std::bind(
&NewDeviceAttachmentSpecInd::newDeviceAttachmentSpecInd1_posted, &NewDeviceAttachmentSpecInd::newDeviceAttachmentSpecInd1_posted,
request.get(), request)); request.get(), request));
@@ -323,7 +323,7 @@ void DeviceManager::removeDeviceAttachmentSpecReq(
}); });
RemoveDeviceAttachmentSpecReq::LockerAndInvoker lockvoker( RemoveDeviceAttachmentSpecReq::LockerAndInvoker lockvoker(
*request, sscl::pptr::mrntt.thread, *request, mrntt::mrntt.thread,
std::bind( std::bind(
&RemoveDeviceAttachmentSpecReq &RemoveDeviceAttachmentSpecReq
::removeDeviceAttachmentSpecReq1_posted, ::removeDeviceAttachmentSpecReq1_posted,
@@ -500,7 +500,7 @@ void DeviceManager::attachStimBuffDeviceReq(
}); });
AttachStimBuffDeviceReq::LockerAndInvoker lockvoker( AttachStimBuffDeviceReq::LockerAndInvoker lockvoker(
*request, sscl::pptr::mrntt.thread, *request, mrntt::mrntt.thread,
std::bind( std::bind(
&AttachStimBuffDeviceReq::attachStimBuffDeviceReq1_posted, &AttachStimBuffDeviceReq::attachStimBuffDeviceReq1_posted,
request.get(), request)); request.get(), request));
@@ -535,7 +535,7 @@ void DeviceManager::detachStimBuffDeviceReq(
}); });
DetachStimBuffDeviceReq::LockerAndInvoker lockvoker( DetachStimBuffDeviceReq::LockerAndInvoker lockvoker(
*request, sscl::pptr::mrntt.thread, *request, mrntt::mrntt.thread,
std::bind( std::bind(
&DetachStimBuffDeviceReq::detachStimBuffDeviceReq1_posted, &DetachStimBuffDeviceReq::detachStimBuffDeviceReq1_posted,
request.get(), request)); request.get(), request));
@@ -626,7 +626,7 @@ void DeviceManager::attachAllUnattachedDevicesFromReq(
auto request = std::make_shared<AttachAllUnattachedDevicesFromReq>( auto request = std::make_shared<AttachAllUnattachedDevicesFromReq>(
specs->size(), specs, caller, std::move(cb)); specs->size(), specs, caller, std::move(cb));
sscl::pptr::mrntt.thread->getIoService().post( mrntt::mrntt.thread->getIoService().post(
STC(std::bind( STC(std::bind(
&AttachAllUnattachedDevicesFromReq &AttachAllUnattachedDevicesFromReq
::attachAllUnattachedDevicesFromReq1_posted, ::attachAllUnattachedDevicesFromReq1_posted,
@@ -723,7 +723,7 @@ void DeviceManager::attachAllUnattachedDevicesFromKnownListReq(
}); });
AttachAllUnattachedDevicesFromKnownListReq::LockerAndInvoker lockvoker( AttachAllUnattachedDevicesFromKnownListReq::LockerAndInvoker lockvoker(
*request, sscl::pptr::mrntt.thread, *request, mrntt::mrntt.thread,
std::bind( std::bind(
&AttachAllUnattachedDevicesFromKnownListReq &AttachAllUnattachedDevicesFromKnownListReq
::attachAllUnattachedDevicesFromKnownListReq1_posted, ::attachAllUnattachedDevicesFromKnownListReq1_posted,
@@ -807,7 +807,7 @@ void DeviceManager::detachAllAttachedDeviceRoles(
DeviceManager::getInstance().attachedDeviceRoles.size(), DeviceManager::getInstance().attachedDeviceRoles.size(),
caller, std::move(cb)); caller, std::move(cb));
sscl::pptr::mrntt.thread->getIoService().post( mrntt::mrntt.thread->getIoService().post(
STC(std::bind( STC(std::bind(
&DetachAllAttachedDeviceRoles::detachAllAttachedDeviceRoles1_posted, &DetachAllAttachedDeviceRoles::detachAllAttachedDeviceRoles1_posted,
request.get(), request))); request.get(), request)));
@@ -816,7 +816,7 @@ void DeviceManager::detachAllAttachedDeviceRoles(
void DeviceManager::initializeDeviceReattacher() void DeviceManager::initializeDeviceReattacher()
{ {
deviceReattacher = std::make_unique<DeviceReattacher>( deviceReattacher = std::make_unique<DeviceReattacher>(
*this, sscl::pptr::mrntt.thread); *this, mrntt::mrntt.thread);
deviceReattacher->start(); deviceReattacher->start();
} }
+1 -1
View File
@@ -8,7 +8,7 @@ namespace director {
Director::Director( Director::Director(
Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread) Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
: sscl::PuppetComponent(static_cast<sscl::PuppetApplication&>(parent), thread) : MindComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
{ {
} }
+2 -1
View File
@@ -3,6 +3,7 @@
#include <spinscale/puppetApplication.h> #include <spinscale/puppetApplication.h>
#include <spinscale/component.h> #include <spinscale/component.h>
#include <mindComponent.h>
#include <functional> #include <functional>
#include <spinscale/callback.h> #include <spinscale/callback.h>
@@ -13,7 +14,7 @@ class Mind;
namespace body { namespace body {
class Body class Body
: public sscl::PuppetComponent : public MindComponent
{ {
public: public:
Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread); Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
+2 -1
View File
@@ -4,6 +4,7 @@
#include <config.h> #include <config.h>
#include <spinscale/puppetApplication.h> #include <spinscale/puppetApplication.h>
#include <spinscale/component.h> #include <spinscale/component.h>
#include <mindComponent.h>
#include <goal.h> #include <goal.h>
#include <lruLifo.h> #include <lruLifo.h>
@@ -14,7 +15,7 @@ class Mind;
namespace director { namespace director {
class Director class Director
: public sscl::PuppetComponent : public MindComponent
{ {
public: public:
Director(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread); Director(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
+66
View File
@@ -0,0 +1,66 @@
#ifndef _MARIONETTE_H
#define _MARIONETTE_H
#include <boost/asio/signal_set.hpp>
#include <cstdint>
#include <atomic>
#include <functional>
#include <memory>
#include <spinscale/component.h>
namespace sscl {
class PuppeteerThread;
} // namespace sscl
namespace smo {
namespace mrntt {
class MarionetteComponent
: public sscl::pptr::PuppeteerComponent
{
public:
MarionetteComponent(const std::shared_ptr<sscl::PuppeteerThread> &thread)
: sscl::pptr::PuppeteerComponent(thread)
{}
~MarionetteComponent() = default;
public:
typedef std::function<void(bool)> mrnttLifetimeMgmtOpCbFn;
void initializeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
void finalizeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> 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<boost::asio::signal_set> signals;
bool callShutdownSalmanoff = false;
};
extern std::shared_ptr<sscl::PuppeteerThread> thread;
extern MarionetteComponent mrntt;
void marionetteInitializeReqCb(bool success);
void marionetteFinalizeReqCb(bool success);
} // namespace mrntt
} // namespace smo
#endif
+3 -2
View File
@@ -11,6 +11,7 @@
#include <spinscale/component.h> #include <spinscale/component.h>
#include <componentThread.h> #include <componentThread.h>
#include <mindThread.h> #include <mindThread.h>
#include <mindComponent.h>
#include <director/director.h> #include <director/director.h>
#include <simulator/simulator.h> #include <simulator/simulator.h>
#include <body/body.h> #include <body/body.h>
@@ -38,9 +39,9 @@ public:
public: public:
director::Director director; director::Director director;
simulator::Simulator canvas; simulator::Simulator canvas;
sscl::PuppetComponent subconscious; MindComponent subconscious;
body::Body body; body::Body body;
sscl::PuppetComponent world; MindComponent world;
private: private:
friend class body::Body; friend class body::Body;
+23
View File
@@ -0,0 +1,23 @@
#ifndef MIND_COMPONENT_H
#define MIND_COMPONENT_H
#include <spinscale/component.h>
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
+5 -7
View File
@@ -6,18 +6,16 @@
namespace smo { namespace smo {
class Mind; // Forward declaration
class MindThread class MindThread
: public sscl::PuppetThread : public sscl::PuppetThread
{ {
public: public:
MindThread(sscl::ThreadId _id) MindThread(
: sscl::PuppetThread(_id) 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 } // namespace smo
+2 -1
View File
@@ -4,6 +4,7 @@
#include <config.h> #include <config.h>
#include <spinscale/puppetApplication.h> #include <spinscale/puppetApplication.h>
#include <spinscale/component.h> #include <spinscale/component.h>
#include <mindComponent.h>
#include <simulator/scene.h> #include <simulator/scene.h>
namespace smo { namespace smo {
@@ -13,7 +14,7 @@ class Mind;
namespace simulator { namespace simulator {
class Simulator class Simulator
: public sscl::PuppetComponent : public MindComponent
{ {
public: public:
Simulator(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread); Simulator(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
+20 -12
View File
@@ -1,23 +1,25 @@
#include <cstdlib>
#include <iostream> #include <iostream>
#include <spinscale/asynchronousContinuation.h> #include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h> #include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h> #include <spinscale/callback.h>
#include <spinscale/callableTracer.h> #include <spinscale/callableTracer.h>
#include <spinscale/component.h> #include <spinscale/component.h>
#include <spinscale/marionette.h> #include <marionette/marionette.h>
#include <componentThread.h> #include <componentThread.h>
#include <deviceManager/deviceManager.h> #include <deviceManager/deviceManager.h>
#include <mindManager/mindManager.h> #include <mindManager/mindManager.h>
namespace sscl { namespace smo {
namespace pptr { namespace mrntt {
class MarionetteComponent::MrnttLifetimeMgmtOp class MarionetteComponent::MrnttLifetimeMgmtOp
: public sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn> : public sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
{ {
public: public:
MrnttLifetimeMgmtOp( MrnttLifetimeMgmtOp(
MarionetteComponent &parent, const std::shared_ptr<sscl::ComponentThread> &caller, MarionetteComponent &parent,
const std::shared_ptr<sscl::ComponentThread> &caller,
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback) sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
: sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>( : sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
caller, callback), caller, callback),
@@ -139,13 +141,13 @@ public:
+ ": Must be executed on Marionette thread"); + ": Must be executed on Marionette thread");
} }
sscl::pptr::mrntt.finalizeReq({nullptr, std::bind( smo::mrntt::mrntt.finalizeReq({nullptr, std::bind(
&sscl::pptr::marionetteFinalizeReqCb, &smo::mrntt::marionetteFinalizeReqCb,
std::placeholders::_1)}); std::placeholders::_1)});
} }
}; };
void sscl::pptr::MarionetteComponent::initializeReq( void MarionetteComponent::initializeReq(
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback) sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
{ {
auto mrntt = sscl::ComponentThread::getSelf(); auto mrntt = sscl::ComponentThread::getSelf();
@@ -165,7 +167,7 @@ void sscl::pptr::MarionetteComponent::initializeReq(
request.get(), request))); request.get(), request)));
} }
void sscl::pptr::MarionetteComponent::finalizeReq( void MarionetteComponent::finalizeReq(
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback) sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
{ {
auto mrntt = sscl::ComponentThread::getSelf(); auto mrntt = sscl::ComponentThread::getSelf();
@@ -185,10 +187,16 @@ void sscl::pptr::MarionetteComponent::finalizeReq(
request.get(), request))); 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 faultyThread = sscl::ComponentThread::getSelf();
auto mrntt = sscl::ComponentThread::getMrntt(); auto mrntt = sscl::ComponentThread::getPptr();
auto request = std::make_shared<TerminationEvent>( auto request = std::make_shared<TerminationEvent>(
faultyThread); faultyThread);
@@ -199,5 +207,5 @@ void sscl::pptr::MarionetteComponent::exceptionInd()
request.get(), request))); request.get(), request)));
} }
} // namespace pptr } // namespace mrntt
} // namespace sscl } // namespace smo
+149 -187
View File
@@ -1,49 +1,26 @@
#include <boostAsioLinkageFix.h> #include <boostAsioLinkageFix.h>
#include <config.h> #include <config.h>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <string>
#include <exception>
#include <opts.h> #include <opts.h>
#include <typeinfo> #include <typeinfo>
#include <boost/asio/signal_set.hpp> #include <spinscale/component.h>
#include <spinscale/asynchronousBridge.h>
#include <spinscale/componentThread.h> #include <spinscale/componentThread.h>
#include <spinscale/marionette.h> #include <marionette/marionette.h>
#include <spinscale/runtime.h>
#include <componentThread.h> #include <componentThread.h>
#include <mindManager/mindManager.h> #include <mindManager/mindManager.h>
#include <salmanoff.h> #include <salmanoff.h>
// Define the global puppeteer thread instance (declared extern in libspinscale)
namespace sscl {
namespace pptr {
std::shared_ptr<PuppeteerThread> thread = std::make_shared<PuppeteerThread>();
std::atomic<int> exitCode;
MarionetteComponent mrntt(std::static_pointer_cast<sscl::ComponentThread>(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 smo {
namespace mrntt {
std::shared_ptr<sscl::PuppeteerThread> thread = std::make_shared<
sscl::PuppeteerThread>(
SmoThreadId::MRNTT,
sscl::pptr::PuppeteerComponent::defaultPuppeteerMain,
mrntt, &MarionetteComponent::preJoltHook);
MarionetteComponent mrntt(thread);
void marionetteInitializeReqCb(bool success) void marionetteInitializeReqCb(bool success)
{ {
@@ -56,169 +33,154 @@ void marionetteInitializeReqCb(bool success)
std::cerr << __func__ << ": Failed to initialize Marionette. Shutting down." std::cerr << __func__ << ": Failed to initialize Marionette. Shutting down."
<< '\n'; << '\n';
sscl::pptr::mrntt.finalizeReq({nullptr, std::bind( mrntt.finalizeReq({nullptr, std::bind(
&sscl::pptr::marionetteFinalizeReqCb, &smo::mrntt::marionetteFinalizeReqCb,
std::placeholders::_1)}); std::placeholders::_1)});
} }
} // namespace smo void marionetteFinalizeReqCb(bool success)
namespace sscl {
void PuppeteerThread::main(PuppeteerThread& self)
{ {
std::string threadName = "smo:" + self.name; if (!success)
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)
{ {
std::ostream *out = &std::cout; std::cerr << __func__ << ": Failed to finalize Marionette." << '\n';
std::string outUsageMsg; // 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<boost::asio::signal_set>(
th->getIoService(), SIGINT);
signals->async_wait(
[](const boost::system::error_code& ec, int signal)
{ {
sscl::pptr::exitCode = EXIT_FAILURE; if (ec) { return; }
out = &std::cerr;
outUsageMsg = std::string(__func__) + ": ";
}
*out << outUsageMsg << e.what() << std::endl; switch (signal) {
} case SIGINT:
catch (const std::exception& e) std::cerr << "SIGINT (Ctrl+C) received. Initiating "
{ "shutdown..." << '\n';
std::cerr << __func__ << ": Exception occurred: " << e.what() break;
<< std::endl; case SIGSEGV:
sscl::pptr::exitCode = EXIT_FAILURE; std::cerr << "FATAL: Segmentation fault detected. "
} "Initiating shutdown..." << '\n';
catch (...) break;
{ default:
std::cerr << __func__ << ": Unknown exception occurred" << std::endl; break;
sscl::pptr::exitCode = EXIT_FAILURE; }
} 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<mrnttLifetimeMgmtOpCbFn>{
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) { if (callShutdownSalmanoff) {
smo::shutdownSalmanoff(); smo::shutdownSalmanoff();
} }
} }
} // namespace sscl void MarionetteComponent::handleTryBlock1TypedException(const std::exception& e)
{
const OptionParser::Exception* optEx =
dynamic_cast<const OptionParser::Exception*>(&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
-1
View File
@@ -1,7 +1,6 @@
#include <iostream> #include <iostream>
#include <spinscale/component.h> #include <spinscale/component.h>
#include <nonNeutralQualia.h> #include <nonNeutralQualia.h>
#include <spinscale/marionette.h>
namespace smo { namespace smo {
+24 -8
View File
@@ -5,24 +5,40 @@
#include <spinscale/asynchronousLoop.h> #include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h> #include <spinscale/callback.h>
#include <spinscale/callableTracer.h> #include <spinscale/callableTracer.h>
#include <spinscale/componentThread.h>
#include <mind.h> #include <mind.h>
#include <mindThread.h> #include <mindThread.h>
#include <director/director.h> #include <director/director.h>
#include <simulator/simulator.h> #include <simulator/simulator.h>
#include <stimBuffApis/stimBuffApiManager.h> #include <stimBuffApis/stimBuffApiManager.h>
#include <spinscale/marionette.h> #include <marionette/marionette.h>
namespace smo { namespace smo {
Mind::Mind(void) Mind::Mind(void)
: sscl::PuppetApplication( : sscl::PuppetApplication(
std::vector<std::shared_ptr<sscl::PuppetThread>>{ std::vector<std::shared_ptr<sscl::PuppetThread>>{
std::make_shared<MindThread>(SmoThreadId::DIRECTOR), std::make_shared<MindThread>(
std::make_shared<MindThread>(SmoThreadId::SIMULATOR), SmoThreadId::DIRECTOR,
std::make_shared<MindThread>(SmoThreadId::SUBCONSCIOUS), sscl::PuppetComponent::defaultPuppetMain, director,
std::make_shared<MindThread>(SmoThreadId::BODY) &MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::SIMULATOR,
sscl::PuppetComponent::defaultPuppetMain, canvas,
&MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::SUBCONSCIOUS,
sscl::PuppetComponent::defaultPuppetMain, subconscious,
&MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::BODY,
sscl::PuppetComponent::defaultPuppetMain, body,
&MindComponent::preJoltHook)
#ifndef CONFIG_WORLD_USE_BODY_THREAD #ifndef CONFIG_WORLD_USE_BODY_THREAD
, std::make_shared<MindThread>(SmoThreadId::WORLD) , std::make_shared<MindThread>(
SmoThreadId::WORLD,
sscl::PuppetComponent::defaultPuppetMain, world,
&MindComponent::preJoltHook)
#endif #endif
} }
), ),
@@ -223,7 +239,7 @@ void Mind::initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
auto request = std::make_shared<MindLifetimeMgmtOp>( auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback); *this, caller, callback);
sscl::pptr::mrntt.thread->getIoService().post( mrntt::mrntt.thread->getIoService().post(
STC(std::bind( STC(std::bind(
&MindLifetimeMgmtOp::initializeReq1_posted, &MindLifetimeMgmtOp::initializeReq1_posted,
request.get(), request))); request.get(), request)));
@@ -235,7 +251,7 @@ void Mind::finalizeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
auto request = std::make_shared<MindLifetimeMgmtOp>( auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback); *this, caller, callback);
sscl::pptr::mrntt.thread->getIoService().post( mrntt::mrntt.thread->getIoService().post(
STC(std::bind( STC(std::bind(
&MindLifetimeMgmtOp::finalizeReq1_posted, &MindLifetimeMgmtOp::finalizeReq1_posted,
request.get(), request))); request.get(), request)));
+29
View File
@@ -0,0 +1,29 @@
#include <iostream>
#include <mindComponent.h>
#include <marionette/marionette.h>
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
-12
View File
@@ -1,12 +0,0 @@
#include <componentThread.h>
#include <mindThread.h>
#include <spinscale/marionette.h>
namespace smo {
void MindThread::handleException()
{
sscl::pptr::mrntt.exceptionInd();
}
} // namespace smo
+1 -1
View File
@@ -6,7 +6,7 @@ namespace simulator {
Simulator::Simulator( Simulator::Simulator(
Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread) Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
: sscl::PuppetComponent(static_cast<sscl::PuppetApplication&>(parent), thread) : MindComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
{ {
} }
+1 -1
View File
@@ -12,7 +12,7 @@
#include <user/senseApiDesc.h> #include <user/senseApiDesc.h>
#include <mind.h> #include <mind.h>
#include <deviceManager/deviceManager.h> #include <deviceManager/deviceManager.h>
#include <spinscale/marionette.h> #include <marionette/marionette.h>
#include <computeManager/computeManager.h> #include <computeManager/computeManager.h>