#Multithreaded WASM

1 messages · Page 1 of 1 (latest)

calm stag
#

I would like to write multithreaded WASM using Zig. I know that multithreaded WASM can be achieved by spawning multiple Web Workers, then giving them all the same WebAssembly.Memory backed by a SharedArrayBuffer. Workers can synchronize and communicate using atomics.

Questions I have:

  • What about the stack space per thread? Since all threads use the same memory, their stacks must not overlap. How do I control this?
  • Are there any synchronization primitives built-in that can help me, or must I handroll all of it? (thinking mutexes, etc.)
  • What things are thread safe? Is the wasm allocator thread safe? If not, do I need to wrap allocations in my own mutex?
storm coyote
calm stag
#

What is a BrkAllocator?

#

Ah well, I can just use BrkAllocator then. If I have a mutex, I can just build my own allocator that, for every operation, locks the mutex and then forwards to the underlying allocator, no? Then I'm good!

storm coyote
#

it gets memory by extending the low part of the address space (the high part being the stack). That's how it was done in old unix OSes (and wasm apparently)

#

I suppose you can do that, yes