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,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 {
|
||||
|
||||
Reference in New Issue
Block a user