62 lines
1.2 KiB
C++
62 lines
1.2 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()
|
||
|
|
{}
|
||
|
|
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
|
||
|
|
public:
|
||
|
|
boost::asio::io_service io_service;
|
||
|
|
boost::asio::io_service::work work;
|
||
|
|
std::thread thread;
|
||
|
|
struct StartupSync {
|
||
|
|
std::mutex mutex;
|
||
|
|
std::condition_variable cv;
|
||
|
|
bool ready;
|
||
|
|
|
||
|
|
StartupSync() : ready(false) {}
|
||
|
|
} startupSync;
|
||
|
|
|
||
|
|
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
|