在网上和 Stackoverflow 上搜索了大量时间后,我意识到 Linux 内核中使用 hrtimers 的具体示例并不多。我发现的任何例子都是模糊的,没有解释他们程序的功能,也没有解释 hrtimers 如何工作得足够好让我理解。
我知道 上有文档/include/linux/hrtimer.h,但该文档不清楚,并且似乎假设我已经熟悉它们。
谁能给出使用这个计时器的基本示例吗?
简单的例子,每100ms回调一次:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
static struct hrtimer test_hrtimer;
static u64 sampling_period_ms = 100; // 100ms
static u32 loop = 0;
static enum hrtimer_restart test_hrtimer_handler(struct hrtimer *timer)
{
pr_info("test_hrtimer_handler: %u\n", ++loop);
hrtimer_forward_now(&test_hrtimer, ms_to_ktime(sampling_period_ms));
return HRTIMER_RESTART;
}
static int __init test_hrtimer_init(void)
{
hrtimer_init(&test_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
test_hrtimer.function = &test_hrtimer_handler;
hrtimer_start(&test_hrtimer, ms_to_ktime(sampling_period_ms), HRTIMER_MODE_REL);
pr_info("init test_hrtimer.\n");
return 0;
}
static void __exit test_hrtimer_exit(void)
{
hrtimer_cancel(&test_hrtimer );
pr_info("exit test_hrtimer.\n");
return;
}
module_init(test_hrtimer_init);
module_exit(test_hrtimer_exit);
MODULE_LICENSE("GPL");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3026 次 |
| 最近记录: |