延迟执行

 

   内核代码(尤其是驱动程序)除了使用定时器或下半部机制以外还需要其他方法来推迟执行任务。这种推迟通常发生在等待硬件完成某些工作时,而且等待时间非常短。

  内核提供了许多延迟方法处理各种延迟要求。不同的方法有不同的处理特点,有些是在延迟任务时挂起处理器,防止处理器执行任何实际工作;另一些不会挂起处理器,所以也不能确保被延迟的代码能够在指定的延迟时间运行。

  • 忙等待

  最简单的延迟方法(虽然也是最不理想的方法)是忙等带。该方法仅仅在想要延迟的时间是节拍的整数倍,或者精确率要求不太高时才可以使用。

  忙循环实现起来很简单,在循环中不断旋转直到希望的时钟节拍数耗尽,比如:

 

  1. unsigned long delay = jiffies + 10;
  2. while(time_before(jiffies,delaly))
  3.   ;

更好的方法应该是在代码等待时,运行内核重新调度执行其他任务:

  1. unsigned long delay = jiffies + 2 * HZ;
  2. while(time_before(jiffies,delay))
  3.   cond_resched();

 

  1. 在<Sched.c(kernel)>中
  2. int __sched cond_resched(void)
  3. {
  4.     if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &
  5.                     system_state == SYSTEM_RUNNING) {
  6.         __cond_resched();
  7.         return 1;
  8.     }
  9.     return 0;
  10. }
  11. static void __cond_resched(void)
  12. {
  13. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  14.     __might_sleep(__FILE__, __LINE__);
  15. #endif
  16.     /*
  17.      * The BKS might be reacquired before we have dropped
  18.      * PREEMPT_ACTIVE, which could trigger a second
  19.      * cond_resched() call.
  20.      */
  21.     do {
  22.         add_preempt_count(PREEMPT_ACTIVE);
  23.         schedule();
  24.         sub_preempt_count(PREEMPT_ACTIVE);
  25.     } while (need_resched());
  26. }

  cond_resched()函数将调度一个新程序投入运行,但是它只有在设置完need_resched标志后,才能生效。也就是说,该方法有效的条件是系统中存在更重要的任务需要执行。注意,因为该方法需要调用调度程序,所以它不能在中断上下文中使用,只能在进程上下文中使用。实际上,所有延迟方法在进程上下文中使用,因为中断处理程序都应该尽可能快地执行。另外,延迟执行不管在哪种情况下都不应该在持有锁时或禁止中断时发生。

  C编译器通常只将变量装载一次,一般情况下不能保证循环中的jiffies变量在每次循环中被读取时都重新被载入。但是要求jiffies在每次循环时都必须重新装载,因为在后台jiffies值会随时钟中断的发生而不断增加。为了解决这个问题,jiffies变量被标记为volatile。

  关键字volatile指示编译器在每次访问变量时都重新从主内存中获得,而不是通过寄存器中变量的别名来访问,从而确保前面的循环能够按预期的方式执行。

  • 短延迟

  有时候,内核代码(通常也是驱动程序)不但需要很短暂的延迟(比时钟节拍还短)而且还要求延迟的时间很精确。这种情况多发生在和硬件同步时,也就是说需要短暂等待某个动作的完成,等待时间往往小于1毫秒,所以不能使用像前面例子中那种基于jiffies的延迟方法。

  内核提供了两个可以处理为妙和毫秒级别的延迟的函数udelay()和mdelay()。前一个函数利用忙循环来将任务延迟到指定的微妙数后运行,后者延迟指定的毫秒数:

  1. 在<Delay.h(include/i386)>中
  2. /* 0x10c7 is 2**32 / 1000000 (rounded up) */
  3. #define udelay(n) (__builtin_constant_p(n) ? / 
  4.     ((n) > 20000 ? __bad_udelay() : __const_udelay((n) * 0x10c7ul)) : /
  5.     __udelay(n))
  6. /* 0x5 is 2**32 / 1000000000 (rounded up) */
  7. #define ndelay(n) (__builtin_constant_p(n) ? / 
  8.     ((n) > 20000 ? __bad_ndelay() : __const_udelay((n) * 5ul)) : /
  9.     __ndelay(n))
  1. 在<Delay.h(include/linux)>中
  2. /*
  3.  * Copyright (C) 1993 Linus Torvalds
  4.  *
  5.  * Delay routines, using a pre-computed "loops_per_jiffy" value.
  6.  */
  7. extern unsigned long loops_per_jiffy;
  8. #include <asm/delay.h>
  9. /*
  10.  * Using udelay() for intervals greater than a few milliseconds can
  11.  * risk overflow for high loops_per_jiffy (high bogomips) machines. The
  12.  * mdelay() provides a wrapper to prevent this.  For delays greater
  13.  * than MAX_UDELAY_MS milliseconds, the wrapper is used.  Architecture
  14.  * specific values can be defined in asm-???/delay.h as an override.
  15.  * The 2nd mdelay() definition ensures GCC will optimize away the 
  16.  * while loop for the common cases where n <= MAX_UDELAY_MS  --  Paul G.
  17.  */
  18. #ifndef MAX_UDELAY_MS
  19. #define MAX_UDELAY_MS   5
  20. #endif
  21. #ifndef mdelay
  22. #define mdelay(n) (/
  23.     (__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : /
  24.     ({unsigned long __ms=(n); while (__ms--) udelay(1000);}))
  25. #endif
  26. #ifndef ndelay
  27. #define ndelay(x)   udelay(((x)+999)/1000)
  28. #endif

 

    udelay()函数依靠执行数次达到延迟效果,而mdelay()函数通过udelay()函数实现。因为内核知道处理器在一秒内能执行多少次循环,所以udelay()函数紧急需要根据指定的延迟时间在1秒中占的比例就能决定需要进行多少次循环就能达到要求的推迟时间。

