Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Continuations and parallel execution

Research recorded on 2026-10-24. This page describes future options, not shipping features. We ship one WasmGC backend.

Decisions have been made since. One-shot suspension is implemented by ordinary calls and saved frames (suspension), and the runtime’s tasks are suspensions, scheduled in snail-scheme, so no host suspends a Wasm stack (task operations). Effect declarations and handlers have gone since this page was written (#877); what it says of ctl operations and handlers reads as suspend and with-prompt. And each instance of a program is single-threaded, talking to others only through queues and shared memory (instances, proposed). So the shared-heap thread pool below is not the plan. The native alternatives remain research.

What the Wasm target needs

Existing Wasm threads share linear memory. That does not make ordinary WasmGC objects, closure displays or references transferable between workers. The Shared-Everything Threads proposal addresses that larger sharing model. A prototype engine flag is not a deployment baseline; we require default support in the runtimes we ship on.

Typed stack switching provides typed continuation creation, suspension and resumption. JSPI gives host-driven asynchronous suspension, but not a language-level first-class continuation API or arbitrary migration of suspended stacks, and it has no counterpart outside the browser engines, so the tasks no longer use it.

These extensions solve different problems. Stack switching does not make the heap shareable. Sharing the heap does not establish handler lifetimes. An implementation of both still needs to preserve the language’s invariants.

Option A: restore a Wasm-to-LLVM backend

The archive archive/llvm-backend-20261012, at 5c277ab6, used BDWGC and translated Binaryen’s folded text into LLVM. The later backend removed by PR #341 was substantially different. Its last revision, fc8bf59a, read Wasm binaries, resolved their types, emitted LLVM, and used a custom Immix collector. Research should start from that later revision, rather than assuming the named archive contains the final implementation.

The later backend’s exact writable roots are a useful foundation. Reference locals lived in root slots; temporary references were published before collecting calls and reloaded after objects could move. The collector was synchronous and single-threaded. RUNTIME and native_roots were process globals explicitly assumed never to be shared between threads. Native worker isolation was not a shared-heap thread pool. See its roots contract and runtime.

A stackful implementation would allocate stacks, switch between them at delimited suspension points, and represent a continuation as a suspended stack chain. The Wasm type checker supplies the continuation’s type; LLVM need not gain a first-class continuation type. One-shot resumptions avoid copying stacks to support repeated resumes. A historical WasmFX prototype, section 5, implemented this using Wasmtime’s fibers and native register/stack switching. That establishes feasibility, not readiness of our old backend.

LLVM’s coroutine intrinsics instead support splitting a function into entry, resume and destruction functions with saved state in a frame. They do not automatically capture arbitrary nested ordinary calls. We would either transform every potentially suspending path or supply a stack-switching runtime. The coro.id.retcon.once convention describes a coroutine yielding once; it does not by itself implement our rule that a captured resumption runs at most once.

The substantial work is making the runtime correct:

  • Give each suspended computation a root chain. Publish live references before suspension and reload them after resumption, since other tasks may have caused a moving collection in between.
  • Enumerate roots from every running and suspended computation. A first multithreaded collector can stop all mutators; it need not collect concurrently. It still needs safepoints and safe allocation coordination.
  • Implement stack limits and foreign-call boundaries. Cross-thread resume must account for thread-local state, not just copy a stack pointer.
  • Implement the shared Wasm memory/object operations and their atomic semantics. Native pointer accessibility alone does not provide them.

A useful first experiment would suspend with live managed references, force a moving collection in another computation, resume, and check the references. Only then should two native mutators run concurrently.

Option B: a C backend informed by Koka

Koka’s usual C backend lowers effects through a selective monadic transformation and generalized evidence passing. At an operation that yields, generated code returns through ordinary C frames. Each relevant caller adds a closure for its remaining computation. At the matching handler, the collected closures form the resumption. Resuming invokes those closures with the appropriate handler bindings. It does not preserve the physical C frames. The compiler transformation and handler runtime show both halves.

The generalized evidence passing paper, sections 2.5–2.8 explains the optimizations: effect types help select handler evidence; tail-resumptive operations can execute in place; generated code builds continuation closures on the yielding path. A yielding flag distinguishes ordinary return values from suspension. Capture does work along the continuation’s path, whereas native stack switching can retain the stack without rebuilding that path as closures. These are different cost models; neither establishes a performance result for snail-scheme without measurement.

Koka supports multiple resumptions. snail-scheme restricts them to one: a second call of resume panics. A C backend would still need closure conversion, effect lowering, a managed-memory strategy, a foreign ABI and a portable runtime. Adopting this technique does not mean embedding Koka or obtaining those pieces automatically. Koka’s reference-counting implementation is also not a requirement of the continuation technique.

Koka explicitly finalizes abandoned resumptions so finally clauses run, and its raw ctl exposes a resume context that must eventually be resumed or finalized; the tour describes that contract. snail-scheme has no finally: a resumption that is never called is reclaimed by the collector, and nothing in its frames runs. The related libmprompt project takes a separate native stack-switching approach; it is not the mechanism described above.

The compiler transformation could also target today’s WasmGC. Therefore waiting for typed stack switching is a choice to avoid another substantial lowering strategy, not proof that ctl is impossible on today’s Wasm.

Handlers and scheduling

A with-prompt’s handler may be invoked repeatedly. Each invocation receives its own resumption. A continuation retains the frames and the with-prompts inside it. The implemented frame translation establishes these lifetimes; alternative backends would need to preserve that same contract.

A shared-heap backend would want a conventional work-stealing pool. That is not the plan: each instance schedules its own tasks, and parallel work runs as more instances (instances). For now, deploy independent Wasm instances and balance requests externally. Neither native backend is on the implementation path of the task rewrite.

Deliberate language limits

snail-scheme is a statically-typed Scheme successor, not an R7RS implementation. call/cc, dynamic-wind, reusable continuations and a separate Scheme compatibility runtime are outside the language. Its implemented control model is one-shot delimited suspension: a second resume panics, and a resumption that is never called runs nothing. Future backends must preserve that contract rather than add a second continuation model.

Foreign callbacks

A pending operation holds the ticket its task waits on. Its completion settles it once and wakes the ticket; a result whose ticket wakes no task is disposed of by the host. An empty ready queue is not runtime exhaustion while waits remain live. Nothing walks language frames: neither GC reachability nor a native Rust/JavaScript exception unwinds them.

Foreign callbacks return normally. They copy transient inputs or hand over an explicitly owned buffer, settle a registration and enqueue snail-scheme work. They do not capture a foreign stack as part of a continuation. Browser operations that need an immediate response, such as event suppression, keep that decision in the synchronous adapter.