Rework: Modularize Mind
Now we have modularized the Mind class to contain all of its ComponentThreads. This enables us to run multiple mind instances within the same SMO process, at least in theory. We probably won't actually do this, but we want to ensure that the design is clean enough to enable it.
This commit is contained in:
+51
-3
@@ -4,21 +4,44 @@
|
||||
#include <config.h>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
#include <director/director.h>
|
||||
#include <simulator/simulator.h>
|
||||
#include <componentThread.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
class Mind
|
||||
class Mind : public std::enable_shared_from_this<Mind>
|
||||
{
|
||||
public:
|
||||
Mind(void) {}
|
||||
Mind(void);
|
||||
~Mind(void) = default;
|
||||
|
||||
void initialize(void);
|
||||
void execute(void);
|
||||
void finalizeReq(std::function<void()> callback);
|
||||
|
||||
// ComponentThread access methods
|
||||
std::shared_ptr<ComponentThread> getComponentThread(
|
||||
ComponentThread::ThreadId id) const;
|
||||
std::shared_ptr<ComponentThread> getComponentThread(
|
||||
const std::string& name) const;
|
||||
// Get all this Mind's component threads.
|
||||
std::vector<std::shared_ptr<ComponentThread>> getMindThreads() const;
|
||||
|
||||
// Thread management methods (moved from ComponentThread)
|
||||
void startAllMindThreadsReq(std::function<void()> callback = nullptr);
|
||||
void pauseAllMindThreadsReq(std::function<void()> callback = nullptr);
|
||||
void resumeAllMindThreadsReq(std::function<void()> callback = nullptr);
|
||||
void exitAllMindThreadsReq(std::function<void()> callback = nullptr);
|
||||
void joltAllMindThreadsReq(std::function<void()> callback = nullptr);
|
||||
|
||||
// CPU distribution method
|
||||
void distributeAndPinThreadsAcrossCpus();
|
||||
|
||||
public:
|
||||
std::thread directorThread;
|
||||
std::thread simulatorThread;
|
||||
@@ -26,9 +49,34 @@ public:
|
||||
|
||||
director::Director director;
|
||||
simulator::Simulator canvas;
|
||||
|
||||
private:
|
||||
/**
|
||||
* 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;
|
||||
// Collection of ComponentThread instances (excluding marionette)
|
||||
std::vector<std::shared_ptr<ComponentThread>> componentThreads;
|
||||
};
|
||||
|
||||
extern Mind mind;
|
||||
// Global Mind instance will be defined in marionette.cpp
|
||||
extern std::shared_ptr<Mind> globalMind;
|
||||
|
||||
} // namespace smo
|
||||
|
||||
|
||||
Reference in New Issue
Block a user