Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an interrupt handler have to be reentrant?

I'm using a static variable inside an interrupt handler, making the interrupt handler non-reentrant.

  1. Is it OK to have a non-reentrant interrupt handler?
  2. When a hardware interrupt is raised, does the event go in some sort of a queue and wait for the current interrupt handler call to finish or does the interrupt handler get called right away?

Thanks

PS. I'm using Linux. The programming language I'm using is C if it makes a difference.

like image 762
Arash Fotouhi Avatar asked Aug 08 '13 17:08

Arash Fotouhi


People also ask

Do interrupt handlers in Linux need to be reentrant?

Interrupt handlers in Linux need not be reentrant. When a given interrupt handler is executing, the corresponding interrupt line is masked out on all processors, preventing another interrupt on the same line from being received. Normally all other interrupts are enabled, so other interrupts are serviced, but the current line is always disabled.

What is the difference between a non-reentrant and a reentrant function?

If a function is reentrant then it can be "interrupted in the middle of its execution and then safely called again" ( Reentrancy (computing) ). If an interrupt handler is non-reentrant, then you could have undefined behavior if you receive another interrupt while your original interrupt handler is executing.

What are interrupt handlers?

Interrupt handlers are initiated by hardware interrupts, software interrupt instructions, or software exceptions, and are used for implementing device drivers or transitions between protected modes of operation, such as system calls . The traditional form of interrupt handler is the hardware interrupt handler.

What is the difference between reentrant and thread-safe?

Reentrant is a function that can be called from an interrupt handler of an interrupt that interrupted the same function running previously. Thread-safe is a function that can run on several concurrent execution units at the same time. likes programming languages. Author has 309 answers and 1.9M answer views 5 y


1 Answers

The short answer is that Interrupt Service Routines are not inherently required to be reentrant. Reentrancy is only required in the case of nested interrupts. If the Operating System you use does not support nested interrupts, then you do not need to worry about reentrancy at all. If it does, you may have control over resetting the interrupt you are servicing so that you should never get a nested interrupt.

EDIT: Now that I know you're using Linux, you might find this link helpful: Can an interrupt handler be preempted by the same interrupt handler?

Essentially the answer to your question is that Linux masks an interrupt when it is asserted so that it won't preempt itself unless a specific flag is passed when registering the ISR.

Here's a relevant quote:

Interrupt handlers in Linux need not be reentrant. When a given interrupt handler is executing, the corresponding interrupt line is masked out on all processors, preventing another interrupt on the same line from being received. Normally all other interrupts are enabled, so other interrupts are serviced, but the current line is always disabled. Consequently, the same interrupt handler is never invoked concurrently to service a nested interrupt. This greatly simplifies writing your interrupt handler.

like image 157
seanmk Avatar answered Nov 14 '22 23:11

seanmk