2 ms·
> For example, volatile is said to be sufficient for interfacing with hardware through memory mapped regions, because that is one area where it is very clear wh
by masklinn 5y ago
> For example, volatile is said to be sufficient for interfacing with hardware through memory mapped regions, because that is one area where it is very clear what the outputs are and one can just make all those accesses volatile so they won't get reordered.
It's not "sufficient" for MMIO, it's literally the sole use case for it, and the sole situation where it's correct, because the hardware will not fuck with MMIO accesses either (and not eliding reads is important because reads might have side effects). Although even then you may need to use atomics anyway if the driver is concurrent, because volatiles don't guarantee any sort of atomicity and the reaction of MMIO devices to concurrent access is very likely to be funky (then again these days MMIO is usually a thing on uCs where there is no uncontrolled and arbitrary concurrency).
> Although, having to go all-volatile is maybe too much of a constraint, when many algorithms (like lock-free queues etc.) only require like 3 non-reordered accesses (2 to begin, 1 to end?) to safeguard a transaction, and then the remaining reads and writes can be reordered arbitrarily (inside the safe bounds).
Except your accesses will be reordered arbitrarily:
* volatile instructions are only of concern for the compiler, everything below it has no idea about them, and if you're not in an MMIO context they'll absolutely try to reorder and remove them (and even when they won't since volatiles are not synchronisation point you have no idea how they'll propagate)
* volatile instructions are not fences, even for the compiler are no "safe bounds" with volatiles unless all the operations are volatile
- jstimpfle 5y ago> even for the compiler are no "safe bounds" with volatiles unless all the operations are volatile That's exactly what I said right? But good to know that volatile won't emit fences.