Connecting Tech Pros Worldwide Help | Site Map

What is Reentrant function?

Newbie
 
Join Date: Aug 2009
Posts: 8
#1: Aug 5 '09
I had some information from internet but I’m unable to get the meaning.

Please find the below mentioned description and please let me know if (common English) are u able to get.

Must hold no static (or global) non-constant data.
True only if shared variable changes address during execution or when program is executed again. Absolute addresses of constant globals are safe unless they are changed from "outside".
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 5 '09

re: What is Reentrant function?


Google knows the answer.

kind regards,

Jos
Newbie
 
Join Date: Aug 2009
Posts: 8
#3: Aug 12 '09

re: What is Reentrant function?


Can you please give the complete meaning? I’m not able understand the meaning of the discretion.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#4: Aug 12 '09

re: What is Reentrant function?


Can you please explain what you don't understand about the definition JosAH pointed to?

I suggest this discussion will be more efficient if you try to tell us what a reentrant function is. We can then correct any errors and fill in any gaps.
Newbie
 
Join Date: Aug 2009
Posts: 8
#5: Aug 12 '09

re: What is Reentrant function?


Functions should be writing any global variable than only the function called as “reentrant”.
Global variable is used in a function but variable value is not modified its read only this function “not reentrant”
Please let me know what is the meaning of mush hold no static and global why they are mentioned “Must hold no static (or global) non-constant data”
Newbie
 
Join Date: Aug 2009
Posts: 8
#6: Aug 12 '09

re: What is Reentrant function?


1. Must hold no static (or global) non-constant data.



int GLOBAL /* define Global variable */

int Function 1(void)

{

int local ; /* define local variable */

local= GLOBAL; /* read the global variable */

local+=1;

}



int Function 2 (void)

{

GLOBAL+=1; /* modifying the value of global variable */

}



The function reentrancy is depends on

Single thread environment ,the function 1 irrespective of accessing global variable in read mode is termed as reentrant
Multiple thread environment, there is a possibility that Value of “GLOBAL” may be overrun by parallel execution of function1 and function2 and hence function 1 called as Non reentrant
Can you please let me know my understating is right or wrong?
Newbie
 
Join Date: Aug 2009
Posts: 8
#7: Aug 12 '09

re: What is Reentrant function?


1. Must hold no static (or global) non-constant data.

int GLOBAL /* define Global variable */
int Function 1(void)
{
int local ; /* define local variable */
local= GLOBAL; /* read the global variable */
local+=1;
}

int Function 2 (void)
{
GLOBAL+=1; /* modifying the value of global variable */
}

Can you please let me know above functions are reentrant or non-reentrant if reentrant please give me reasons.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,158
#8: Aug 12 '09

re: What is Reentrant function?


Actually re-entrancy is only a issue in a program that has more than one thread of execution (either multiple treads or a thread with interrupt routines or multiple treads with interrupt routines).

In a single thread environment re-entrancy is not an issue since the is only 1 thread of execution only 1 instance of the function can be running at any time.

In a multiple threaded environment then accessing non-constant global data makes a thread non-re-entrant. That is both your functions Function1 and Function2 are not re-entrant because the access the non-constant global variable GLOBAL.

The non-constant part is important because if the variable is a constant then it can not be changed so it always has the same effect on function execution so this is re-entrant.

Expand|Select|Wrap|Line Numbers
  1. const int GLOBAL = 5;  /* define constant Global variable */
  2. int Function1(void)
  3. {
  4.     int local ; /* define local variable */
  5.     local = GLOBAL; /* read the global variable */
  6.     local+=1;
  7.     return local;
  8. }
  9.  
Another alternative is to pass the data that is making the function not re-entrant so this is re-enrant

Expand|Select|Wrap|Line Numbers
  1. int GLOBAL = 5;  /* define Global variable */
  2.  
  3. int Function1(int input_value)
  4. {
  5.     int local ; /* define local variable */
  6.     local = input_value; /* read the local parameter */
  7.     local+=1;
  8.     return local;
  9. }
  10.  
  11. /* Some other code elsewhere */
  12.  
  13. result = Function1(GLOBAL);
  14.  
Function1 is no re-entrant as it only works on local variables and parameters, however th place calling Function1 is not re-entrant as it uses the global variable GLOBAL.


The real test of re-entrancy is are you access data that a different portion of the program (including the same function running in a different thread) might be changing at the same time?
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#9: Aug 12 '09

re: What is Reentrant function?


Another term you might run into is "thread-safe". It refers to whether it is safe to run a particular function in multiple threads. Banfa's link explains the subtle distinction between thread-safety and reentrancy. Evfery reentrant function is also thread-safe; but a thread-safe function is not necessarily reentrant.

As Banfa said, the real criteria for whether a function is reentrant is whether there are any portions of it where improper operation could occur if multiple instances of the function were running at once. These portions of the function are sometimes referred to as "critical regions".

For example, Function1 reads a global variable and returns that value plus one without changing the global variable. Multiple copies of Function1 will not interfere with each other -- the function appears to be reentrant. However, Function2 increments that same global variable. If Function1 and Function2 run simultaneously then it is possible for Function2 to increment the global after it has been read by Function1, but before Function1 returns. Does this make Function1 nonreentrant? It depends on the consequences of Function1 returning an out-of-date value. I suggest there is no critical region in Function1; the critical region is actually in Function1's callers, starting from just before the call to Function1 and extending to where it is finished using the return value from Function1.

For example, Function2 increments a global variable. The compiler might implement this operation by reading the global variable into a register, incrementing the register, and writing the new value back to the global variable. If so, then another instance of Function2 could interfere with itself if it occurs after the read operation, but before the write operation. The effect would be to nullify the second instance of Function2. This problem can be avoided if the compiler can be coerced into implementing the increment operation via an "atomically" (for example, via read-modify-write. Unfortunately, there is no portable way to force the compiler to do this; if for no other reason, because some processors have no support for read-modify-write. Even if you can increment the global variable atomically, you have to look at the callers of Function2 to see if they would be adversely affected by multiple Function2 threads.
Reply

Tags
reentrant function