您的位置:首页 > 运维架构 > Linux

linux时间管理 之 内核定时器

2012-09-28 15:26 288 查看
1、内核定时器的定义

    struct timer_list {

           /*

           * All fields that change during normal runtime grouped to the

           * same cacheline

           */

           struct list_head entry;

           unsigned long expires;

           struct tvec_base *base;

 

           void (*function)(unsigned long);

           unsigned long data;                                                                                                           

 

            int slack;

 

  #ifdef CONFIG_TIMER_STATS

            void *start_site;

            char start_comm[16];

            int start_pid;

  #endif

  #ifdef CONFIG_LOCKDEP

            struct lockdep_map lockdep_map;

  #endif

  };
   unsigned long expires :指定定时器的到期时间,用jiffies表示。

   void (*function)(unsigned long):定时器到期后调用的定时器函数。

   unsigned long data:定时器对象中携带的数据。通常当成实际参数传递给定时器函数function。

2、init_timer

      初始化定时器结构题timer_list中与内核相关的部分。不包括expires,function,data。

3、add_timer

      将定时器对象加入到系统中,这样在定时器到期后,定时器函数被调用。

      系统通过一个PER_CPU型的数据结构来管理驱动程序中向系统提交的定时器。

      struct tvec_base {

                spinlock_t   lock;

                struct timer_list *running_timer;

                unsigned long timer_jiffies;

                unsigned long next_timer;

                struct tvec_root tv1;

                struct tvec tv2;

                struct tvec tv3;

                struct tvec tv4;

                struct tvec tv5;

     } ____cacheline_aligned;

     tv1,tv2,tv3,tv4,tv5是数组,每个数组中代表一个到时的jiffies值。

     每次时钟中断的中断处理例程的下半部,TIMER_SOFTIRQ,都会扫描tvec_base,如果定时器到期,则调用其定时器函数。

    4、del_timer  del_timer_sync

      用来删除定时器

      区别:在但处理器系统上二者等同。

                 在多处理器系统中,del_timer_sync除了像del_timer一样将定时器从系统中删除外,可以保证在函数返回的时候,没有任何处理器正在执行该定时器函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息