spinscale: Move thread init/jolt/exit logic into PuppetApplication

This commit is contained in:
2025-12-27 14:01:15 -04:00
parent cd77f4b02d
commit f862db922e
5 changed files with 308 additions and 251 deletions
+4 -40
View File
@@ -2,14 +2,13 @@
#define _MIND_H
#include <config.h>
#include <thread>
#include <functional>
#include <memory>
#include <unordered_map>
#include <string>
#include <spinscale/callback.h>
#include <spinscale/component.h>
#include <spinscale/puppetApplication.h>
#include <componentThread.h>
#include <mindThread.h>
#include <director/director.h>
@@ -18,7 +17,8 @@
namespace smo {
class Mind : public std::enable_shared_from_this<Mind>
class Mind
: public PuppetApplication
{
public:
Mind(void);
@@ -35,24 +35,9 @@ public:
// Get all this Mind's component threads.
std::vector<std::shared_ptr<MindThread>> getMindThreads() const;
// Thread management methods (moved from ComponentThread)
typedef std::function<void()> mindThreadLifetimeMgmtOpCbFn;
void joltAllMindThreadsReq(Callback<mindThreadLifetimeMgmtOpCbFn> callback);
void startAllMindThreadsReq(
Callback<mindThreadLifetimeMgmtOpCbFn> callback);
void pauseAllMindThreadsReq(
Callback<mindThreadLifetimeMgmtOpCbFn> callback);
void resumeAllMindThreadsReq(
Callback<mindThreadLifetimeMgmtOpCbFn> callback);
void exitAllMindThreadsReq(Callback<mindThreadLifetimeMgmtOpCbFn> callback);
// CPU distribution method
void distributeAndPinThreadsAcrossCpus();
private:
// Collection of ComponentThread instances (excluding marionette)
std::vector<std::shared_ptr<MindThread>> componentThreads;
public:
director::Director director;
simulator::Simulator canvas;
@@ -62,31 +47,10 @@ public:
private:
friend class body::Body;
/**
* Indicates whether all mind threads have been JOLTed at least once.
*
* JOLTing serves two critical purposes:
*
* 1. **Global Constructor Sequencing**: Since pthreads begin executing while
* global constructors are still being executed, globally defined pthreads
* cannot depend on global objects having been constructed. JOLTing is done
* by the CRT's main thread within main(), which provides a sequencing
* guarantee that global constructors have been called.
*
* 2. **shared_from_this Safety**: shared_from_this() requires a prior
* shared_ptr handle to be established. The global list of
* shared_ptr<ComponentThread> guarantees that at least one shared_ptr to
* each ComponentThread has been initialized before JOLTing occurs.
*
* This flag ensures that JOLTing happens exactly once and provides
* a synchronization point for the entire system initialization.
*/
bool threadsHaveBeenJolted = false,
bodyComponentInitialized = false;
bool bodyComponentInitialized = false;
private:
class MindLifetimeMgmtOp;
class MindThreadLifetimeMgmtOp;
};
namespace mind {