Skip to main content
 首页 » 操作系统

Linux 调度器之抢占

2022年07月19日125dyllove98

一、开关抢占函数

1. preempt_disable/preempt_enable

(1) 关抢占

//include/linux/preempt.h 
#define preempt_disable() \ 
do { \ 
    preempt_count_inc(); \ 
    barrier(); \ 
} while (0) 
 
#define preempt_count_inc() preempt_count_add(1) //include/linux/preempt.h 
 
#define preempt_count_add(val)    __preempt_count_add(val) //include/linux/preempt.h 
 
static inline void __preempt_count_add(int val) //arch/arm64/include/asm/preempt.h 
{ 
    /* 使用的是 current->thread_info.preempt.count */ 
    u32 pc = READ_ONCE(current_thread_info()->preempt.count); 
    pc += val; 
    WRITE_ONCE(current_thread_info()->preempt.count, pc); 
}

(2) 开抢占

#define preempt_enable() \ 
do { \ 
    barrier(); \ 
    if (unlikely(preempt_count_dec_and_test())) \ 
        __preempt_schedule(); \ 
} while (0)

使能 CONFIG_PREEMPTION 的情况下,开抢占后,若抢占计数减为0会触发一次调度

3. 只要对 current->thread_info.preempt.count 进行加减的函数都会起到开关抢占的功能,如关中断、关软中断。

二、抢占粒度


三、抢占作用时机


四、开关抢占的区别

Linux进程调度与抢占:https://www.cnblogs.com/hellokitty2/p/10741600.html


本文参考链接:https://www.cnblogs.com/hellokitty2/p/16382905.html