发布日期:2022-06-18 17:01 点击次数:129
他人的训戒,咱们的道路!
Linux中断处分可用下图回归:
图中形色了中断处分中的下半部分都有哪些机制,以及何如凭证施行的业务场景、落拓条目来进行选拔。
不错看出:这些不同的完结之间,有些是重迭的,或者是相互取代的相干。
也正因为此,它们之间的使用神气确切是大同小异,至少是在API接口函数的使用神气上,从使用这的角度来看,都口角常访佛的。
这篇著作,咱们就通过施行的代码操作,来演示一下责任队伍(workqueue)的使用神气。
责任队伍是什么责任队伍是Linux操作系统中,进行中断下半部分处分的病笃神气!
从称呼上不错猜到:一个责任队伍就概况业务层常用的音讯队伍雷同,内部存放着好多的责任项恭候着被处分。
责任队伍中有两个病笃的结构体:责任队伍(workqueue_struct) 和 责任项(work_struct):
struct workqueue_struct { struct list_head pwqs; /* WR: all pwqs of this wq */ struct list_head list; /* PR: list of all workqueues */ ... char name[WQ_NAME_LEN]; /* I: workqueue name */ ... /* hot fields used during command issue, aligned to cacheline */ unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ };
struct work_struct { atomic_long_t data; struct list_head entry; work_func_t func; // 指向处分函数 #ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map; #endif };
在内核中,责任队伍中的整个责任项,是通过链表串在全部的,而况恭候着操作系统中的某个线程挨个取出来处分。
这些线程, 国产精品亚洲综合一区在线观看不错是由驱动顺次通过 kthread_create 创建的线程,也不错是由操作系统事前就创建好的线程。
这里就触及到一个遴选的问题了。
如若咱们的处分函数很浅显,那么就莫得必要创建一个单独的线程来处分了。
原因有二:
创建一个内核线程是很奢靡资源的,如若函数很浅显,很快履行结束之后再关闭线程,太划不来了,焉知非福; 如若每一个驱动顺次编写者都毫无节制地创建内核线程,那么内核中将会存在浩荡无须要的线程,chinesegay男男猛男无套诚然了骨子上也曾系统资源消耗和履行成果的问题;为了幸免这种情况,于是操作系统就为咱们事前创建好一些责任队伍和内核线程。
咱们只需要把需要处分的责任项,平直添加到这些事前创建好的责任队伍中就不错了,它们就会被相应的内核线程取出来处分。
举例底下这些责任队伍,便是内核默许创建的(include/linux/workqueue.h):
/* * System-wide workqueues which are always present. * * system_wq is the one used by schedule[_delayed]_work[_on](). * Multi-CPU multi-threaded. There are users which expect relatively * short queue flush time. Don't queue works which can run for too * long. * * system_highpri_wq is similar to system_wq but for work items which * require WQ_HIGHPRI. * * system_long_wq is similar to system_wq but may host long running * works. Queue flushing might take relatively long. * * system_unbound_wq is unbound workqueue. Workers are not bound to * any specific CPU, not concurrency managed, and all queued works are * executed immediately as long as max_active limit is not reached and * resources are available. * * system_freezable_wq is equivalent to system_wq except that it's * freezable. * * *_power_efficient_wq are inclined towards saving power and converted * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise, * they are same as their non-power-efficient counterparts - e.g. * system_power_efficient_wq is identical to system_wq if * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info. */ extern struct workqueue_struct *system_wq; extern struct workqueue_struct *system_highpri_wq; extern struct workqueue_struct *system_long_wq; extern struct workqueue_struct *system_unbound_wq; extern struct workqueue_struct *system_freezable_wq; extern struct workqueue_struct *system_power_efficient_wq; extern struct workqueue_struct *system_freezable_power_efficient_wq;
以上这些默许责任队伍的创建代码是(kernel/workqueue.c):
int __init workqueue_init_early(void) { ... system_wq = alloc_workqueue("events", 0, 0); system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); system_long_wq = alloc_workqueue("events_long", 0, 0); system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE); system_freezable_wq = alloc_workqueue("events_freezable", WQ_FREEZABLE, 0); system_power_efficient_wq = alloc_workqueue("events_power_efficient", WQ_POWER_EFFICIENT, 0); system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", WQ_FREEZABLE | WQ_POWER_EFFICIENT, 0); ... }
此外,由于责任队伍 system_wq 被使用的频率很高,于是内核就封装了一个浅显的函数(schedule_work)给咱们使用:
/** * schedule_work - put work task in global workqueue * @work: job to be done * * Returns
Powered by 最近2019中文字幕在线高清 @2013-2022 RSS地图 HTML地图