70 lines
1.7 KiB
C++
70 lines
1.7 KiB
C++
|
|
#include <iostream>
|
||
|
|
#include <componentThread.h>
|
||
|
|
|
||
|
|
namespace hk {
|
||
|
|
|
||
|
|
namespace director {
|
||
|
|
ComponentThread director;
|
||
|
|
}
|
||
|
|
namespace simulator {
|
||
|
|
ComponentThread canvas;
|
||
|
|
}
|
||
|
|
namespace subconscious {
|
||
|
|
ComponentThread subconscious;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::unordered_map<std::thread::id, ComponentThread&>
|
||
|
|
ComponentThread::componentThreads =
|
||
|
|
{
|
||
|
|
{director::director.thread.get_id(), director::director},
|
||
|
|
{simulator::canvas.thread.get_id(), simulator::canvas},
|
||
|
|
{subconscious::subconscious.thread.get_id(), subconscious::subconscious}
|
||
|
|
};
|
||
|
|
|
||
|
|
void ComponentThread::signalThread(std::thread::id id)
|
||
|
|
{
|
||
|
|
auto it = componentThreads.find(id);
|
||
|
|
if (it == componentThreads.end())
|
||
|
|
{
|
||
|
|
throw std::runtime_error(std::string(__func__)
|
||
|
|
+ ": Thread ID not found in componentThreads map");
|
||
|
|
}
|
||
|
|
|
||
|
|
ComponentThread& componentThread = it->second;
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(componentThread.startupSync.mutex);
|
||
|
|
componentThread.startupSync.ready = true;
|
||
|
|
}
|
||
|
|
componentThread.startupSync.cv.notify_one();
|
||
|
|
}
|
||
|
|
|
||
|
|
void ComponentThread::main(ComponentThread& self)
|
||
|
|
{
|
||
|
|
// We sleep on spawn until the main thread tells us to continue.
|
||
|
|
{
|
||
|
|
std::unique_lock<std::mutex> lock(self.startupSync.mutex);
|
||
|
|
self.startupSync.cv.wait(lock, [&self]() {
|
||
|
|
return self.startupSync.ready;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
std::cout << __func__ << ": Starting event loop." << std::endl;
|
||
|
|
self.getIoService().run();
|
||
|
|
std::cout << __func__ << ": Exiting." << std::endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ComponentThread::validateThreadIds(void)
|
||
|
|
{
|
||
|
|
for (const auto& [id, componentThread] : componentThreads)
|
||
|
|
{
|
||
|
|
// std::thread::id() is usable as an invalid ID.
|
||
|
|
if (id == std::thread::id())
|
||
|
|
{
|
||
|
|
throw std::runtime_error(
|
||
|
|
std::string(__func__) + ": Invalid Thread ID.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace hk
|