Skip to content

Commit fa15fb7

Browse files
committed
fix(coro): stop spawning waiter threads for async futures
1 parent 358b042 commit fa15fb7

File tree

1 file changed

+6
-53
lines changed

1 file changed

+6
-53
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -342,40 +342,14 @@ class [[nodiscard("unnecessary construction")]] AwaitableScalar
342342
std::promise<T> _promise;
343343
};
344344

345-
[[nodiscard("unexpected call")]] bool await_ready() const noexcept
345+
[[nodiscard("unexpected call")]] constexpr bool await_ready() const noexcept
346346
{
347-
return std::visit(
348-
[](const auto& value) noexcept {
349-
using value_type = std::decay_t<decltype(value)>;
350-
351-
if constexpr (std::is_same_v<value_type, T>)
352-
{
353-
return true;
354-
}
355-
else if constexpr (std::is_same_v<value_type, std::future<T>>)
356-
{
357-
using namespace std::literals;
358-
359-
return value.wait_for(0s) != std::future_status::timeout;
360-
}
361-
else if constexpr (std::is_same_v<value_type,
362-
std::shared_ptr<const response::Value>>)
363-
{
364-
return true;
365-
}
366-
},
367-
_value);
347+
return true;
368348
}
369349

370350
void await_suspend(std::coroutine_handle<> h) const
371351
{
372-
std::thread(
373-
[this](std::coroutine_handle<> h) noexcept {
374-
std::get<std::future<T>>(_value).wait();
375-
h.resume();
376-
},
377-
std::move(h))
378-
.detach();
352+
h.resume();
379353
}
380354

381355
[[nodiscard("unnecessary construction")]] T await_resume()
@@ -472,35 +446,14 @@ class [[nodiscard("unnecessary construction")]] AwaitableObject
472446
std::promise<T> _promise;
473447
};
474448

475-
[[nodiscard("unexpected call")]] bool await_ready() const noexcept
449+
[[nodiscard("unexpected call")]] constexpr bool await_ready() const noexcept
476450
{
477-
return std::visit(
478-
[](const auto& value) noexcept {
479-
using value_type = std::decay_t<decltype(value)>;
480-
481-
if constexpr (std::is_same_v<value_type, T>)
482-
{
483-
return true;
484-
}
485-
else if constexpr (std::is_same_v<value_type, std::future<T>>)
486-
{
487-
using namespace std::literals;
488-
489-
return value.wait_for(0s) != std::future_status::timeout;
490-
}
491-
},
492-
_value);
451+
return true;
493452
}
494453

495454
void await_suspend(std::coroutine_handle<> h) const
496455
{
497-
std::thread(
498-
[this](std::coroutine_handle<> h) noexcept {
499-
std::get<std::future<T>>(_value).wait();
500-
h.resume();
501-
},
502-
std::move(h))
503-
.detach();
456+
h.resume();
504457
}
505458

506459
[[nodiscard("unnecessary construction")]] T await_resume()

0 commit comments

Comments
 (0)