时间管理 2> 长延迟

3880阅读 0评论2015-06-16 lsxuan驱动
分类:嵌入式

 4种方法:
1> 忙等待:

点击(此处)折叠或打开

  1. void 1_delay(u32 sec)
  2. {
  3.     unsigned long end_point = jiffies + sec*HZ;

  4.     while(!time_before_eq(end_point, jiffies)){
  5.         /*nothing*/;
  6.     }
  7. }

点击(此处)折叠或打开

  1. #define time_after(a,b) \
  2.           (typecheck(unsigned long, a) && \
  3.           typecheck(unsigned long, b) && \
  4.           ((long)(b) - (long)(a) < 0))        //学会这招!!,复习有符号到无符号的转换
  5.  #define time_before(a,b) time_after(b,a)      //如此巧妙
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

2> 让出CPU:

点击(此处)折叠或打开

  1. void 2_delay(u32 sec)
  2.   {
  3.           unsigned long end_point = jiffies + sec*HZ;
  4.  
  5.           while(!time_before_eq(end_point, jiffies)){
  6.                   schedule();
  7.          }
  8.  }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


3> 等待队列实现:

点击(此处)折叠或打开

  1. void 3_delay(u32 s)
  2.  {
  3.           wait_queue_head_t head;
  4.           init_waitqueue_head(&head);
  5.  
  6.           wait_event_interruptible_timeout(head, 0, s*HZ);
  7.   }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

4> 最理想的实现:

点击(此处)折叠或打开

  1. void 4_delay(u32 s)
  2. {
  3.         set_current_state(TASK_INTERRUPTIBLE);
  4.         schedule_timeout(s*HZ);
  5. }


上一篇:时间管理 1> 内核定时器
下一篇:时间管理 3> 短延迟