我有一个带有两个“ struct timer_list”的c结构。到期后,两个timer_list都调用相同的回调函数。在回叫功能中,有一种方法可以找到哪个计时器已到期。还有一件事,timer_pending(&tmp-> timeout)可以达到这个目的,还是仅给出计时器是否至少启动了一次而计时器是否过期才给出输出。该代码建议仅检查.next字段是否为NULL。请提供一些见解。
struct tmp {
struct timer_list a;
struct timer_list b;
}
/*Both the timer calls function func upon expiry and passes stuct tmp as
argument*/
static void func(unsigned long x) {
struct tmp *tmp1 = (struct tmp *)x;
//Find out whether timer a or timer b has expired.
}
答案 0 :(得分:0)
警告:此问题适用于Linux Kernel计时器API的旧接口。自提交以来,计时器界面已更改:
commit e99e88a9d2b067465adaa9c111ada99a041bef9a
Author: Kees Cook <keescook@chromium.org>
Date: Mon Oct 16 14:43:17 2017 -0700
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
[..]
我给出的答案当然适用于旧计时器界面。新界面略有不同,并且更加简洁。
因此,如果要区分哪个计时器即将到期,则不应将包含两个计时器的结构(例如struct tmp
)传递给setup_timer
函数。
换句话说,根据您的问题,您正在使用以下方式设置计时器:
struct tmp t;
setup_timer(&t.a, func, (unsigned long)&t);
setup_timer(&t.b, func, (unsigned long)&t);
您应该这样做:
struct tmp t;
setup_timer(&t.a, func, (unsigned long)&t.a);
setup_timer(&t.b, func, (unsigned long)&t.b);
然后使用container_of
魔法从struct timer_list
到struct tmp
。我只是在猜测这个问题是由某种家庭作业引起的,因此我将其余部分留给读者练习。喜欢学习!