Timeout: use a plain callback, not an unnecessary coroutine

This commit is contained in:
Alexander A. Klimov 2024-11-26 15:04:27 +01:00
parent 92ab913226
commit faaeb4eb2e
2 changed files with 5 additions and 30 deletions

View File

@ -148,8 +148,6 @@ void AsioConditionVariable::Wait(boost::asio::yield_context yc)
void Timeout::Cancel()
{
m_Cancelled.store(true);
boost::system::error_code ec;
m_Timer.cancel(ec);
}

View File

@ -172,42 +172,19 @@ public:
template<class Executor, class TimeoutFromNow, class OnTimeout>
Timeout(boost::asio::io_context& io, Executor& executor, TimeoutFromNow timeoutFromNow, OnTimeout onTimeout)
: m_Timer(io)
: m_Timer(io, timeoutFromNow)
{
Ptr keepAlive (this);
m_Cancelled.store(false);
m_Timer.expires_from_now(std::move(timeoutFromNow));
IoEngine::SpawnCoroutine(executor, [this, keepAlive, onTimeout](boost::asio::yield_context yc) {
if (m_Cancelled.load()) {
return;
m_Timer.async_wait(boost::asio::bind_executor(executor, [onTimeout = std::move(onTimeout)](boost::system::error_code ec) {
if (!ec) {
onTimeout();
}
{
boost::system::error_code ec;
m_Timer.async_wait(yc[ec]);
if (ec) {
return;
}
}
if (m_Cancelled.load()) {
return;
}
auto f (onTimeout);
f();
});
}));
}
void Cancel();
private:
boost::asio::deadline_timer m_Timer;
std::atomic<bool> m_Cancelled;
};
}