不要使用udelay()函数处理超过1毫秒的延迟,延迟超过1毫秒使用mdelay()更安全。

  千万要注意不要在持有锁时或禁止中断时使用忙等待,因为这时忙等待会时系统响应速度和性能大打折扣。

   内核如何知道处理器在一秒内能执行多少循环呢?

  BogoMIPS值记录的是处理器在给定时间内忙循环执行的次数。其实就是记录处理器在空闲时速度有多快。它主要被udelay()和mdelay()函数使用。该值存放在变量loops_per_jiffies中,可以从文件/proc/cpuinfo中读到它。延迟循环函数使用loops_per_jiffies值来计算为提供精确延迟而需要进行多少次循环。内核在启动时利用函数calibrate_delay()函数计算loops_per_jiffies值,该函数在文件init/main.c中使用到。

 

  1. 在<init/main.c>中
  2. /*
  3.  * This should be approx 2 Bo*oMips to start (note initial shift), and will
  4.  * still work even if initially too large, it will just take slightly longer
  5.  */
  6. unsigned long loops_per_jiffy = (1<<12);
  7. 在<init/Calibrate.h>中
  8. void __devinit calibrate_delay(void)
  9. {
  10.     unsigned long ticks, loopbit;
  11.     int lps_precision = LPS_PREC;
  12.     if (preset_lpj) {
  13.         loops_per_jiffy = preset_lpj;
  14.         printk("Calibrating delay loop (skipped)... "
  15.             "%lu.%02lu BogoMIPS preset/n",
  16.             loops_per_jiffy/(500000/HZ),
  17.             (loops_per_jiffy/(5000/HZ)) % 100);
  18.     } else if ((loops_per_jiffy = calibrate_delay_direct()) != 0) {
  19.         printk("Calibrating delay using timer specific routine.. ");
  20.         printk("%lu.%02lu BogoMIPS (lpj=%lu)/n",
  21.             loops_per_jiffy/(500000/HZ),
  22.             (loops_per_jiffy/(5000/HZ)) % 100,
  23.             loops_per_jiffy);
  24.     } else {
  25.         loops_per_jiffy = (1<<12);
  26.         printk(KERN_DEBUG "Calibrating delay loop... ");
  27.         while ((loops_per_jiffy <<= 1) != 0) {
  28.             /* wait for "start of" clock tick */
  29.             ticks = jiffies;
  30.             while (ticks == jiffies)
  31.                 /* nothing */;
  32.             /* Go .. */
  33.             ticks = jiffies;
  34.             __delay(loops_per_jiffy);
  35.             ticks = jiffies - ticks;
  36.             if (ticks)
  37.                 break;
  38.         }
  39.         /*
  40.          * Do a binary approximation to get loops_per_jiffy set to
  41.          * equal one clock (up to lps_precision bits)
  42.          */
  43.         loops_per_jiffy >>= 1;
  44.         loopbit = loops_per_jiffy;
  45.         while (lps_precision-- && (loopbit >>= 1)) {
  46.             loops_per_jiffy |= loopbit;
  47.             ticks = jiffies;
  48.             while (ticks == jiffies)
  49.                 /* nothing */;
  50.             ticks = jiffies;
  51.             __delay(loops_per_jiffy);
  52.             if (jiffies != ticks)   /* longer than 1 tick */
  53.                 loops_per_jiffy &= ~loopbit;
  54.         }
  55.         /* Round the value and print it */
  56.         printk("%lu.%02lu BogoMIPS (lpj=%lu)/n",
  57.             loops_per_jiffy/(500000/HZ),
  58.             (loops_per_jiffy/(5000/HZ)) % 100,
  59.             loops_per_jiffy);
  60.     }
  61. }
  • schedule_timeout()

