boost::capy::ContextAwaitable
Describes types that can be co_await‐ed in Capy‐coroutines and that can be passed the information about the execution environment.
Synopsis
template<typename A>
concept ContextAwaitable = std::move_constructible<A> &&
requires(
A a,
std::coroutine_handle<> h,
await_context const* env)
{
{ a.await_ready() } ‐> std::same_as<bool>;
{ a.await_suspend(h, env) } ‐> AwaitSuspendResult;
a.await_resume();
};
Description
See the tutorial section The ContextAwaitable Protocol for the description of the execution environment propagation mechanism in Capy.
await_ready
In the context of processing a co_await expression, says if the expression can be computed synchronously, without engaging the further awaiting machinery.
Returns:
-
truewhen the operation can be computed synchronously via immediately callingawait_resume, -
falsewhenawait_suspendneeds to be called.
await_suspend
In the context of processing a co_await expression, instructs the coroutine machinery, which coroutine needs to be launched or resumed.
preconditions: a.await_ready() == false.
Parameters:
-
h— the handle to the just suspended coroutine, -
env— the execution environment of the just suspended coroutine.
Effects: If this operations instructs a coroutine to be resumed, it shall make sure that the coroutine's promise type is passed the env parameter, in a manner specific to A.
Returns: The signature has one of the these return types: void, bool and std::coroutine_handle<P> for any type P.
If the return type is void, instructs the coroutine machinery that the control shall be returned to the resumer of the coroutine that invoked the co_await expression. Takes the ownership for scheduling the resumption of the coroutine represented by h via either env‐>executor.post or env‐>executor.dispatch.
If the return type is bool:
-
value
trueindicates the behavior equivalent to that of thevoidreturn type; -
value
falseinstructs the coroutine machinery to resume the coroutine represented byhand to immediately invokeawait_resume.
If the return type is std::coroutine_handle<P>, instructs the compiler to resume the coroutine represented by the returned handle, in a tail call manner (not consuming the stack).
Note:
-
Returning
his equivalent to returningfalsein theboolreturn type signature. -
Returning
std::noop_coroutine()is equivalent to using thevoidreturn type, returningtruein theboolreturn type signature.
Teardown: An awaitable that holds the coroutine for later, rather than resuming it before returning, keeps it as a continuation whose owner is env‐>owner. If the awaitable or whatever holds the coroutine for it is torn down first, it passes that continuation to abandon, which unwinds the chain from its owner. A continuation without an owner is destroyed alone, leaving the frames above it holding a destroyed frame.
Lifetime: The object of type await_context pointed to by env remains valid for the duration of the async operation represented by A. This is guarantee is maintained by run, run_async and the other functions that "start a task".
await_resume
In the context of processing a co_await expression, when the suspended coroutine is being resumed or upon immediate resumption, returns a value — if any — that shall be returned from the co_await expression.
If it throws an exception, the exception is propagated out of the co_await expression into the awaiting coroutine's scope.
Returns: value intended to be returned from the co_await expression. The return type determines the type of the enclosing co_await expression and can be void.
Example
The example demonstrates a "leaf" awaitable: one that is associated directly with a system's I/O operation but no coroutine.
class my_awaitable
{
capy::await_context const* env_ = nullptr;
capy::continuation cont_;
std::error_code ec_ {};
public:
bool await_ready() const noexcept { return false; }
std::coroutine_handle<>
await_suspend(std::coroutine_handle<> h, capy::await_context const* env) noexcept
{
env_ = env; // store the pointer, never a copy
cont_ = capy::continuation{h};
auto completion = [this](std::error_code ec) noexcept
{
ec_ = ec; // publish result; touch *this
env_->executor.post(cont_); // only before post, never after
};
start_my_io_op(env_->stop_token, completion);
return std::noop_coroutine(); // go back to scheduler
}
capy::results<> await_resume() const noexcept { return {ec_}; }
};
See Also
Created with MrDocs