I am looking for opinions on how to create a code sequence that is written in "C" that must be performed as an uninterruptable sequence. I want to disable interrupts (globally) execute a code sequence and then re-enable interrupts. I am looking for your inputs as I don't see how to guarantee this from what I know about the "C" standard. I think the compiler is allowed to optimize the sequence so that the actual linear code could be placed outside my expected enable interrupt and disable interrupt sequence (start/end points). The compiler knows that the enable/disable of the interrupt is volatile and must be performed but it doesn't know that there is an architectural dependency to the code order I want. In other words it could be that part of my sequence gets optimized outside of where the interrupt is not globally disabled. As the opcode creation behavior is still correct but it is not from a system behavior point of view.
Outside of writing it in assembly has anyone experienced this and how did you end up handling it. Thanks in advance for your inputs.
Outside of writing it in assembly has anyone experienced this and how did you end up handling it.
Depending on the compiler you use, it may already offer facilities to make parts of the code or whole functions uninterruptible.
If you use an operating system, it may offer such facilities, too.
If you are really worried about wild compiler optimizations resulting in things being done completely out of the intended sequence, there's always the option of checking the generated assembly code for conformity to the expectations and adjusting the optimization level if the instructions are not in the correct order. Usually, the compiler manual will state which optimizations are permitted at which optimization level.
There are also other options, like using a "wrapper" function written in assembly which takes care of disabling interrupts, calls the C function that is supposed to be uninterruptible, and then restores the interrupt state.