我有一个 c 结构,里面有两个“struct timer_list”。两个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.
}
最佳答案
警告:这个问题适用于 Linux 内核计时器 API 的旧接口(interface)。定时器接口(interface)自提交后发生了变化:
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
.我只是猜测这个问题是由某种家庭作业引起的,所以我会把剩下的留给读者作为练习。享受学习!
关于c - 如何检查 struct timer_list 是否过期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51333247/