高分辨率定時(shí)器(hrtimer)以ktime_t來定義時(shí)間, 精度可以達(dá)到納秒級別 ,ktime_t定義如下:
typedef s64 ktime_t;
可以用ktime_set來初始化一個(gè)ktime對象,常用方法如下:
ktime_t t = ktime_set(secs, nsecs);
高分辨率hrtimer結(jié)構(gòu)體定義如下:
struct hrtimer {
struct timerqueue_node node;
ktime_t _softexpires;
enum hrtimer_restart (*function)(struct hrtimer *);
struct hrtimer_clock_base *base;
unsigned long state;
......
};
enum hrtimer_restart {
HRTIMER_NORESTART, /* Timer is not restarted */
HRTIMER_RESTART, /* Timer must be restarted */
};
struct hrtimer結(jié)構(gòu)體中最主要的成員就是回調(diào)函數(shù)function,回調(diào)函數(shù)的返回值可以為HRTIMER_NORESTART或HRTIMER_RESTART。HRTIMER_NORESTART代表不需要重啟定時(shí)器,HRTIMER_RESTART代表需要重啟定時(shí)器。
最常用的接口如下:
hrtimer_init(struct hrtimer *timer, clockid_t clock_id , enum hrtimer_mode mode)
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
hrtimer_forward_now(struct hrtimer *timer,ktime_t interval)
hrtimer_cancel(struct hrtimer *timer)
hrtimer_init:初始化 struct hrtimer結(jié)構(gòu)對象。clockid_t 是時(shí)鐘的類型, 種類很多,常見的有四種:
CLOCK_REALTIME:系統(tǒng)實(shí)時(shí)時(shí)間。CLOCK_MONOTONIC:從系統(tǒng)啟動(dòng)時(shí)開始計(jì)時(shí),自系統(tǒng)開機(jī)以來的單調(diào)遞增時(shí)間CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間,包含該進(jìn)程下的所有線程。CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間。
mode 是時(shí)間的模式,可以是 HRTIMER_MODE_ABS, 表示絕對時(shí)間, 也可以是 HRTIMER_MODE_REL,表 示相對時(shí)間。hrtimer_start:啟動(dòng)定時(shí)器。tim 是設(shè)定的到期時(shí)間, mode 和hrtimer_init中的mode參數(shù)含義相同。hrtimer_forward_now: 修改到期時(shí)間為從現(xiàn)在開始之后的 interval 時(shí)間。hrtimer_cancel:取消定時(shí)器。
-
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1956瀏覽量
88559 -
Linux
+關(guān)注
關(guān)注
88文章
11763瀏覽量
219091 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3368瀏覽量
123711
發(fā)布評論請先 登錄
#硬聲創(chuàng)作季 #Linux 學(xué)Linux-2.18.1 高精度延時(shí)實(shí)驗(yàn)-GPT定時(shí)器原理-2
GPT高精度延時(shí)定時(shí)器簡介
Linux和RTOS的時(shí)鐘和定時(shí)器怎么使用
長時(shí)間高精度定時(shí)器
Linux驅(qū)動(dòng)高精度定時(shí)器hrtimer
評論