std::concurrent::combine

std/src/concurrent/combine.mo

structCombine.Duration#
pub struct Combine.Duration {
    millis : I64,
}
defsleepIO#
pub def sleepIO (ms : I64) : IO Unit
defall#
pub def all {A : Type} (fibers : List (Fiber A)) : IO (Result FiberError (List A))

Every fiber's result, or the first error met awaiting them in order. The fibers after a failed one are not awaited.

defrace#
pub def race (fibers : List (Fiber A)) : IO (Result FiberError A)

The answer of whichever fiber stops first, error or not; every other one is cancelled. Parks on wait_any until a contestant stops, so a win is seen as soon as it happens rather than up to a millisecond late, and a race held open by sleepers costs no timer wakeups.

On the Rust host wait_any answers none (it cannot park), so this reports err cancelled there. That is why test_race_single and test_race_first_to_finish in combine_test.mo are COMPILED ONLY: they assert ok, which the host's err cancelled cannot satisfy.

A cancelled loser stops at its next IO bind (cancellation is cooperative), so nothing waits for it and no later side effect runs. Racing zero fibers has no value of A to hand back; callers pass at least one fiber.

The continuation is spelled fn idx => race_pick … idx, NOT race_pick (Shared.of fibers) as a partial application, and that is LOAD-BEARING rather than stylistic: codegen's IO-bind fusion (io_bind_plan) only claims a bind whose continuation is a literal lambda. A partial application falls through to IO.bind's reference body, which matches its first argument -- and when this fiber is cancelled, wait_any answers the cancellation sentinel rather than an IO box, so that match dereferences a non-object and the fiber SEGFAULTS instead of unwinding. See plans/implementations/2026-11-15-unfused-io-bind-dereferences-the-cancel-sentinel.md.

typeTimeout#
pub type Timeout A {
    finished (result : Result FiberError A),
    expired
}

The outcome of a bounded wait: the fiber finished (with its own result), or the duration expired first.

deftimeout#
pub def timeout {A : Type} (duration : Combine.Duration) (f : Fiber A) : IO (Result FiberError (Timeout A))

Wait at most duration for f, racing a sleeper against a fiber that awaits it. The waiter is listed first so the lazy host -- where a poll runs the deferred action -- degrades to await_fiber, its only honest behaviour; the compiled backend races them for real.

expired cancels f cooperatively (it stops at its next IO bind) and cancels the waiter, which may itself stay blocked in await_fiber f until f finishes; nothing waits for either, and timeout returns as soon as the timer wins.

STILL THREE THREADS, deliberately, and P1 did not change that: race no longer polls, so a bounded wait costs no timer WAKEUPS any more, but the timer is still a fiber. Retiring it needs NO event loop: wait_any already parks on a condvar, so a pthread_cond_timedwait deadline on that same park expires the timer inline. What actually stops it is that wait_any answers an INDEX, so an expired deadline has no value that cannot be confused with a contestant; the fix is a second timeout-aware primitive (or a distinct sentinel) plus deciding what race does with -1. Not attempted here because nothing in the tree uses timeout for real work -- motes/moon uses SO_RCVTIMEO, motes/moose its own connect deadline -- so it would be speculative churn on a path with no caller (monad-fiber-scheduler.md's P4 scope note).