Reentrant function
In the real-time system design, multiple tasks often call the same function. If this function is unfortunately designed as a non-reentrant function, different tasks may modify the data of other tasks that call this function, resulting in unpredictable consequences. So what is a reentrant function? Reentrant refers to a process that can be called by multiple tasks. When a task is called, you do not have to worry about data errors. Non-reentrant functions are considered unsafe functions in real-time system design.
Most functions that meet the following conditions cannot be reentrant:
(1) The function uses a static data structure;
(2) the malloc () or free () function is called in the function body;
(3) standard I/O functions are called in the function body.
How can I write a reentrant function? The global variables are not accessed in the function body, and static local variables are not used. The default state (auto) local variables are used only, and the written functions are reentrant. If you must access global variables, remember to use mutex semaphores to protect global variables. You can also call the function to interrupt the function before it is called.
A reentrant function can be called by more than one task without worrying about data corruption. The reentrant function can be interrupted at any time and run again after a period of time, but the corresponding data will not be lost. You can reload the function or only use local variables, that is, save them in the CPU register or stack. Or use global variables to protect global variables.
Statement 2:
A reentrant function is simply a function that can be interrupted. That is to say, you can interrupt the execution of this function at any time, and execute another code During task scheduling without any errors. The non-reentrant function uses some system resources, such as the global variable area and the interrupt vector table. Therefore, if it is interrupted, problems may occur, therefore, such functions cannot run in a multi-task environment.
Basic upper and lower functions cannot be reentrant.
(1) The function uses a static data structure;
(2) the malloc () or free () function is called in the function body;
(3) standard I/O functions are called in the function body.
The only way to turn a non-reentrant function into a reentrant is to rewrite it using the reentrant rule.
In fact, it is very simple. As long as there are several rules that are easy to understand, the written functions are reentrant.
First, do not use global variables. Because other code may overwrite these variable values.
Second, when interacting with the hardware, remember to perform operations like disinterrupt (), that is, disable hardware interruption. When interaction is completed, remember to open the interrupt. In some series, this is called "Enter/exit the core" or use OS _enter_kernal/OS _exit_kernal to describe it. // This is critical zone protection
Third, you cannot call any function that cannot be reentrant.
Fourth, use the stack with caution. OS _enter_kernal is recommended before use.
There are also some rules that are well understood. In short, remember one sentence at a time to ensure that interruption is safe!