Linux, C, GCC, ARM (Allwinner H3). Is it possible to get the following equivalent functionality (pseudocode):
#pragma optimizations-stop
#pragma registers-flush
a=10;
while(*b != 0) c++;
if(a == 0) { do something; }
#pragma optimizations-start
with result of location of a
being explicitly written with value 10, then every loop cycle value of b
is read from the memory and then read value from location b
is compared to 0, and then if not zero c
is rmw'ed with +1, and when loop breaks location of a
is being explicitly read from the memory and compared to 0?
And, what's important, instructions are executed in the specific order programmer laid them out in the program, not as compiler or CPU thinks the best.
Please withhold with discussion of what is impossible, let's discuss how to achieve the required result.
If it is not possible at the C/GCC level, I will be glad for suggestions to implement that as inline assembly.
a
,b
andc
asvolatile
. Is that what you’re after?volatile
does not perform synchronisation, which is necessary for multithreading. Nevertheless,volatile
does ensure that every variable is read and written to as specified in your program. The reason concurrent access isn’t well-defined is due to where these variables are stored on multiprocessor architectures (simplified).volatile
definitely does 1), it definitely does not do 2) and it may or may not do 3) depending on how literal one reads the C standard. The most sensible way to read the standard is that the implementation (compiler + system) is not allowed to perform re-ordering across a volatile access = memory barrier. However, some chose to read the standard as volatile access might not be re-ordered in relation to another side effect.