c - 如何检查 struct timer_list 是否过期?

标签 c linux timer linux-kernel open-source

我有一个 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_liststruct tmp .我只是猜测这个问题是由某种家庭作业引起的,所以我会把剩下的留给读者作为练习。享受学习!

关于c - 如何检查 struct timer_list 是否过期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51333247/

相关文章:

Javascript 暂停计时器问题...无法计算

.net - 在耗时事件中使用启动和停止 .Net 计时器

c - 将整数向下舍入到最接近的 4 倍数的标准方法

linux - awk 脚本中的 $0 和 $1(美元符号 0 和 1)是什么?

linux - 如何在 Linux 中获得最准确的实时周期性中断?

linux - 如何在 Linux 中读取 CUDA .cubin 二进制文件?

linux - 强制引导进入 Linux 命令行

c++ - 仅使用取消引用将 C 样式字符串复制到免费存储区

c - 带 double 的 SSE,不值得吗?

c - 为什么要检查函数中 fscanf 返回值 < 1?