What is Reentrant function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kp7796
    New Member
    • Aug 2009
    • 8

    #1

    What is Reentrant function?

    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
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Google knows the answer.

    kind regards,

    Jos

    Comment

    • kp7796
      New Member
      • Aug 2009
      • 8

      #3
      Can you please give the complete meaning? I’m not able understand the meaning of the discretion.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        • kp7796
          New Member
          • Aug 2009
          • 8

          #5
          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”

          Comment

          • kp7796
            New Member
            • Aug 2009
            • 8

            #6
            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?

            Comment

            • kp7796
              New Member
              • Aug 2009
              • 8

              #7
              function reentrancy

              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.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                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.

                Code:
                const int GLOBAL = 5;  /* define constant Global variable */
                int Function1(void)
                {
                    int local ; /* define local variable */
                    local = GLOBAL; /* read the global variable */
                    local+=1;
                    return local;
                }
                Another alternative is to pass the data that is making the function not re-entrant so this is re-enrant

                Code:
                int GLOBAL = 5;  /* define Global variable */
                
                int Function1(int input_value)
                {
                    int local ; /* define local variable */
                    local = input_value; /* read the local parameter */
                    local+=1;
                    return local;
                }
                
                /* Some other code elsewhere */
                
                result = Function1(GLOBAL);
                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?

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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.

                  Comment

                  Working...