中断下半部

转自:http://blog.csdn.net/sudolee/article/details/6885634

1>work_queue:<linux/workqueue.h> __3.0.4
2>description:
中断下半部,在内核线程的进程上下文中执行推后的工作.
它是唯一能在进程上下文运行的中断下半部实现机制,也只有它才可以睡眠.
3>创建推后的工作:

  1. DECLARE_WORK(const char *name, void (*func)(struct work_struct *work));  
  2. INIT_WORK(struct work_struct *work, void (*func)(struct work_struct *work));  
  3.   
  4. struct work_struct {  
  5.     atomic_long_t data;  
  6.     struct list_head entry;  
  7.     work_func_t func;                                                                                                                       
  8. #ifdef CONFIG_LOCKDEP  
  9.     struct lockdep_map lockdep_map;  
  10. #endif  
  11. };  
  12. typedef void (*work_func_t)(struct work_struct *work);  

4>工作队列处理函数:

  1. /* 除了不能访问用户空间,它的确比软中断和tasklet方便不少.  
  2.  * 只有系统调用陷入内核,用户空间的内存才会被映射.  
  3.  */  
  4. void *func(struct work_struct *work);  

5>调度(提交)工作:

  1. /* 把创建好的工作提交到缺省的worker线程(每个cpu一个,event/cpuno.).  
  2.  * 有时把这种工作队列也叫共享队列,因为很多驱动程序都将推后执行的工作提交到默认worker线程.  
  3.  */  
  4. int schedule_work(struct work_struct *work);  
  5. int schedule_delayed_work(struct delayed_work *work, unsigned long delay);  
  6. int schedule_on_each_cpu(work_func_t func);  

6>刷新工作队列:

  1. /* 实际上,它只是等待(睡眠),直到缺省工作队列上的工作被执行.  
  2.  * complete()实现.  
  3.  */  
  4. void flush_scheduled_work(void);  
  5. /*等待指定的工作被执行*/  
  6. bool flush_work(struct work_struct *work);  
  7. bool flush_delayed_work(struct delayed_work *dwork);  

7>取消工作:

  1. bool cancel_delayed_work(struct delayed_work *work);  
  2. bool cancel_work_sync(struct work_struct *work);  
  3. bool cancel_delayed_work_sync(struct delayed_work *dwork);  

8>创建一个新的workqueue和对应的worker线程(每个cpu一个).

  1. /* 如果一个任务是处理器密集型和性能要求严格的 
  2.  * 那么可以选择创建自己的worker(工作者)线程 
  3.  * 当然,每个处理器一个. 
  4.  */  
  5. struct workqueue_struct *create_workqueue(const char *name);  
  6.   
  7. /* struct workqueue_struct *alloc_workqueue(const char *name, unsigned int flags, int max_active); 
  8.  * the maximum number of execution contexts per cpu, up to 512. 
  9.  */  

9>调度(提交)队列

  1. int queue_work(struct workqueue_struct *wq, struct work_struct *work);  
  2. int queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay);  

10>刷新一个工作队列:

  1. /* 等待(睡眠),直到指定队列上的工作被执行. 
  2.  * 事实上除了指定工作队列,它和flush_scheduled_work()没有区别. 
  3.  */  
  4. void flush_workqueue(struct workqueue_struct *wq);  

11>释放一个工作队列:

    1. void destroy_workqueue(struct workqueue_struct *wq);  
原文地址:https://www.cnblogs.com/rzq232/p/2837067.html