27. Stackless coroutines, structured tasks, and race freedom proved from permissions¶
Date: 2026-11-21
Status: Accepted (2026-11-21, with deadlock freedom for channel communication added at Dean's request)
Context¶
Dean wants coroutines or green threads in Pia, locking and synchronisation and communication primitives, and a prover that says whether access to a resource could race. ADR 0014 made Pia concurrent by default and ruled out races on tiles by moving them to workers, but did not cover tasks, locks, atomics, channels or shared resources in general. ADR 0004 requires an unproved race to be a compile error. The detail is in docs/concurrency.md.
Decision¶
- Stackless coroutines. A function that suspends is compiled to a state machine whose frame has a size known at compile time, lowered through LLVM's
llvm.coro.*intrinsics. Stackful green threads are rejected, because their stack size is a goal Pia cannot prove in general. "Green threads" are tasks on a scheduler written in Pia (Pia.Task: work-stealing workers overPia.Sys.Thread, kqueue for I/O, a deterministic mode for tests and counterexample replay).spreadand the collectives run on the same scheduler. - Keywords
waitandyield, and an@suspendseffect that is inferred inside a module and written in exported interfaces. Blocking foreign calls are marked@blockingand must go throughPia.Task.offloadfrom suspending code. - Structured tasks.
spawnreturns a@lineartask<T>that must be joined inside thescopethat started it, so tasks may borrow from their parent. No detached tasks in the first version. Cancellation is an explicit token. - Primitives in the library (
Pia.Sync,Pia.Chan):mutex<T>andrwlock<T>own their data and are reached only through scopedlockclosures that may not suspend, with the data type's refinement as the lock invariant.atomic<T>, channels whosesendtakesmoved,once,barrier,semaphoreandevent. No bare condition variables. - Static lock levels. Each lock has
@level(n). A task may only acquire a lock above every level it holds, and interfaces record@acquires(n). Lock-order deadlock is then a type error. - Race freedom from permissions. Every pair of possibly concurrent accesses must be justified by ownership, shared
fixedreading, proved disjointness, a lock, atomicity, or a happens-before edge. The type checker settles most of these, using thesendableandshareableconcepts (derived over fields) and task boundaries. Disjointness goes to FPL (ADR 0020), then Z3. The model checker settles the rest: the primitives' own implementations, protocols and deadlock, and user code built from atomics. Non-memory resources (positioned file I/O,stdout) carry footprints that are checked the same way. - Deadlock freedom for channel communication (CSP). Every scope that uses channels has a deadlock goal: no reachable state has a set of tasks that are all blocked forever, each waiting on a
send,recv,join, barrier orselectthat only another task in the set could complete. This includes partial deadlocks. Both channel ends are@linear(more senders come fromclone), so the tasks able to wake each waiter are known, and channels are always bounded. The goal is proved in three stages. First, the channel topology is extracted from ownership: a forest whose tasks drain their inputs is deadlock-free by a lemma. Second, per-end usage protocols are inferred (and written as@protocolon exported parameters), checked for compatibility by the protocol metaprogram, and matched against library patterns with proved lemmas for any number of tasks: pipeline, worker pool, request and reply, scatter and gather. Third, the model checker searches the remaining scopes' protocol automata for a deadlocked state. - Memory orders. Sequential consistency and release/acquire are checked from the start.
relaxedis accepted where its value orders nothing, and otherwise is an unproved obligation until the model checker has an RC11 mode. - The model checker is exhaustive. It uses DPOR and symmetry reduction. A search that hits a limit is unproved, never proved.
- Unproved races, deadlocks and
relaxeduses are compile errors, with a witness (FPL sample, Z3 model or schedule).--allow-unproved=race:...,deadlock:...andrelaxed:...accept them as trust items. - Certificates.
@suspends,@blocking,@acquires,@threadsafe/@serial,sendable/shareableof exported types, and module-state footprints are part of the interface and its hash (ADR 0024). The certificate records each race goal's stage and, for the model checker, the exhaustive search size. Accepted races,@threadsafeforeign claims and still-trusted primitives are trust items.
Consequences¶
- The borrow checker becomes the race checker. Most programs never reach the model checker.
- Function colouring is visible, and generic code over both colours needs effect polymorphism in generics (M9).
- The model checker is substantial work. Until it lands, the primitives are trusted wrappers over
os_unfair_lockand pthreads, and the trust report says so. - Races between processes, for example on the file system, are out of scope.
- Channel protocols join the interface, so changing how an exported function uses a channel changes its interface hash.
- Implementation is tracked in #74.