进程调度API之cond_resched

来源:互联网 发布:蜂窝网络 编辑:程序博客网 时间:2024/06/17 01:35
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)) {#让出cpupreempt_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());}

原创粉丝点击