- Forward


Reentrancy
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Two Related Definitions
Back SMYC Forward
  • Origin of the Term "Reentrant":
    • A function is reentrant if its execution can be interrupted and it can be called again (before the previous invocation is completed)
  • Origin of the Term "Sharable Code":
    • A function is reentrant if its instructions are invariant and, hence, can be shared across instances
Explicitly Reentrant
Back SMYC Forward
  • The Concept:
    • The reentrancy of the function can be asserted regardless of how it is called
  • Ensuring Explicit Reentrancy:
    • Accept all arguments by value
    • Do not accept pointers as arguments
    • Use only local variables (i.e., in C use only automatic variables, no static variables or variables with file scope; in Java use only variables with local scope)
Implicitly Reentrant
Back SMYC Forward
  • The Concept:
    • The reentrancy of the function depends on the caller (as well as the callee)
  • Relaxing Explicit Reentrancy:
    • Accept arguments by reference or pointer arguments but ensure that the caller only passes referenecs/pointers to appropriate variables
Common Causes of Non-Reentrancy
Back SMYC Forward
  • Modification of "Global" Variables:
    • Consider the example of a function that updates a "global" linked list (e.g., malloc() in C) -- what might happen if one call is interrupted by another?
  • Return of Statically Allocated Memory:
    • One call would "overwrite" the information being returned by another
  • Internal Bookeeping Using Statically Allocated Memory:
    • Consider what happens when a function uses a statically allocated buffer (e.g., printf() in C)
There's Always More to Learn
Back -