定时器中断 timer_jiffies这个值的说明

4545阅读 0评论2012-02-29 jinxinxin163
分类:LINUX

刚开始的时候,对tvec_base结构的timer_jiffies变量的值怎么都想不通,不知道是什么意思?
后来通过看代码,终于明白了,通俗的讲,就是:
当前正在处理的定时器的到期时间
说到这里,就有人有疑问了,照你这么说,timer_jiffies不就等于jiffies了么?
非也,大家想想定时器延时执行的情况吧,在那种情况下,jiffies就大于timer_jiffies!

  1. static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
  2. {
  3.     unsigned long expires = timer->expires;
  4.     unsigned long idx = expires - base->timer_jiffies;
  5.     struct list_head *vec;

  6.     if (idx < TVR_SIZE) {
  7.         int i = expires & TVR_MASK;
  8.         vec = base->tv1.vec + i;
  9.     } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  10.         int i = (expires >> TVR_BITS) & TVN_MASK;
  11.         vec = base->tv2.vec + i;
  12.     } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  13.         int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  14.         vec = base->tv3.vec + i;
  15.     } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  16.         int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  17.         vec = base->tv4.vec + i;
  18.     } else if ((signed long) idx < 0) {
  19.         /*
  20.          * Can happen if you add a timer with expires == jiffies,
  21.          * or you set a timer to go off in the past
  22.          */
  23.         vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  24.     } else {
  25.         int i;
  26.         /* If the timeout is larger than 0xffffffff on 64-bit
  27.          * architectures then we use the maximum timeout:
  28.          */
  29.         if (idx > 0xffffffffUL) {
  30.             idx = 0xffffffffUL;
  31.             expires = idx + base->timer_jiffies;
  32.         }
  33.         i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  34.         vec = base->tv5.vec + i;
  35.     }
  36.     /*
  37.      * Timers are FIFO:
  38.      */
  39.     list_add_tail(&timer->entry, vec);
  40. }
  41. internal_add_timer计算expires和当前正在处理的定时器的到期时间的差值,把定时器插入到不同的链表中。
  42. 实例1:
  43. 假设timer->expires=12,假设此时的jiffies和timer_jiffies都是9
  44. 那么此timer将会放入base->tv1.vec+2的list后面

  45. 但是,针对timer_jiffies在__run_timers()中的使用,我还是有些疑问的:
  46. static inline void __run_timers(struct tvec_base *base)
    {
        struct timer_list *timer;

        spin_lock_irq(&base->lock);
        while (time_after_eq(jiffies, base->timer_jiffies)) {
            struct list_head work_list;
            struct list_head *head = &work_list;
            int index = base->timer_jiffies & TVR_MASK;(TVR_MASK=255)
  47.         //对这个语句还是有些疑问的,这样就可以得到index么?要知道base->timer_jiffies的起始值可是比较随机的   
  48.         //jiffies阿
  49.         //若按照实例1的假设,index为9,直接大于2了
  50. 权且记下,等待下回分解



上一篇:linux内核抢占
下一篇:linux墙上时间xtime与高精度时钟gettimeofday