繁体   English   中英

在Linux内核中使用hrtimers

[英]Using hrtimers in the Linux Kernel

在网上和在Stackoverflow上搜索了很多时间之后,我意识到在Linux内核中没有很多使用hrtimers的具体示例。 我发现的任何示例都含糊不清,没有说明其程序的功能,也没有说明hrtimers的工作原理足以使我理解。

我知道/include/linux/hrtimer.h有文档,但是该文档尚不清楚,似乎以为我已经熟悉它们。

谁能给出使用此计时器的基本示例?

简单示例,每100毫秒回调一次:

#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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM