-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The wasmtime::Store
type should be thread-safe
#777
Comments
|
This commit removes the `signature_cache` field from the `Store` type and performs a few internal changes which are aimed to be a bit forward looking towards bytecodealliance#777, making `Store` threadsafe. The changes made here are: * The `SignatureRegistry` internal type now contains the reverse map that `signature_cache` was serving to do. This is populated on calls to `register` automatically and is accompanied by a `lookup` method as well. * The `register_wasmtime_signature` and `lookup_wasmtime_signature` methods were removed from `Store` and now instead work by using the `Compiler::signatures` field. * The `SignatureRegistry` type was updated to have interior mutability. The global `Compiler` type is highly likely to get shared across many threads through `Store`, so it needs some form of lock somewhere for mutation of the registry of signatures and this commit opts to put it inside `SignatureRegistry` which will eventually allow for the removal of most `&mut self` method on `Compiler`.
This commit removes the `signature_cache` field from the `Store` type and performs a few internal changes which are aimed to be a bit forward looking towards bytecodealliance#777, making `Store` threadsafe. The changes made here are: * The `SignatureRegistry` internal type now contains the reverse map that `signature_cache` was serving to do. This is populated on calls to `register` automatically and is accompanied by a `lookup` method as well. * The `register_wasmtime_signature` and `lookup_wasmtime_signature` methods were removed from `Store` and now instead work by using the `Compiler::signatures` field. * The `SignatureRegistry` type was updated to have interior mutability. The global `Compiler` type is highly likely to get shared across many threads through `Store`, so it needs some form of lock somewhere for mutation of the registry of signatures and this commit opts to put it inside `SignatureRegistry` which will eventually allow for the removal of most `&mut self` method on `Compiler`.
This commit removes the `signature_cache` field from the `Store` type and performs a few internal changes which are aimed to be a bit forward looking towards #777, making `Store` threadsafe. The changes made here are: * The `SignatureRegistry` internal type now contains the reverse map that `signature_cache` was serving to do. This is populated on calls to `register` automatically and is accompanied by a `lookup` method as well. * The `register_wasmtime_signature` and `lookup_wasmtime_signature` methods were removed from `Store` and now instead work by using the `Compiler::signatures` field. * The `SignatureRegistry` type was updated to have interior mutability. The global `Compiler` type is highly likely to get shared across many threads through `Store`, so it needs some form of lock somewhere for mutation of the registry of signatures and this commit opts to put it inside `SignatureRegistry` which will eventually allow for the removal of most `&mut self` method on `Compiler`.
@alexcrichton why is this impossible? |
@demimarie-parity the way things are set up right now you instantiate
The current state of affairs is that basically |
@alexcrichton what if we used |
Unfortunatey it's not quite as easy as that. In Rust there's no way to statically have a reference counted type that's Failing that we'd have to invent our own solution in wasmtime which is much more invasive than just using an |
What would need to change to allow JIT-compiled code to be used by multiple threads simultaneously? |
Ah yes so sharing |
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 |
As proposed in #708 the
Store
type in the wasmtime API should be thread-safe, meaning it should implement bothSend
andSync
. Currently, however, it has a few items that are not thread safe and/or need audits:Theglobal_exports
field 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 Replace global-exports mechanism with caller-vmctx mechanism #390signature_cache
field requires interior mutability and currently uses aRefCell
. While this could be switched to aMutex
I 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?context
field has a containedcompiler
field which has a number of issues. NamelyRc
andRefCell
are not thread safe (but can be swapped forArc
/Mutex
if necessary), but the underlyingCompiler
type has a number of trait objects, raw pointers, etc, which would need auditing. I think the solution here is going to be makingCompiler
itself threadsafe by redesigning it's internals.The text was updated successfully, but these errors were encountered: