3 ms·
ajross has a point here that if I'm not targetting any hardware which has that memory-level reordering for loads or stores, then I shouldn't need any special pr
by kazinator 3y ago
ajross has a point here that if I'm not targetting any hardware which has that memory-level reordering for loads or stores, then I shouldn't need any special primitive for that. Just volatile should work:
volatile int i;
volatile int *x;
if (i == 3) {
y = x[i];
}
Even if this becomes x[3], volatile should work well enough to prevent the reordering: the access to x[] shouldn't precede the access to i. Now volatile might cause two accesses to i. That's my problem, which I could try to defeat with caching:
int local_i = i;
if (local_i == 3)
y = x[3]; // Let's do this ourselves myself
Still, everything should be cool; the access to i and to x[3] should be in that order thanks to volatile. We have one access to i.
If, in spite of the compiler obeying our volatile, the processor and memory hardware is rordering our accesses, then we need some fancy primitive, because special instructions have to be used.
The fancy primitive should exist only for the hardware problem, not for the compiler's ordering of instructions.
- reitzensteinm 3y agoVolatile will infect everything, though - a big part of the utility of the synchronized methods on atomics is that they'll impose orderings on non atomic loads and stores.
- gpderetta 3y agoyou had to mark x volatile though which is suboptimal and generally not something that you can always do [1]: for example if you are writing a queue that transport pointers as a library, you can't guarantee that all stores and loads to the pointed objects (and anything transitively reachable from them) are made through a pointer-to-volatile, nor you would want to. That would be equivalent to having to mark volatile objects accessed in a critical section which is obviously wrong. [1] I'm also ignoring the fact that volatile really doesn't have the right semantics in C++11/C11.
- kazinator 3y ago> volatile really doesn't have the right semantics So let's not fix that, but invent new cruft. C defines volatile for several purposes of its own, and that's it. - In a function that uses setjmp, volatile must be used to mark variables that are modified after the context is saved with setjmp but before control returns with longjmp. Otherwise there is a problem, like the longjmp spuriously restoring the original values of those variables (due to those variables having been pulled into the machine state being saved and restored, like registers). - When an asynchronous signal handler (e.g. for SIGINT) sets a global variable which is checked by the interrupted code, that variable has to be "volatile sig_atomic_t". Any usefulness of volatile for actual concurrency is courtesy of the compiler, whether empirical or documented.
- gpderetta 3y agoVolatile already has semantics that are both too strong and too weak to implement atomics. Reusing volatile for atomics was rejected very early during the memory model standardization process. As an extension, MSVC did for a time add atomic and acquire/release semantics to volatile. This was deprecated because it broke too much stuff. edit: after your edit, I'm not sure if you arguing for volatile-for-atomic or not.
- kazinator 3y agoWell, yes; it's painfully obvious that you won't obtain, say, an atomic increment or compare-swap out of any simple combination of volatile anything, and it's more harmful than good to even try.
- jcranmer 3y agovolatile is truly a semantic mess. The effective definition of "suppresses optimizations" is difficult to work with, because the boundary between an optimization and weird feature interactions is incredibly murky. (e.g., don't ask what happens with volatile bitfields. Just don't.) It's better to replace it with things that have clearer semantics, even if they don't have the full functionality. The use of dedicated atomic intrinsics is one such clear replacement. Indeed I've worked with hardware where it was more obvious how to lower an atomic load or store than it was to lower a volatile load or store, because the technical definition of volatile really doesn't comport with the practical meaning of "don't optimize this" (crazy hardware does things like that).