每个cpu上都有一个workqueue,在实际使用过程中可以很方面的通过
work_on_cpu来让一个函数运行在指定cpu上的thread context中
其源码分析如下:
long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
{
struct work_for_cpu wfc = { .fn = fn, .arg = arg };
#新建一个work
INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
#在percpu上的workqueue中调度这个work
schedule_work_on(cpu, &wfc.work);
#等待这个work 执行完
flush_work(&wfc.work);
#销毁这个work
destroy_work_on_stack(&wfc.work);
return wfc.ret;
}
从schedule_work_on 看是将work当道system_wq这个qorkqueue中运行,这个workque是系统已经
建立好的,不需要用户建立
static inline bool schedule_work_on(int cpu, struct work_struct *work)
{
return queue_work_on(cpu, system_wq, work);
}
从flush_work 的实现看这里会通过wait_for_completion 来等待这个work执行完成
bool flush_work(struct work_struct *work)
{
struct wq_barrier barr;
if (WARN_ON(!wq_online))
return false;
if (start_flush_work(work, &barr)) {
wait_for_completion(&barr.done);
destroy_work_on_stack(&barr.work);
return true;
} else {
return false;
}
}
work_on_cpu
最新推荐文章于 2025-05-06 04:26:10 发布