Thursday, December 22, 2011

Reentrancy & Reentrant function

What we mean by reentrancy and reentrant function? What is it’s significance in programming? What are the conditions that a function to be reentrant?

Reentrancy is applicable in concurrent programming. Cooperative scheduling need not to consider reentrancy.If one thread tries to change the value of shared data at the same time as another thread tries to read the value, the result is not predictable. We call it as race condition.

A reentrant function guarantees it’s functionality even when the function is invoked (reentered) from concurrent multiple threads. If the function is using variables with static extent, the results will be unpredictable because of race conditions. A library developer need to take care in writing reentrant code. Sometimes the bug lies in the library rather than programmer’s code. It is not easy to recreate such bugs during fix.
A Reentrant Function shall satisfy the following conditions,
  1. Should not call another non-reentrant function
  2. Should not access static life time variables (static/extern)
  3. Should not include self modifying code
Example:To make it simple. Think of a C library function printf. When printf accesses your terminal to print the characters. Now say, process A runs a printf statement and it reached line 5 of printf function.(line 5 in source of printf in C library). Now consider that another process, B issues a printf at the time when A reached line 5. The context for printf for B should be from line 1, NOT from line 5. This is the case of a reentrant function, which maintains its state of execution, in multiple processes or thread.
 
Thread safety and Reentrant functions
Thread safety and reentrant functions are connected. Every thread safe function is reentrant, but the converse need not be true. A thread safe function can be called from multiple threads even when the function accessing shared data. A thread safe function is guaranteed to serialize accessing any shared data.
An example is string class (C++). Most implementations of string class are reentrant but not thread safe. One can create different instances of string class across multiple threads, but can’t access same instance atomically from multiple threads.
Reentrancy is key while writing critical sections, signal handlers, interrupt service routines, etc…

Reentrant Function vs Functions with Reentrancy:
The above statement may look strange. There are different memory models of processors. Some architectures use a common set of memory locations to pass arguments to functions. In such case the functions can’t maintain reentrancy when invoked multiple times.  Code development for such processors must qualify the function prototype with compiler provided keywords to ensure reentrancy.


Useful Links:
http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.genprogc%2Fdoc%2Fgenprogc%2Fwriting_reentrant_thread_safe_code.htm
http://en.wikipedia.org/wiki/Talk:Thread_safety
http://en.wikipedia.org/wiki/Thread-safe

2 comments:

  1. http://www.geeksforgeeks.org/archives/9096

    ReplyDelete
  2. Re-entrancy is a useful, memory-saving technique for multiprogrammed timesharing systems. A Reentrant function is one in which multiple users can share a single copy of a program during the same period.

    Reentrancy has 2 key aspects:
    * The program code cannot modify itself.
    * The local data for each user process must be stored separately.

    To be non-reentrant, a computer program or routine:

    Should hold a static(or global) non-constant data.

    ReplyDelete