Function sync_wait
Synopsis
#include <lib/inc/drogon/utils/coroutine.h>
template <typename AWAIT>
auto sync_wait(AWAIT &&await)
Description
No description yet.
Source
Lines 399-452 in lib/inc/drogon/utils/coroutine.h.
template <typename AWAIT>
auto sync_wait(AWAIT &&await)
{
using value_type = typename await_result<AWAIT>::type;
std::condition_variable cv;
std::mutex mtx;
std::atomic<bool> flag = false;
std::exception_ptr exception_ptr;
if constexpr (std::is_same_v<value_type, void>)
{
[&, lk = std::unique_lock(mtx)]() -> AsyncTask {
try
{
co_await await;
}
catch (...)
{
exception_ptr = std::current_exception();
}
flag = true;
cv.notify_one();
}();
std::unique_lock lk(mtx);
cv.wait(lk, [&]() { return (bool)flag; });
if (exception_ptr)
std::rethrow_exception(exception_ptr);
}
else
{
optional<value_type> value;
[&, lk = std::unique_lock(mtx)]() -> AsyncTask {
try
{
value = co_await await;
}
catch (const std::exception &e)
{
exception_ptr = std::current_exception();
}
flag = true;
}();
std::unique_lock lk(mtx);
cv.wait(lk, [&]() { return (bool)flag; });
assert(value.has_value() == true || exception_ptr);
if (exception_ptr)
std::rethrow_exception(exception_ptr);
return value.value();
}
}