linux顶半部和底半部中断机制

来源:互联网 发布:淘宝省钱app 编辑:程序博客网 时间:2024/06/14 22:01
[cpp] view plaincopy
  1. //tasklet使用模板  
  2. /*定义tasklet和底半部函数相关联*/  
  3. void xxx_do_tasklet(unsigned long);  
  4. DECLARE_TASKLET(xxx_tasklet,xxx_do_tasklet,0);  
  5. /*中断处理底半部*/  
  6. void xxx_do_tasklet(unsigned long)  
  7. {  
  8.     ............  
  9. }  
  10. /*中断处理顶半部*/  
  11. irqreturn_t xxx_interrupt(int irq,void *dev_id,struct pt_regs *regs)  
  12. {  
  13.     .....................  
  14.     /*调度xxx_do_tasklet函数在适当的时候执行。*/  
  15.     tasklet_schedule(&xxx_tasklet);  
  16.     ......................  
  17. }  
  18. /*设备驱动模块加载函数*/  
  19. int __init xxx_init(void)  
  20. {  
  21.     .................  
  22.     /*申请中断*/  
  23.     result=request_irq(xxx_irq,xxx_interrupt,SA_INTERRUPT,"XX",NULL);  
  24.     ..................  
  25. }  
  26. /*设备驱动模块卸载函数*/  
  27. void __exit xxx_exit(void)  
  28. {  
  29.     .....................  
  30.     free_irq(xxx_irq,xxx_interrupt);  
  31.     .....................  
  32. }  
  33.   
  34. //工作队列使用模板  
  35. /*定义工作队列和关联函数*/  
  36. struct work_struct xxx_wq;  
  37. void xxx_do_work(unsigned long);  
  38. /*中断处理底半部*/  
  39. void xxx_do_work(unsigned long)  
  40. {  
  41.     ....................  
  42. }  
  43. /*中断处理顶半部*/  
  44. irqreturn_t xxx_interrupt(int irq,void *dev_id,struct pt_regs *regs)  
  45. {  
  46.     ................  
  47.     schedule_work(&xxx_wq);  
  48.     ...................  
  49. }  
  50. /*设备驱动模块加载函数*/  
  51. int xxx_init(void)  
  52. {  
  53.     ................  
  54.     /*申请中断*/  
  55.     result=request_irq(xxx_irq,xxx_interrupt,SA_INTERRUPT,"xxx",NULL);  
  56.     .................  
  57.     /*初始化队列*/  
  58.     INIT_WORK(&xxx_wq,(void (*)(void *)) xxx_do_work,NULL);  
  59.     .................  
  60. }  
  61. /*设备驱动模块卸载函数*/  
  62. void xxx_exit(void)  
  63. {  
  64.     ...............  
  65.     /*释放中断*/  
  66.     free_irq(xxx_irq,xxx_interrupt);  
  67.     ................  
  68. }  
  69.   
  70. /*软中断和tasklet仍然运行于中断上下文,而工作队列则运行于进程上下文。因此,软中断和tasklet处理函数中不能睡眠,而工作队列处理函数中允许睡眠。 
  71. local_bh_disable()和local_bh_enbale()是内核中用于禁止和使能 中断和tasklet底半部机制的函数*/