2025-01-10 17:37:49 -04:00
|
|
|
#ifndef COMPONENT_THREAD_H
|
|
|
|
|
#define COMPONENT_THREAD_H
|
|
|
|
|
|
2025-11-03 22:18:45 -04:00
|
|
|
#include <boostAsioLinkageFix.h>
|
2025-07-28 07:20:44 -04:00
|
|
|
#include <atomic>
|
2025-01-10 17:37:49 -04:00
|
|
|
#include <thread>
|
|
|
|
|
#include <unordered_map>
|
2025-10-16 01:00:48 -04:00
|
|
|
#include <boost/asio/io_service.hpp>
|
2025-01-10 17:37:49 -04:00
|
|
|
#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-09-03 14:43:00 -04:00
|
|
|
#include <memory>
|
2025-09-27 18:30:09 -04:00
|
|
|
#include <callback.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
|
|
|
|
2025-09-03 14:43:00 -04:00
|
|
|
class Mind; // Forward declaration
|
2025-09-14 11:07:05 -04:00
|
|
|
class MarionetteThread;
|
|
|
|
|
class MindThread;
|
2025-09-03 14:43:00 -04:00
|
|
|
|
2025-01-10 17:37:49 -04:00
|
|
|
class ComponentThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
2025-07-28 07:20:44 -04:00
|
|
|
enum ThreadId
|
|
|
|
|
{
|
|
|
|
|
MRNTT = 0,
|
|
|
|
|
DIRECTOR,
|
|
|
|
|
SIMULATOR,
|
|
|
|
|
SUBCONSCIOUS,
|
|
|
|
|
BODY,
|
|
|
|
|
WORLD,
|
|
|
|
|
N_ITEMS
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-14 11:07:05 -04:00
|
|
|
protected:
|
|
|
|
|
ComponentThread(ThreadId _id)
|
|
|
|
|
: id(_id), name(getThreadName(_id)),
|
|
|
|
|
work(io_service)
|
2025-01-10 17:37:49 -04:00
|
|
|
{}
|
|
|
|
|
|
2025-09-14 11:07:05 -04:00
|
|
|
public:
|
|
|
|
|
virtual ~ComponentThread() = default;
|
|
|
|
|
|
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-08-03 08:22:45 -04:00
|
|
|
static const std::shared_ptr<ComponentThread> getSelf(void);
|
2025-11-06 21:40:32 -04:00
|
|
|
static bool tlsInitialized(void);
|
2025-09-14 11:07:05 -04:00
|
|
|
static std::shared_ptr<MarionetteThread> getMrntt();
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
typedef void (mainFn)(ComponentThread &self);
|
2025-08-03 08:22:45 -04:00
|
|
|
|
2025-08-03 09:18:45 -04:00
|
|
|
// CPU management methods
|
|
|
|
|
static int getAvailableCpuCount();
|
2025-08-03 08:22:45 -04:00
|
|
|
|
2025-09-11 18:41:45 -04:00
|
|
|
typedef std::function<void()> mindShutdownIndOpCbFn;
|
2025-08-03 08:22:45 -04:00
|
|
|
// Intentionally doesn't take a callback.
|
2025-09-11 18:41:45 -04:00
|
|
|
void exceptionInd(const std::shared_ptr<ComponentThread> &faultyThread);
|
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
|
|
|
std::atomic<bool> keepLooping;
|
|
|
|
|
|
|
|
|
|
static const std::string getThreadName(ThreadId id)
|
|
|
|
|
{
|
|
|
|
|
if (id < 0 || id >= ComponentThread::N_ITEMS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Invalid thread ID");
|
|
|
|
|
}
|
2025-09-03 14:43:00 -04:00
|
|
|
|
|
|
|
|
// Use function-local static to ensure proper initialization order
|
|
|
|
|
static const std::string threadNames[N_ITEMS] = {
|
|
|
|
|
"mrntt",
|
|
|
|
|
"director",
|
|
|
|
|
"simulator",
|
|
|
|
|
"subconscious",
|
|
|
|
|
"body",
|
|
|
|
|
"world"
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
return threadNames[id];
|
|
|
|
|
}
|
2025-09-14 11:07:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MarionetteThread
|
|
|
|
|
: public std::enable_shared_from_this<MarionetteThread>,
|
|
|
|
|
public ComponentThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MarionetteThread()
|
|
|
|
|
: ComponentThread(MRNTT),
|
|
|
|
|
thread(main, std::ref(*this))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void main(MarionetteThread& self);
|
|
|
|
|
void initializeTls(void);
|
2025-09-11 18:41:45 -04:00
|
|
|
|
2025-09-14 11:07:05 -04:00
|
|
|
public:
|
|
|
|
|
std::thread thread;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MindThread
|
|
|
|
|
: public std::enable_shared_from_this<MindThread>, public ComponentThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum class ThreadOp
|
|
|
|
|
{
|
|
|
|
|
START,
|
|
|
|
|
PAUSE,
|
|
|
|
|
RESUME,
|
|
|
|
|
EXIT,
|
|
|
|
|
JOLT,
|
|
|
|
|
N_ITEMS
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MindThread(ThreadId _id, Mind& parent)
|
|
|
|
|
: ComponentThread(_id),
|
|
|
|
|
pinnedCpuId(-1),
|
|
|
|
|
pause_work(pause_io_service),
|
|
|
|
|
parent(parent),
|
|
|
|
|
thread(main, std::ref(*this))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void main(MindThread& self);
|
|
|
|
|
void initializeTls(void);
|
|
|
|
|
|
|
|
|
|
Mind& getParent() const { return parent; }
|
|
|
|
|
|
|
|
|
|
// Thread management methods
|
|
|
|
|
typedef std::function<void()> threadLifetimeMgmtOpCbFn;
|
2025-09-27 18:30:09 -04:00
|
|
|
void startThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
|
|
|
|
void exitThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
|
|
|
|
void pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
|
|
|
|
void resumeThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
2025-09-14 11:07:05 -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-09-27 18:30:09 -04:00
|
|
|
void joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
2025-09-14 11:07:05 -04:00
|
|
|
|
|
|
|
|
// CPU management methods
|
|
|
|
|
void pinToCpu(int cpuId);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
int pinnedCpuId;
|
|
|
|
|
boost::asio::io_service pause_io_service;
|
|
|
|
|
boost::asio::io_service::work pause_work;
|
|
|
|
|
Mind& parent;
|
|
|
|
|
std::thread thread;
|
|
|
|
|
|
|
|
|
|
public:
|
2025-09-11 18:41:45 -04:00
|
|
|
class ThreadLifetimeMgmtOp;
|
|
|
|
|
class MindShutdownIndOp;
|
2025-01-10 17:37:49 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
namespace mrntt {
|
2025-09-14 22:17:19 -04:00
|
|
|
extern std::shared_ptr<MarionetteThread> thread;
|
|
|
|
|
} // namespace mrntt
|
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
|