boost::capy::context_awaitable_promise_base
CRTP mixin that adds I/O awaitable support to a promise type.
Synopsis
Declared in <boost/capy/ex/context_awaitable_promise_base.hpp>
template<typename Derived>
class context_awaitable_promise_base
: public frame_alloc_mixin
Description
Inherit from this class to enable these capabilities in your coroutine:
1. Frame allocation — The mixin provides operator new/delete that use the thread‐local frame allocator set by run_async.
2. Environment storage — The mixin stores a pointer to the await_context containing the executor, stop token, and allocator for this coroutine.
3. Environment access — Coroutine code can retrieve the environment via co_await this_coro::environment, or individual fields via co_await this_coro::executor, co_await this_coro::stop_token, and co_await this_coro::frame_allocator.
Basic Usage
For coroutines that need to access their execution environment:
// Both examples on this page show the same minimal promise_type
// deliberately: the point here is the mixin's environment access, not
// variations on promise plumbing.
struct my_task
{
struct promise_type : capy::context_awaitable_promise_base<promise_type>
{
my_task get_return_object()
{
return my_task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
// Resumes the awaiting coroutine by symmetric transfer, as the
// base class requires: the continuation stored by
// set_continuation() must be handed back here, or the awaiting
// coroutine is never resumed.
auto final_suspend() noexcept
{
struct awaiter
{
promise_type* p_;
bool await_ready() const noexcept { return false; }
std::coroutine_handle<>
await_suspend(std::coroutine_handle<>) const noexcept
{
return p_->continuation();
}
void await_resume() const noexcept {}
};
return awaiter{this};
}
void return_void() {}
// Capture rather than swallow. A real awaitable rethrows
// this from its own await_resume().
void unhandled_exception() noexcept { ep_ = std::current_exception(); }
std::exception_ptr ep_;
};
std::coroutine_handle<promise_type> h;
};
my_task example()
{
auto env = co_await capy::this_coro::environment;
// Access env->executor, env->stop_token, env->frame_allocator
// Or use fine-grained accessors:
auto ex = co_await capy::this_coro::executor;
auto token = co_await capy::this_coro::stop_token;
auto* alloc = co_await capy::this_coro::frame_allocator;
}
Custom Awaitable Transformation
If your promise needs to transform awaitables (e.g., for affinity or logging), override transform_awaitable instead of await_transform:
struct promise_type : capy::context_awaitable_promise_base<promise_type>
{
template<typename A>
auto transform_awaitable(A&& a)
{
// Your custom transformation logic
return std::forward<A>(a);
}
};
The mixin's await_transform intercepts this_coro::environment_tag and the fine‐grained tag types (this_coro::executor_tag, this_coro::stop_token_tag, this_coro::frame_allocator_tag), then delegates all other awaitables to your transform_awaitable.
Making Your Coroutine a ContextAwaitable
The mixin handles the "inside the coroutine" part—accessing the environment. To receive the environment when your coroutine is awaited (satisfying ContextAwaitable), implement the await_suspend overload on your coroutine return type:
// Same minimal promise_type as the Basic Usage example above -- this
// section's point is the await_suspend overload below, not the promise.
struct my_task
{
struct promise_type : capy::context_awaitable_promise_base<promise_type>
{
my_task get_return_object()
{
return my_task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
// Resumes the awaiting coroutine by symmetric transfer, as the
// base class requires: the continuation stored by
// set_continuation() must be handed back here, or the awaiting
// coroutine is never resumed.
auto final_suspend() noexcept
{
struct awaiter
{
promise_type* p_;
bool await_ready() const noexcept { return false; }
std::coroutine_handle<>
await_suspend(std::coroutine_handle<>) const noexcept
{
return p_->continuation();
}
void await_resume() const noexcept {}
};
return awaiter{this};
}
void return_void() {}
// Capture rather than swallow. A real awaitable rethrows
// this from its own await_resume().
void unhandled_exception() noexcept { ep_ = std::current_exception(); }
std::exception_ptr ep_;
};
std::coroutine_handle<promise_type> h_;
// ContextAwaitable await_suspend receives and stores the environment,
// then resumes into the coroutine body via symmetric transfer
std::coroutine_handle<> await_suspend(std::coroutine_handle<> cont, capy::await_context const* env)
{
h_.promise().set_continuation(cont);
h_.promise().set_environment(env);
return h_;
}
};
Ownership
A coroutine's frame is owned by the object that awaits it, such as the parent's task, so a chain is destroyed from its top down, each frame freeing the one it awaits. The suspended coroutine at the bottom is held by whoever will resume it: an operation, queue or primitive. A holder that will never resume it calls abandon, which unwinds the chain's segment from its owner (see chain_owner) rather than destroying the bottom frame. Destroying a frame that is still awaited, other than in that unwind, calls std::terminate.
Thread Safety
The environment is stored during await_suspend and read during co_await this_coro::environment. These occur on the same logical thread of execution, so no synchronization is required.
|
A chain waiting on a primitive stored in its own frames owns itself through that primitive; it is freed only by a stop request or by the primitive being signalled. A mutex must outlive every coroutine holding or handed its lock. A cross‐thread |
Base Classes
Name |
Description |
Mixin that adds frame‐allocator‐aware allocation to a promise type. |
Member Functions
Name |
Description |
Intercept co_await expressions. |
|
Return and consume the stored continuation handle. |
|
Return the stored execution environment. |
|
Store the continuation to resume on completion. |
|
Store a pointer to the execution environment. |
|
Transform an awaitable before co_await. |
Static Member Functions
Name |
Description |
Deallocate a coroutine frame. |
|
Allocate a coroutine frame. |
Derived Classes
Name |
Description |
Stores |