【问题标题】:add_timer inside kernel timer function doesn't require scheduling?内核定时器函数中的 add_timer 不需要调度?
【发布时间】:2016-05-25 10:36:34
【问题描述】:
当我们使用内核定时器时,内核定时器运行在软件中断中,所以内核定时器函数运行在定时器中断上下文中。
void timer_func(unsigned long arg)
{
my_timer.expires = jiffies + HZ;
add_timer(&my_timer);
}
那么add_timer()内部的内核定时器函数不需要调度?
因为在中断上下文中调度被禁用了。
【问题讨论】:
标签:
linux-kernel
linux-device-driver
interrupt
interrupt-handling
【解决方案1】:
是的,add_timer 函数可以在中断上下文中使用。在定时器回调函数中调用它是重复做某事的标准方式。
【解决方案2】:
我认为 OP 的意思是“它如何在中断 ctx 中工作?”。
所以, add_timer() 本身并不直接调用 schedule() ;它调用 mod_timer():
void add_timer(struct timer_list *timer)
{
BUG_ON(timer_pending(timer));
mod_timer(timer, timer->expires);
}
EXPORT_SYMBOL(add_timer);
正如上面这个函数的代码注释清楚地表明:
"...
* The kernel will do a ->function(->data) callback from the
* timer interrupt at the ->expires point in the future. The
* current time is 'jiffies'.
..."
所以它不会直接调用 schedule();它将在到期时调用您的计时器功能。请注意,定时器功能在(软)中断上下文中运行,因此不要执行任何会直接或间接调用 schedule() 的操作。