简体   繁体   English

从计时器回调函数内部调用mod_timer

[英]Calling mod_timer from inside timer callback function

I'm writing a kernel module for linux, and I want my timer to re-set itself. 我正在为Linux编写一个内核模块,并且希望自己的计时器重新设置。 To this end, I thought to call mod_timer from inside the timer's callback function, as shown: 为此,我想从计时器的回调函数内部调用mod_timer ,如下所示:

static void sched_send(unsigned long data)
{
    send_now();
    mod_timer(&test_timer, jiffies+(get_interval()*HZ));
}

static void timer_start(void)
{
    set_log_msg("Meep meep!");

    test_timer.function = sched_send;
    test_timer.expires = jiffies + HZ*get_interval();
}

However, I've read mod_timer deletes the timer and re-adds it. 但是,我读过mod_timer删除了计时器并重新添加了它。 Will it cause problems? 会引起问题吗? If so, is there a better way to create a repeating timer for kernel modules? 如果是这样,是否有更好的方法为内核模块创建重复计时器?

Your function timer_start() will have to call add_timer() after it sets up the function and the expiration time. 您的函数timer_start()在设置函数和到期时间后必须调用add_timer() Once the timer function triggers, your timer is no longer active, so all you have to do is reset the .expires field to your new value and call add_timer() again. 一旦计时器函数触发,您的计时器将不再活动,因此您要做的就是将.expires字段重置为新值,然后再次调用add_timer() Be sure you provide a clean way to stop rescheduling the timer, for example on module unload. 确保您提供了一种干净的方法来停止重新计划计时器,例如在模块卸载时。

send_now();
if(!terminate_timer) {
    test_timer.expires = jiffies + HZ*get_interval();
    add_timer(&test_timer);
}

It is safe to execute mod_timer from the timer callback. 从计时器回调中执行mod_timer是安全的。

From the kernel source (kernel/timer.c): 从内核源代码(kernel / timer.c):

/* mod_timer(timer, expires) is equivalent to:
*
*     del_timer(timer); timer->expires = expires; add_timer(timer);
*     ...
*/

As for del_timer , 至于del_timer

/*
* del_timer - deactive a timer.
* @timer: the timer to be deactivated
*
* del_timer() deactivates a timer - this works on both active and inactive
* timers. 
* ...
*/

As noted by Peter, you need to invoke add_timer anytime you want to start/restart the timer. 正如Peter所指出的,您需要在要启动/重新启动计时器的任何时间调用add_timer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM