免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1145 | 回复: 0
打印 上一主题 下一主题

内核多线程编程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-07 13:53 |只看该作者 |倒序浏览
内核多线程编程 收藏
最近项目中为了解决通信设备高速传输丢数问题,使用了工作队列的方式开多线程,然后异步读取一个缓冲buffer的方法。现在内核中多线程编程的方法基本在实际项目中都使用过来,小总结一下。

/**//*
* @Brief: 内核下软中断,定时器,推后执行,工作队列(多线程)的实现模板
* @Author:liyangth@gmail.com
* @ChangeLog:
*    2008-04-27    liy    定时器,和工作队列实现
*
*/
#include
#include
#include
#include
#include     //内核定时器timer;
#include     //tasklet
#include     //工作队列
struct my_s...{
    struct timer_list my_timer;
    struct tasklet_struct my_tlet;
    struct workqueue_struct my_workqueue;
    struct work_struct my_work;
};
struct my_s *gp_my_module;
/**//* call back function */
/**//* 内核定时器 */
void my_timer_callback_fn(unsigned long arg)
...{
    struct other_struct *data = (struct other_struct *)arg;
    mod_timer(&gp_my_module->my_timer, jiffies + 10);    //10ms
   
    /**//* just do it */
}


/**//* 软中断tasklet */
void my_tasklet_callback_fn(unsigned long arg)
...{
    struct other_struct *data = (struct other_struct *)arg;
   
    /**//* just do it */
    if(1)...{    //高优先级调度
        tasklet_hi_schedule(&gp_my_module->my_tlet);   
    }else...{   
        tasklet_schedule(&gp_my_module->my_tlet);   
    }
}


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


_init()
...{
    struct other_struct *data;
    data = kmalloc(sizeof(struct other_struct), GFP_KERNEL);
    gp_my_module = kmalloc(sizeof(struct my_s), GFP_KERNEL);
    /**//* 内核定时器 */
    init_timer(&gp_my_module->my_timer);
    gp_my_module->my_timer.data = (unsigned long)data;
    gp_my_module->my_timer.function = my_timer_callback_fn;
    //gp_my_module->my_timer.expires = j + tdelay; /* parameter */
   
    /**//* 软中断tasklet */
    tasklet_init(&gp_my_module->my_tlet, my_tasklet_callback_fn, (unsigned long)data);
    /**//* 工作队列 */
    &gp_my_module->my_workqueue = create_workqueue("mymoduleworkqueue");
    INIT_WORK(&gp_my_module->my_work,my_work_callbakc_fn, data);
   
}



/**//* 卸载 */
_exit()
...{
    /**//* 停止内核定时器 */
    del_timer(&gp_my_module->my_timer);
    /**//* 软中断tasklet */
    tasklet_kill(&gp_my_module->my_tlet);
    /**//* 工作队列 */
    destroy_workqueue(&gp_my_module->my_workqueue);
   
}

_开始,启动多线程,callback函数被执行,跑起来
...{
    /**//* 内核定时器 */
    mod_timer(&gp_my_module->my_timer, jiffies + 10);    //10ms
    /**//* 软中断tasklet */
    if(1)...{    //高优先级调度
        tasklet_hi_schedule(&gp_my_module->my_tlet);   
    }else...{   
        tasklet_schedule(&gp_my_module->my_tlet);   
    }
    /**//* 工作队列 */
    queue_work(&gp_my_module->my_workqueue, &gp_my_module->my_work);
}
[/url]


本文来自ChinaUnix博客,如果查看原文请点:[url]http://blog.chinaunix.net/u2/75633/showart_2020641.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP