kthread_worker就像是一个工作者(员工,工人),kthread_work就像是需要工作者去完成的工作。将需要完成的工作打包成kthread_work,然后交给kthread_worker去完成。
1、kthread_worker,kthread_work结构体类型,以及后面出现的API,都声明在include/linux/kthread.h中,如下:
struct kthread_worker {
unsigned int flags;
spinlock_t lock;
struct list_head work_list;
struct list_head delayed_work_list;
struct task_struct *task;
struct kthread_work *current_work;
};
struct kthread_work {
struct list_head node;
kthread_work_func_t func;
struct kthread_worker *worker;
/* Number of canceling calls that are running at the moment. */
int canceling;
};
struct kthread_delayed_work {
struct kthread_work work;
struct timer_list timer;
};
kthread_delayed_work:表示提交给kthread_worker的工作,不需要立马执行,要延后执行,用它,而不是用kthread_work。后面会看到用法。
从使用角度看,我们只需要知道有这三个结构体,就可以了,不用详细具体的了解结构里面每个成员代表的意义。重点是下面API的使用。
2、定义和初始化kthread_worker,kthread_work
第一种方法,用宏:
下列宏都声明在kthread.h里面
#define DEFINE_KTHREAD_WORKER(worker) \
struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
#define DEFINE_KTHREAD_WORK(work, fn) \
struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
struct kthread_delayed_work dwork = \
KTHREAD_DELAYED_WORK_INIT(dwork, fn)
用法,很简单,如下,定义个zi_worker和zi_work:
void zi_fn(struct kthread_work *work)
{
}
DEFINE_KTHREAD_WORKER(zi_worker);
DEFINE_KTHREAD_WORK(zi_work,zi_fn);
注意,zi_fn函数必须符合kthread_work_func_t类型函数,
kthread_work_func_t也声明在kthread.h里面,如下:
typedef void (*kthread_work_func_t)(struct kthread_work *work);
第二种,定义和初始化:
struct kthread_worker zi_worker;
struct kthread_work zi_work;
void zi_fn(struct kthread_work *work)
{
}
kthread_init_worker(&zi_worker);
kthread_init_work(&zi_worke,zi_fn);
以上zi_fn其实,就是作为需要的工作,打包到khtread_work里面,下面就该是将需要完成的工作提交给kthread_worker去完成。
3、提交kthread_work到kthread_worker中去执行
直接看例子:
struct kthread_worker zi_worker;
struct kthread_work zi_work;
struct task_struct *w = NULL;
void zi_fn(struct kthread_work *work)
{
}
kthread_init_worker(&zi_worker);
kthread_init_work(&zi_worke,zi_fn);
w = khtread_run(kthread_worker_fn,&zi_worker,"zi worker");
kthread_queue_work(&zi_worker,&zi_work);
kthread_queue_work()将zi_work提交到zi_worker去完成,
kthread_worker_fn()是系统提供的函数,只需按照例子中的方法,去创建线程,后提交ktread_work到kthread_worker后,线程函数kthread_worker_fn就会运行kthread_work携带的函数zi_fn()。
当然,我们也可以自己实现像kthread_worker_fn这样的函数,但是一般情况下,是没有必要的。
另外,延后执行提交的例子如下:
struct kthread_worker zi_worker;
struct kthread_delayed_work zi_work_delay;
struct task_struct *w = NULL;
void zi_fn(struct kthread_work *work)
{
}
kthread_init_worker(&zi_worker);
kthread_init_delayed_work(&zi_worke_delay,zi_fn);
w = khtread_run(kthread_worker_fn,&zi_worker,"zi worker");
kthread_queue_delayed_work(&zi_worker,&zi_work_delay,100);//表示延后100jiffes再执行函数zi_fn
其他API说明如下:
//创建kthread_worker,可以替代上面说到定义和初始化khtread_worker
struct kthread_worker *
kthread_create_worker(unsigned int flags, const char namefmt[], ...);
//在具体那个cpu上创建kthread_worer,在多核CPU上可用
__printf(3, 4) struct kthread_worker *
kthread_create_worker_on_cpu(int cpu, unsigned int flags,
const char namefmt[], ...);
//刷新kthread_work,直到work被执行后才返回,用于确保work被执行
void kthread_flush_work(struct kthread_work *work);
//刷新kthread_worker,直到worker上的work被处理完后,才返回
void kthread_flush_worker(struct kthread_worker *worker);
//退出kthread_work,将work,从worker上取下,
//未被执行的work将不会再执行,
//正在执行的work,等待完成后,返回
//已经执行work,此函数直接返回,不做任何动作
bool kthread_cancel_work_sync(struct kthread_work *work);
//同上,这个用于延后执行kthread_work的版本
bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
//销毁worker
void kthread_destroy_worker(struct kthread_worker *worker);
注意:以上API都是基于4.10的,3.10的名字有微小的差别,具体视内核版本,一定要注意这点