Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657063
  • 博文数量: 185
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2107
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(185)

文章存档

2024年(1)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: 嵌入式

2011-04-29 00:02:21

在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]#

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