进程调度API之cond_resched

cond_resched() 用于让出cpu给其他进程运行,相对于yield函数,这个函数使用更为广泛一点.
其使用的例程如下:
void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
		 int ahead)
{
	struct buffer_head *bh;

	hpfs_lock_assert(s);

	hpfs_prefetch_sectors(s, secno, ahead);

	cond_resched();
#让出cpu后,下一次执行时从这里开始.
	*bhp = bh = sb_bread(s, hpfs_search_hotfix_map(s, secno));
	if (bh != NULL)
		return bh->b_data;
	else {
		pr_err("%s(): read error\n", __func__);
		return NULL;
	}
}
其源码分析如下:
#define cond_resched() ({			\
	___might_sleep(__FILE__, __LINE__, 0);	\ #调用这个函数的thread 不能sleep,如果发生sleep的时候,___might_sleep 会打印处callstack
	_cond_resched();			\ #调用此函数让出cpu
})
_cond_resched 成功让出cpu,返回1,失败的返回0,但是我们一般不关心这个返回值
int __sched _cond_resched(void)
{
#检测此thread 是否符合被抢占的条件,这个函数之前的博文分析过
	if (should_resched(0)) {
#让出cpu
		preempt_schedule_common();
		return 1;
	}
	return 0;
}
static void __sched notrace preempt_schedule_common(void)
{
	do {

		preempt_disable_notrace();
		preempt_latency_start(1);
#核心是通过时调用__schedule 来让出cpu
		__schedule(true);
		preempt_latency_stop(1);
		preempt_enable_no_resched_notrace();

		/*
		 * Check again in case we missed a preemption opportunity
		 * between schedule and now.
		 */
	} while (need_resched());
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值