bffc32519b
Next step is to get the unified event loops working generically and then we can begin region-splitting up the data in harikoff. We'll assign all the global resource managers to Marionette and then assign the Mind components to the respective component threads.
70 lines
1.5 KiB
C++
70 lines
1.5 KiB
C++
#ifndef COMPONENT_THREAD_H
|
|
#define COMPONENT_THREAD_H
|
|
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
#include <condition_variable>
|
|
#include <boost/asio.hpp>
|
|
#include <stdexcept>
|
|
|
|
namespace hk {
|
|
|
|
class ComponentThread
|
|
{
|
|
public:
|
|
ComponentThread()
|
|
: work(io_service), startupSync(),
|
|
thread(ComponentThread::main, std::ref(*this))
|
|
{}
|
|
|
|
boost::asio::io_service& getIoService(void) { return io_service; }
|
|
|
|
static boost::asio::io_service& getEventLoop(
|
|
std::thread::id id = std::this_thread::get_id())
|
|
{
|
|
auto it = componentThreads.find(id);
|
|
if (it == componentThreads.end())
|
|
{
|
|
throw std::runtime_error(std::string(__func__)
|
|
+ ": Thread ID not found in componentThreads map");
|
|
}
|
|
|
|
return it->second.getIoService();
|
|
}
|
|
|
|
static void main(ComponentThread &self);
|
|
static void signalThread(std::thread::id id);
|
|
static void validateThreadIds(void);
|
|
|
|
public:
|
|
boost::asio::io_service io_service;
|
|
boost::asio::io_service::work work;
|
|
struct StartupSync {
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
bool ready;
|
|
|
|
StartupSync() : ready(false) {}
|
|
} startupSync;
|
|
|
|
/* Always ensure that this is last so that the thread is spawned after
|
|
* everything else.
|
|
*/
|
|
std::thread thread;
|
|
static std::unordered_map<std::thread::id, ComponentThread&> componentThreads;
|
|
};
|
|
|
|
namespace director {
|
|
extern ComponentThread director;
|
|
}
|
|
namespace simulator {
|
|
extern ComponentThread canvas;
|
|
}
|
|
namespace subconscious {
|
|
extern ComponentThread subconscious;
|
|
}
|
|
|
|
} // namespace hk
|
|
|
|
#endif // COMPONENT_THREAD_H
|