Concurrency
xu f(args) schedules a coroutine. Scheduling determines which ready task can make progress; parallelism means tasks execute at the same time on different CPU threads. Spawning a coroutine alone promises neither an execution order nor CPU parallelism. The spelling xu.pkg.f is a package-qualified call, not a spawn.
All blocks below were checked with the official SDK v0.10.12. Save each as a separate .xu file. Use xu check for complete programs and xu check --lib for function-only examples. Expected diagnostics are actual substrings. Complete programs also passed native xu run; browser Run uses the WASM subset described below.
Scheduling and rendezvous
A zero-capacity channel needs a sender and a receiver to rendezvous. A receive on an open empty channel waits; a buffered send waits when the buffer is full. These channel waits let the scheduler run other ready coroutines. Use a channel to wait for a result before main returns, rather than assuming that spawn means completion.
@main
fn worker(id int, ch chan int) {
ch <- id * 10
}
fn main() int {
ch := chan int{cap: 0}
xu worker(1, ch)
println(<-ch)
<< 0
}
Expected xu check: pass. xu run prints 10.
Bounded-Stack 16KB Micro-Coroutines & Zero-Copy Rendezvous (0.13.3+)
Xu 0.13.3 introduces deep optimizations for channel rendezvous and task scheduling:
- Direct Stack-to-Stack Zero-Copy Rendezvous: When a receiver waits on an unbuffered (
cap: 0) channel, its destination stack slot address is directly registered with the channel; the waking sender dereferences and copies the value directly into the receiver stack, bypassing intermediate heap and buffer allocations. - Call-Graph Bounded Stack Analysis & 16KB Tiny Slab: The compiler derives whole-program maximum call stack depths and local frame bounds; coroutines proven to have bounded stack usage without recursive growth are automatically allocated from a dedicated 16KB Tiny Slab pool (
task_spawn_tiny), reducing high-concurrency memory footprints by 98.4%.
Ordonnanceur multicœur M:N et #sync/pool (0.14.4+ / 0.15.0)
L’ordonnancement de coroutines multicœur M:N (#sync/pool) est activé par défaut sous Linux et macOS à partir de Xu 0.14.4, permettant l’exécution parallèle des coroutines sur plusieurs threads OS :
- Ordonnancement multicœur par défaut (0.14.4+) : configurez et consultez le parallélisme de l’ordonnanceur via
pool.set_max_procs(N)etpool.max_procs(), ou créez des pools de workers bornés avecpool.new_thread_pool(N). Le cache d’allocation par worker, la synchronisation thread-safe du tas, des appels FFI, des signaux et des E/S, ainsi que la gestion propre detask_drain()/task_yield()maintiennent l’ordre des minuteurs en arrière-plan et des attentes d’E/S lorsquemainse termine. - Durcissement de l’ordonnanceur et des canaux (0.15.0) : Xu 0.15.0 corrige les livelocks de drainage d’
EventSet, optimise les chemins rapides des canaux tamponnés et ajoute une isolation par ligne de cache sur l’état workerMainsi que des intrinsèques atomiques matériels ARM64/x86-64.
Send and receive: transfer owned data, share channel handles
ch <- value sends and <-ch receives. Sending an owning value transfers ownership; scalars copy. Channel handles may be passed to workers and still used by the caller. References, including references nested inside containers, cannot cross the xu or channel boundary. This does not establish safety for arbitrary shared state in foreign code.
@main
fn main() int {
ch := chan string{cap: 1}
text := "owned"
ch <- text
println(<-ch)
<< 0
}
Expected xu check: pass. xu run prints owned.
@main
fn bad() {
ch := chan string{cap: 1}
text := "owned"
ch <- text
println(text)
}
Expected xu check --lib: fail; diagnostic containing use of moved value 'text'.
@main
fn worker(value &int) {}
fn bad() {
value := 1
xu worker(&value)
}
Expected xu check --lib: fail; diagnostic containing xu argument cannot be a reference type (M17).
Close: drain buffered values, then receive zero
close(ch) marks the channel closed; it does not discard buffered data. This integer channel first returns the queued 7, then 0 without waiting. Do not use zero as a unique end marker if zero is valid data; define an application-level message/completion protocol. Designate one closer after all sends finish, and close only once.
@main
fn main() int {
ch := chan int{cap: 1}
ch <- 7
close(ch)
println(<-ch)
println(<-ch)
<< 0
}
Expected xu check: pass. xu run prints 7, then 0.
@main
fn bad() {
close(1)
}
Expected xu check --lib: fail; diagnostic containing close requires a channel, found int.
Sending on a closed channel is a runtime error, not a static ownership error. This function passes checking; a native test harness calling it exited with status 1. The browser evaluator reports send on closed channel. It is deliberately not a runnable example.
@main
fn wrong() {
ch := chan int{cap: 1}
close(ch)
ch <- 1
}
Expected xu check --lib: pass.
Select: wait for readiness, or probe without blocking
Each arm is a send or receive followed by its body. Without else, select waits until an operation is ready. With else, it immediately takes that branch if no operation is ready: this is a non-blocking readiness probe, not a timeout. Receiving from a closed empty channel is ready and yields zero. Do not depend on arm order or fairness when several arms are ready.
@main
fn main() int {
ch := chan int{cap: 1}
select {
ch <- 7 { println("sent") }
else { println("full") }
}
select {
value := <-ch { println(value) }
else { println("empty") }
}
select {
value := <-ch { println(value) }
else { println("empty") }
}
<< 0
}
Expected xu check: pass. xu run prints sent, then 7, then empty.
A select must contain at least one communication arm; else alone is invalid.
@main
fn bad() {
select {
else { println("idle") }
}
}
Expected xu check --lib: fail; diagnostic containing select requires at least one send/recv case.
Cancellation and blocking are separate concerns
Local SDK only. Cancellation is cooperative: a task must observe the signal. Here the caller closes a dedicated stop channel; the worker can observe it while waiting for a job because both operations are in the same select. A separate acknowledgement lets the caller wait for worker completion. No job is sent, so only the stop arm becomes ready.
@main
fn worker(jobs chan int, stop chan int, done chan int) {
select {
signal := <-stop { println("stopped") }
job := <-jobs { println(job) }
}
done <- 1
}
fn main() int {
jobs := chan int{cap: 0}
stop := chan int{cap: 0}
done := chan int{cap: 0}
xu worker(jobs, stop, done)
close(stop)
println(<-done)
<< 0
}
Expected xu check: pass. xu run prints stopped, then 1.
Closing stop does not forcibly interrupt another channel receive or an unrelated blocking OS/FFI call. Put cancellation in the same select as the operation being awaited, or use that API's documented cancellation mechanism. Release owned resources and explicitly dispose native handles on cancellation as on every other exit. If work and cancellation are both ready, allow for the work arm to win; cancellation has no priority guarantee.
A successful check does not prove progress. This function has no sender or closer, so its receive cannot complete in isolation. It is checked as a library fragment and is not executed.
@main
fn wait_without_sender() int {
ch := chan int{cap: 0}
<< <-ch
}
Expected xu check --lib: pass.
A busy loop repeatedly taking else does not wait for work. Prefer a blocking select with a stop arm when waiting is the intended behavior. CPU-only work and blocking host calls also need an explicit progress/cancellation design; xu is not a universal non-blocking wrapper.
Native and browser WASM boundaries
| Concern | Native SDK | This site's browser Playground |
|---|---|---|
| Execution | Custom AOT backend; hosted runtime has OS worker threads and can run coroutines in parallel. | MIR interpreter inside WASM; tasks are scheduled cooperatively in a browser worker, without CPU parallelism between Xu tasks. |
| Channel waits | Park waiting coroutines and wake them on communication or close. | Yield to other interpreted tasks; all tasks blocked produces a channel-deadlock diagnostic. Execution also has a step limit. |
| Host operations | OS, network and native/FFI APIs depend on package and target support; an OS call can block a worker. | Browser sandbox and canvas UI subset; native files, sockets, C FFI and desktop UI are outside this runtime. |
| What these examples prove | Checking and the listed outputs on Linux x64; no performance or scheduling-order guarantee. | Simple send/receive, close and non-blocking select examples also ran here; this is not evidence for arbitrary WASM hosts. |
Confirmed browser limitation: the blocking multi-arm select in the cancellation example reports unsupported playground builtin: 151 in this evaluator. That program is local-only; non-blocking select with else above works in the browser. Native I/O cancellation is a separate API/target concern.
To be verified: cross-platform native runs, CPU-parallel throughput, and cancellation of live native I/O are not covered by these examples. Native API helpers are marked local-only. See Playground for sandbox limits and Memory for lifetime and disposal rules.