Files
sx/examples/concurrency/1805-concurrency-io-blocking-async.sx
agra aae7d72a66 refactor: retire bespoke Task async; one stack behind context.io (Phase 5)
Converge the Io unification (PLAN-IO-UNIFY Phase 5). The bespoke fiber-task layer
in sched.sx — Task / TaskState / TaskErr / go / wait / cancel(Task), plus
Scheduler.task_allocs and its deinit bookkeeping (~130 lines) — is removed. There
is now ONE async stack: context.io.async / await / cancel / race / sleep over the
Io protocol, with the Scheduler as the fiber Io's engine + driver (spawn /
yield_now / suspend_self / wake / run / block_on_fd remain as the raw primitives;
race stays in sched.sx because it needs meta.sx's make_enum/make_variant).

Migrated the four go/wait users to context.io:
- 1813 — interleave + cancel (sequence 1 2 3 42 100 -99)
- 1817 — m1 end-to-end (completion in deadline order, sum 123)
- 1819 — double-AWAIT loud-abort via the Future one-awaiter guard
- 1820 — deinit: dropped the go/task_allocs tasks; now exercises timers/io_waiters/
  kq cleanup (freed=2, live=3 = the documented per-spawn closure-env residual)

Updated readme.md (the user-facing async section documents context.io.async /
await / race / sleep) and the stale sched.go/sched.Task comments in io.sx.

Suite 854/0; no .ir churn (Task removal touched no snapshotted IR); migrated
examples byte-identical on aarch64-macOS + aarch64-linux. PLAN-IO-UNIFY Phases 0-5
all complete — the two parallel async stacks are now one, behind context.io.
2026-06-28 10:14:17 +03:00

32 lines
1.4 KiB
Plaintext

// B1.2 / B2 — the async ergonomic layer over the `Io` capability, blocking
// default. `context.io.async(worker)` submits a NULLARY `worker: Closure() -> $R`
// and returns a `*Future($R)` handle; under the blocking `CBlockingIo` the worker
// runs to completion inline, so the Future is born `.ready`. `f.await()` yields
// the result (a value-failable `($R, !IoErr)`, handled with `or`).
// `context.io.now_ms()` reads the clock through the same capability.
//
// Worker form: a nullary failable lambda capturing any inputs at the CALL SITE
// (`() -> (i64, !) => compute(a, b)`) — the colorblind shape that also works when
// the worker is deferred onto a fiber (a captured variadic pack can't cross the
// fiber boundary).
#import "modules/std.sx";
main :: () {
// Inputs captured at the call site. The worker is FAILABLE
// (`Closure() -> ($R, !)`) — the unified Phase 3 shape; a body that never
// raises is a degenerate failable that always succeeds.
s := context.io.async(() -> (i64, !) => 40 + 2);
print("sum: {}\n", s.await() or { -1 });
d := context.io.async(() -> (i64, !) => 21 * 2);
print("double: {}\n", d.await() or { -1 });
// A worker that closes over a local.
base := 42;
n := context.io.async(() -> (i64, !) => base);
print("nullary: {}\n", n.await() or { -1 });
// The Io capability also carries a clock.
if context.io.now_ms() >= 0 { print("clock ok\n"); }
}