Libspinscale: begin splitting it off
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#ifndef _BODY_COMPONENT_H
|
||||
#define _BODY_COMPONENT_H
|
||||
|
||||
#include <component.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <functional>
|
||||
#include <callback.h>
|
||||
#include <spinscale/callback.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef COMPONENT_H
|
||||
#define COMPONENT_H
|
||||
|
||||
#include <config.h>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <callback.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
class Mind;
|
||||
class ComponentThread;
|
||||
|
||||
class Component
|
||||
{
|
||||
public:
|
||||
Component(const std::shared_ptr<ComponentThread> &thread);
|
||||
~Component() = default;
|
||||
|
||||
public:
|
||||
std::shared_ptr<ComponentThread> thread;
|
||||
|
||||
public:
|
||||
};
|
||||
|
||||
class MindComponent
|
||||
: public Component
|
||||
{
|
||||
public:
|
||||
MindComponent(Mind &parent, const std::shared_ptr<ComponentThread> &thread);
|
||||
~MindComponent() = default;
|
||||
|
||||
public:
|
||||
Mind &parent;
|
||||
};
|
||||
|
||||
} // namespace smo
|
||||
|
||||
#endif // COMPONENT_H
|
||||
@@ -1,178 +1,25 @@
|
||||
#ifndef COMPONENT_THREAD_H
|
||||
#define COMPONENT_THREAD_H
|
||||
#ifndef SMO_COMPONENT_THREAD_H
|
||||
#define SMO_COMPONENT_THREAD_H
|
||||
|
||||
#include <boostAsioLinkageFix.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <boost/asio/io_service.hpp>
|
||||
#include <stdexcept>
|
||||
#include <queue>
|
||||
#include <functional>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
#include <memory>
|
||||
#include <callback.h>
|
||||
#include <string>
|
||||
// Include the spinscale componentThread.h which provides ComponentThread, PuppetThread, etc.
|
||||
#include <spinscale/componentThread.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
class Mind; // Forward declaration
|
||||
class MarionetteThread;
|
||||
class MindThread;
|
||||
|
||||
class ComponentThread
|
||||
// Application-specific thread ID enum
|
||||
// Using regular enum (not enum class) to allow implicit conversion to ThreadId
|
||||
enum SmoThreadId : ThreadId
|
||||
{
|
||||
public:
|
||||
enum ThreadId
|
||||
{
|
||||
MRNTT = 0,
|
||||
DIRECTOR,
|
||||
SIMULATOR,
|
||||
SUBCONSCIOUS,
|
||||
BODY,
|
||||
WORLD,
|
||||
N_ITEMS
|
||||
};
|
||||
|
||||
protected:
|
||||
ComponentThread(ThreadId _id)
|
||||
: id(_id), name(getThreadName(_id)),
|
||||
work(io_service)
|
||||
{}
|
||||
|
||||
public:
|
||||
virtual ~ComponentThread() = default;
|
||||
|
||||
void cleanup(void);
|
||||
|
||||
boost::asio::io_service& getIoService(void) { return io_service; }
|
||||
|
||||
static const std::shared_ptr<ComponentThread> getSelf(void);
|
||||
static bool tlsInitialized(void);
|
||||
static std::shared_ptr<MarionetteThread> getMrntt();
|
||||
|
||||
typedef void (mainFn)(ComponentThread &self);
|
||||
|
||||
// CPU management methods
|
||||
static int getAvailableCpuCount();
|
||||
|
||||
typedef std::function<void()> mindShutdownIndOpCbFn;
|
||||
// Intentionally doesn't take a callback.
|
||||
void exceptionInd(const std::shared_ptr<ComponentThread> &faultyThread);
|
||||
// Intentionally doesn't take a callback.
|
||||
void userShutdownInd();
|
||||
|
||||
public:
|
||||
ThreadId id;
|
||||
std::string name;
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_service::work work;
|
||||
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");
|
||||
}
|
||||
|
||||
// Use function-local static to ensure proper initialization order
|
||||
static const std::string threadNames[N_ITEMS] = {
|
||||
"mrntt",
|
||||
"director",
|
||||
"simulator",
|
||||
"subconscious",
|
||||
"body",
|
||||
"world"
|
||||
};
|
||||
|
||||
return threadNames[id];
|
||||
}
|
||||
MRNTT = 0,
|
||||
DIRECTOR,
|
||||
SIMULATOR,
|
||||
SUBCONSCIOUS,
|
||||
BODY,
|
||||
WORLD,
|
||||
N_ITEMS
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
void startThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
||||
void exitThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
||||
void pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
||||
void resumeThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void joltThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback);
|
||||
|
||||
// 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:
|
||||
class ThreadLifetimeMgmtOp;
|
||||
class MindShutdownIndOp;
|
||||
};
|
||||
|
||||
namespace mrntt {
|
||||
extern std::shared_ptr<MarionetteThread> thread;
|
||||
} // namespace mrntt
|
||||
|
||||
} // namespace smo
|
||||
|
||||
#endif // COMPONENT_THREAD_H
|
||||
#endif // SMO_COMPONENT_THREAD_H
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <sstream>
|
||||
#include <user/deviceAttachmentSpec.h>
|
||||
#include <deviceManager/deviceRole.h>
|
||||
#include <qutex.h>
|
||||
#include <spinscale/qutex.h>
|
||||
|
||||
namespace smo {
|
||||
namespace device {
|
||||
|
||||
@@ -10,12 +10,12 @@
|
||||
#include <functional>
|
||||
#include <user/senseApiDesc.h>
|
||||
#include <user/deviceAttachmentSpec.h>
|
||||
#include <asynchronousLoop.h>
|
||||
#include <spinscale/asynchronousLoop.h>
|
||||
#include <deviceManager/device.h>
|
||||
#include <deviceManager/deviceRole.h>
|
||||
#include <deviceManager/deviceReattacher.h>
|
||||
#include <callback.h>
|
||||
#include <qutex.h>
|
||||
#include <spinscale/callback.h>
|
||||
#include <spinscale/qutex.h>
|
||||
|
||||
namespace smo {
|
||||
namespace device {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <boost/asio/deadline_timer.hpp>
|
||||
#include <spinLock.h>
|
||||
#include <spinscale/spinLock.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#define DIRECTOR_H
|
||||
|
||||
#include <config.h>
|
||||
#include <component.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <goal.h>
|
||||
#include <lruLifo.h>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <component.h>
|
||||
#include <spinscale/component.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
|
||||
@@ -7,10 +7,11 @@
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <callback.h>
|
||||
#include <spinscale/callback.h>
|
||||
|
||||
#include <component.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <componentThread.h>
|
||||
#include <mindThread.h>
|
||||
#include <director/director.h>
|
||||
#include <simulator/simulator.h>
|
||||
#include <body/body.h>
|
||||
@@ -28,8 +29,7 @@ public:
|
||||
void finalizeReq(Callback<mindLifetimeMgmtOpCbFn> callback);
|
||||
|
||||
// ComponentThread access methods
|
||||
std::shared_ptr<MindThread> getComponentThread(
|
||||
ComponentThread::ThreadId id) const;
|
||||
std::shared_ptr<MindThread> getComponentThread(ThreadId id) const;
|
||||
std::shared_ptr<MindThread> getComponentThread(
|
||||
const std::string& name) const;
|
||||
// Get all this Mind's component threads.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef MIND_THREAD_H
|
||||
#define MIND_THREAD_H
|
||||
|
||||
#include <memory>
|
||||
#include <componentThread.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
class Mind; // Forward declaration
|
||||
|
||||
class MindThread
|
||||
: public PuppetThread
|
||||
{
|
||||
public:
|
||||
MindThread(ThreadId _id, Mind& parent)
|
||||
: PuppetThread(_id),
|
||||
parent(parent)
|
||||
{
|
||||
}
|
||||
|
||||
Mind& getParent() const { return parent; }
|
||||
|
||||
protected:
|
||||
void handleException() override;
|
||||
|
||||
public:
|
||||
Mind& parent;
|
||||
};
|
||||
|
||||
} // namespace smo
|
||||
|
||||
#endif // MIND_THREAD_H
|
||||
@@ -2,7 +2,7 @@
|
||||
#define SIMULATOR_H
|
||||
|
||||
#include <config.h>
|
||||
#include <component.h>
|
||||
#include <spinscale/component.h>
|
||||
#include <simulator/scene.h>
|
||||
|
||||
namespace smo {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <dlfcn.h>
|
||||
#include <functional>
|
||||
#include <user/senseApiDesc.h>
|
||||
#include <qutex.h>
|
||||
#include <spinscale/qutex.h>
|
||||
|
||||
namespace smo {
|
||||
namespace stim_buff {
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <asynchronousLoop.h>
|
||||
#include <spinscale/asynchronousLoop.h>
|
||||
#include <stimBuffApis/stimBuffApiLib.h>
|
||||
#include <user/deviceAttachmentSpec.h>
|
||||
#include <callback.h>
|
||||
#include <qutex.h>
|
||||
#include <spinscale/callback.h>
|
||||
#include <spinscale/qutex.h>
|
||||
|
||||
namespace smo {
|
||||
namespace stim_buff {
|
||||
|
||||
Reference in New Issue
Block a user