await — the readiness plane (L4 graph)

The third data call. await blocks until the vertex’s value is assigned again, or until the deadline expires. It is single-shot and lives wholly in the state plane: it observes assigns at its own vertex and takes no part in propagation (reference 02 §Assign, propagate, and the coalescing sweep). The mental model is epoll on one identity — read/write are the data plane, :field writes the control plane, await the readiness plane, all on one vertex (CONTEXT.md §Field-write).

What to notice

  • await is not subtree-scoped. A subscription observes its vertex and every descendant (vertical bubbling, RFC-0005); await does not. The example writes a descendant first and the waiter stays parked — the write to the vertex itself is what wakes it.

  • A timeout is a distinct answer. The second await expires and returns TIMEOUT, which is what lets a consumer distinguish a quiet vertex from a delivered value. An await carrying a :field selector is refused outright (SCHEMA_NOT_FOUND) rather than silently giving a whole-vertex wakeup — not exercised here, see reference 02 §Owner-declared application fields.

  • Ordering is the caller’s. The example sleeps 50 ms to let the waiter park, then join()s after the write, so every wake is complete and visible when the checks run. There is no stated ordering between a registration and a concurrent operation on the same address (reference 02 §Registration racing a concurrent operation) — a caller that needs one orders it itself.

Source

  1/*
  2 * SPDX-License-Identifier: Apache-2.0
  3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
  4 */
  5
  6/**
  7 * @file
  8 * @brief ONE CONCEPT — the readiness plane: `await` blocks until the next store here.
  9 *
 10 * `await` is the third of the three data calls (`CONTEXT.md` §read / write / await). It is
 11 * single-shot and lives wholly in the state plane: it observes assigns AT ITS OWN VERTEX and
 12 * is not subtree-scoped, so a write to a descendant does not wake it
 13 * (`docs/reference/02-graph-model.md` §Assign, propagate). A deadline that expires answers
 14 * `TIMEOUT`, which is why a consumer can tell a quiet vertex from a delivered value.
 15 *
 16 * Runs under ctest as `example_graph_await`; returns non-zero on any failed check.
 17 */
 18
 19#include <atomic>
 20#include <chrono>
 21#include <cstdint>
 22#include <cstdio>
 23#include <cstring>
 24#include <span>
 25#include <string_view>
 26#include <thread>
 27
 28#include "libtracer/tracer.hpp"
 29
 30namespace {
 31
 32using namespace std::chrono_literals;
 33using tr::graph::path_t;
 34using tr::graph::role_t;
 35using tr::graph::status_t;
 36
 37/** @brief An owned one-segment view over @p text. */
 38tr::view::view_t value_of(std::string_view text) {
 39    return *tr::view::over_bytes(std::as_bytes(std::span<const char>(text.data(), text.size())));
 40}
 41
 42/** @brief Report expectation @p what and record a failure on @p ok. */
 43void check(bool& ok, bool cond, const char* what) {
 44    std::printf("  [%s] %s\n", cond ? "ok" : "FAIL", what);
 45    ok = ok && cond;
 46}
 47
 48/** @brief This application's clock, in milliseconds: the `now` it hands the graph. */
 49std::uint64_t now_ms() {
 50    return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
 51                                          std::chrono::steady_clock::now().time_since_epoch())
 52                                          .count());
 53}
 54
 55/**
 56 * @brief The application's side of an await's deadline: pass `now` to expire_awaits until
 57 *        @p done is set. libtracer reads no clock, so this loop is what ends a wait that is due.
 58 */
 59void drive_until(tr::graph::graph_t& g, const std::atomic<bool>& done) {
 60    while (!done.load(std::memory_order_acquire)) {
 61        (void)g.expire_awaits(now_ms());
 62        std::this_thread::sleep_for(std::chrono::milliseconds(1));
 63    }
 64}
 65
 66}  // namespace
 67
 68int main() {
 69    tr::graph::graph_t g;
 70    bool ok = true;
 71
 72    const auto temp = g.register_vertex(path_t("/sensor/temp"), role_t::STORED_VALUE);
 73    const auto child = g.register_vertex(path_t("/sensor/temp/raw"), role_t::STORED_VALUE);
 74
 75    // A waiter parks in await(); the writer wakes it with the value it stored. Its deadline,
 76    // 2 s on this app's clock, bounds a write that lands before it parks: that costs a
 77    // TIMEOUT, never a hang.
 78    bool woke = false;
 79    std::atomic<bool> answered{false};
 80    std::thread waiter([&] {
 81        const std::uint64_t now = now_ms();
 82        const auto r = g.await(temp, now, now + 2000);
 83        woke = r && (*r)->only().bytes().size() == 5;
 84        answered.store(true, std::memory_order_release);
 85    });
 86    std::this_thread::sleep_for(50ms);  // let the waiter park before the write lands
 87
 88    (void)g.write(child, value_of("noise"));  // a DESCENDANT write — not this vertex
 89    (void)g.write(temp, value_of("21.5C"));
 90    drive_until(g, answered);
 91    waiter.join();
 92    check(ok, woke, "await returns the value assigned at its own vertex");
 93
 94    // await is not subtree-scoped, and the deadline is honoured: nothing writes /sensor/temp
 95    // again, so this one expires rather than picking up the descendant write above.
 96    tr::graph::result_t<tr::graph::value_ref_t> quiet;
 97    std::atomic<bool> done{false};
 98    std::thread late([&] {
 99        const std::uint64_t now = now_ms();
100        quiet = g.await(temp, now, now + 20);
101        done.store(true, std::memory_order_release);
102    });
103    drive_until(g, done);
104    late.join();
105    check(ok, !quiet && quiet.error() == status_t::TIMEOUT, "an expired deadline answers TIMEOUT");
106    std::printf("await woke once and timed out once, as expected\n");
107    return ok ? 0 : 1;
108}

See also: graph module · in-process pub/sub (the same primitive alongside subscriptions) · concurrency and scaling.