workqueue机制分析之process_one_work分析

工作者线程不断执行,从work_poll结构中卸下一个work, 然后进入函数process_one_work

来执行这个work.

process_one_work(struct worker *worker, struct work_struct *work)

下面一行行代码分析下:

首先,struct pool_workqueue *pwq = get_work_pwq(work);

我们知道了,原来,work_struct 结构体中一直有个域指向了pool_workqueue.得到pwq结构体是为了得到workqueue,[ by pwq->wq], pwq中有许多属于锁啊,包括是不是绑定在CPU上啊这些信息, 都是在处理一个任务的时候需要的

struct worker_pool *pool = worker->pool;

每一个worker对应着一个池子,worker_pool, 

[一个work可能要被多个线程执行,什么情况下会如此呢?]

然后把当前的工作, 加入到woker->hentry的哈希表中

pool->busy_hash是一个哈希表struct hlist_head name[1 << (bits)], 这是个开链的哈希表,

把这个worker链到这个woker_pool上的哈希表上

[看来,并不是一个work_poll对应这一个worker, 多个线程对应着一个worker_poll, 这样设计有其合理性啊,因为如果系统当前比较空闲,但是就是有大量的工作被挂载了当前的work_pool上面, 这样的话, 我们就要多个线程来执行啊]

对于UNBOUND的线程来说,一个work_poll对应着多个worker线程看来是很正常的事情.

然后就是对worker->current_work设置:

worker->current_work = work;

worker->current_func = work->func;

worker->current_pwq = pwq;

然后最重要的一件事情就是worker->current_func(work);

这里就是调用实际的工作函数了:

比如wb_workfn了!

转载于:https://www.cnblogs.com/honpey/p/5135195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为下面这段代码写一个makefile文件,#include <linux/init.h> #include <linux/module.h> #include <linux/workqueue.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/timekeeping.h> MODULE_LICENSE("GPL"); static struct workqueue_struct* workqueue; static struct work_struct work1; static struct delayed_work work2; static int times; module_param(times, int, 5); int get_time(char* buff, int len) { struct timespec64 ts; struct tm tm_res; char time_string[40]; ktime_get_real_ts64(&ts); time64_to_tm(ts.tv_sec, 0, &tm_res); buff=tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec; return 0; } static void real_time_work(struct work_struct* work) { char buff[40]; get_time(buff, sizeof(buff)); printk(KERN_INFO "%s\n",buff); } static void delayed_work(struct work_struct* work) { char buff[40]; get_time(buff, sizeof(buff)); printk(KERN_INFO "this is a delay work :%s\n", buff); } static int __init module_init(void) { printk(KERN_INFO "Initializing workqueue module.\n"); workqueue = create_workqueue("workqueue"); INIT_WORK(&work1, real_time_work); INIT_DELAYED_WORK(&work2, delayed_work); queue_work(workqueue, &work1); queue_delayed_work(workqueue, &work2, msecs_to_jiffies(10000)); return 0; } static void __exit module_exit(void) { printk(KERN_INFO "Exiting workqueue module.\n"); flush_workqueue(workqueue); destroy_workqueue(workqueue); } module_init(module_init); module_exit(module_exit);要求. 把加载、卸载内核模块以 install/uninstall 写入 Makefile 文件中。
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值