Function sync_wait
Synopsis
#include <lib/inc/drogon/utils/coroutine.h>
template <typename Await>
auto sync_wait(Await &&await)
Description
No description yet.
Mentioned in
- Testing Framework / Asynchronous testing
Source
Lines 565-625 in lib/inc/drogon/utils/coroutine.h.
template <typename Await>
auto sync_wait(Await &&await)
{
static_assert(is_awaitable_v<std::decay_t<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;
std::unique_lock lk(mtx);
if constexpr (std::is_same_v<value_type, void>)
{
auto task = [&]() -> AsyncTask {
try
{
co_await await;
}
catch (...)
{
exception_ptr = std::current_exception();
}
std::unique_lock lk(mtx);
flag = true;
cv.notify_all();
};
std::thread thr([&]() { task(); });
cv.wait(lk, [&]() { return (bool)flag; });
thr.join();
if (exception_ptr)
std::rethrow_exception(exception_ptr);
}
else
{
optional<value_type> value;
auto task = [&]() -> AsyncTask {
try
{
value = co_await await;
}
catch (...)
{
exception_ptr = std::current_exception();
}
std::unique_lock lk(mtx);
flag = true;
cv.notify_all();
};
std::thread thr([&]() { task(); });
cv.wait(lk, [&]() { return (bool)flag; });
assert(value.has_value() == true || exception_ptr);
thr.join();
if (exception_ptr)
std::rethrow_exception(exception_ptr);
return std::move(value.value());
}
}