-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
infra: encapsulate usage of asio co_spawn #2248
Conversation
silkworm/infra/concurrency/spawn.hpp
Outdated
|
||
template <typename Executor, typename F> | ||
auto spawn_and_async_wait(const Executor& ex, F&& f, | ||
typename boost::asio::constraint< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nitpick, but perhaps this and all typenames below could be replaced by c++20 concept, i.e.:
requires boost::asio::is_executor<Executor>::value || boost::asio::execution::is_executor<Executor>::value
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I just copy-pasted from Asio to make it work in the first place, then I forgot to refactor. Thanks!
I'd like that co_spawn + use_awaitable be our default. Ideally it should have the shortest name. Spawn with future should only be used in async-sync adapters or tests, so a longer name should be fine. I suggest names like:
If you prefer to keep the refactoring as is, and not rename, at minimum consider that spawn_and_wait and spawn_and_async_wait names are a bit inconsistent/misleading, because they don't wait in the same sense. Nevertheless, overall this is a good idea to cut the useless 3rd parameter and simplify. |
Sure, I think you have a good point here.
I had this name in my first iteration locally, so it seems to be a good candidate!
I'll check if this is used in many places or maybe only as test facility in Recap:
@battlmonstr do you prefer leaving |
@canepat yes, I'd leave it inside infra as is. It feels fine to use co_spawn directly inside the infra lib. |
This PR rationalises and encapsulates the usage of
boost::asio::co_spawn
in our codebase with the following objectives:co_spawn
we use (e.g. we should not usedetached
anywhere)The result is three different spawn functions:
spawn
: it'sco_spawn
called withuse_future
returning the future result to the caller (who decides at the call site if waiting or not)spawn_and_wait
: same as above but returning the result after waiting for the futurespawn_and_async_wait
: it'sco_spawn
called withuse_awaitable
The only exception to the above rules is in our custom versions of
awaitable_wait_for_all
andawaitable_wait_for_one
, where I've deliberately decided to leave the code as similar as possible to Asio for ease of comparison, without introducing a customspawn_deferred
. I would like to hear what @battlmonstr thinks about this, though.Extras