workqueue机制

workqueue机制是Linux内核中用于异步处理任务的一种方法,它允许在进程上下文中执行,能睡眠且便于驱动开发者编写中断处理代码。与传统bottom half不同,workqueue通过集中管理的工作线程池减少资源浪费并保持并发处理的灵活性。系统启动时初始化默认工作队列,并通过alloc_workqueue()创建。工作队列、工作、工作线程池和worker之间有明确的结构和交互,如CMWQ(concurrent-manage workqueue),在处理工作时能根据需要动态创建和销毁worker线程,解决并发问题和资源管理。
摘要由CSDN通过智能技术生成

workqueue和其他的bottom half最大的不同是它是运行在进程上下文中的,它可以睡眠,这和其他bottom half机制有本质的不同,大大方便了驱动工程师撰写中断处理代码。当然,驱动模块也可以自己创建一个kernel thread来解决defering work,但是,如果每个driver都创建自己的kernel thread,那么内核线程数量过多,这会影响整体的性能。因此,最好的方法就是把这些需求汇集起来,提供一个统一的机制,也就是传说中的work queue了

具体的流程是这样的 分为工作队列(workqueue)、工作(work)、工作线程池和工作队列渠道(pool_workqueue)、worker(工作线程)、worker_pool(工作线程池)

我们这里分析的是CMWQ机制 也就是concurrent-manage workqueue

工作队列
1、workqueue。定义如下:
struct workqueue_struct {
struct cpu_workqueue_struct *cpu_wq; -----per-cpu work queue struct
struct list_head list; ---workqueue list 负责加入链表
const char *name;
int singlethread; ----single thread or multi thread
int freezeable; ----和电源管理相关的一个flag
};
系统在启动的时候会调用init_workqueues函数来初始化几个默认的workqueue 通过create_workqueue()等一些接口创建

2.workqueue
struct workqueue_struct {
struct list_head pwqs; //所有的pool_workqueue 都挂入链表中
struct list_head list; //所有的workqueue挂入该链表

struct pool_workqueue __percpu *cpu_pwqs; -----指向per cpu的pool workqueue
struct pool_workqueue __rcu *numa_pwq_tbl[]; ----指向per node的pool workqueue
};

3、work
struct work_struct {
atomic_long_t data; //低比特位是标志位 高比特位是worker_pool的ID号或者其他
struct list_head entry;
work_func_t func; //调度函数 work被调度执行的时候就是调用这个函数
};

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

4、pool_workqueue
struct pool_workqueue {
struct worker_pool pool; / I: the associated pool */
struct workqueue_struct wq; / I: the owning workqueue */
int work_color; /* L: current color */
int flush_color; /* L: flushing color */
int refcnt; /* L: reference count */
int nr_in_flight[WORK_NR_COLORS];
/* L: nr of in_flight works */
int nr_active; /* L: nr

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值