// // Created by anton on 6/24/22. // #ifndef PMP_GLOBAL_THREAD_POOL_H #define PMP_GLOBAL_THREAD_POOL_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "call_frame.h" #include "helpers.h" namespace pmp::core { class global_thread_pool { private: call_frame* m_root_frame; // frame creation / lookup stuff std::atomic m_last_id; std::unordered_map m_frames; std::mutex m_frames_lock; // thread stuff std::atomic m_active; const size_t m_thread_count; std::vector m_threads; boost::lockfree::stack> stack; void worker(); public: explicit global_thread_pool(call_frame* root_frame) : m_root_frame(root_frame), m_last_id(0), m_frames(), m_frames_lock(), m_active(true), m_thread_count(std::thread::hardware_concurrency()), stack(100) { std::cout << "Init GlobalThreadPool" << std::endl; m_threads = std::vector(m_thread_count); for (size_t i = 0; i < m_thread_count; i++) { m_threads[i] = std::thread([&] { worker(); }); } } ~global_thread_pool() { if (m_active) { std::cout << "GlobalThreadPool destructor called before join!" << std::endl; join(); } assert(!m_active); } frame_id_t insert_frame(void *argument, void *scope, call_frame_target *target_function, frame_id_t parent); void stop() { m_active = false; } void join(); size_t thread_count() const { return m_thread_count; } size_t thread_count() { return m_thread_count; } }; } #endif //PMP_GLOBAL_THREAD_POOL_H