work_on_cpu

每个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;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值