IssueRaiser

Proposal: stackless coroutines as low-level primitives

This proposal assumes the existence of #23367. ## Background At the time of writing, @andrewrk and @jacobly0 are [exploring the idea](https://github.com/ziglang/zig/tree/async-await-demo) of implementing async/await in userland, based on the new `Io` interface. This allows using different approaches to implement async, such as single-threaded blocking, thread pool blocking, or green threads / fibers. The latter approach is likely to become the preferred method, because it is fairly performant and conceptually simple. At this point it seems incredibly likely that this will become the path forward for async/await in Zig; it has a lot of advantages! However, stackful coroutines via green threads do have some flaws. One of the most significant is that they simply aren't supported on some targets, such as Wasm and SPIR-V. Wasm is a particularly interesting example, because using stackless async in Wasm can be desirable in order to integrate with the JavaScript event loop -- for instance, expensive work in Wasm ought to periodically yield to the browser to allow other work to happen. The only way we can really do that is through stackless coroutines, which require language-level support. **In general, stackless coroutines will have broader support than stackful coroutines, because all of the suspend/resume machinery is lowered by the compiler to synchronous constructs.** #6025 proposes re-introducing stackless coroutines, as in stage1, to the self-hosted compiler. **This propos

Posted by mlugg on Apr 3, 2025, 12:09 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,000 issues0 comments

pricing (E-commerce)

Proposal: stackless coroutines as low-level primitives

mluggApr 3, 2025, 12:09 AM

This proposal assumes the existence of #23367. ## Background At the time of writing, @andrewrk and @jacobly0 are [exploring the idea](https://github.com/ziglang/zig/tree/async-await-demo) of implementing async/await in userland, based on the new `Io` interface. This allows using different approaches to implement async, such as single-threaded blocking, thread pool blocking, or green threads / fibers. The latter approach is likely to become the preferred method, because it is fairly performant and conceptually simple. At this point it seems incredibly likely that this will become the path forward for async/await in Zig; it has a lot of advantages! However, stackful coroutines via green threads do have some flaws. One of the most significant is that they simply aren't supported on some targets, such as Wasm and SPIR-V. Wasm is a particularly interesting example, because using stackless async in Wasm can be desirable in order to integrate with the JavaScript event loop -- for instance, expensive work in Wasm ought to periodically yield to the browser to allow other work to happen. The only way we can really do that is through stackless coroutines, which require language-level support. **In general, stackless coroutines will have broader support than stackful coroutines, because all of the suspend/resume machinery is lowered by the compiler to synchronous constructs.** #6025 proposes re-introducing stackless coroutines, as in stage1, to the self-hosted compiler. **This propos

Comments (0)

No comments yet. Start the discussion.

Comment on this issue