First page Back Continue Last page Overview Graphics
A routine is called reentrant if it can be used by multiple programming threads without data loss.
To achieve reentrancy, registers or local variables must be used to hold local data.
Registers may be used because the call to the ISR pushes all of the registers onto the stack.
For other local variables, we allocate a segment of bytes in the stack.
Suppose that a subroutine holds data in global variables. If Task A is executing a subroutine when an interrupt occurs, and subsequently Task B enters the subroutine, data may be lost. For example if Task A has just loaded a number 5 into a global variable when the interrupt occurs and then Task B loads a 9 into the same global variable, when Task A resumes execution, the wrong value will be held in the global variable.
Local variable in stack may be created when a subroutine is called. If the subroutine creates and loads a local variable, and it is interrupted and called again from the interrupting thread, another copy of the local variable will be created to hold the new data. By using local variables, when the original task resumes execution, all data will be intact.