11 years ago.

Re-entrant procedure: Can y=++x be interrupted between the increment and assignment?

In this scenario, x is a global variable (buffer pointer) that is used by procedures with varying interrupt priority. I know one solution is to disable interrupts before the assignment and re-enable afterwards, but I was wondering if this was really necessary (and I am not sure what happens if an interrupt occurs while disabled - I don't want to miss any). What does such a line of code get compiled to? Similarly, can y=x++ be interrupted between the assignment and the increment?

In short, I am trying to make a local copy of the buffer pointer and simultaneously increment it (the global pointer) so any higher priority calls will not overwrite the entry assigned to the current, local procedure.

1 Answer

11 years ago.

I think it can be interrupted, you effectively have: y=x=x+1; I don't think there is a single arm instruction for this double assignment. Next question is what happens when you briefly disable interrupts? The latency will increase slightly but the interrupts will keep pending so it is unlikely that you miss any. Last question is: do you need to? Only if the interrupt routine writes to x. Any other routine can be safely executed, there is no access to the local y and x can be safely read although the value will depend on the exact timing. Problems arise when the interrupt routine also modifies x because there is a chance that one of the two modifications is missed. With a buffer like you have (classic producer-consumer process) you typically must protect the number of occupied (or empty) places, each process has its own pointer.

Accepted Answer