免费注册 查看新帖 |

Chinaunix

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

中断的下半部分之三work_queue [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:44 |只看该作者 |倒序浏览
在linux中使用工作队列work_queue
软硬件环境:linux-2.6.36/s3c2440

工作队列在进程上下文中运行,允许重新调度甚至睡眠
test.c

#include <linux/module.h>
#include <linux/init.h>

#include <linux/mm.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#include <asm/current.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhanglong");

struct work_struct my_work;

void work_handle(struct work_struct* arg)
{
    long data = atomic_long_read(&arg->data);

    printk("current: %s, pid: %d\n", current->comm, current->pid);
    ssleep(3);
    printk("after sleep, data = %ld\n", data);    
    //printk("&my_work = 0x%x\n", (unsigned int)&my_work);    

}

irqreturn_t irq_handle(int irq, void *dev_id)
{
    schedule_work(&my_work);
    printk("----irq handle. dev_id = %d\n", (int)dev_id);
    return 0;
}

int test_init(void)
{
    int ret = 0;

    INIT_WORK(&my_work, work_handle);

    set_irq_type(IRQ_EINT0, IRQ_TYPE_EDGE_RISING);
    ret = request_irq(IRQ_EINT0, irq_handle, 0, "my irq", (void *)123);
    if (ret) {    
        printk("request irq failed.\n");
        return -EBUSY;
    }

    return 0;
}

void test_exit(void)
{
    free_irq(IRQ_EINT0, (void *)123);
}

module_init(test_init);
module_exit(test_exit);


Makefile

KERNEL    = /media/STUDY/linux/kernel/my2440-2.6.36
#KERNEL    = /lib/modules/$(shell uname -r)/build

default:
    make -C $(KERNEL) M=$(shell pwd) modules

clean:
    make -C $(KERNEL) M=$(shell pwd) modules clean

modules_install:
    make -C $(KERNEL) M=$(shell pwd) modules_install INSTALL_MOD_PATH=/home/zl/s3c2440_nfs
    depmod -a -b /home/zl/s3c2440_nfs 2.6.13-my2440

obj-m    += test.o


make后插入生成的test.ko模块。按下GPF0所在的按键,终端会有输出:
[root@zhanglong mywork]# ----irq handle. dev_id = 123
current: kworker/0:1, pid: 267
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
----irq handle. dev_id = 123
after sleep, data = 0
current: kworker/0:1, pid: 267
after sleep, data = 0

[root@zhanglong mywork]#

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP