Introduce AsioConditionVariable

This commit is contained in:
Alexander A. Klimov 2024-12-05 11:08:37 +01:00
parent 659bb9e03a
commit 1e24adf247
2 changed files with 42 additions and 0 deletions

View File

@ -143,6 +143,30 @@ 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(boost::posix_time::pos_infin);
}
void AsioConditionVariable::Wait(boost::asio::yield_context yc)
{
boost::system::error_code ec;
m_Timer.async_wait(yc[ec]);
}
bool AsioConditionVariable::NotifyOne()
{
boost::system::error_code ec;
return m_Timer.cancel_one(ec);
}
size_t AsioConditionVariable::NotifyAll()
{
boost::system::error_code ec;
return m_Timer.cancel(ec);
}
/**
* Cancels any pending timeout callback.
*

View File

@ -55,6 +55,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::deadline_timer m_Timer;
};
/**
* Async I/O engine
*