Take the following example code:
static volatile bool pending = false;
void __attribute__((interrupt(TIMER0_A0_VECTOR))) TIMER0_A0_ISR (void)
{
pending = true;
}
int main(void)
{
while(true) {
if (!pending)
sleep();
pending = false;
// do stuff
}
}
Assume that the sleep function puts the hardware to sleep and that an interrupt wakes the hardware up, so that the sleep function will return immidiately after the interrupt.
There is a race condition here: if the interrupt happens after the if statement but before the sleep, we sleep until the next interrupt. This is a problem in my real-world counterpart of this code. How can I avoid this problem?
I am working with the msp430g2433.