2025-01-10 17:37:49 -04:00
|
|
|
#ifndef COMPONENT_THREAD_H
|
|
|
|
|
#define COMPONENT_THREAD_H
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
#include <atomic>
|
2025-01-10 17:37:49 -04:00
|
|
|
#include <thread>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
|
#include <stdexcept>
|
2025-07-28 07:20:44 -04:00
|
|
|
#include <queue>
|
|
|
|
|
#include <functional>
|
2025-08-03 09:18:45 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
#include <unistd.h>
|
2025-01-10 17:37:49 -04:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
namespace smo {
|
2025-01-10 17:37:49 -04:00
|
|
|
|
|
|
|
|
class ComponentThread
|
2025-07-28 07:20:44 -04:00
|
|
|
: public std::enable_shared_from_this<ComponentThread>
|
2025-01-10 17:37:49 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2025-07-28 07:20:44 -04:00
|
|
|
enum ThreadId
|
|
|
|
|
{
|
|
|
|
|
MRNTT = 0,
|
|
|
|
|
DIRECTOR,
|
|
|
|
|
SIMULATOR,
|
|
|
|
|
SUBCONSCIOUS,
|
|
|
|
|
BODY,
|
|
|
|
|
WORLD,
|
|
|
|
|
N_ITEMS
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ComponentThread(ThreadId id)
|
|
|
|
|
: id(id), name(getThreadName(id)),
|
|
|
|
|
work(io_service), pause_work(pause_io_service),
|
2025-08-03 09:18:45 -04:00
|
|
|
pinnedCpuId(-1),
|
2025-07-28 07:20:44 -04:00
|
|
|
thread(
|
|
|
|
|
((id == MRNTT) ? marionetteMain : main),
|
|
|
|
|
std::ref(*this))
|
2025-01-10 17:37:49 -04:00
|
|
|
{}
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
void cleanup(void);
|
|
|
|
|
|
2025-01-10 17:37:49 -04:00
|
|
|
boost::asio::io_service& getIoService(void) { return io_service; }
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
void initializeTls(void);
|
2025-08-03 08:22:45 -04:00
|
|
|
static const std::shared_ptr<ComponentThread> getSelf(void);
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
static std::shared_ptr<ComponentThread> getComponentThread(
|
|
|
|
|
ThreadId id = N_ITEMS)
|
2025-01-10 17:37:49 -04:00
|
|
|
{
|
2025-07-28 07:20:44 -04:00
|
|
|
if (id < 0 || id > N_ITEMS)
|
2025-01-10 17:37:49 -04:00
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
2025-07-28 07:20:44 -04:00
|
|
|
+ ": Invalid thread ID");
|
2025-01-10 17:37:49 -04:00
|
|
|
}
|
2025-07-28 07:20:44 -04:00
|
|
|
return componentThreads[id];
|
|
|
|
|
}
|
2025-01-10 17:37:49 -04:00
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
// Overload: search by name
|
|
|
|
|
static std::shared_ptr<ComponentThread> getComponentThread(
|
|
|
|
|
const std::string& name)
|
|
|
|
|
{
|
|
|
|
|
for (auto& thread : componentThreads) {
|
|
|
|
|
if (thread->name == name) { return thread; }
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Thread name not found in componentThreads map");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boost::asio::io_service& getEventLoop(
|
|
|
|
|
ThreadId id = MRNTT)
|
|
|
|
|
{
|
|
|
|
|
return getComponentThread(id)->getIoService();
|
2025-01-10 17:37:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
typedef void (mainFn)(ComponentThread &self);
|
|
|
|
|
static mainFn main, marionetteMain;
|
|
|
|
|
|
|
|
|
|
// Thread management methods
|
|
|
|
|
void startThreadReq(std::function<void()> callback = nullptr);
|
|
|
|
|
void exitThreadReq(std::function<void()> callback = nullptr);
|
|
|
|
|
void pauseThreadReq(std::function<void()> callback = nullptr);
|
|
|
|
|
void resumeThreadReq(std::function<void()> callback = nullptr);
|
2025-08-15 13:16:23 -04:00
|
|
|
/**
|
|
|
|
|
* JOLTs this thread to begin processing after global initialization.
|
|
|
|
|
*
|
|
|
|
|
* JOLTing is the mechanism that allows threads to enter their main
|
|
|
|
|
* event loops and set up TLS vars after all global constructors have
|
|
|
|
|
* completed. This prevents race conditions during system startup.
|
|
|
|
|
*/
|
2025-08-03 08:22:45 -04:00
|
|
|
void joltThreadReq(std::function<void()> callback = nullptr);
|
|
|
|
|
|
|
|
|
|
// Convenience wrappers
|
|
|
|
|
static void startAllMindThreadsReq(std::function<void()> callback = nullptr);
|
|
|
|
|
static void pauseAllMindThreadsReq(std::function<void()> callback = nullptr);
|
|
|
|
|
static void resumeAllMindThreadsReq(std::function<void()> callback = nullptr);
|
|
|
|
|
static void exitAllMindThreadsReq(std::function<void()> callback = nullptr);
|
|
|
|
|
static void joltAllMindThreadsReq(std::function<void()> callback = nullptr);
|
|
|
|
|
|
2025-08-03 09:18:45 -04:00
|
|
|
// CPU management methods
|
|
|
|
|
static int getAvailableCpuCount();
|
|
|
|
|
void pinToCpu(int cpuId);
|
|
|
|
|
static void distributeAndPinThreadsAcrossCpus();
|
|
|
|
|
|
2025-08-03 08:22:45 -04:00
|
|
|
enum class ThreadOp
|
|
|
|
|
{
|
|
|
|
|
START,
|
|
|
|
|
PAUSE,
|
|
|
|
|
RESUME,
|
|
|
|
|
EXIT,
|
|
|
|
|
JOLT,
|
|
|
|
|
N_ITEMS
|
|
|
|
|
};
|
|
|
|
|
static void execOpOnAllMindThreadsReq(
|
|
|
|
|
ThreadOp op, std::function<void()> callback = nullptr);
|
|
|
|
|
|
|
|
|
|
// Intentionally doesn't take a callback.
|
2025-07-28 07:20:44 -04:00
|
|
|
void exceptionInd(ComponentThread& thread);
|
2025-08-10 14:07:27 -04:00
|
|
|
// Intentionally doesn't take a callback.
|
|
|
|
|
void userShutdownInd();
|
2025-01-11 04:34:49 -04:00
|
|
|
|
2025-01-10 17:37:49 -04:00
|
|
|
public:
|
2025-07-28 07:20:44 -04:00
|
|
|
ThreadId id;
|
|
|
|
|
std::string name;
|
2025-01-10 17:37:49 -04:00
|
|
|
boost::asio::io_service io_service;
|
|
|
|
|
boost::asio::io_service::work work;
|
2025-07-28 07:20:44 -04:00
|
|
|
boost::asio::io_service pause_io_service;
|
|
|
|
|
boost::asio::io_service::work pause_work;
|
|
|
|
|
std::atomic<bool> keepLooping;
|
2025-08-03 09:18:45 -04:00
|
|
|
int pinnedCpuId;
|
2025-08-15 13:16:23 -04:00
|
|
|
/**
|
|
|
|
|
* 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 atomic flag ensures that JOLTing happens exactly once and provides
|
|
|
|
|
* a synchronization point for the entire system initialization.
|
|
|
|
|
*/
|
2025-08-10 13:29:34 -04:00
|
|
|
static std::atomic<bool> threadsHaveBeenJolted;
|
2025-01-10 17:37:49 -04:00
|
|
|
|
2025-01-11 04:34:49 -04:00
|
|
|
/* Always ensure that this is last so that the thread is spawned after
|
2025-07-28 07:20:44 -04:00
|
|
|
* everything else is constructed.
|
2025-01-11 04:34:49 -04:00
|
|
|
*/
|
|
|
|
|
std::thread thread;
|
2025-07-28 07:20:44 -04:00
|
|
|
static std::array<std::shared_ptr<ComponentThread>, ComponentThread::N_ITEMS>
|
|
|
|
|
componentThreads;
|
|
|
|
|
|
|
|
|
|
static const std::string threadNames[ComponentThread::N_ITEMS];
|
|
|
|
|
static const std::string getThreadName(ThreadId id)
|
|
|
|
|
{
|
|
|
|
|
if (id < 0 || id >= ComponentThread::N_ITEMS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Invalid thread ID");
|
|
|
|
|
}
|
|
|
|
|
return threadNames[id];
|
|
|
|
|
}
|
2025-01-10 17:37:49 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
namespace mrntt {
|
|
|
|
|
extern std::shared_ptr<ComponentThread> mrntt;
|
|
|
|
|
}
|
2025-01-10 17:37:49 -04:00
|
|
|
namespace director {
|
2025-07-28 07:20:44 -04:00
|
|
|
extern std::shared_ptr<ComponentThread> director;
|
2025-01-10 17:37:49 -04:00
|
|
|
}
|
|
|
|
|
namespace simulator {
|
2025-07-28 07:20:44 -04:00
|
|
|
extern std::shared_ptr<ComponentThread> canvas;
|
2025-01-10 17:37:49 -04:00
|
|
|
}
|
|
|
|
|
namespace subconscious {
|
2025-07-28 07:20:44 -04:00
|
|
|
extern std::shared_ptr<ComponentThread> subconscious;
|
|
|
|
|
}
|
|
|
|
|
namespace body {
|
|
|
|
|
extern std::shared_ptr<ComponentThread> body;
|
|
|
|
|
}
|
|
|
|
|
namespace world {
|
|
|
|
|
extern std::shared_ptr<ComponentThread> world;
|
2025-01-10 17:37:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
} // namespace smo
|
2025-01-10 17:37:49 -04:00
|
|
|
|
|
|
|
|
#endif // COMPONENT_THREAD_H
|