36c79f3a2e
This commit significantly restructures the way we setup threading in SMO. We now don't use the CRT main() thread at all. It's only used as a mechanism to ensure that Marionette doesn't execute before global constructors have been executed. JOLTing: This is a simple ASIO post()ed message that makes each thread setup its thread-local data pointer to its own ComponentThread object, and then enter its main ASIO run() loop to await commands from Marionette. Exception bubbling: We now cleanly cause mind threads to report their exceptions to marionette, so that marionette can cleanly shut the mind down in an orderly fashion. Thread Control messaging API: A namespace of asynchronous messages to be post()ed to threads to control them. It enables us to pause and resume threads. This will be very useful for Marionette when we add the ability for it to suspend Salmanoff's running mind, inject new goals, inspect current state, etc; and then resume the mind's execution.
133 lines
3.2 KiB
C++
133 lines
3.2 KiB
C++
#ifndef COMPONENT_THREAD_H
|
|
#define COMPONENT_THREAD_H
|
|
|
|
#include <atomic>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
#include <boost/asio.hpp>
|
|
#include <stdexcept>
|
|
#include <queue>
|
|
#include <functional>
|
|
|
|
namespace smo {
|
|
|
|
class ComponentThread
|
|
: public std::enable_shared_from_this<ComponentThread>
|
|
{
|
|
public:
|
|
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),
|
|
thread(
|
|
((id == MRNTT) ? marionetteMain : main),
|
|
std::ref(*this))
|
|
{}
|
|
|
|
void cleanup(void);
|
|
|
|
boost::asio::io_service& getIoService(void) { return io_service; }
|
|
|
|
void initializeTls(void);
|
|
const std::shared_ptr<ComponentThread> getSelf(void);
|
|
|
|
static std::shared_ptr<ComponentThread> getComponentThread(
|
|
ThreadId id = N_ITEMS)
|
|
{
|
|
if (id < 0 || id > N_ITEMS)
|
|
{
|
|
throw std::runtime_error(std::string(__func__)
|
|
+ ": Invalid thread ID");
|
|
}
|
|
return componentThreads[id];
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
|
|
|
|
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);
|
|
void exceptionInd(ComponentThread& thread);
|
|
|
|
public:
|
|
ThreadId id;
|
|
std::string name;
|
|
boost::asio::io_service io_service;
|
|
boost::asio::io_service::work work;
|
|
boost::asio::io_service pause_io_service;
|
|
boost::asio::io_service::work pause_work;
|
|
std::atomic<bool> keepLooping;
|
|
|
|
/* Always ensure that this is last so that the thread is spawned after
|
|
* everything else is constructed.
|
|
*/
|
|
std::thread thread;
|
|
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];
|
|
}
|
|
};
|
|
|
|
namespace mrntt {
|
|
extern std::shared_ptr<ComponentThread> mrntt;
|
|
}
|
|
namespace director {
|
|
extern std::shared_ptr<ComponentThread> director;
|
|
}
|
|
namespace simulator {
|
|
extern std::shared_ptr<ComponentThread> canvas;
|
|
}
|
|
namespace subconscious {
|
|
extern std::shared_ptr<ComponentThread> subconscious;
|
|
}
|
|
namespace body {
|
|
extern std::shared_ptr<ComponentThread> body;
|
|
}
|
|
namespace world {
|
|
extern std::shared_ptr<ComponentThread> world;
|
|
}
|
|
|
|
} // namespace smo
|
|
|
|
#endif // COMPONENT_THREAD_H
|