在linux内核任务,hrtimer在Linux内核中重复任务

我的目标是使用hrtimer结构在

linux内核中创建一个定期任务.我希望它每500毫秒重复一次.

但是,我对hrtimer在linux内核中的工作方式有点困惑(参见linux / hrtimer.h).我知道时间是指定的,回调应该返回HRTIMER_RESTART或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");

}

有人能解释一下重置计时器在回调函数中的作用吗?谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值