2024-09-04 14:08:50 +10:00
|
|
|
#ifndef _MIND_H
|
|
|
|
|
#define _MIND_H
|
|
|
|
|
|
2025-01-04 07:33:15 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
#include <thread>
|
2025-08-10 13:12:17 -04:00
|
|
|
#include <functional>
|
2025-09-03 14:43:00 -04:00
|
|
|
#include <memory>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <string>
|
2024-09-08 01:04:41 +10:00
|
|
|
|
2025-01-04 07:33:15 -04:00
|
|
|
#include <director/director.h>
|
|
|
|
|
#include <simulator/simulator.h>
|
2025-09-03 14:43:00 -04:00
|
|
|
#include <componentThread.h>
|
2024-09-04 14:08:50 +10:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
namespace smo {
|
2025-01-04 11:32:37 -04:00
|
|
|
|
2025-09-03 14:43:00 -04:00
|
|
|
class Mind : public std::enable_shared_from_this<Mind>
|
2024-09-04 14:08:50 +10:00
|
|
|
{
|
2025-01-04 13:21:33 -04:00
|
|
|
public:
|
2025-09-03 14:43:00 -04:00
|
|
|
Mind(void);
|
|
|
|
|
~Mind(void) = default;
|
2025-08-10 13:12:17 -04:00
|
|
|
|
2025-07-30 09:09:38 -04:00
|
|
|
void initialize(void);
|
2025-01-04 13:21:33 -04:00
|
|
|
void execute(void);
|
2025-08-10 13:12:17 -04:00
|
|
|
void finalizeReq(std::function<void()> callback);
|
2025-01-04 13:21:33 -04:00
|
|
|
|
2025-09-03 14:43:00 -04:00
|
|
|
// 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();
|
|
|
|
|
|
2024-09-04 14:08:50 +10:00
|
|
|
public:
|
2025-01-04 07:33:15 -04:00
|
|
|
std::thread directorThread;
|
|
|
|
|
std::thread simulatorThread;
|
|
|
|
|
std::thread subconsciousThread;
|
2024-09-04 14:08:50 +10:00
|
|
|
|
2025-01-04 07:33:15 -04:00
|
|
|
director::Director director;
|
2025-01-04 18:17:19 -04:00
|
|
|
simulator::Simulator canvas;
|
2025-09-03 14:43:00 -04:00
|
|
|
|
|
|
|
|
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;
|
2024-09-04 14:08:50 +10:00
|
|
|
};
|
|
|
|
|
|
2025-09-03 14:43:00 -04:00
|
|
|
// Global Mind instance will be defined in marionette.cpp
|
|
|
|
|
extern std::shared_ptr<Mind> globalMind;
|
2025-08-10 13:12:17 -04:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
} // namespace smo
|
2025-01-04 11:32:37 -04:00
|
|
|
|
2024-09-04 14:08:50 +10:00
|
|
|
#endif
|