From c40c15ee1117b2a2a3e1ca0b70e3c329b3181ea6 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Dec 2024 11:08:37 +0100 Subject: [PATCH] Introduce `AsioConditionVariable` --- lib/base/io-engine.cpp | 22 ++++++++++++++++++++++ lib/base/io-engine.hpp | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index 69bff7231..5284b518e 100644 --- a/lib/base/io-engine.cpp +++ b/lib/base/io-engine.cpp @@ -142,6 +142,28 @@ void AsioDualEvent::WaitForClear(boost::asio::yield_context yc) m_IsFalse.Wait(std::move(yc)); } +AsioConditionVariable::AsioConditionVariable(boost::asio::io_context& io) + : m_Timer(io) +{ + m_Timer.expires_at(decltype(m_Timer)::clock_type::time_point::max()); +} + +void AsioConditionVariable::Wait(boost::asio::yield_context yc) +{ + boost::system::error_code ec; + m_Timer.async_wait(yc[ec]); +} + +bool AsioConditionVariable::NotifyOne() +{ + return m_Timer.cancel_one(); +} + +size_t AsioConditionVariable::NotifyAll() +{ + return m_Timer.cancel(); +} + /** * Cancels any pending timeout callback. * diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index cf61b8595..285d4a6f4 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #if BOOST_VERSION >= 108700 # include @@ -56,6 +57,24 @@ private: bool m_Done; }; +/** + * Condition variable which doesn't block I/O threads + * + * @ingroup base + */ +class AsioConditionVariable +{ +public: + AsioConditionVariable(boost::asio::io_context& io); + + void Wait(boost::asio::yield_context yc); + bool NotifyOne(); + size_t NotifyAll(); + +private: + boost::asio::steady_timer m_Timer; +}; + /** * Async I/O engine *