#ifndef COMPONENT_THREAD_H #define COMPONENT_THREAD_H #include #include #include #include #include namespace smo { 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 componentThreads; }; namespace director { extern ComponentThread director; } namespace simulator { extern ComponentThread canvas; } namespace subconscious { extern ComponentThread subconscious; } } // namespace smo #endif // COMPONENT_THREAD_H