10 定时器

第6课.定时器
代码

1 定时器的时间单位

定时器对应软中断TIMER_SOFTIRQ

在内核中.config文件中CONFIG_HZ配置为每秒linux的时钟滴答次数,没发生一次全局变量jiffies便会增加1

在日常使用中使用HZ表示一秒

2 常用函数及其数据结构

函数和宏 描述
DEFINE_TIMER 定义一个timer,初始化function、expires等
timer_setup 初始化timer,为初始化function、expires等赋值
add_timer 将定时器添加到timer链接到的timer_base上
add_timer_on 将定时器添加到timer链接到的timer_base上,指定CPU
mod_timer 将定时器添加到timer链接到的timer_base上,修改定时器到期时间
del_timer 删除定时器,从链表中删除
del_timer_sync 同步删除定时器,从链表中删除
msec_to_jiffies 将毫秒转化为jiffies
  • struct timer_list *timer (timer_list结构体)

    struct timer_list {
    	/*
    	 * All fields that change during normal runtime grouped to the
    	 * same cacheline
    	 */
    	struct hlist_node	entry;				/* 链表的节点,链接到base包含的链表 */
    	unsigned long		expires;			/* 到期时间 */
    	void			(*function)(unsigned long);		/* 回调函数 */
    	unsigned long		data;
    	u32			flags;								/* 标志 */
    
    #ifdef CONFIG_TIMER_STATS
    	int			start_pid;
    	void			*start_site;
    	char			start_comm[16];
    #endif
    #ifdef CONFIG_LOCKDEP
    	struct lockdep_map	lockdep_map;
    #endif
    };
    
  • setup_timer(timer, fn, data)

    初始化timer_list设置函数和参数

    于init_timer类似

  • init_timer(timer)

    初始timer_list

  • add_timer(struct timer_list *timer)

    向内核添加定时器。timer->expires表示超时时间

  • mod_timer(struct timer_list *timer, unsigned long expires)

    修改定时器超时时间,等价于del_timer(timer); timer->expires = expires; add_timer(timer)

  • del_timer(struct timer_list *timer)

    删除定时器

posted @ 2023-03-12 15:15  人民广场的二道贩子  阅读(32)  评论(0)    收藏  举报