更理想的延迟方法时使用schedule_timeout()函数。该方法会让需要延迟执行的任务睡眠到指定的延迟时间耗尽后再重新运行。但该方法也不能保证睡眠时间正好等于指定的延迟时间,只能尽量使睡眠时间接近指定的延迟时间。当指定时间到期后,内核唤醒被延迟的任务并将其重新放回运行队列:

 

  1. set_current_state(TASK_INTERRUPTABLE);
  2. schedule_timeout(s*HZ);//参数是延迟的相对时间,单位为jiffies

注意,在调用schedule_timeout()函数前,必须将任务状态设置成TASK_INTERRUPTABLE和TASK_UNINTERRUPTABLE状态之一,否则任务不会睡眠。

  注意,由于schedule_timeout()函数需要调用调度程序,所以调用它的代码必须保证能够睡眠。调用代码必须处于进程上下文中,并且不能持有锁。

  1. 在(Timer.c(kernel))中
  2. /**
  3.  * schedule_timeout - sleep until timeout
  4.  * @timeout: timeout value in jiffies
  5.  *
  6.  * Make the current task sleep until @timeout jiffies have
  7.  * elapsed. The routine will return immediately unless
  8.  * the current task state has been set (see set_current_state()).
  9.  *
  10.  * You can set the task state as follows -
  11.  *
  12.  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  13.  * pass before the routine returns. The routine will return 0
  14.  *
  15.  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  16.  * delivered to the current task. In this case the remaining time
  17.  * in jiffies will be returned, or 0 if the timer expired in time
  18.  *
  19.  * The current task state is guaranteed to be TASK_RUNNING when this
  20.  * routine returns.
  21.  *
  22.  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  23.  * the CPU away without a bound on the timeout. In this case the return
  24.  * value will be %MAX_SCHEDULE_TIMEOUT.
  25.  *
  26.  * In all cases the return value is guaranteed to be non-negative.
  27.  */
  28. fastcall signed long __sched schedule_timeout(signed long timeout)
  29. {
  30.     struct timer_list timer;
  31.     unsigned long expire;
  32.     switch (timeout)
  33.     {
  34.     case MAX_SCHEDULE_TIMEOUT: //用于检查任务是否无限期的睡眠,如果这样的话,函数不会为它设置定时器
  35.         /*
  36.          * These two special cases are useful to be comfortable
  37.          * in the caller. Nothing more. We could take
  38.          * MAX_SCHEDULE_TIMEOUT from one of the negative value
  39.          * but I' d like to return a valid offset (>=0) to allow
  40.          * the caller to do everything it want with the retval.
  41.          */
  42.         schedule();
  43.         goto out;
  44.     default:
  45.         /*
  46.          * Another bit of PARANOID. Note that the retval will be
  47.          * 0 since no piece of kernel is supposed to do a check
  48.          * for a negative retval of schedule_timeout() (since it
  49.          * should never happens anyway). You just have the printk()
  50.          * that will tell you if something is gone wrong and where.
  51.          */
  52.         if (timeout < 0) {
  53.             printk(KERN_ERR "schedule_timeout: wrong timeout "
  54.                 "value %lx/n", timeout);
  55.             dump_stack();
  56.             current->state = TASK_RUNNING;
  57.             goto out;
  58.         }
  59.     }
  60.     expire = timeout + jiffies;
  61.     setup_timer(&timer, process_timeout, (unsigned long)current); /*创建定时器,设置超时时间,设置超时函数 */
  62.     __mod_timer(&timer, expire); /*激活定时器 */
  63.     schedule(); /*选择其他任务运行 */
  64.     del_singleshot_timer_sync(&timer); /*销毁定时器*/
  65.     timeout = expire - jiffies;
  66.  out:
  67.     return timeout < 0 ? 0 : timeout;
  68. }

 

 

  1. 在<Time.h(include/linux)>中
  2. static inline void setup_timer(struct timer_list * timer,
  3.                 void (*function)(unsigned long),
  4.                 unsigned long data)
  5. {
  6.     timer->function = function;
  7.     timer->data = data;
  8.     init_timer(timer);
  9. }
  10. 在<Time.c(kernel)>中
  11. //当定时器超时,该函数被调用
  12. static void process_timeout(unsigned long __data)
  13. {
  14.     wake_up_process((struct task_struct *)__data);
  15. }

schedule_timeout()函数是内核定时器的一个简单应用。

  • 设置超时时间,在等待队列上睡眠

  进程上下文中的代码为了等待特定事件发生,可以将自己放入等待队列,然后调用调度程序去执行新任务。一旦事件发生后,内核调用wake_up()函数唤醒在睡眠队列上的任务使其重新投入运行。

   有时,等待队列上的某个任务可能既在等待一个特定的事件到来,又在等待一个特定的时间到期,在这种情况下,代码可以简单地使用schedule_timeout()函数代替schedule()函数,当希望指定时间到期,任务就会被唤醒。代码需要检查被唤醒的原因,可能是被事件唤醒,可能是因为延迟时间到期,也可能是因为接收到了信号;然后执行相应的操作。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值