簡體   English   中英

如何檢查struct timer_list是否過期?

[英]How to check if struct timer_list is expired?

我有一個帶有兩個“ 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 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 magic從struct timer_list轉到struct tmp 我只是在猜測這個問題是由某種家庭作業引起的,因此我將其余部分留給讀者練習。 享受學習!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM