Linux中断分层--工作队列

1. 工作队列是一种将任务推后执行的方式,它把推后的任务交由一个内核线程去执行。这样中断的下半部会在进程上下文执行,他允许重新调度甚至睡眠。每个被推后的任务叫做“工作”,由这些工作组成的队列称为工作队列。

2. Linux内核使用struct workqueue_struct来描述一个工作队列

struct workqueue_struct {
    struct cpu_workqueue_struct *cpu_wq;
    struct list_head list;
    const char *name;
    int singlethread;
    int freezeable;        /* Freeze threads during suspend */
    int rt;
};


3. Linux内核使用struct work_struct来描述一个工作项

struct work_struct {
    atomic_long_t data;
    struct list_head entry;
    work_func_t func;
};

  其中,work_func_t原型为

typedef void (*work_func_t)(struct work_struct *work);


4. 在驱动程序中使用工作队列有两种方式

(1)共享工作队列:

  在Linux系统中,内核为了方便用户编程,已经默认实现了一个所有进程都可以使用的工作队列(其对应的内核线程是kevent线程,其在Linux启动时创建,该线程被创建之后就处于sleep状态,当我们使用schedule_work函数时,才会唤醒该线程。当工作队列上的所有节点被执行完毕,该线程又会处于休眠状态,直到schedule_work再次被调用)。

① 编写自己的work_struct工作函数

② 定义自己的work_struct结构体

③ 初始化work_struct结构体,把工作函数地址赋值给work_struct->func

③在适当位置使用schedule_work函数完成向系统工作队列添加自己的work_struct

(2)自定义工作队列

①创建工作队列:create_workqueue

#define create_workqueue(name) __create_workqueue((name), 0, 0, 0)

②创建工作:INIT_WORK

#define INIT_WORK(_work, _func)                        
    do {                                
        static struct lock_class_key __key;            
                                    
        (_work)->data = (atomic_long_t) WORK_DATA_INIT();    
        lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);
        INIT_LIST_HEAD(&(_work)->entry);            
        PREPARE_WORK((_work), (_func));                
    } while (0)

③提交工作:queue_work

int queue_work(struct workqueue_struct *wq, struct work_struct *work)
{
    int ret;

    ret = queue_work_on(get_cpu(), wq, work);
    put_cpu();

    return ret;
}

5. 简单示例:

① 共享工作队列

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

struct workqueue_struct *my_wq;
struct work_struct *work1;
struct work_struct *work2;

MODULE_LICENSE("GPL");

void work1_func(struct work_struct *work)
{
    printk("this is work1->
");    
}

void work2_func(struct work_struct *work)
{
    printk("this is work2->
");    
}

int init_que(void)
{    
    //1. 创建工作1
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);
    
    //2. 挂载(提交)工作1
    schedule_work(work1);
   
    //3. 创建工作2
    work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work2, work2_func);
    
    //4. 挂载(提交)工作2
    schedule_work(work2);
        
    return 0;
}

void clean_que()
{
    
}

module_init(init_que);
module_exit(clean_que);

②自定义工作队列

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

struct workqueue_struct *my_wq;
struct work_struct *work1;
struct work_struct *work2;

MODULE_LICENSE("GPL");

void work1_func(struct work_struct *work)
{
    printk("This is work1->
");    
}

void work2_func(struct work_struct *work)
{
    printk("This is work2->
");    
}

int init_que(void)
{    
    //1. 创建工作队列
    my_wq = create_workqueue("my_que");
        
    //2. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);
    
    //3. 挂载(提交)工作
    queue_work(my_wq,work1);
     
    //2. 创建工作
    work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work2, work2_func);
    
    //3. 挂载(提交)工作
    queue_work(my_wq,work2);
        
    return 0;
}

void clean_que()
{
    
}

module_init(init_que);
module_exit(clean_que);

 注:事实上,printk函数是不可重入函数,需要加锁机制来保护。

原文地址:https://www.cnblogs.com/wulei0630/p/9508643.html