// SPDX-License-Identifier: Apache-2.0 // Copyright Pionix GmbH and Contributors to EVerest #ifndef EVENTQUEUE_HPP #define EVENTQUEUE_HPP #include #include #include #include #include namespace module { template class EventQueue { public: using events_t = std::vector; private: events_t pending; std::mutex mux; std::condition_variable cv; public: void push(const E& event) { { std::lock_guard lock(mux); pending.push_back(event); } cv.notify_all(); } events_t get_events() { std::lock_guard lock(mux); events_t active; pending.swap(active); return active; } events_t wait() { std::unique_lock ul(mux); cv.wait(ul, [this]() { return !pending.empty(); }); events_t active; pending.swap(active); ul.unlock(); return active; } template events_t wait_for(const std::chrono::duration& rel_time) { std::unique_lock ul(mux); if (!cv.wait_for(ul, rel_time, [this]() { return !pending.empty(); })) { return {}; } events_t active; pending.swap(active); return active; } }; } // namespace module #endif