Skip to content

Commit df70e4b

Browse files
Ryan-CW-CodeRbb666
authored andcommitted
[kernel] rt_scheduler_lock_nest使用原子操作
1 parent 98cb15a commit df70e4b

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/scheduler_up.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c
3232
* 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits
3333
* 2025-08-04 Pillar Add rt_scheduler_critical_switch_flag
34+
* 2025-08-20 RyanCW rt_scheduler_lock_nest use atomic operations
3435
*/
3536

3637
#define __RT_IPC_SOURCE__
@@ -49,7 +50,7 @@ rt_uint8_t rt_thread_ready_table[32];
4950
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
5051

5152
extern volatile rt_atomic_t rt_interrupt_nest;
52-
static rt_int16_t rt_scheduler_lock_nest;
53+
static rt_atomic_t rt_scheduler_lock_nest;
5354
rt_uint8_t rt_current_priority;
5455

5556
static rt_int8_t rt_scheduler_critical_switch_flag;
@@ -649,22 +650,9 @@ RTM_EXPORT(rt_exit_critical_safe);
649650
*/
650651
rt_base_t rt_enter_critical(void)
651652
{
652-
rt_base_t level;
653653
rt_base_t critical_level;
654654

655-
/* disable interrupt */
656-
level = rt_hw_interrupt_disable();
657-
658-
/*
659-
* the maximal number of nest is RT_UINT16_MAX, which is big
660-
* enough and does not check here
661-
*/
662-
rt_scheduler_lock_nest ++;
663-
critical_level = rt_scheduler_lock_nest;
664-
665-
/* enable interrupt */
666-
rt_hw_interrupt_enable(level);
667-
655+
critical_level = rt_atomic_add(&rt_scheduler_lock_nest, 1) + 1;
668656
return critical_level;
669657
}
670658
RTM_EXPORT(rt_enter_critical);
@@ -722,7 +710,7 @@ RTM_EXPORT(rt_exit_critical);
722710
*/
723711
rt_uint16_t rt_critical_level(void)
724712
{
725-
return rt_scheduler_lock_nest;
713+
return (rt_uint16_t)rt_atomic_load(&rt_scheduler_lock_nest);
726714
}
727715
RTM_EXPORT(rt_critical_level);
728716

0 commit comments

Comments
 (0)