我的目标是使用hrtimer
struct 在 linux 内核中创建一个循环任务。我希望它每 500 毫秒重复一次。
但是,我hrtimer
对 linux 内核中的工作方式有点困惑(请参阅 参考资料linux/hrtimer.h
)。我知道指定了时间,回调应该返回HRTIMER_RESTART
or HRTIMER_NORESTART
。我在网上找到了一些资源,指出需要使用该hrtimer_forward
方法在回调中重置计时器。但是,我看到的消息来源对于如何添加时间有点不清楚。这是我到目前为止的代码:
static struct hrtimer timer;
static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
printk(KERN_ERR "Callback\n");
//I know something needs to go here to reset the timer
return HRTIMER_RESTART;
}
static int init_timer(void)
{
ktime_t ktime;
unsigned long delay_in_ms = 500L;
printk(KERN_ERR "Timer being set up\n");
ktime = ktime_set(0,delay_in_ms*1E6L);
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = &timer_callback;
printk(KERN_ERR "Timer starting to fire\n");
printk(KERN_ERR "in %ldms %ld\n", delay_in_ms, jiffies);
hrtimer_start(&timer, ktime, HRTIMER_MODE_REL);
return 0;
}
static void clean_load_balancing_timer(void)
{
int cancelled = hrtimer_cancel(&timer);
if (cancelled)
printk(KERN_ERR "Timer still running\n");
else
printk(KERN_ERR "Timer cancelled\n");
}
有人可以准确解释在回调函数中重置计时器是如何工作的吗?谢谢!