turbolent / w2c2

Translates WebAssembly modules to portable C
MIT License
701 stars 36 forks source link

Are module instances thread-safe? #93

Open jonathanmarvens opened 5 months ago

jonathanmarvens commented 5 months ago

I asked this question in the wasm2c project, but I’m also curious what w2c2’s semantics are when it comes to this type of usage.

Basically, is it undefined behavior to instantiate a module in thread A and then call into the instance in threads B, C, D, etc.? We can assume only one thread ever calls into the instance at any time.

I’m currently using a runtime in which this behavior is supported and well-defined, but I want to switch to either wasm2c or w2c2.

For the sake of understanding what this project supports (or at least intends to support), we can ignore the fact that pre-C11 multithreading is undefined and assume the compiler enforces a well-defined C11-like memory model anyway.

Thank you!

turbolent commented 5 months ago

is it undefined behavior to instantiate a module in thread A and then call into the instance in threads B, C, D, etc.? We can assume only one thread ever calls into the instance at any time.

(from the linked issue:)

is it okay if a module is instantiated by thread A, and then (after that has provably completed) thread B calls an exported function from that module instance, and then (after that has provably completed) thread C calls an exported function from the same module instance?

AFAICS, module instances do not have per-thread state, so as long as access is properly separated, such a usage should work. Please let me know if you are running into problems