
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0011: The runtime's own context switch
Status: Accepted by the project owner on 2026-10-09 (PRs #54–#59); proposed 2026-10-09 (issue #35, v1 milestone #45); needs engineering review (#44).
Context
Actors and ctl handler bodies run on fibers (ADR 0004, ADR 0003 Revision 2). Until now the fibers were corosensei coroutines. ADR 0004 Revision 1 planned the runtime's own context switch, so that actors can move between scheduler threads on a mechanism the runtime controls. The multi-threaded scheduler (M6) moved corosensei fibers between threads behind a Send wrapper, as a stopgap.
Decision
A small fiber module (
runtime/louise_rt/src/fiber.rs) replaces corosensei behind the same API:Coroutine::with_stack,resume,Yielder::suspendandinto_stack.schedandhandlerschanged only their imports.Stacks are the runtime's own: an anonymous mapping (
MAP_NORESERVE, committed lazily) with aPROT_NONEguard page below it. The scheduler keeps its pool of finished actor stacks.The switch on x86_64 Linux is about twenty instructions of assembly (
tl_fiber_switch):- it saves the System V callee-saved registers (rbx, rbp, r12–r15) and the MXCSR and x87 control words on the current stack;
- it stores the stack pointer, loads the other fiber's, and restores the same state from it.
A suspended fiber is therefore exactly its stack plus that saved stack pointer. It holds no reference to the thread it ran on, so it can resume on any scheduler thread. A new fiber starts in
tl_fiber_trampoline, whose CFI marks it as the outermost frame, so backtraces stop there.Values cross the switch through a boxed channel: the input of
resume, the value ofsuspend, and the body's result.Other targets keep corosensei, on the runtime's stacks, behind the same API. The x86_64 switch is tested here and in CI; an aarch64 switch would be written the same way (x19–x28, x29, x30, d8–d15), but there is no aarch64 machine or emulator to test it on yet. Shipping untested assembly would be worse than keeping the dependency there.
cargo check --target aarch64-unknown-linux-gnukeeps the fallback compiling.Rules unchanged:
- a suspended fiber is never dropped (its frames never released what they hold; the runtime leaks it rather than reuse its stack);
- code on a fiber must not hold thread-local state across a suspension. The
Sendargument for actor fibers (sched::Fiber) now rests on this module alone.
Consequences
- Performance: none measurable. The
effectsbenchmark (actlgenerator, 100k switches each way) takes the same time as with corosensei, about 1.7 s for the whole program, interleaved runs. corosensei's switch was already minimal. The gain is control: one less dependency on x86_64 Linux, stacks the runtime owns, and a migration argument that depends only on code in this repository. - ASan: fibers switch stacks without
__sanitizer_start_switch_fiberannotations, as with corosensei. CI's ASan runs pass. Annotating would make ASan's view of stacks exact, if a false report ever appears. - Tests:
fiber::testscover values flowing both ways, nested fibers with floating-point state, resuming on another thread, and deep recursion. Every native program runs on it, with 1 and 4 threads, under ASan too.
Alternatives considered
- Keep corosensei everywhere. It works, but leaves the migration argument depending on a crate that declares its coroutines
!Send. ucontext(swapcontext). Portable, but it saves and restores the signal mask with a system call on every switch.- Write the aarch64 switch now, untested. Rejected: see above. It comes with an aarch64 CI runner.
Note — 2026-10-09: aarch64, and ThreadSanitizer (issue #45)
Status: Accepted by the project owner on 2026-10-12; needs engineering review (#44).
The aarch64 switch is written, but opt-in
- What:
tl_fiber_switchfor aarch64 Linux, written like the x86_64 one (fiber.rs,archmodule).- It saves the AAPCS64 callee-saved state on the current stack: x19–x28, fp (x29), lr (x30), d8–d15 and FPCR, in a 176-byte frame, so
spstays 16-byte aligned. FPSR holds only status flags and is not saved. - It stores
sp, loads the other fiber's, restores the same state, and returns to the restored lr. - A new fiber's frame holds
startin x19,entryin x20, fp 0 and lr =tl_fiber_trampoline. The trampoline callsentry(start), and its CFI marks lr undefined, so backtraces stop there.
- It saves the AAPCS64 callee-saved state on the current stack: x19–x28, fp (x29), lr (x30), d8–d15 and FPCR, in a 176-byte frame, so
- Untested on hardware. There is still no aarch64 machine or emulator here. The decision above (untested assembly must not become the default) stands:
- aarch64 keeps corosensei unless the runtime is built with
--cfg louise_rt_aarch64_switch; - CI type-checks both configurations, and builds the runtime with the switch, so the assembly is at least assembled. The disassembly was reviewed against the frame layout above.
- aarch64 keeps corosensei unless the runtime is built with
- Multi-shot snapshots (ADR 0017) need only the saved stack pointer, so the opt-in aarch64 switch has them too; they are as untested there as the switch.
- Before making it the default: run
fiber::testsand the native tests (louise_codegen_c --test native, on 1 and 4 threads) on aarch64 Linux. Then delete the cfg, and corosensei with it if no other target needs it. - Not handled: pointer authentication and BTI. The switch saves lr as it is. Code built with
-mbranch-protection=pac-retsigns lr in its own frames, which the switch does not touch, so this should be compatible; it is unverified.
ThreadSanitizer sees the fibers
- With
--cfg louise_rt_tsan(onlyscripts/tsan.shsets it), each fiber is registered with TSan (__tsan_create_fiber), and every switch is announced just before it happens (__tsan_switch_to_fiber). Elsewhere these calls compile to nothing. - Why: unannounced, TSan believes a thread stays on one stack. Its shadow call stack goes out of balance as actors move between threads, and TSan crashed at exit (
tests/run/actors.lse). - The announcing call must be inlined into the switching function. As a function of its own, TSan instruments it too: it entered on one fiber and left on the other, and a fiber's shadow stack underflowed on its first frame.
- Multi-shot snapshots (ADR 0017) carry TSan's call depth. Restoring a snapshot puts frames back on a fiber's stack, possibly after the fiber finished, but TSan's shadow call stack for the fiber still has the depth it had last; the restored frames' returns underflowed it, and TSan crashed in
tests/run/multishot.lse. A snapshot records the depth the fiber had when it suspended, andrestoresets the fiber's TSan depth back to it: it pops frames, or pushes placeholders whose return address is unknown (0 in reports). Only TSan builds record the depth: storing it on every suspension, even as a constant, made thectlgenerator oftests/bench/effects25–38% slower. - See ADR 0004, Revision 5, for the CI step.
Note — 2026-10-12: the switch on aarch64 by default, and no corosensei (issue #97)
Status: Proposed (2026-10-12); needs engineering review (#44). The owner decided that Louise supports Linux on x86_64 and aarch64 only.
The exit criterion above is met, under qemu.
scripts/aarch64.shcompiles for aarch64 here (rustc's target, clang--target, lld, against an arm64 Fedora 44 sysroot that podman makes once) and runs the binaries through qemu-user. Results:fiber::testspass, now including the snapshot and hibernation tests, which were limited to x86_64;- the runtime's other unit tests pass (43 in total);
louise_codegen_c --test nativepasses on 4 then 1 threads, multi-shot programs included, and again with every parked actor hibernated (LOUISE_PARKED_EPOCH_MS=0).
qemu runs the instructions, not a real memory model, so it validates the switch's logic, not its behaviour under real weak memory ordering. No aarch64 hardware has run it yet.
The switch is the default on aarch64, and
--cfg louise_rt_aarch64_switchis gone.corosensei is removed. Its only remaining use was the fallback for other targets. Any target other than Linux on x86_64 or aarch64 now fails to compile, with a message naming this ADR. macOS (different assembler directives, symbol names and page size) moves to the v2 backlog (#63).
Snapshots are always available.
Coroutine::snapshotandhibernateno longer returnOption, and "multi-shot resumptions are not supported on this target" can no longer happen.The page size comes from
sysconf, not a constant 4096: aarch64 kernels may use 16 or 64 KiB pages, where a 4 KiB guard page andmadviseoffset would be wrong.The
Sendargument for actor fibers stays where it is (sched::Fiber). It never depended on corosensei: it is about thread-locals in the code running on fibers, and the runtime's ownCoroutineis just as!Send.CI runs
scripts/aarch64.shwhere podman and qemu are installed, and only type-checks and assembles the runtime for aarch64 elsewhere (the forge's runner has no qemu).Still unverified: pointer authentication and BTI, as the note above says.