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:
+1
-1
Submodule libspinscale updated: e6a924a3f7...a7521f3760
@@ -2,29 +2,33 @@
|
||||
#include <pthread.h>
|
||||
#include <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[])
|
||||
{
|
||||
// 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;
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace smo {
|
||||
namespace body {
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -30,61 +30,4 @@ std::string ComponentThread::getThreadName(sscl::ThreadId id)
|
||||
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
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include <deviceManager/deviceManager.h>
|
||||
#include <deviceManager/deviceReattacher.h>
|
||||
#include <stimBuffApis/stimBuffApiManager.h>
|
||||
#include <spinscale/marionette.h>
|
||||
#include <marionette/marionette.h>
|
||||
#include <mind.h>
|
||||
|
||||
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<AttachAllUnattachedDevicesFromReq>(
|
||||
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<DeviceReattacher>(
|
||||
*this, sscl::pptr::mrntt.thread);
|
||||
*this, mrntt::mrntt.thread);
|
||||
|
||||
deviceReattacher->start();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace director {
|
||||
|
||||
Director::Director(
|
||||
Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
|
||||
: sscl::PuppetComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
|
||||
: MindComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <spinscale/puppetApplication.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <mindComponent.h>
|
||||
#include <functional>
|
||||
#include <spinscale/callback.h>
|
||||
|
||||
@@ -13,7 +14,7 @@ class Mind;
|
||||
namespace body {
|
||||
|
||||
class Body
|
||||
: public sscl::PuppetComponent
|
||||
: public MindComponent
|
||||
{
|
||||
public:
|
||||
Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <config.h>
|
||||
#include <spinscale/puppetApplication.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <mindComponent.h>
|
||||
#include <goal.h>
|
||||
#include <lruLifo.h>
|
||||
|
||||
@@ -14,7 +15,7 @@ class Mind;
|
||||
namespace director {
|
||||
|
||||
class Director
|
||||
: public sscl::PuppetComponent
|
||||
: public MindComponent
|
||||
{
|
||||
public:
|
||||
Director(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
|
||||
|
||||
@@ -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
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <spinscale/component.h>
|
||||
#include <componentThread.h>
|
||||
#include <mindThread.h>
|
||||
#include <mindComponent.h>
|
||||
#include <director/director.h>
|
||||
#include <simulator/simulator.h>
|
||||
#include <body/body.h>
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <config.h>
|
||||
#include <spinscale/puppetApplication.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <mindComponent.h>
|
||||
#include <simulator/scene.h>
|
||||
|
||||
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<sscl::PuppetThread> &thread);
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <spinscale/asynchronousContinuation.h>
|
||||
#include <spinscale/asynchronousLoop.h>
|
||||
#include <spinscale/callback.h>
|
||||
#include <spinscale/callableTracer.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <spinscale/marionette.h>
|
||||
#include <marionette/marionette.h>
|
||||
#include <componentThread.h>
|
||||
#include <deviceManager/deviceManager.h>
|
||||
#include <mindManager/mindManager.h>
|
||||
|
||||
namespace sscl {
|
||||
namespace pptr {
|
||||
namespace smo {
|
||||
namespace mrntt {
|
||||
|
||||
class MarionetteComponent::MrnttLifetimeMgmtOp
|
||||
: public sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
|
||||
{
|
||||
public:
|
||||
MrnttLifetimeMgmtOp(
|
||||
MarionetteComponent &parent, const std::shared_ptr<sscl::ComponentThread> &caller,
|
||||
MarionetteComponent &parent,
|
||||
const std::shared_ptr<sscl::ComponentThread> &caller,
|
||||
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
|
||||
: sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
|
||||
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<mrnttLifetimeMgmtOpCbFn> 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<mrnttLifetimeMgmtOpCbFn> 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<TerminationEvent>(
|
||||
faultyThread);
|
||||
@@ -199,5 +207,5 @@ void sscl::pptr::MarionetteComponent::exceptionInd()
|
||||
request.get(), request)));
|
||||
}
|
||||
|
||||
} // namespace pptr
|
||||
} // namespace sscl
|
||||
} // namespace mrntt
|
||||
} // namespace smo
|
||||
|
||||
+149
-187
@@ -1,49 +1,26 @@
|
||||
#include <boostAsioLinkageFix.h>
|
||||
#include <config.h>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <exception>
|
||||
#include <opts.h>
|
||||
#include <typeinfo>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
#include <spinscale/asynchronousBridge.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <spinscale/marionette.h>
|
||||
#include <marionette/marionette.h>
|
||||
#include <spinscale/runtime.h>
|
||||
#include <componentThread.h>
|
||||
#include <mindManager/mindManager.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 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)
|
||||
{
|
||||
@@ -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<boost::asio::signal_set>(
|
||||
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<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) {
|
||||
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,7 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <spinscale/component.h>
|
||||
#include <nonNeutralQualia.h>
|
||||
#include <spinscale/marionette.h>
|
||||
|
||||
|
||||
namespace smo {
|
||||
|
||||
+24
-8
@@ -5,24 +5,40 @@
|
||||
#include <spinscale/asynchronousLoop.h>
|
||||
#include <spinscale/callback.h>
|
||||
#include <spinscale/callableTracer.h>
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <mind.h>
|
||||
#include <mindThread.h>
|
||||
#include <director/director.h>
|
||||
#include <simulator/simulator.h>
|
||||
#include <stimBuffApis/stimBuffApiManager.h>
|
||||
#include <spinscale/marionette.h>
|
||||
#include <marionette/marionette.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
Mind::Mind(void)
|
||||
: sscl::PuppetApplication(
|
||||
std::vector<std::shared_ptr<sscl::PuppetThread>>{
|
||||
std::make_shared<MindThread>(SmoThreadId::DIRECTOR),
|
||||
std::make_shared<MindThread>(SmoThreadId::SIMULATOR),
|
||||
std::make_shared<MindThread>(SmoThreadId::SUBCONSCIOUS),
|
||||
std::make_shared<MindThread>(SmoThreadId::BODY)
|
||||
std::make_shared<MindThread>(
|
||||
SmoThreadId::DIRECTOR,
|
||||
sscl::PuppetComponent::defaultPuppetMain, director,
|
||||
&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
|
||||
, std::make_shared<MindThread>(SmoThreadId::WORLD)
|
||||
, std::make_shared<MindThread>(
|
||||
SmoThreadId::WORLD,
|
||||
sscl::PuppetComponent::defaultPuppetMain, world,
|
||||
&MindComponent::preJoltHook)
|
||||
#endif
|
||||
}
|
||||
),
|
||||
@@ -223,7 +239,7 @@ void Mind::initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
|
||||
auto request = std::make_shared<MindLifetimeMgmtOp>(
|
||||
*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<mindLifetimeMgmtOpCbFn> callback)
|
||||
auto request = std::make_shared<MindLifetimeMgmtOp>(
|
||||
*this, caller, callback);
|
||||
|
||||
sscl::pptr::mrntt.thread->getIoService().post(
|
||||
mrntt::mrntt.thread->getIoService().post(
|
||||
STC(std::bind(
|
||||
&MindLifetimeMgmtOp::finalizeReq1_posted,
|
||||
request.get(), request)));
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -6,7 +6,7 @@ namespace simulator {
|
||||
|
||||
Simulator::Simulator(
|
||||
Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
|
||||
: sscl::PuppetComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
|
||||
: MindComponent(static_cast<sscl::PuppetApplication&>(parent), thread)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <user/senseApiDesc.h>
|
||||
#include <mind.h>
|
||||
#include <deviceManager/deviceManager.h>
|
||||
#include <spinscale/marionette.h>
|
||||
#include <marionette/marionette.h>
|
||||
#include <computeManager/computeManager.h>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user