boost::capy::ContextRunnable

Requires a ContextAwaitable that exposes handle and release, and whose promise takes a continuation and an environment from outside a coroutine.

Synopsis

template<typename T>
concept ContextRunnable = ContextAwaitable<T> &&
    requires { typename T::promise_type; } &&
    requires(T& t, T const& ct, typename T::promise_type const& cp, typename T::promise_type& p)
    {
        { ct.handle() } noexcept ‐> std::same_as<std::coroutine_handle<typename T::promise_type>>;
        { cp.exception() } noexcept ‐> std::same_as<std::exception_ptr>;
        { t.release() } noexcept;
        { p.set_continuation(std::coroutine_handle<>{}) } noexcept;
        { p.set_environment(static_cast<await_context const*>(nullptr)) } noexcept;
    } &&
    (std::is_void_v<decltype(std::declval<T&>().await_resume())> ||
     requires(typename T::promise_type& p) {
         p.result();
     });

Description

Extends ContextAwaitable with the operations that run and run_async need. Those operations start a task, transfer ownership of the coroutine frame, and retrieve results or exceptions after completion.

Syntactic Requirements

  • T must satisfy ContextAwaitable.

  • T::promise_type must be a valid type.

  • ct.handle() (callable on a const task) returns std::coroutine_handle<T::promise_type>, must be noexcept.

  • t.release() releases ownership, must be noexcept.

  • p.exception() returns std::exception_ptr, must be noexcept.

  • p.result() returns the task result (required for non‐void tasks).

  • p.set_continuation(h) sets the continuation handle, must be noexcept.

  • p.set_environment(env) sets the execution environment, must be noexcept.

Semantic Requirements

The handle operation provides access to the coroutine:

  • Returns the typed coroutine handle for the task's frame.

  • The task retains ownership; destroying the task destroys the frame.

The release operation transfers ownership:

  • After release(), destroying the task does not destroy the frame.

  • The caller becomes responsible for resuming and destroying the frame.

The exception operation retrieves failure state:

  • Returns the exception stored by the promise if the coroutine completed with an unhandled exception.

  • Returns nullptr if no exception was thrown.

The result operation retrieves success state (non‐void tasks):

  • Returns the value passed to co_return.

  • Behavior is undefined if called when exception() is non‐null.

The set_continuation operation establishes the continuation:

  • Sets the coroutine handle to resume when this task reaches final_suspend.

  • The functions that start a task use it to wire the task back to the trampoline.

The set_environment operation establishes the execution environment:

  • Sets the await_context pointer that propagates executor, stop token, and allocator through the coroutine chain.

  • The pointed‐to await_context must outlive the coroutine.

Requirements

A type awaited through run or the combinators must also take part in the ownership protocol:

  • The object owns the frame, and destroying the object destroys it; a destroyed frame never destroys its continuation. A chain given up with abandon is unwound from its owner downward, each object destroying the frame it owns.

  • Destroying the object, or assigning over it, while the frame is lent to an awaiting chain calls std::terminate, unless the chain's owner (environment()‐>owner) has been abandoned.

Conforming Signatures

class T
{
public:
    struct promise_type
    {
        std::exception_ptr exception() noexcept;
        int result();  // non-void tasks only
        void set_continuation(std::coroutine_handle<>) noexcept;
        void set_environment(capy::await_context const*) noexcept;
    };

    std::coroutine_handle<promise_type> handle() const noexcept;
    void release() noexcept;
};

Template Parameters

Name

Description

T

The task type.

See Also