一、linux内核抢占介绍

1.抢占发生的必要条件

a.preempt_count抢占计数必须为0,不为0说明其它地方调用了禁止抢占的函数,比如spin_lock系列函数。
b.中断必须是使能的状态,因为抢占动作要依赖中断。

preempt_schedule()具体源码实现参考如下:

asmlinkage __visible void __sched notrace preempt_schedule(void)
{
    /*
     * If there is a non-zero preempt_count or interrupts are disabled,
     * we do not want to preempt the current task. Just return..
     */
    /*preempt_disable()会增加preempt_count的计数*/
    if (likely(!preemptible()))
        return;
    preempt_schedule_common();
}
#define preemptible()    (preempt_count() == 0 && !irqs_disabled())
View Code

相关文章: