Split: Cleanly split spinscale off from SMO

Remaining areas to split off:
* The handleLoopException, exceptionInd logic.
* getThreadName().
This commit is contained in:
2026-02-22 17:38:53 -04:00
parent e6a924a3f7
commit a7521f3760
9 changed files with 320 additions and 106 deletions

View File

@@ -2,8 +2,8 @@
#define COMPONENT_H
#include <config.h>
#include <atomic>
#include <memory>
#include <functional>
#include <spinscale/callback.h>
#include <spinscale/puppetApplication.h>
@@ -40,13 +40,22 @@ class PuppetComponent
: public Component
{
public:
virtual void handleLoopExceptionHook() = 0;
PuppetComponent(
PuppetApplication &parent,
const std::shared_ptr<PuppetThread> &thread);
~PuppetComponent() = default;
static void defaultPuppetMain(const PuppetThread::EntryFnArguments &args);
public:
PuppetApplication &parent;
protected:
virtual void postJoltHook() {}
virtual void preLoopHook() {}
virtual void postLoopHook() {}
};
namespace pptr {
@@ -55,10 +64,26 @@ class PuppeteerComponent
: public Component
{
public:
virtual void handleLoopExceptionHook() = 0;
PuppeteerComponent(const std::shared_ptr<PuppeteerThread> &thread);
~PuppeteerComponent() = default;
static void defaultPuppeteerMain(
const PuppeteerThread::EntryFnArguments &args);
protected:
virtual void postJoltHook() {}
virtual void tryBlock1Hook() {}
virtual void preLoopHook() {}
virtual void postLoopHook() {}
virtual void postTryBlock1CatchHook() {}
virtual void handleTryBlock1TypedException(const std::exception& e);
virtual void handleTryBlock1UnknownException();
};
extern std::atomic<int> exitCode;
} // namespace pptr
} // namespace sscl

View File

@@ -19,9 +19,14 @@
namespace sscl {
class PuppetComponent;
class PuppeteerThread;
class PuppetThread;
namespace pptr {
class PuppeteerComponent;
}
// ThreadId is a generic type - application-specific enums should be defined elsewhere
typedef uint8_t ThreadId;
@@ -29,8 +34,7 @@ class ComponentThread
{
protected:
ComponentThread(ThreadId _id)
: id(_id), name(getThreadName(_id)),
work(io_service)
: id(_id), name(getThreadName(_id)), work(io_service)
{}
public:
@@ -45,9 +49,11 @@ public:
static const std::shared_ptr<ComponentThread> getSelf(void);
static bool tlsInitialized(void);
static std::shared_ptr<PuppeteerThread> getMrntt();
typedef void (mainFn)(ComponentThread &self);
static void setPuppeteerThread(const std::shared_ptr<PuppeteerThread> &t);
static void setPuppeteerThreadId(ThreadId id);
static std::shared_ptr<PuppeteerThread> getPptr();
static std::shared_ptr<PuppeteerThread> getPuppeteer()
{ return getPptr(); }
// CPU management methods
static int getAvailableCpuCount();
@@ -71,16 +77,44 @@ class PuppeteerThread
public ComponentThread
{
public:
PuppeteerThread(ThreadId id = 0)
: ComponentThread(id),
thread(main, std::ref(*this))
{
}
typedef void (*preJoltHookFn)(PuppeteerThread &);
struct EntryFnArguments
{
PuppeteerThread &usableBeforeJolt;
/** EXPLANATION:
* The `Puppet*Component` ref points at the Component object which this
* thread is associated with. However, we have no guarantee that this
* object has been constructed at the point of OS thread entry.
*
* Hence this ref must be dereferenced only after JOLT.
*/
pptr::PuppeteerComponent &useOnlyAfterJolt;
preJoltHookFn preJoltHook;
};
using entryPointFn = std::function<void(const EntryFnArguments &)>;
PuppeteerThread(
ThreadId id, entryPointFn entryPoint,
pptr::PuppeteerComponent &component,
preJoltHookFn preJoltFn)
: ComponentThread(id),
entryFnArguments(*this, component, preJoltFn),
thread(std::move(entryPoint), std::cref(entryFnArguments))
{}
static void main(PuppeteerThread& self);
void initializeTls(void);
void exitLoop(void);
public:
EntryFnArguments entryFnArguments;
/** EXPLANATION:
* Must always be memberwise-initialized last.
* This ensures that the ref to this `ComponentThread` object, which is
* passed to the entry point function, is fully constructed when the OS
* thread begins executing.
*/
std::thread thread;
};
@@ -89,6 +123,20 @@ class PuppetThread
public ComponentThread
{
public:
typedef void (*preJoltHookFn)(PuppetThread &);
struct EntryFnArguments
{
PuppetThread &usableBeforeJolt;
/** See comment above in:
* PuppeteerThread::EntryFnArguments::useOnlyAfterJolt.
*/
PuppetComponent &useOnlyAfterJolt;
preJoltHookFn preJoltHook;
};
using entryPointFn = std::function<void(const EntryFnArguments &)>;
enum class ThreadOp
{
START,
@@ -99,17 +147,18 @@ public:
N_ITEMS
};
PuppetThread(ThreadId _id)
: ComponentThread(_id),
PuppetThread(
ThreadId _id, entryPointFn entryPoint, PuppetComponent &component,
preJoltHookFn preJoltFn)
: ComponentThread(_id),
pinnedCpuId(-1),
pause_work(pause_io_service),
thread(main, std::ref(*this))
{
}
entryFnArguments(*this, component, preJoltFn),
thread(std::move(entryPoint), std::cref(entryFnArguments))
{}
virtual ~PuppetThread() = default;
static void main(PuppetThread& self);
void initializeTls(void);
// Thread management methods
@@ -137,17 +186,16 @@ public:
// CPU management methods
void pinToCpu(int cpuId);
protected:
/**
* Handle exception - called from main() when an exception occurs.
* Derived classes can override to provide application-specific handling.
*/
virtual void handleException() {}
public:
int pinnedCpuId;
boost::asio::io_service pause_io_service;
boost::asio::io_service::work pause_work;
public:
EntryFnArguments entryFnArguments;
/** Must always be memberwise-initialized last.
* See comment on `PuppeteerThread::thread` for explanation.
*/
std::thread thread;
public:
@@ -156,11 +204,7 @@ public:
namespace pptr {
extern std::shared_ptr<PuppeteerThread> thread;
// Forward declaration for puppeteer thread ID management
// Must be after sscl namespace so ThreadId is defined
extern ThreadId puppeteerThreadId;
void setPuppeteerThreadId(ThreadId id);
} // namespace pptr
} // namespace sscl

View File

@@ -1,59 +0,0 @@
#ifndef _MARIONETTE_H
#define _MARIONETTE_H
#include <cstdint>
#include <atomic>
#include <memory>
#include <spinscale/component.h>
namespace sscl {
class PuppeteerThread;
extern std::shared_ptr<sscl::PuppeteerThread> thread;
namespace pptr {
class MarionetteComponent
: public sscl::Component
{
public:
MarionetteComponent(const std::shared_ptr<sscl::ComponentThread> &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();
private:
class MrnttLifetimeMgmtOp;
class TerminationEvent;
};
extern std::atomic<int> exitCode;
void exitMarionetteLoop();
void marionetteFinalizeReqCb(bool success);
extern MarionetteComponent mrntt;
} // namespace pptr
struct CrtCommandLineArgs
{
CrtCommandLineArgs(int argc, char *argv[], char *envp[])
: argc(argc), argv(argv), envp(envp)
{}
int argc;
char **argv;
char **envp;
static void set(int argc, char *argv[], char *envp[]);
};
extern CrtCommandLineArgs crtCommandLineArgs;
} // namespace sscl
#endif // _MARIONETTE_H

View File

@@ -0,0 +1,23 @@
#ifndef RUNTIME_H
#define RUNTIME_H
namespace sscl {
struct CrtCommandLineArgs
{
CrtCommandLineArgs(int argc, char *argv[], char *envp[])
: argc(argc), argv(argv), envp(envp)
{}
int argc;
char **argv;
char **envp;
static void set(int argc, char *argv[], char *envp[]);
};
extern CrtCommandLineArgs crtCommandLineArgs;
} // namespace sscl
#endif // RUNTIME_H