reentrant function and non-reentrant function "Go"

Source: Internet
Author: User

This article was reproduced from: http://www.cnblogs.com/parrynee/archive/2010/01/29/1659071.html

reentrant functions


In the design of real-time systems, multiple tasks often call the same function. If this function is unfortunately designed to be a non-reentrant function, then different tasks calling this function may modify the data of other tasks invoking the function, which can lead to unpredictable consequences. So what is reentrant function? Reentrant refers to a process that can be called by multiple tasks, and the task does not have to worry about whether the data will go wrong when it is called. Non-reentrant functions are considered unsafe functions in the design of real-time systems.


Most of the functions that meet the following conditions are non-reentrant:


(1) A static data structure is used in the function body;


(2) the malloc () or free () function is called in the function body;


(3) The function body calls the standard I/O function.


How can I write a reentrant function? In the function body does not access those global variables, do not use static local variables, persist in using only the default state (auto) Local variables, the written function will be reentrant. If you must access global variables, remember to protect global variables with mutually exclusive semaphores. or call the function before the shutdown interrupt, call and then open the interrupt.

Reentrant functions can be called by more than one task without worrying about data corruption. Reentrant functions can be interrupted at any time, and can be run after a period of time, while the corresponding data is not lost. You can re-enter the function or use only local variables, either in the CPU registers or on the stack, or with global variables, to protect the global variables.

Myth 2:


A reentrant function is simply: a function that can be interrupted. That is, you can break his run at any time the function executes, and execute another piece of code without any errors when the task is scheduled. The non-reentrant function is not able to run in a multitasking environment because it uses some system resources, such as the global variable area, the interrupt vector table, and so on, so that if he is interrupted, there may be a problem.

Basically the following function is non-reentrant
(1) A static data structure is used in the function body;


(2) the malloc () or free () function is called in the function body;


(3) The function body calls the standard I/O function.

The only way to turn a non-reentrant function into reentrant is to rewrite it with reentrant rules.
In fact it is very simple, as long as you follow a few very easy to understand the rules, then the written function is reentrant.

First, do not use global variables. Because other code is likely to overwrite these variable values.

Second, when interacting with hardware, remember to perform operations like Disinterrupt (), which is to turn off hardware interrupts. Complete the interaction remember to open the interrupt, in some series, this is called "Enter/exit core" or use os_enter_kernal/os_exit_kernal to describe. This is critical zone protection.

Third, you cannot call any function that is not reentrant.

IV, use the stack with caution. It is best to os_enter_kernal first before use.

There are some rules that are well understood, in short, always remember a word: to ensure that the interruption is safe!

reentrant function and non-reentrant function "Go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.