libunifex icon indicating copy to clipboard operation
libunifex copied to clipboard

Generalise coroutine task<T> type to support passing through receiver context queries to parent receiver/coroutine

Open lewissbaker opened this issue 4 years ago • 1 comments

We would ideally like to be able to define coroutine tasks that can pass through context from the caller to child coroutines to allow passing things like stop_token, current scheduler, allocator transparently through algorithms implemented as coroutines in the same way that we do for most senders.

Since coroutines are inherently type-erased, this will mean that we need to add the ability to parameterise the task type on the set of query CPOs that should be passed through from the parent context.

template<typename T, typename... CPOs>
class task { ... };

template<typename T>
using cancellable_task = task<T,
  overload<inplace_stop_token(const this_&) noexcept>(get_stop_token)>>;

This will probably require hooking up the adaption of senders to the SenderAwaitable type to turn them into awaitables in the await_transform() rather than relying on operator co_await() so that we have the type-information from the promise early enough when defining the coroutine-receiver type to pass to the sender's connect() implementation.

Note that for some CPOs we may need to implement some special logic to adapt/type-erase across the boundary. eg. we might want to adapt from whatever stop-token type the caller has to the target stop-token type by attaching a stop_callback to the caller's stop-token. Some investigation will be required to determine what the appropriate strategy should be here.

lewissbaker avatar Mar 13 '20 20:03 lewissbaker

This has been partially implemented in #170 and #172, which add the ability to pass through stop-tokens and also have the co_await inject a receiver that forwards queries onto the promise.

Still needs some work to allow task<T> to be further parameterised with a list of other CPOs to forward through.

lewissbaker avatar Aug 22 '20 17:08 lewissbaker