-1

I am aware that I certainly can't use msleep or usleep or any such function for introducing delays in a kernel ISR routine.
I have a kernel driver which have certain ISRs defined inside it. In one of the ISR block I have to insert a certain delay of order of millisecs. Lets say:

{  
//A  
//here I need sleep  
//B  
} 

can I use something like:

{  
//A  
for(i=0;i<1000;i++);
//B  
}  

Lets say my processor is executing at 1Gbps, will the above for loop give me a delay of 1000 usecs, i.e. 1ms?

mdsingh
  • 973
  • 2
  • 13
  • 20
  • 1
    Functions `udelay` and `ndelay` implements *busy-waiting* delays, so you may use them in ISR. Waiting interval for the first function is measured in microseconds (1 / 1 000 000), for other one - in nanoseconds (1 / 1 000 000 000). – Tsyvarev Apr 05 '16 at 06:39
  • Yeah I realized that later. That worked. Thanks. – mdsingh Apr 05 '16 at 09:51

3 Answers3

0

You must not sleep inside an interrupt handler.

Furthermore, you should wait for a long time inside an interrupt handler; this would block all proccesses and all other interrupts on the same CPU.

If you driver needs to do two things at different times, it should use a second interrupt or a timer to do the second thing.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

I would be interested to hear about the reasons for having an intentional delay in an ISR. Generally speaking, it's a no-no. If you think you need one, then most probably it means that you need to rethink your code design.

As for introducing microscopic delays, one thing that I have used is cpu_relax(). This function is also used in the kernel to implement the above mentioned udelay() and ndelay() for some CPU architectures. I would advise you to take a look and see where and how this function is used in the Linux kernel. That might give you some ideas for your specific situation.

Aleksey
  • 623
  • 5
  • 10
-2

Functions udelay and ndelay implements busy-waiting delays, so you may use them in ISR. As suggested by Tsyvarev.

mdsingh
  • 973
  • 2
  • 13
  • 20