Skip to content
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

Merged
merged 6 commits into from
Aug 20, 2024
Merged

Conversation

canepat
Copy link
Member

@canepat canepat commented Aug 18, 2024

This PR rationalises and encapsulates the usage of boost::asio::co_spawn in our codebase with the following objectives:

  • define in one place which flavours of completion tokens for co_spawn we use (e.g. we should not use detached anywhere)
  • make the code at call site more readable and less verbose

The result is three different spawn functions:

  1. spawn: it's co_spawn called with use_future returning the future result to the caller (who decides at the call site if waiting or not)
  2. spawn_and_wait: same as above but returning the result after waiting for the future
  3. spawn_and_async_wait: it's co_spawn called with use_awaitable

The only exception to the above rules is in our custom versions of awaitable_wait_for_all and awaitable_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 custom spawn_deferred. I would like to hear what @battlmonstr thinks about this, though.

Extras

@canepat canepat marked this pull request as ready for review August 19, 2024 07:16
@canepat canepat added the maintenance Some maintenance work (fix, refactor, rename, test...) label Aug 19, 2024

template <typename Executor, typename F>
auto spawn_and_async_wait(const Executor& ex, F&& f,
typename boost::asio::constraint<
Copy link
Contributor

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 ?

Copy link
Member Author

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!

@battlmonstr
Copy link
Contributor

battlmonstr commented Aug 19, 2024

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:

  1. spawn_future
  2. spawn_future_and_wait or spawn_future_and_get (do we really need those std::future wrappers?)
  3. spawn or spawn_task (spawn_task is friendlier for "find-and-replace" later)

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.

@canepat
Copy link
Member Author

canepat commented Aug 20, 2024

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.

Sure, I think you have a good point here.

I suggest names like:

  1. spawn_future

I had this name in my first iteration locally, so it seems to be a good candidate!

  1. spawn_future_and_wait or spawn_future_and_get (do we really need those std::future wrappers?)

I'll check if this is used in many places or maybe only as test facility in test_util::ContextTestBase.

Recap:

  1. spawn_future
  2. spawn_future_and_wait (if needed)
  3. spawn_task

@battlmonstr do you prefer leaving awaitable_wait_for_all and awaitable_wait_for_one as they are to keep them closer to the original Asio code or introducing spawn_deferred? We use asio::deferred completion token only there.

@battlmonstr
Copy link
Contributor

@canepat yes, I'd leave it inside infra as is. It feels fine to use co_spawn directly inside the infra lib.

@canepat canepat merged commit cd716cd into master Aug 20, 2024
4 checks passed
@canepat canepat deleted the infra_encapsulate_co_spawn branch August 20, 2024 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Some maintenance work (fix, refactor, rename, test...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

infra: remove preprocessor define for co_spawn_sw
3 participants