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

[cpp]  view plain copy
  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底半部机制的函数*/  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值