Files
bux-lang/examples/async.bux
T
dimgigov 5ec755743d feat: Phase 8.3 true non-blocking async/await with stackful coroutines
- C runtime: ucontext-based coroutines (bux_async_spawn/yield/await/run)
- Round-robin scheduler in bux_async_run() manages ready queue
- spawn Func() without args → bux_async_spawn() creates coroutine
- await → blocks/yields until coroutine completes
- async func → emitted as regular C function, runs on dedicated stack
- Interleaved execution: multiple coroutines yield cooperatively
- Example: examples/async.bux demonstrates WorkA/WorkB interleaving
- All 20 examples pass, selfhost build works
2026-06-01 01:06:46 +03:00

33 lines
730 B
Plaintext

import Std::Io::PrintLine;
extern func bux_async_yield();
extern func bux_async_run();
extern func bux_async_spawn(fn: *void) -> *void;
extern func bux_async_await(handle: *void);
async func WorkA() {
PrintLine("WorkA: step 1");
bux_async_yield();
PrintLine("WorkA: step 2");
bux_async_yield();
PrintLine("WorkA: done");
}
async func WorkB() {
PrintLine("WorkB: step 1");
bux_async_yield();
PrintLine("WorkB: step 2");
bux_async_yield();
PrintLine("WorkB: done");
}
func Main() -> int {
PrintLine("Main: start");
let h1: *void = spawn WorkA();
let h2: *void = spawn WorkB();
bux_async_await(h1);
bux_async_await(h2);
PrintLine("Main: all done");
return 0;
}