5 ms·
What is the difference with Ordering::Relaxed?
by dunkelheit 7y ago
What is the difference with Ordering::Relaxed?
- ComputerGuru 7y agoOrdering::Relaxed guarantees atomicity (but not coherence). On 32-bit or 64-bit platforms, a (aligned) 32-bit read or write will always be atomic anyway.
- dunkelheit 7y agoWhat do you mean by coherence? If it is the property that all updates to a single memory location are seen in a consistent order by all threads then Relaxed guarantees that.
- wilsonthewhale 7y agoit doesn't. From cppreference (whose model Rust roughly follows): "Relaxed operation: there are no synchronization or ordering constraints imposed on other reads or writes, only this operation's atomicity is guaranteed"
- dunkelheit 7y agoStill I think I am correct. The docs (https://doc.rust-lang.org/std/sync/atomic/enum.Ordering.html#variant.Relaxed https://doc.rust-lang.org/std/sync/atomic/enum.Ordering.html...) point to https://llvm.org/docs/Atomics.html#monotonic https://llvm.org/docs/Atomics.html#monotonic which states that "It essentially guarantees that if you take all the operations affecting a specific address, a consistent ordering exists." which is what I've said.
- ComputerGuru 7y agoYes, a consistent ordering exists but that ordering isn’t necessarily what you think or want it to be. The consistency is present based off the order the reads and writes were executed in; you’ll never read back a value that wasn’t at some point stored there. Which can happen with non-atomic reads/writes, where you’ll read part of one write and part of the next. Reads can still be reordered before writes, as no memory barriers are issued.
- dunkelheit 7y agoWell that doesn't contradict anything that I have said :) My question remains - what do you mean by coherence? I argue that it is precisely the property that there is a consistent order of operations a single memory memory location so you can't say that Relaxed operations don't satisfy coherence.
- frankenbee 7y agoHm
- gpderetta 7y agoYou are correct. IIRC Coherency is the only property of relaxed atomic operations, as opposed of plain load/stores where concurrent modification and access is just UB.
- comex 7y agoAn assembly-level read or write will always be atomic anyway, and in fact a relaxed atomic read/write typically compiles down to the exact same assembly as a normal read/write. However, there are compiler optimizations that can break atomicity, such as splitting accesses into multiple parts; using (relaxed) atomics instructs the compiler not to perform those optimizations.
- carllerche 7y agoRelaxed ordering impacts the compiler. There are a number of optimizations it cannot make. I am not sure exactly what those are, but it is measurable.
- dunkelheit 7y agoProbably some reordering? Would be interesting to compare generated assembly.
- carllerche 7y agoI believe there are some LLVM "bugs" with regards to Relaxed ordering. I don't know the specifics.
- CodesInChaos 7y agoThe only forbidden optimization I could come up with is: x = load-relaxed(a) ... f(x) cannot be turned into x0 = load-relaxed(a) ... x1 = load-relaxed(a) f(x1) while this would be possible with normal memory access. so under register pressure the compiler would be required to spill something onto the stack instead of just reloading `x` from the original location.
- carllerche 7y agoLast time I spoke about this topic w/ the rust compiler devs, they mentioned there were some LLVM "bugs" with regards to optimizations and Relaxed, as in LLVM is too conservative.