Chinaunix首页 | 论坛 | 博客
  • 博客访问: 136154
  • 博文数量: 79
  • 博客积分: 11
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-19 22:16
文章分类
文章存档

2015年(1)

2014年(9)

2013年(43)

2012年(26)

我的朋友

分类: LINUX

2014-08-27 00:08:54

原文地址:Linux内核多线程编程 作者:chenshko

最近项目中为了解决通信设备高速传输丢数问题,使用了工作队列的方式开多线程,然后异步读取一个缓冲buffer的方法。现在内核中多线程编程的方法基本在实际项目中都使用过来,小总结一下。

  1. /*
  2.  * @Brief: 内核下软中断,定时器,推后执行,工作队列(多线程)的实现模板
  3.  * @Author:liyangth@gmail.com
  4.  * @ChangeLog:
  5.  * 2008-04-27 liy 定时器,和工作队列实现
  6.  *
  7.  */

  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>


  12. #include <linux/timer.h> //内核定时器timer;

  13. #include <linux/interrupt.h> //tasklet

  14. #include <linux/workqueue.h> //工作队列



  15. struct my_s...{
  16.     struct timer_list my_timer;
  17.     struct tasklet_struct my_tlet;
  18.     struct workqueue_struct my_workqueue;
  19.     struct work_struct my_work;
  20. };


  21. struct my_s *gp_my_module;

  22. /**//* call back function */
  23. /**//* 内核定时器 */
  24. void my_timer_callback_fn(unsigned long arg)
  25. {
  26.     struct other_struct *data = (struct other_struct *)arg;

  27.     mod_timer(&gp_my_module->my_timer, jiffies + 10); //10ms

  28.     
  29.     /**//* just do it */

  30. }

  31. /**//* 软中断tasklet */
  32. void my_tasklet_callback_fn(unsigned long arg)
  33. {
  34.     struct other_struct *data = (struct other_struct *)arg;
  35.     
  36.     /**//* just do it */

  37.     if(1)...{ //高优先级调度

  38.         tasklet_hi_schedule(&gp_my_module->my_tlet);
  39.     }else...{
  40.         tasklet_schedule(&gp_my_module->my_tlet);
  41.     }

  42. }

  43. /**//* 工作队列 */
  44. /**//* work的函数原型是 void (*fn)(void *)
  45.  * 所以工作队列的callback函数的定义更加灵活 */
  46. void my_work_callbakc_fn(struct other_struct *arg)
  47. {
  48.     /* just do it */
  49. }


  50. _init()
  51. {
  52.     struct other_struct *data;

  53.     data = kmalloc(sizeof(struct other_struct), GFP_KERNEL);
  54.     gp_my_module = kmalloc(sizeof(struct my_s), GFP_KERNEL);

  55.     /**//* 内核定时器 */
  56.     init_timer(&gp_my_module->my_timer);
  57.     gp_my_module->my_timer.data = (unsigned long)data;
  58.     gp_my_module->my_timer.function = my_timer_callback_fn;
  59.     //gp_my_module->my_timer.expires = j + tdelay; /* parameter */

  60.     
  61.     /**//* 软中断tasklet */
  62.     tasklet_init(&gp_my_module->my_tlet, my_tasklet_callback_fn, (unsigned long)data);

  63.     /**//* 工作队列 */
  64.     &gp_my_module->my_workqueue = create_workqueue("mymoduleworkqueue");
  65.     INIT_WORK(&gp_my_module->my_work,my_work_callbakc_fn, data);

  66.     
  67. }

  68. /**//* 卸载 */
  69. _exit()
  70. {
  71.     /**//* 停止内核定时器 */
  72.     del_timer(&gp_my_module->my_timer);

  73.     /**//* 软中断tasklet */
  74.     tasklet_kill(&gp_my_module->my_tlet);

  75.     /**//* 工作队列 */
  76.     destroy_workqueue(&gp_my_module->my_workqueue);
  77.     
  78. }
  79. _开始,启动多线程,callback函数被执行,跑起来
  80. {
  81.     /**//* 内核定时器 */
  82.     mod_timer(&gp_my_module->my_timer, jiffies + 10); //10ms


  83.     /**//* 软中断tasklet */
  84.     if(1)...{ //高优先级调度

  85.         tasklet_hi_schedule(&gp_my_module->my_tlet);
  86.     }else...{
  87.         tasklet_schedule(&gp_my_module->my_tlet);
  88.     }

  89.     /**//* 工作队列 */
  90.     queue_work(&gp_my_module->my_workqueue, &gp_my_module->my_work);

  91. }

 

阅读(749) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~