fibers B1.2 COMPLETE: async/await/cancel examples (1805/1806)

With the three surface blockers fixed (0151 generic inference, 0152
Atomic(bool), 0153 re-export failable channel), the M:1 async surface works
end-to-end on the blocking Io default. Landed the corpus examples:

- 1805-concurrency-io-blocking-async.sx: context.io.async(lambda, ..args)
  runs the worker inline, await() or {…} yields the result; context.io.now_ms()
  reads the monotonic clock. Prints sum: 42 / double: 42 / clock ok.
- 1806-concurrency-io-cancel.sx: f.cancel() marks the future canceled so a
  later await() raises error.Canceled out of its (R, !IoErr) channel, caught
  with or. Prints ok: 7 / canceled: -99.

B1.2 (Io capability on Context + async/await/cancel + blocking CBlockingIo) is
complete. Suite green 732/0. Next: B1.3 (fiber runtime).
This commit is contained in:
agra
2026-06-21 05:59:04 +03:00
parent 68c1991e11
commit 37d68e72be
9 changed files with 106 additions and 61 deletions

View File

@@ -0,0 +1,23 @@
// B1.2 — the async ergonomic layer over the `Io` capability, blocking
// default. `context.io.async(worker, ..args)` runs the worker to completion
// inline and returns a `.ready` Future($R); `f.await()` yields the result
// (a value-failable `($R, !IoErr)`, handled with `or`). `context.io.now_ms()`
// reads the monotonic clock through the same capability.
//
// Worker form: a lambda whose params are annotated at the call site
// (`(a: i64, b: i64) -> i64 => …`); `..args` forwards the call-site
// arguments to it.
#import "modules/std.sx";
main :: () {
// Homogeneous args.
s := context.io.async((a: i64, b: i64) -> i64 => a + b, 40, 2);
print("sum: {}\n", s.await() or { -1 });
// Single arg.
d := context.io.async((x: i64) -> i64 => x * 2, 21);
print("double: {}\n", d.await() or { -1 });
// The Io capability also carries a monotonic clock.
if context.io.now_ms() >= 0 { print("clock ok\n"); }
}

View File

@@ -0,0 +1,17 @@
// B1.2 — cancellation rides the `!` error channel (model (a)). `f.cancel()`
// sets the per-future cancel flag + marks `state = .canceled`, so a
// subsequent `f.await()` raises `error.Canceled` out of its value-failable
// `($R, !IoErr)` — caught here with `or`. A future that is NOT canceled
// awaits its value normally.
#import "modules/std.sx";
main :: () {
// Not canceled → await yields the value.
ok := context.io.async((n: i64) -> i64 => n, 7);
print("ok: {}\n", ok.await() or { -1 });
// Canceled → await raises .Canceled → the `or` default is taken.
c := context.io.async((n: i64) -> i64 => n, 7);
c.cancel();
print("canceled: {}\n", c.await() or { -99 });
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,3 @@
sum: 42
double: 42
clock ok

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,2 @@
ok: 7
canceled: -99