Linux内核定时器

一、内核定时器定义:

struct timer_list {
    struct list_head entry;
    unsigned long expires;

    void (*function)(unsigned long);
    unsigned long data;

    struct tvec_base *base;
#ifdef CONFIG_TIMER_STATS
    void *start_site;
    char start_comm[16];
    int start_pid;
#endif
#ifdef CONFIG_LOCKDEP
    struct lockdep_map lockdep_map;
#endif
};

二、使用方法:

  ①struct timer_list timer;

   void fun (unsigned long arg);

   unsigned long = 0;

  ②init_timer (&timer);

  ③setup_timer (&timer, fun, (unsigned long )arg);  

  ④timer.expires = jiffires + 60 * HZ; //自定义一个超时时间,jiffires是计时器的当前值(单位是中断),HZ是每秒对应的中断次数。

  ⑤add_timer (&timer);

  ⑥如果是希望周期运行,则每次在注册的函数执行完毕后需要重新设置、激活定时器,可以使用mod_timer()。内核注释:

    * mod_timer(timer, expires) is equivalent to:
    *
    * del_timer(timer); timer->expires = expires; add_timer(timer);

 

   mod_timer (&timer, jiffires + 60 * HZ); //写在fun函数的最后

  ⑦del_timer (&timer);

 

ps:个人快速笔记,如有错误期待指出。

posted @ 2014-08-24 10:53  svking  阅读(246)  评论(0编辑  收藏  举报