-
Notifications
You must be signed in to change notification settings - Fork 49
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
Does the wasm memory model have tear-free non-atomic loads? #199
Comments
Yes, naturally aligned |
Relatedly, are non-atomic stores guaranteed to be observed by "later" non-atomic loads on another thread? (or by atomic loads?) |
It depends what you mean by "later". There is a concept of All operations taking place sequentially in the same thread are So if you have a non-atomic store to
However, if all the atomic_sc accesses in the above example are changed to non-atomics, this is not guaranteed (in the model or in real hardware). |
IIUC, if there are no atomics forming that happens-before relationship across threads A and B, then a non-atomic store on thread A might in principle never be observed on thread B, even if it's sitting in a loop doing non-atomic loads of the same address. |
Yes, that's right. EDIT: I originally wrote the below, but in light of the point about hoisting at the bottom, I don't think it's advisable to use this code 🙃 It can still be useful to (very carefully) do a loop of non-atomic loads in certain circumstances, so long as the read value is then "confirmed" using an atomic load or CAS. For example, the following isn't an unreasonable spin-lock implementation (assuming Wasm semantics for non-atomics and CAS - in C11 you need a relaxed atomic load instead of a non-atomic load to avoid UB).
Technically the memory model says that it's never guaranteed that the
Actually, thinking things through, it might be permissible for a Wasm compiler to hoist the |
Yeah, this is basically what I was getting at. Non-atomics never make happens-before relationships with anything on other threads (other than indirectly via a program order relationship with an atomic on the same thread). All that is to say, maybe we should add some cheaper atomics (acq/rel and maybe relaxed?) to wasm soon :) |
My personal vision is acq/rel, and then a "load-store-ordered" atomic which is marginally stronger than relaxed but avoids a lot of the mess associated with it (probably implemented using the "bogus conditional branch" strategy benchmarked in sections 4.22 and 5.2 of this paper). I have to say though, currently I'm pushing hard to get current threads phase 4 landed, so I'd rather not spend too much time thinking about extensions before this is finished. |
We recently learned that Musl (used in both wasi-libc and Emscripten) is using non-atomic 32-bit aligned loads from memory locations which can be stored to by other threads without synchronization, apparently on the assumption that compilers will always codegen these to a single instruction, effectively intending something like a very relaxed ordering. On Wasm, LLVM compiles these to plain
i32.load
wasm instructions.Are there any guarantees in the proposed Wasm memory model that a plain aligned
i32.load
of memory that can be simultaneously stored to by aligned i32 stores in other threads won't tear?The text was updated successfully, but these errors were encountered: