Add coroutine to wait until event loop ends (#1500)

This commit is contained in:
Martin Chang 2023-03-09 10:13:18 +08:00 committed by GitHub
parent d4c0e063f1
commit ab5259b290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -697,6 +697,21 @@ struct [[nodiscard]] LoopAwaiter : CallbackAwaiter<void>
std::function<void()> taskFunc_;
};
struct [[nodiscard]] EndAwaiter : CallbackAwaiter<void>
{
EndAwaiter(trantor::EventLoop *loop) : loop_(loop)
{
assert(loop);
}
void await_suspend(std::coroutine_handle<> handle)
{
loop_->runOnQuit([handle]() { handle.resume(); });
}
private:
trantor::EventLoop *loop_{nullptr};
};
} // namespace internal
inline internal::TimerAwaiter sleepCoro(
@ -723,6 +738,12 @@ inline internal::LoopAwaiter queueInLoopCoro(
return {workLoop, std::move(taskFunc), resumeLoop};
}
inline internal::EndAwaiter untilQuit(trantor::EventLoop *loop)
{
assert(loop);
return {loop};
}
template <typename T, typename = std::void_t<>>
struct is_resumable : std::false_type
{