C++ · Receivers and Deferred Calls

Receivers and Deferred Calls

#include <uform/types.hpp> 4.0.0-rc.1 · c58749fe

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/uform/cpp/receivers-and-deferred-calls.md' | claude -p "How would this fit my code?"

The receiver decides where a verb runs and how it completes, while the verb and its arguments stay the same:

  • model.text_encode(..) runs on the calling thread.
  • model.on(executor).text_encode(..) blocks on the executor, failing status_code_t::contended_k while another call holds it.
  • model.deferred(executor).text_encode(..) submits the call and answers a deferred.

model<receiver> writes every verb once for all three: model_t owns the model and loads it, while on and deferred answer a model_on<completion> that refers to it and the executor, and trainer<receiver> does the same for a trainer. A blocking verb answers a status_t when it outputs nothing else and an expected<T> otherwise, so nothing about asynchrony reaches a caller who never asks for it. Each class spells that choice once, as its deferrable<T>, which names a deferred<T> under a deferred receiver.

uf::deferred<uf::encoded_t> call =
    model.deferred(executor).text_encode(texts, uf::encode_outputs_t::embeddings_k, uform_scalar_f32_k, arena); // running now
other_work();
auto [encoded, status] = call.wait(); // blocks until final; destruction waits too

Any coroutine type awaits a deferred call, with any loop offering post(void (*)(void*) noexcept, void*), so one model encodes on two executors at once:

app::task encode_both(uf::model_t const& model, uf::executor_t const& east_executor, uf::executor_t const& west_executor,
                      uf::tape_view<char> texts, app::loop& loop) {
    auto [east_arena, east_arena_status] = uf::arena_t::make({.executor = east_executor.handle()});
    auto [west_arena, west_arena_status] = uf::arena_t::make({.executor = west_executor.handle()});
    uf::deferred<uf::encoded_t> east_call = model.deferred(east_executor)
                                                .text_encode(texts, uf::encode_outputs_t::embeddings_k,
                                                             uform_scalar_f32_k, east_arena);
    uf::deferred<uf::encoded_t> west_call = model.deferred(west_executor)
                                                .text_encode(texts, uf::encode_outputs_t::embeddings_k,
                                                             uform_scalar_f32_k, west_arena);
    auto [east_encoded, east_status] = co_await east_call.wait(loop); // both calls were already running
    auto [west_encoded, west_status] = co_await west_call.wait(loop);
    if (east_status.failed() || west_status.failed()) co_return;
    merge(east_encoded, west_encoded);
}

A deferred never moves, since the library writes into it, so it lives on the caller’s stack or in the awaiting coroutine’s frame. status answers status_code_t::deferred_k until the call is final, then its final status, and wait() blocks until then and answers what a blocking call would have. co_await call.wait(loop) suspends the coroutine and re-polls the status through loop, any type with post(void (*)(void*) noexcept, void*), so the awaiter posts two words and allocates nothing, as Rust’s waker re-wakes itself. The library has no wake-up, so a scheduler runs other posted work between polls, and its own coroutine type owns the frame. Destroying an unfinished deferred waits for the call.

The library reads a deferred call’s inputs after the verb returns, so the tapes, spans, handlers and reports a deferred verb names must outlive the call, which C++ leaves to the caller. A deferred call allocates nothing: its outcome and outputs live inside the deferred itself. A refusal the SDK catches before the call crosses answers a deferred that is final from the start. An executor without threads of its own may run a deferred call at submission, final when the verb returns.

API

Types

awaiterA deferred being awaited, living in the awaiting coroutine's frame: each poll reads the status word, and one not yet final posts itself again.
types.hpp:176 ↗
template <typename value_type_,
          a_scheduler scheduler_type_>
class awaiter
answer_valueWhat a final answer_type_ carries beside its status, void for a bare status_t.
types.hpp:205 ↗
template <typename answer_type_>
struct answer_value
progress_tA progress handler the caller keeps for one call, passed as its own state; empty hears nothing.
types.hpp:510 ↗
class progress_t
ClassdeferredA submitted call: its outcome and its outputs, owned until the call is final, while the receiver, inputs, arena and handlers it was handed stay allocated and unchanged until then.7
types.hpp:118 ↗
template <typename value_type_ = void>
class deferred

It never moves, as the library writes into it, and its one owner polls status, blocks in wait or awaits wait on its own loop; destruction waits, so nothing the call writes into goes away under it.

answer_typeWhat wait answers: what the blocking call would have.
types.hpp:124 ↗
using answer_type = std::conditional_t<std::is_void_v<value_type_>, status_t, expected<value_type_>>;
deferredAn empty slot, final from the start, which resubmit fills.
types.hpp:127 ↗
deferred() noexcept=default;
template <typename submit_type_>
requires std::invocable<submit_type_ const&, uform_outcome_t*, outputs<value_type_>*>
deferred(submit_type_ const & submit) noexcept;

Runs submit with this call's outcome and outputs, where the library reports.

deferred(status_t status) noexcept;

A call refused before it crossed, final from the start.

deferred(deferred const &)=delete;
resubmitWaits only while the slot's previous call still runs, then runs submit in its place.
types.hpp:139 ↗
template <typename submit_type_>
requires std::invocable<submit_type_ const&, uform_outcome_t*, outputs<value_type_>*>
void resubmit(submit_type_ const & submit) noexcept;
operator=
types.hpp:150 ↗
deferred & operator=(deferred const &)=delete;
~deferred
types.hpp:151 ↗
~deferred() noexcept;
statusstatus_code_t::deferred_k until the call is final, then its final status, polled without blocking.
types.hpp:155 ↗
status_code_t status() noexcept;
waitBlocks until the call is final and answers what a blocking call would have.
types.hpp:158 ↗
answer_type wait() noexcept;
template <a_scheduler scheduler_type_>
awaiter<value_type_, scheduler_type_> wait(
    scheduler_type_ & loop) noexcept;

Awaits the call on loop, which polls its status until it is final.

Constants

is_deferredWhether answer_type_ is a call the caller holds rather than a final answer.
types.hpp:199 ↗
bool is_deferred;