Linux: hrtimer 用法

hrtimer = high res timer

内核为高精度定时器重新设计了一套软件架构,它可以为我们提供纳秒级的定时精度,以满足对精确时间有迫切需求的应用程序或内核驱动,例如多媒体应用,音频设备的驱动程序等等。以下的讨论用hrtimer(high resolution timer)表示高精度定时器。


#include

static struct hrtimer hrtimer;

//init

    hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    hrtimer.function = timer_hr_interrupt;

//

static enum hrtimer_restart timer_hr_interrupt(struct hrtimer *timer)
{  
    do_something();
    return HRTIMER_NORESTART;
}


void start_timer(void)

{

hrtimer_start(&hrtimer, ktime_set(0, first_time* NSEC_PER_USEC), HRTIMER_MODE_REL);

}


static void do_something(void)

{

// do something

...

//start next timer

hrtimer_start(&hrtimer, ktime_set(0, next_time* NSEC_PER_USEC), HRTIMER_MODE_REL);

}

你可能感兴趣的:(Linux: hrtimer 用法)