alexcrichton closed Issue #777:
As proposed in #708 the
Storetype in the wasmtime API should be thread-safe, meaning it should implement bothSendandSync. Currently, however, it has a few items that are not thread safe and/or need audits:
- ~~The
global_exportsfield is anRc-protected data structure used by wasmtime internals. I don't know myself what it would take to remove this, but @sunfishcode looks like he does in https://github.com/bytecodealliance/wasmtime/pull/390~~- The
signature_cachefield requires interior mutability and currently uses aRefCell. While this could be switched to aMutexI think it would be best to avoid this altogether. I don't personally know why this field exists, @yurydelendik do you know why it's here?- The
contextfield has a containedcompilerfield which has a number of issues. NamelyRcandRefCellare not thread safe (but can be swapped forArc/Mutexif necessary), but the underlyingCompilertype has a number of trait objects, raw pointers, etc, which would need auditing. I think the solution here is going to be makingCompileritself threadsafe by redesigning it's internals.
DemiMarie-parity commented on Issue #777:
@alexcrichton why is this impossible?
alexcrichton commented on Issue #777:
@DemiMarie-parity the way things are set up right now you instantiate
Instancestructures into aStore. TheStorekeeps a handle on eachInstance, so effectively aStorestores eachInstance. This is done to keep eachInstancealive since we don't have a GC right now to otherwise understand when there are no references to anInstance.
Instances themselves are fundamentally notSyncbecause operations like global get/set aren't atomic. They're ideallySend, however, but with theRcthat we're using for memory management that forces them to not beSend.The current state of affairs is that basically
Storestores enough instance-local information that it can't be shared across threads because instances can't be shared across threads. There may still be internal refactorings or possible designs to fix this, but overall our threading story isn't great right now unfortunately.
DemiMarie-parity commented on Issue #777:
@alexcrichton what if we used
Arcinstead? I imagine that reference count operations are not in hot paths.
alexcrichton commented on Issue #777:
Unfortunatey it's not quite as easy as that. In Rust there's no way to statically have a reference counted type that's
Sendbut notSync, which is morally what we'd want here. The only static solution for that is to entirely remove the reference counting, but that doesn't jive well with the memory management that we have in wasmtime today.Failing that we'd have to invent our own solution in wasmtime which is much more invasive than just using an
Arcinstead of anRc, and I'm not really sure if that's even possible much less workable.
DemiMarie-parity commented on Issue #777:
What would need to change to allow JIT-compiled code to be used by multiple threads simultaneously?
DemiMarie-parity commented on Issue #777:
The first idea that comes to mind is to separate the JIT-compiled code (which is immutable) from the data (which is mutable). So to execute some code, one would first need to compile it, which would return an immutable
CompiledBlobobject. One would then need to create anInstancefrom theCompiledBlob. Since aCompiledBlobis immutable, making itSend + Syncis trivial.
DemiMarie-parity deleted a comment on Issue #777:
The first idea that comes to mind is to separate the JIT-compiled code (which is immutable) from the data (which is mutable). So to execute some code, one would first need to compile it, which would return an immutable
CompiledBlobobject. One would then need to create anInstancefrom theCompiledBlob. Since aCompiledBlobis immutable, making itSend + Syncis trivial.
alexcrichton commented on Issue #777:
Ah yes so sharing
Storeis its own issue but sharing compiled code is definitely something that we want to enable. MakingModulebothSendandSyncshould be easier than makingStoreeitherSendorSync, and that's largely just a matter of internal refactorings.
yurydelendik commented on Issue #777:
FWIW wasm-c-api planed to shared a module between two stores via wasm_module_share/wasm_module_obtain , see https://github.com/WebAssembly/wasm-c-api/blob/master/example/threads.c#L34
Last updated: Dec 13 2025 at 19:03 UTC