I’ve been working on a CPU ray tracer in Zig and recently ran into trouble when trying to parallelize it for WASM in the browser. Most browsers support sharing WASM memory between Web Workers, and setting up Zig for this use case is simple enough (following this comment [0]; and also setting lib.shared_memory = true
). But workers sharing the same WASM memory will end up stepping on each other’s toes as they all use the same stack. As far as I understand, tools like Emscripten and wasm-bindgen-rayon provide some support for managing per-thread stacks (I found this discussion [1] to be very helpful). Does Zig have anything analogous? I’ve gone digging through github issues and the stdlib source code, but haven’t quite found anything.
I have seen the recently added experimental WASI threading support [2], but AFAIK this applies to wasm32-wasi
and not wasm32-freestanding
.
I’m also aware that not sharing WASM memory between workers will work for my use case, but I’m hoping to learn where things stand for shared memory specifically.
Oh, and one other thing. It’s now possible to enable a multi-threaded build for WASM by passing the -fno-single-threaded
flag to zig build-lib
(see this commit [3]). Is there an equivalent option in build.zig
?
(Apparently new users can only have 2 links per-post, so the last 2 have the github prefix removed.)
[0] Allow importing memory to WASM binaries · Issue #8633 · ziglang/zig · GitHub
[1] What happens when we write to Memory.Buffer · Issue #1452 · WebAssembly/design · GitHub
[2] ziglang/zig/pull/16207
[3] ziglang/zig/pull/16207/commits/87b8a0567b0f54415aeecd879d3f1a4e12014d22