All Topics  
Reentrant

 

   Email Print
   Bookmark   Link






 

Reentrant



 
 
A computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 or routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 is described as reentrant if it can be safely executed concurrently
Concurrency (computer science)

In computer science, concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other....
; that is, the routine can be re-entered while it is already running. To be reentrant, a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 or routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
:



Multiple levels of 'user/object/process priority
Priority queue

A priority queue is an abstract data type in computer programming that supports the following three operations:* InsertWithPriority: add an element to the Queue_ with an associated priority...
' and/or multiprocessing
Multiprocessing

Multiprocessing is the use of two or more CPU within a single computer system. The term also refers to the ability of a system to support more than one processor and/or the ability to allocate tasks between them....
 usually complicate the control of reentrant code. Also, IO code is usually not reentrant because it relies on shared, singleton resources such as disks.

Reentrancy is a key feature of functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
.

Typically recursive subroutines
Recursion (computer science)

Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
 need to be reentrant. Also, subroutines that are directly or indirectly called from an interrupt handler typically need to be reentrant.

Reentrant interrupt handler
A "reentrant interrupt handler" is an interrupt handler
Interrupt handler

An interrupt handler, also known as an interrupt service routine , is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt....
 that re-enables interrupts early in the interrupt handler.






Discussion
Ask a question about 'Reentrant'
Start a new discussion about 'Reentrant'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 or routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 is described as reentrant if it can be safely executed concurrently
Concurrency (computer science)

In computer science, concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other....
; that is, the routine can be re-entered while it is already running. To be reentrant, a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 or routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
:

  • Must hold no static
    Static variable

    In computer programming, the data value of a static variable persists for the life of the running process. Typically, a static variable has a broader Scope than other variables....
     (or global) non-constant data.
  • Must not return the address to static (or global) non-constant data.
  • Must work only on the data provided to it by the caller.
  • Must not rely on locks to singleton
    Singleton pattern

    In software engineering, the singleton pattern is a design pattern that is used to restrict Instantiation of a class to one object-oriented programming....
     resources.
  • Must not modify its own code -- no self-modifying code
    Self-modifying code

    In computer science, self-modifying code is Code that alters its own Instruction while it is Execution - usually to reduce the instruction path length and improve performance....
  • Must not call non-reentrant computer program
    Computer program

    Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
    s or routines
    Subroutine

    In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
    .


Multiple levels of 'user/object/process priority
Priority queue

A priority queue is an abstract data type in computer programming that supports the following three operations:* InsertWithPriority: add an element to the Queue_ with an associated priority...
' and/or multiprocessing
Multiprocessing

Multiprocessing is the use of two or more CPU within a single computer system. The term also refers to the ability of a system to support more than one processor and/or the ability to allocate tasks between them....
 usually complicate the control of reentrant code. Also, IO code is usually not reentrant because it relies on shared, singleton resources such as disks.

Reentrancy is a key feature of functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
.

Typically recursive subroutines
Recursion (computer science)

Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
 need to be reentrant. Also, subroutines that are directly or indirectly called from an interrupt handler typically need to be reentrant.

Reentrant interrupt handler


A "reentrant interrupt handler" is an interrupt handler
Interrupt handler

An interrupt handler, also known as an interrupt service routine , is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt....
 that re-enables interrupts early in the interrupt handler. This may reduce interrupt latency
Interrupt latency

In real-time operating systems, interrupt latency is the time between the generation of an interrupt by a device and the servicing of the device which generated the interrupt....
.

Jack Ganssle recommends re-enabling interrupts as soon as possible in the interrupt handler.

John Regehr recommends avoiding such early re-enabling of interrupts unless it is necessary.

Examples

In the following piece of C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 code, neither functions f nor g are reentrant.

int g_var = 1;

int f

int g

In the above, f depends on a global variable
Global variable

In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms....
 g_var; thus, if two threads execute it and access g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

These slightly-altered versions are reentrant:

int f(int i)

int g(int i)

Relation to thread safety


Both concepts of reentrancy and thread safety relate to the way functions handle resources. However, they are not the same. While the concept of reentrancy can affect the external interface of a function, thread safety only concerns the implementation of the function and not its external interface.

  • In most cases, to make a non-reentrant function reentrant, its external interface must be modified such that all data is provided by the caller of the function.


  • To make a thread-unsafe function thread-safe, only the implementation needs to be changed, usually by adding synchronization
    Synchronization (computer science)

    In computer science, synchronization refers to one of two distinct but related concepts: synchronization of process , and synchronization of data....
     blocks to protect shared resources from concurrent accesses by different threads.


Therefore, reentrancy is a more fundamental property than thread-safe
Thread-safe

Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads....
ty and by definition, leads to thread-safe
Thread-safe

Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads....
ty: Every reentrant function is thread-safe; however, not every thread-safe function is reentrant.

See also

  • Referential transparency (computer science)


External links

  • Article "" by Dipak K Jha
  • "," from AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs, 2nd edition, 1999.