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

Add blocking io_service::process_events_until_complete(task) function #30

Open
lewissbaker opened this issue Aug 12, 2017 · 2 comments
Open

Comments

@lewissbaker
Copy link
Owner

Once eager tasks are eliminated in #29 it will make it no longer possible to start executing a task on a single thread that enters the io_service::process_events() event loop.

The only way to start a task will be sync_wait(task) introduced in #27, however you can't then (easily) enter the process_events() event loop to process I/O completion events that are raised.

Adding an io_service::process_events_until_complete(task) function would allow starting a lazy_task and then entering the io_service event loop in such a way that it will exit from the event loop once the provided task completes.
eg.

lazy_task<> run(io_service& io);

int main()
{
  io_service io;
  io.process_events_until_complete(run());
  return 0;
}
@lewissbaker
Copy link
Owner Author

Bike-shedding:
Should the name of this method be similar to the sync_wait() function from #27?
eg. io_service::sync_wait(task)
Should it also return/unpack the result of the task (returning the value and/or rethrowing the exception).

@lewissbaker
Copy link
Owner Author

Technically, if you only wanted to have a single thread servicing the io_service events you could potentially use sync_wait() and when_all() to implement this.

eg.

lazy_task<> run(io_service& io);

int main()
{
  io_service io;
  sync_wait(when_all(
    [&]() -> lazy_task<>
    {
      auto stopIoServiceOnExit = on_scope_exit([&] { io.stop(); });
      co_await run();
    }(),
    [&]() -> lazy_task<>
    {
      io.process_events();
      co_return;
    }()));

  return 0; 
}

This does have the down-side that it doesn't allow re-entrant calls to the event-loop.
As soon as one task calls io_service::stop() then all event-loop calls will exit.

It would still be nice to wrap this up in a function, and potentially make it a bit more efficient.
eg. using cppcoro::detail::continuation-based callbacks rather than creating extra coroutine frames to setup the continuations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant