lk对临界区的操作

来源:互联网 发布:金蝶数据库恢复 编辑:程序博客网 时间:2024/06/05 17:10
在lk中当要进入临界区的时候一般会用enter_critical_section 和 exit_critical_section来保护
static inline __ALWAYS_INLINE void enter_critical_section(void)
{
critical_section_count++;
if (critical_section_count == 1)
arch_disable_ints();
}
从enter_critical_section 函数中可以看到只有第一次进入的时候才关掉中断。


static inline __ALWAYS_INLINE void exit_critical_section(void)
{
critical_section_count--;
if (critical_section_count == 0)
arch_enable_ints();
}


其中arch_enable_ints 和 arch_disable_ints的实现分别如下
/* void arch_enable_ints(void); */
FUNCTION(arch_enable_ints)
mrs r0, cpsr
bic r0, r0, #(1<<7)/* clear the I bit */
msr cpsr_c, r0
bx lr


/* void arch_disable_ints(void); */
FUNCTION(arch_disable_ints)
mrs r0, cpsr
orr r0, r0, #(1<<7)
msr cpsr_c, r0
bx lr

可见在这两个临界区中是是禁止中断的。进入临界区的时候critical_section_count 会加1
离开临界区的时候会减1,可以临界区是可以嵌套的。
当临界区嵌套是可以直接调用两次enter_critical_section
对应的离开临界区也要调用两次exit_critical_section。


对应的code 类似:


enter_critical_section()
enter_critical_section()
.
.
.
exit_critical_section()
exit_critical_section()


第二次进入临界区的时候,可以调用
static inline void inc_critical_section(void) { critical_section_count++; }
对应的离开临界区的时候
static inline void dec_critical_section(void) { critical_section_count--; }


对应的code 类似:
enter_critical_section()
inc_critical_section()
.
.
.
dec_critical_section()
exit_critical_section()


当某些操作需要判断当前是否在临界区中,例如临界区中中断是被关掉的。可以用in_critical_section来判断,
static inline __ALWAYS_INLINE bool in_critical_section(void)
{
return critical_section_count > 0;
}

1 0
原创粉丝点击