mirror of
https://github.com/latentPrion/libspinscale.git
synced 2026-02-27 14:36:04 +00:00
Split: Cleanly split spinscale off from SMO
Remaining areas to split off: * The handleLoopException, exceptionInd logic. * getThreadName().
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <spinscale/puppetApplication.h>
|
||||
#include <spinscale/marionette.h>
|
||||
|
||||
namespace sscl {
|
||||
|
||||
Component::Component(const std::shared_ptr<ComponentThread> &thread)
|
||||
: thread(thread)
|
||||
: thread(thread)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -16,10 +18,76 @@ parent(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void PuppetComponent::defaultPuppetMain(
|
||||
const PuppetThread::EntryFnArguments &args)
|
||||
{
|
||||
PuppetThread &thr = args.usableBeforeJolt;
|
||||
PuppetComponent &comp = args.useOnlyAfterJolt;
|
||||
|
||||
if (args.preJoltHook) { args.preJoltHook(thr); }
|
||||
|
||||
/** FIXME:
|
||||
* Figure out why we don't call reset() here, and then explicitly document
|
||||
* it.
|
||||
*/
|
||||
thr.getIoService().run();
|
||||
thr.initializeTls();
|
||||
|
||||
comp.postJoltHook();
|
||||
comp.preLoopHook();
|
||||
|
||||
/* We loop here because when an exception is caught, we need to first catch
|
||||
* it in the catch blocks and invoke handleLoopExceptionHook so the
|
||||
* application can respond (e.g. notify a controller). We then re-enter
|
||||
* the loop to await control messages.
|
||||
*
|
||||
* We can't just exit on our own. Rather, keepLooping must be set to false
|
||||
* by the application when shutdown is desired.
|
||||
*/
|
||||
for (thr.keepLooping = true; thr.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.
|
||||
*/
|
||||
thr.getIoService().reset();
|
||||
thr.getIoService().run();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
sendExceptionInd = true;
|
||||
std::cerr << thr.name << ":" << __func__
|
||||
<< ": Exception occurred: " << e.what() << "\n";
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
sendExceptionInd = true;
|
||||
std::cerr << thr.name << ":" << __func__
|
||||
<< ": Unknown exception occurred" << "\n";
|
||||
}
|
||||
|
||||
if (sendExceptionInd)
|
||||
{
|
||||
comp.handleLoopExceptionHook();
|
||||
}
|
||||
}
|
||||
|
||||
comp.postLoopHook();
|
||||
}
|
||||
|
||||
namespace pptr {
|
||||
|
||||
MarionetteComponent::MarionetteComponent(
|
||||
const std::shared_ptr<sscl::ComponentThread> &thread)
|
||||
PuppeteerComponent::PuppeteerComponent(
|
||||
const std::shared_ptr<sscl::PuppeteerThread> &thread)
|
||||
: sscl::Component(thread)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -8,35 +8,48 @@
|
||||
#include <spinscale/asynchronousContinuation.h>
|
||||
#include <spinscale/callback.h>
|
||||
#include <spinscale/callableTracer.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <spinscale/marionette.h>
|
||||
|
||||
namespace sscl {
|
||||
|
||||
namespace pptr {
|
||||
/* Global variable to store the puppeteer thread ID
|
||||
* Default value is 0, but should be set by application code via
|
||||
* setPuppeteerThreadId().
|
||||
* ComponentThread::setPuppeteerThreadId().
|
||||
*/
|
||||
ThreadId puppeteerThreadId = 0;
|
||||
/* Global puppeteer thread instance - defined here but initialized by
|
||||
* application code.
|
||||
/* Global puppeteer thread instance - assigned by application code
|
||||
* (e.g. smo::mrntt::thread) via setPuppeteerThread().
|
||||
*/
|
||||
std::shared_ptr<PuppeteerThread> thread;
|
||||
|
||||
void setPuppeteerThreadId(ThreadId id)
|
||||
{
|
||||
puppeteerThreadId = id;
|
||||
}
|
||||
|
||||
} // namespace pptr
|
||||
|
||||
thread_local std::shared_ptr<ComponentThread> thisComponentThread;
|
||||
|
||||
// Implementation of static method
|
||||
std::shared_ptr<PuppeteerThread> ComponentThread::getMrntt()
|
||||
void ComponentThread::setPuppeteerThreadId(ThreadId id)
|
||||
{
|
||||
return sscl::pptr::thread;
|
||||
pptr::puppeteerThreadId = id;
|
||||
}
|
||||
|
||||
void ComponentThread::setPuppeteerThread(
|
||||
const std::shared_ptr<PuppeteerThread> &t
|
||||
)
|
||||
{
|
||||
pptr::thread = t;
|
||||
}
|
||||
|
||||
std::shared_ptr<PuppeteerThread> ComponentThread::getPptr()
|
||||
{
|
||||
return pptr::thread;
|
||||
}
|
||||
|
||||
void PuppeteerThread::exitLoop(void)
|
||||
{
|
||||
keepLooping = false;
|
||||
getIoService().stop();
|
||||
std::cout << name << ": Signaled main loop to exit." << "\n";
|
||||
}
|
||||
|
||||
void PuppeteerThread::initializeTls(void)
|
||||
@@ -191,7 +204,7 @@ void PuppetThread::joltThreadReq(
|
||||
+ ": invoked on puppeteer thread");
|
||||
}
|
||||
|
||||
std::shared_ptr<PuppeteerThread> puppeteer = sscl::pptr::thread;
|
||||
std::shared_ptr<PuppeteerThread> puppeteer = pptr::thread;
|
||||
|
||||
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
|
||||
puppeteer, selfPtr, callback);
|
||||
|
||||
99
src/puppeteerComponent.cpp
Normal file
99
src/puppeteerComponent.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <spinscale/componentThread.h>
|
||||
|
||||
namespace sscl {
|
||||
namespace pptr {
|
||||
|
||||
std::atomic<int> exitCode{0};
|
||||
|
||||
void PuppeteerComponent::defaultPuppeteerMain(
|
||||
const PuppeteerThread::EntryFnArguments &args)
|
||||
{
|
||||
PuppeteerThread &thr = args.usableBeforeJolt;
|
||||
PuppeteerComponent &comp = args.useOnlyAfterJolt;
|
||||
|
||||
if (args.preJoltHook) { args.preJoltHook(thr); }
|
||||
|
||||
thr.getIoService().reset();
|
||||
thr.getIoService().run();
|
||||
thr.initializeTls();
|
||||
|
||||
comp.postJoltHook();
|
||||
|
||||
try {
|
||||
comp.tryBlock1Hook();
|
||||
comp.preLoopHook();
|
||||
|
||||
/* We loop here because when an exception occurs, we need to
|
||||
* both direct the puppet 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 puppet
|
||||
* threads, and to post messages to our own event loop to
|
||||
* initiate our own orderly exit.
|
||||
*/
|
||||
for (thr.keepLooping = true; thr.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.
|
||||
*/
|
||||
thr.getIoService().reset();
|
||||
thr.getIoService().run();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
sendExceptionInd = true;
|
||||
std::cerr << thr.name << ":main: Exception occurred: "
|
||||
<< e.what() << "\n";
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
sendExceptionInd = true;
|
||||
std::cerr << thr.name
|
||||
<< ":main: Unknown exception occurred" << "\n";
|
||||
}
|
||||
|
||||
if (sendExceptionInd)
|
||||
{
|
||||
comp.handleLoopExceptionHook();
|
||||
}
|
||||
}
|
||||
|
||||
comp.postLoopHook();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
comp.handleTryBlock1TypedException(e);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
comp.handleTryBlock1UnknownException();
|
||||
}
|
||||
|
||||
comp.postTryBlock1CatchHook();
|
||||
}
|
||||
|
||||
void PuppeteerComponent::handleTryBlock1TypedException(const std::exception& e)
|
||||
{
|
||||
std::cerr << "main: Exception occurred: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
void PuppeteerComponent::handleTryBlock1UnknownException()
|
||||
{
|
||||
std::cerr << "main: Unknown exception occurred" << std::endl;
|
||||
}
|
||||
|
||||
} // namespace pptr
|
||||
} // namespace sscl
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <spinscale/marionette.h>
|
||||
#include <spinscale/runtime.h>
|
||||
|
||||
namespace sscl {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user