Async/Await (CPS) & Multi-Core Parallelism
Stackless CPS async fn coroutines, std/async OS-thread tasks, timers, race/all combinators, and deterministic multi-core parallel collections in Wysna.
Esta página aún no está disponible en tu idioma.
Wysna provides two complementary concurrency models built on top of its thread-safe, lock-free atomic ARC + CoW runtime:
- Stackless CPS
async fn/awaitCoroutines: Cooperative asynchronous control flow lowered into verified MIR state machines across Seed C99, LLVM, and Selfhost Stage-2. - Deterministic Multi-Core OS Threading: Real OS-thread worker pools exposed through
std/async(spawn,all,race) andstd/algorithms(parallel_map,parallel_filter,parallel_reduce,parallel_batch) plusstd/http.batch_get.
1. Stackless CPS async fn & await
Section titled “1. Stackless CPS async fn & await”Declare asynchronous functions with async fn and suspend inside them with await. Coroutines use lazy deferred start and compile to compact state machines without allocating a separate OS stack per coroutine:
async fn load_rate(region: str) -> int ! str { if region == "EC" { return 15 } return Err(f"Unsupported region: {region}")}
async fn compute_tax(amount: int, region: str) -> int ! str { # await composes directly with ? and else err precedence let rate = await load_rate(region)? return (amount * rate) / 100}
fn main() -> unit { let tax = await compute_tax(1000, "EC") else err { print(f"Tax lookup failed: {err}") 0 } print(f"Tax: {tax}")}2. Structured Tasks & OS Threads (std/async)
Section titled “2. Structured Tasks & OS Threads (std/async)”Use std/async when you need background OS-thread execution (pthread on POSIX / CreateThread on Windows), cooperative timers, or task combinators:
import "std/async"
fn main() -> unit { let t1 = async.spawn(fn() -> int { return 21 * 2 }) let t2 = async.sleep_ms(25)
let results = async.all([t1, t2]) else err { print(f"Task failed: {err}") [] } print(results)}Key std/async APIs:
async.ready(value)/async.failed(err)— create immediately resolved or rejectedTask[T]handles.async.sleep_ms(ms) -> Task[int]— non-blocking monotonic timer task.async.spawn(fn() -> T) -> Task[T]— dispatch work onto a real OS thread with automatic result hydration.async.wait(task) -> T ! str,async.all(tasks) -> list[T] ! str,async.race(tasks) -> T ! str— await one, join all, or race tasks cleanly.async.poll_events(timeout_ms) -> int/async.is_ready(task) -> bool— non-blocking readiness inspection.
3. Deterministic Multi-Core Parallel Collections
Section titled “3. Deterministic Multi-Core Parallel Collections”Because Wysna’s collections use pure value semantics with atomic ARC + CoW, parallel workers cannot silently corrupt shared mutable state. std/algorithms preserves strict input index ordering identically to sequential execution:
from std.algorithms use { parallel_map, parallel_filter, parallel_reduce }
fn main() -> unit { let data = [1, 2, 3, 4, 5, 6, 7, 8]
let squares = parallel_map[int, int](data, fn(n: int) -> int { return n * n }) let evens = parallel_filter[int](squares, fn(n: int) -> bool { return n % 2 == 0 }) let total = parallel_reduce[int](evens, 0, fn(a: int, b: int) -> int { return a + b })
print(f"squares={squares}, total={total}")}For concurrent HTTP fetching, std/http.batch_get(urls) and std/http.batch_get_all(urls) dispatch requests across a bounded worker pool while preserving exact input URL order.