From 2774d650a1b481b96b70af0a21ca042a0cd84fe2 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Sun, 1 Jun 2025 20:43:23 +0800 Subject: [PATCH 01/15] =?UTF-8?q?linux=20tulpe=E6=8A=A5=E9=94=99=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=8Cllvm-arm=E7=BC=96=E8=AF=91=E5=99=A8=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/cmake.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/cmake.py b/tools/cmake.py index e998a48e8cd..6a6aa9c37d6 100644 --- a/tools/cmake.py +++ b/tools/cmake.py @@ -45,7 +45,7 @@ def GenerateCFiles(env, project, project_name): tool_path_conv["CMAKE_ASM_COMPILER"] = tool_path_conv_helper(rtconfig.AS) tool_path_conv["CMAKE_AR"] = tool_path_conv_helper(rtconfig.AR) tool_path_conv["CMAKE_LINKER"] = tool_path_conv_helper(rtconfig.LINK) - if rtconfig.PLATFORM in ['gcc']: + if rtconfig.PLATFORM in ['gcc','llvm-arm']: tool_path_conv["CMAKE_SIZE"] = tool_path_conv_helper(rtconfig.SIZE) tool_path_conv["CMAKE_OBJDUMP"] = tool_path_conv_helper(rtconfig.OBJDUMP) tool_path_conv["CMAKE_OBJCOPY"] = tool_path_conv_helper(rtconfig.OBJCPY) @@ -99,7 +99,7 @@ def GenerateCFiles(env, project, project_name): AS += ".exe" AR += ".exe" LINK += ".exe" - if rtconfig.PLATFORM in ['gcc']: + if rtconfig.PLATFORM in ['gcc','llvm-arm']: SIZE += ".exe" OBJDUMP += ".exe" OBJCOPY += ".exe" @@ -129,7 +129,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("SET(CMAKE_CXX_FLAGS \""+ CXXFLAGS + "\")\n") cm_file.write("SET(CMAKE_CXX_COMPILER_WORKS TRUE)\n\n") - if rtconfig.PLATFORM in ['gcc']: + if rtconfig.PLATFORM in ['gcc','llvm-arm']: cm_file.write("SET(CMAKE_OBJCOPY \""+ OBJCOPY + "\")\n") cm_file.write("SET(CMAKE_SIZE \""+ SIZE + "\")\n\n") elif rtconfig.PLATFORM in ['armcc', 'armclang']: @@ -137,7 +137,7 @@ def GenerateCFiles(env, project, project_name): LINKER_FLAGS = '' LINKER_LIBS = '' - if rtconfig.PLATFORM in ['gcc']: + if rtconfig.PLATFORM in ['gcc','llvm-arm']: LINKER_FLAGS += '-T' elif rtconfig.PLATFORM in ['armcc', 'armclang']: LINKER_FLAGS += '--scatter' @@ -186,7 +186,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("ADD_DEFINITIONS(\n") for i in env['CPPDEFINES']: - cm_file.write("\t-D" + i + "\n") + cm_file.write("\t-D" + str(i).replace("(", "").replace(")", "").replace(",", " ") + "\n") cm_file.write(")\n\n") libgroups = [] @@ -290,7 +290,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("\n") cm_file.write("# Interface library search paths\n") - if rtconfig.PLATFORM in ['gcc']: + if rtconfig.PLATFORM in ['gcc','llvm-arm']: for group in libgroups: if not 'LIBPATH' in group.keys(): continue From 231078179bf436ea7c4b1ad31f3127f7cac10f64 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 3 Jun 2025 08:47:11 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=EF=BC=8C=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 376 ++++++++++++++++++++++++++------------------- 1 file changed, 216 insertions(+), 160 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 95e3cc7fb64..961703f0bab 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -30,26 +30,28 @@ * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c * 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits + * 2025-06-01 htl5241 fix task miss and performance improvement */ #define __RT_IPC_SOURCE__ #include #include -#define DBG_TAG "kernel.scheduler" -#define DBG_LVL DBG_INFO +#define DBG_TAG "kernel.scheduler" +#define DBG_LVL DBG_INFO #include -rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; +rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; rt_uint32_t rt_thread_ready_priority_group; +rt_base_t rt_thread_ready_highest_priority; #if RT_THREAD_PRIORITY_MAX > 32 /* Maximum priority level, 256 */ rt_uint8_t rt_thread_ready_table[32]; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ extern volatile rt_atomic_t rt_interrupt_nest; -static rt_int16_t rt_scheduler_lock_nest; -rt_uint8_t rt_current_priority; +static rt_int16_t rt_scheduler_lock_nest; +rt_uint8_t rt_current_priority; #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); @@ -67,7 +69,8 @@ static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); * * @param hook is the hook function. */ -void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) +void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, + struct rt_thread *to)) { rt_scheduler_hook = hook; } @@ -86,26 +89,25 @@ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) /**@}*/ #endif /* RT_USING_HOOK */ -static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio) +static inline void _scheduler_update_highest_priority(void) { - struct rt_thread *highest_priority_thread; - rt_ubase_t highest_ready_priority; - #if RT_THREAD_PRIORITY_MAX > 32 rt_ubase_t number; - number = __rt_ffs(rt_thread_ready_priority_group) - 1; - highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; + rt_thread_ready_highest_priority = + (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else - highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = + __rt_ffs(rt_thread_ready_priority_group) - 1; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ +} +static inline struct rt_thread * +_scheduler_get_priority_thread(rt_ubase_t priority) +{ /* get highest ready priority thread */ - highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[highest_ready_priority].next); - - *highest_prio = highest_ready_priority; - - return highest_priority_thread; + return RT_THREAD_LIST_NODE_ENTRY( + rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) @@ -147,10 +149,9 @@ void rt_system_scheduler_init(void) rt_base_t offset; rt_scheduler_lock_nest = 0; - LOG_D("start scheduler: max priority 0x%02x", - RT_THREAD_PRIORITY_MAX); + LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); - for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) { rt_list_init(&rt_thread_priority_table[offset]); } @@ -171,13 +172,15 @@ void rt_system_scheduler_init(void) void rt_system_scheduler_start(void) { struct rt_thread *to_thread; - rt_ubase_t highest_ready_priority; - to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority); + _scheduler_update_highest_priority(); + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); rt_cpu_self()->current_thread = to_thread; rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; /* switch to new thread */ @@ -187,6 +190,82 @@ void rt_system_scheduler_start(void) /* never come back */ } +/** + * @brief This function will insert a thread to the system ready queue. The state of + * thread will be set as READY and the thread will be removed from suspend queue. + * + * @param thread is the thread to be inserted. + * + * @note Please do not invoke this function in user application. + */ +static inline void _rt_sched_insert_thread(struct rt_thread *thread) +{ + /* READY thread, insert to ready queue */ + RT_SCHED_CTX(thread).stat = + RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + /* there is no time slices left(YIELD), inserting thread before ready list*/ + if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) + { + rt_list_insert_before( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ + else + { + rt_list_insert_after( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + + LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); + + /* set priority mask */ +#if RT_THREAD_PRIORITY_MAX > 32 + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= + RT_SCHED_PRIV(thread).high_mask; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; +} + +/** + * @brief This function will remove a thread from system ready queue. + * + * @param thread is the thread to be removed. + * + * @note Please do not invoke this function in user application. + */ +static inline void _rt_sched_remove_thread(struct rt_thread *thread) +{ + LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); + + /* remove thread from ready list */ + rt_list_remove(&RT_THREAD_LIST_NODE(thread)); + if (rt_list_isempty( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]))) + { +#if RT_THREAD_PRIORITY_MAX > 32 + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= + ~RT_SCHED_PRIV(thread).high_mask; + if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) + { + rt_thread_ready_priority_group &= + ~RT_SCHED_PRIV(thread).number_mask; + } +#else + rt_thread_ready_priority_group &= + ~RT_SCHED_PRIV(thread).number_mask; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + } +} + /** * @addtogroup group_Thread * @cond @@ -201,127 +280,135 @@ void rt_system_scheduler_start(void) void rt_schedule(void) { rt_base_t level; + /* need_insert_from_thread: need to insert from_thread to ready queue */ + int need_insert_from_thread; + /* using local variable to avoid unecessary function call */ + struct rt_thread *curr_thread; struct rt_thread *to_thread; struct rt_thread *from_thread; - /* using local variable to avoid unecessary function call */ - struct rt_thread *curr_thread = rt_thread_self(); - /* disable interrupt */ level = rt_hw_interrupt_disable(); /* check the scheduler is enabled or not */ - if (rt_scheduler_lock_nest == 0) + if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { - rt_ubase_t highest_ready_priority; + curr_thread = rt_thread_self(); - if (rt_thread_ready_priority_group != 0) + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { - /* need_insert_from_thread: need to insert from_thread to ready queue */ - int need_insert_from_thread = 0; + if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) + { + to_thread = curr_thread; + } + else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) + { + to_thread = curr_thread; + } + else + { + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); + need_insert_from_thread = 1; + } + } + else + { + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); + need_insert_from_thread = 0; + } - to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority); + if (to_thread != curr_thread) + { + /* if the destination thread is not the same as current thread */ + rt_current_priority = + (rt_uint8_t)rt_thread_ready_highest_priority; + from_thread = curr_thread; + rt_cpu_self()->current_thread = to_thread; - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, + (from_thread, to_thread)); + + if (need_insert_from_thread) { - if (RT_SCHED_PRIV(curr_thread).current_priority < highest_ready_priority) - { - to_thread = curr_thread; - } - else if (RT_SCHED_PRIV(curr_thread).current_priority == highest_ready_priority - && (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) - { - to_thread = curr_thread; - } - else - { - need_insert_from_thread = 1; - } + _rt_sched_remove_thread(from_thread); + _rt_sched_insert_thread(from_thread); } - if (to_thread != curr_thread) + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - /* if the destination thread is not the same as current thread */ - rt_current_priority = (rt_uint8_t)highest_ready_priority; - from_thread = curr_thread; - rt_cpu_self()->current_thread = to_thread; + RT_SCHED_CTX(from_thread).stat &= + ~RT_THREAD_STAT_YIELD_MASK; + } - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); + _rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = + RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); - if (need_insert_from_thread) - { - rt_sched_insert_thread(from_thread); - } + _scheduler_update_highest_priority(); - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; - } + /* switch to new thread */ + LOG_D("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, + RT_NAME_MAX, to_thread->parent.name, + to_thread->sp, RT_NAME_MAX, + from_thread->parent.name, from_thread->sp); - rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); + RT_SCHEDULER_STACK_CHECK(to_thread); - /* switch to new thread */ - LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, - RT_NAME_MAX, to_thread->parent.name, to_thread->sp, - RT_NAME_MAX, from_thread->parent.name, from_thread->sp); + if (rt_interrupt_nest == 0) + { + extern void rt_thread_handle_sig( + rt_bool_t clean_state); - RT_SCHEDULER_STACK_CHECK(to_thread); + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, + (from_thread)); - if (rt_interrupt_nest == 0) - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); + rt_hw_context_switch( + (rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp); - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + /* enable interrupt */ + rt_hw_interrupt_enable(level); - rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp); +#ifdef RT_USING_SIGNALS + /* check stat of thread for signal */ + level = rt_hw_interrupt_disable(); + if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + extern void rt_thread_handle_sig( + rt_bool_t clean_state); + + RT_SCHED_CTX(curr_thread).stat &= + ~RT_THREAD_STAT_SIGNAL_PENDING; - /* enable interrupt */ rt_hw_interrupt_enable(level); -#ifdef RT_USING_SIGNALS - /* check stat of thread for signal */ - level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); - - RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; - - rt_hw_interrupt_enable(level); - - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); - } - else - { - rt_hw_interrupt_enable(level); - } -#endif /* RT_USING_SIGNALS */ - goto __exit; + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); } else { - LOG_D("switch in interrupt"); - - rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp, from_thread, to_thread); + rt_hw_interrupt_enable(level); } +#endif /* RT_USING_SIGNALS */ + goto __exit; } else { - rt_sched_remove_thread(curr_thread); - RT_SCHED_CTX(curr_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(curr_thread).stat & ~RT_THREAD_STAT_MASK); + LOG_D("switch in interrupt"); + + rt_hw_context_switch_interrupt( + (rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); } } } - /* enable interrupt */ rt_hw_interrupt_enable(level); - __exit: return; } @@ -330,18 +417,22 @@ void rt_schedule(void) void rt_sched_thread_startup(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ + RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ + RT_SCHED_PRIV(thread).high_mask = + 1L + << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ #else - RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + RT_SCHED_PRIV(thread).number_mask = + 1L << RT_SCHED_PRIV(thread).current_priority; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* change thread stat, so we can resume it */ RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; } -void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) +void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, + rt_uint8_t priority) { rt_list_init(&RT_THREAD_LIST_NODE(thread)); @@ -353,12 +444,12 @@ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_ui /* don't add to scheduler queue as init thread */ RT_SCHED_PRIV(thread).number_mask = 0; #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = 0; + RT_SCHED_PRIV(thread).number = 0; RT_SCHED_PRIV(thread).high_mask = 0; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* tick init */ - RT_SCHED_PRIV(thread).init_tick = tick; + RT_SCHED_PRIV(thread).init_tick = tick; RT_SCHED_PRIV(thread).remaining_tick = tick; } @@ -382,33 +473,14 @@ void rt_sched_insert_thread(struct rt_thread *thread) /* it's current thread, it should be RUNNING thread */ if (thread == rt_current_thread) { - RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + RT_SCHED_CTX(thread).stat = + RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); goto __exit; } - /* READY thread, insert to ready queue */ - RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - /* there is no time slices left(YIELD), inserting thread before ready list*/ - if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ - else - { - rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - - LOG_D("insert thread[%.*s], the priority: %d", - RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority); + _rt_sched_insert_thread(thread); - /* set priority mask */ -#if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; + _scheduler_update_highest_priority(); __exit: /* enable interrupt */ @@ -431,24 +503,9 @@ void rt_sched_remove_thread(struct rt_thread *thread) /* disable interrupt */ level = rt_hw_interrupt_disable(); - LOG_D("remove thread[%.*s], the priority: %d", - RT_NAME_MAX, thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); + _rt_sched_remove_thread(thread); - /* remove thread from ready list */ - rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) - { -#if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; - if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) - { - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; - } -#else - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - } + _scheduler_update_highest_priority(); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -468,16 +525,16 @@ void rt_exit_critical_safe(rt_base_t critical_level) { if (critical_level != rt_scheduler_lock_nest) { - int dummy = 1; + int dummy = 1; _critical_error_occurred = 1; - rt_kprintf("%s: un-compatible critical level\n" \ + rt_kprintf("%s: un-compatible critical level\n" "\tCurrent %d\n\tCaller %d\n", __func__, rt_scheduler_lock_nest, critical_level); rt_backtrace(); - while (dummy) ; + while (dummy); } } rt_hw_interrupt_enable(level); @@ -485,14 +542,14 @@ void rt_exit_critical_safe(rt_base_t critical_level) rt_exit_critical(); } -#else /* !RT_DEBUGING_CRITICAL */ +#else /* !RT_DEBUGING_CRITICAL */ void rt_exit_critical_safe(rt_base_t critical_level) { rt_exit_critical(); } -#endif/* RT_DEBUGING_CRITICAL */ +#endif /* RT_DEBUGING_CRITICAL */ RTM_EXPORT(rt_exit_critical_safe); /** @@ -505,12 +562,11 @@ rt_base_t rt_enter_critical(void) /* disable interrupt */ level = rt_hw_interrupt_disable(); - /* * the maximal number of nest is RT_UINT16_MAX, which is big * enough and does not check here */ - rt_scheduler_lock_nest ++; + ++rt_scheduler_lock_nest; critical_level = rt_scheduler_lock_nest; /* enable interrupt */ @@ -530,7 +586,7 @@ void rt_exit_critical(void) /* disable interrupt */ level = rt_hw_interrupt_disable(); - rt_scheduler_lock_nest --; + --rt_scheduler_lock_nest; if (rt_scheduler_lock_nest <= 0) { rt_scheduler_lock_nest = 0; From 0d86ecf3f5d19eddd820e04280ed4d5379990c17 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 3 Jun 2025 21:59:02 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E6=9B=BF=E6=8D=A2static=20inline?= =?UTF-8?q?=E4=B8=BArt=5Finline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 961703f0bab..f2394783a0e 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -89,7 +89,7 @@ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) /**@}*/ #endif /* RT_USING_HOOK */ -static inline void _scheduler_update_highest_priority(void) +rt_inline void _scheduler_update_highest_priority(void) { #if RT_THREAD_PRIORITY_MAX > 32 rt_ubase_t number; @@ -102,7 +102,7 @@ static inline void _scheduler_update_highest_priority(void) #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } -static inline struct rt_thread * +rt_inline struct rt_thread * _scheduler_get_priority_thread(rt_ubase_t priority) { /* get highest ready priority thread */ @@ -198,7 +198,7 @@ void rt_system_scheduler_start(void) * * @note Please do not invoke this function in user application. */ -static inline void _rt_sched_insert_thread(struct rt_thread *thread) +rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { /* READY thread, insert to ready queue */ RT_SCHED_CTX(thread).stat = @@ -239,7 +239,7 @@ static inline void _rt_sched_insert_thread(struct rt_thread *thread) * * @note Please do not invoke this function in user application. */ -static inline void _rt_sched_remove_thread(struct rt_thread *thread) +rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) { LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, From 200f74853cc64119b921a5aa6520a66db68b402e Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 3 Jun 2025 23:16:32 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index f2394783a0e..92ae3e53451 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -33,6 +33,7 @@ * 2025-06-01 htl5241 fix task miss and performance improvement */ +#include "rttypes.h" #define __RT_IPC_SOURCE__ #include #include @@ -45,8 +46,8 @@ rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; rt_uint32_t rt_thread_ready_priority_group; rt_base_t rt_thread_ready_highest_priority; #if RT_THREAD_PRIORITY_MAX > 32 -/* Maximum priority level, 256 */ -rt_uint8_t rt_thread_ready_table[32]; + /* Maximum priority level, 256 */ + rt_uint8_t rt_thread_ready_table[32]; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ extern volatile rt_atomic_t rt_interrupt_nest; @@ -107,7 +108,7 @@ _scheduler_get_priority_thread(rt_ubase_t priority) { /* get highest ready priority thread */ return RT_THREAD_LIST_NODE_ENTRY( - rt_thread_priority_table[priority].next); + rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) @@ -175,7 +176,7 @@ void rt_system_scheduler_start(void) _scheduler_update_highest_priority(); to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + rt_thread_ready_highest_priority); rt_cpu_self()->current_thread = to_thread; @@ -208,7 +209,7 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { rt_list_insert_before( &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + .current_priority]), &RT_THREAD_LIST_NODE(thread)); } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ @@ -216,7 +217,7 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { rt_list_insert_after( &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + .current_priority]), &RT_THREAD_LIST_NODE(thread)); } @@ -248,7 +249,7 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); if (rt_list_isempty( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) .current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 @@ -280,8 +281,6 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) void rt_schedule(void) { rt_base_t level; - /* need_insert_from_thread: need to insert from_thread to ready queue */ - int need_insert_from_thread; /* using local variable to avoid unecessary function call */ struct rt_thread *curr_thread; struct rt_thread *to_thread; @@ -292,6 +291,9 @@ void rt_schedule(void) /* check the scheduler is enabled or not */ if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { + /* need_insert_from_thread: need to insert from_thread to ready queue */ + rt_bool_t need_insert_from_thread = RT_FALSE; + curr_thread = rt_thread_self(); if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) @@ -307,15 +309,14 @@ void rt_schedule(void) else { to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); - need_insert_from_thread = 1; + rt_thread_ready_highest_priority); + need_insert_from_thread = RT_TRUE; } } else { to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); - need_insert_from_thread = 0; + rt_thread_ready_highest_priority); } if (to_thread != curr_thread) From 90dff707e199a8a46fa304c3681e9b6db6632cc1 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 3 Jun 2025 23:23:00 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 92ae3e53451..b41b61efa3c 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -281,6 +281,8 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) void rt_schedule(void) { rt_base_t level; + /* need_insert_from_thread: need to insert from_thread to ready queue */ + rt_bool_t need_insert_from_thread; /* using local variable to avoid unecessary function call */ struct rt_thread *curr_thread; struct rt_thread *to_thread; @@ -291,9 +293,8 @@ void rt_schedule(void) /* check the scheduler is enabled or not */ if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { - /* need_insert_from_thread: need to insert from_thread to ready queue */ - rt_bool_t need_insert_from_thread = RT_FALSE; + need_insert_from_thread = RT_FALSE; curr_thread = rt_thread_self(); if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) From b92f8a90d302919c5cf37a93cd138bc1f23b8efd Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Wed, 4 Jun 2025 07:05:42 +0800 Subject: [PATCH 06/15] =?UTF-8?q?Revert=20"linux=20tulpe=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=8Cllvm-arm=E7=BC=96=E8=AF=91=E5=99=A8?= =?UTF-8?q?=E6=8A=A5=E9=94=99"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2774d650a1b481b96b70af0a21ca042a0cd84fe2. --- tools/cmake.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/cmake.py b/tools/cmake.py index 6a6aa9c37d6..e998a48e8cd 100644 --- a/tools/cmake.py +++ b/tools/cmake.py @@ -45,7 +45,7 @@ def GenerateCFiles(env, project, project_name): tool_path_conv["CMAKE_ASM_COMPILER"] = tool_path_conv_helper(rtconfig.AS) tool_path_conv["CMAKE_AR"] = tool_path_conv_helper(rtconfig.AR) tool_path_conv["CMAKE_LINKER"] = tool_path_conv_helper(rtconfig.LINK) - if rtconfig.PLATFORM in ['gcc','llvm-arm']: + if rtconfig.PLATFORM in ['gcc']: tool_path_conv["CMAKE_SIZE"] = tool_path_conv_helper(rtconfig.SIZE) tool_path_conv["CMAKE_OBJDUMP"] = tool_path_conv_helper(rtconfig.OBJDUMP) tool_path_conv["CMAKE_OBJCOPY"] = tool_path_conv_helper(rtconfig.OBJCPY) @@ -99,7 +99,7 @@ def GenerateCFiles(env, project, project_name): AS += ".exe" AR += ".exe" LINK += ".exe" - if rtconfig.PLATFORM in ['gcc','llvm-arm']: + if rtconfig.PLATFORM in ['gcc']: SIZE += ".exe" OBJDUMP += ".exe" OBJCOPY += ".exe" @@ -129,7 +129,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("SET(CMAKE_CXX_FLAGS \""+ CXXFLAGS + "\")\n") cm_file.write("SET(CMAKE_CXX_COMPILER_WORKS TRUE)\n\n") - if rtconfig.PLATFORM in ['gcc','llvm-arm']: + if rtconfig.PLATFORM in ['gcc']: cm_file.write("SET(CMAKE_OBJCOPY \""+ OBJCOPY + "\")\n") cm_file.write("SET(CMAKE_SIZE \""+ SIZE + "\")\n\n") elif rtconfig.PLATFORM in ['armcc', 'armclang']: @@ -137,7 +137,7 @@ def GenerateCFiles(env, project, project_name): LINKER_FLAGS = '' LINKER_LIBS = '' - if rtconfig.PLATFORM in ['gcc','llvm-arm']: + if rtconfig.PLATFORM in ['gcc']: LINKER_FLAGS += '-T' elif rtconfig.PLATFORM in ['armcc', 'armclang']: LINKER_FLAGS += '--scatter' @@ -186,7 +186,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("ADD_DEFINITIONS(\n") for i in env['CPPDEFINES']: - cm_file.write("\t-D" + str(i).replace("(", "").replace(")", "").replace(",", " ") + "\n") + cm_file.write("\t-D" + i + "\n") cm_file.write(")\n\n") libgroups = [] @@ -290,7 +290,7 @@ def GenerateCFiles(env, project, project_name): cm_file.write("\n") cm_file.write("# Interface library search paths\n") - if rtconfig.PLATFORM in ['gcc','llvm-arm']: + if rtconfig.PLATFORM in ['gcc']: for group in libgroups: if not 'LIBPATH' in group.keys(): continue From cad855b228160dfc9dc475b5f2fc57fca48c2d95 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 17:34:39 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index b41b61efa3c..37a53ebce75 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -433,6 +433,19 @@ void rt_sched_thread_startup(struct rt_thread *thread) RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; } +/** + * @brief Initialize thread's scheduling private data + * + * @param thread Pointer to the thread control block + * @param tick Initial time slice value for the thread + * @param priority Initial priority of the thread + * + * @details This function: + * - Initializes the thread's list node + * - Sets initial and current priority (must be < RT_THREAD_PRIORITY_MAX) + * - Initializes priority masks (number_mask, number, high_mask for >32 priorities) + * - Sets initial and remaining time slice ticks + */ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) { From b8b258a334b06694cf0824b15296086e2915cfe7 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 17:52:42 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E5=86=B2=E7=AA=81=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 37a53ebce75..45800de9ea4 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -46,8 +46,8 @@ rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; rt_uint32_t rt_thread_ready_priority_group; rt_base_t rt_thread_ready_highest_priority; #if RT_THREAD_PRIORITY_MAX > 32 - /* Maximum priority level, 256 */ - rt_uint8_t rt_thread_ready_table[32]; +/* Maximum priority level, 256 */ +rt_uint8_t rt_thread_ready_table[32]; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ extern volatile rt_atomic_t rt_interrupt_nest; @@ -108,7 +108,7 @@ _scheduler_get_priority_thread(rt_ubase_t priority) { /* get highest ready priority thread */ return RT_THREAD_LIST_NODE_ENTRY( - rt_thread_priority_table[priority].next); + rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) @@ -176,7 +176,7 @@ void rt_system_scheduler_start(void) _scheduler_update_highest_priority(); to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + rt_thread_ready_highest_priority); rt_cpu_self()->current_thread = to_thread; @@ -209,7 +209,7 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { rt_list_insert_before( &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + .current_priority]), &RT_THREAD_LIST_NODE(thread)); } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ @@ -217,7 +217,7 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { rt_list_insert_after( &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + .current_priority]), &RT_THREAD_LIST_NODE(thread)); } @@ -249,7 +249,7 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); if (rt_list_isempty( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) .current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 @@ -293,9 +293,8 @@ void rt_schedule(void) /* check the scheduler is enabled or not */ if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { - need_insert_from_thread = RT_FALSE; - curr_thread = rt_thread_self(); + curr_thread = rt_thread_self(); if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { @@ -310,14 +309,14 @@ void rt_schedule(void) else { to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + rt_thread_ready_highest_priority); need_insert_from_thread = RT_TRUE; } } else { to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + rt_thread_ready_highest_priority); } if (to_thread != curr_thread) @@ -446,8 +445,7 @@ void rt_sched_thread_startup(struct rt_thread *thread) * - Initializes priority masks (number_mask, number, high_mask for >32 priorities) * - Sets initial and remaining time slice ticks */ -void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, - rt_uint8_t priority) +void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) { rt_list_init(&RT_THREAD_LIST_NODE(thread)); From bbddbc392dd312b8874c1695584f450330e88313 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 20:58:59 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E7=94=A8linux-kernel=20clang-format=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 656 ++++++++++++++++++++++----------------------- 1 file changed, 319 insertions(+), 337 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 45800de9ea4..edee7e6f7dc 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -42,17 +42,17 @@ #define DBG_LVL DBG_INFO #include -rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; +rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; rt_uint32_t rt_thread_ready_priority_group; -rt_base_t rt_thread_ready_highest_priority; +rt_base_t rt_thread_ready_highest_priority; #if RT_THREAD_PRIORITY_MAX > 32 /* Maximum priority level, 256 */ rt_uint8_t rt_thread_ready_table[32]; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ extern volatile rt_atomic_t rt_interrupt_nest; -static rt_int16_t rt_scheduler_lock_nest; -rt_uint8_t rt_current_priority; +static rt_int16_t rt_scheduler_lock_nest; +rt_uint8_t rt_current_priority; #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); @@ -71,9 +71,9 @@ static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); * @param hook is the hook function. */ void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, - struct rt_thread *to)) + struct rt_thread *to)) { - rt_scheduler_hook = hook; + rt_scheduler_hook = hook; } /** @@ -84,7 +84,7 @@ void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, */ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) { - rt_scheduler_switch_hook = hook; + rt_scheduler_switch_hook = hook; } /**@}*/ @@ -93,53 +93,51 @@ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) rt_inline void _scheduler_update_highest_priority(void) { #if RT_THREAD_PRIORITY_MAX > 32 - rt_ubase_t number; - number = __rt_ffs(rt_thread_ready_priority_group) - 1; - rt_thread_ready_highest_priority = - (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; + rt_ubase_t number; + number = __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = + (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else - rt_thread_ready_highest_priority = - __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = + __rt_ffs(rt_thread_ready_priority_group) - 1; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } -rt_inline struct rt_thread * -_scheduler_get_priority_thread(rt_ubase_t priority) +rt_inline struct rt_thread *_scheduler_get_priority_thread(rt_ubase_t priority) { - /* get highest ready priority thread */ - return RT_THREAD_LIST_NODE_ENTRY( - rt_thread_priority_table[priority].next); + /* get highest ready priority thread */ + return RT_THREAD_LIST_NODE_ENTRY( + rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) { - rt_base_t level; - if (!plvl) - return -RT_EINVAL; + rt_base_t level; + if (!plvl) + return -RT_EINVAL; - level = rt_hw_interrupt_disable(); - *plvl = level; + level = rt_hw_interrupt_disable(); + *plvl = level; - return RT_EOK; + return RT_EOK; } rt_err_t rt_sched_unlock(rt_sched_lock_level_t level) { - rt_hw_interrupt_enable(level); + rt_hw_interrupt_enable(level); - return RT_EOK; + return RT_EOK; } rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) { - if (rt_thread_self()) - { - /* if scheduler is available */ - rt_schedule(); - } - rt_hw_interrupt_enable(level); - - return RT_EOK; + if (rt_thread_self()) { + /* if scheduler is available */ + rt_schedule(); + } + rt_hw_interrupt_enable(level); + + return RT_EOK; } /** @@ -147,22 +145,21 @@ rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) */ void rt_system_scheduler_init(void) { - rt_base_t offset; - rt_scheduler_lock_nest = 0; + rt_base_t offset; + rt_scheduler_lock_nest = 0; - LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); + LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); - for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) - { - rt_list_init(&rt_thread_priority_table[offset]); - } + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) { + rt_list_init(&rt_thread_priority_table[offset]); + } - /* initialize ready priority group */ - rt_thread_ready_priority_group = 0; + /* initialize ready priority group */ + rt_thread_ready_priority_group = 0; #if RT_THREAD_PRIORITY_MAX > 32 - /* initialize ready table */ - rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); + /* initialize ready table */ + rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } @@ -172,23 +169,23 @@ void rt_system_scheduler_init(void) */ void rt_system_scheduler_start(void) { - struct rt_thread *to_thread; + struct rt_thread *to_thread; - _scheduler_update_highest_priority(); - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + _scheduler_update_highest_priority(); + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); - rt_cpu_self()->current_thread = to_thread; + rt_cpu_self()->current_thread = to_thread; - rt_sched_remove_thread(to_thread); + rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; - /* switch to new thread */ + /* switch to new thread */ - rt_hw_context_switch_to((rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch_to((rt_uintptr_t)&to_thread->sp); - /* never come back */ + /* never come back */ } /** @@ -201,36 +198,35 @@ void rt_system_scheduler_start(void) */ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { - /* READY thread, insert to ready queue */ - RT_SCHED_CTX(thread).stat = - RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - /* there is no time slices left(YIELD), inserting thread before ready list*/ - if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - rt_list_insert_before( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ - else - { - rt_list_insert_after( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - - LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); - - /* set priority mask */ + /* READY thread, insert to ready queue */ + RT_SCHED_CTX(thread).stat = + RT_THREAD_READY | + (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + /* there is no time slices left(YIELD), inserting thread before ready list*/ + if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { + rt_list_insert_before( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ + else { + rt_list_insert_after( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + + LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); + + /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= - RT_SCHED_PRIV(thread).high_mask; + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= + RT_SCHED_PRIV(thread).high_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; } /** @@ -242,29 +238,27 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) */ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) { - LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); - - /* remove thread from ready list */ - rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]))) - { + LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); + + /* remove thread from ready list */ + rt_list_remove(&RT_THREAD_LIST_NODE(thread)); + if (rt_list_isempty( + &(rt_thread_priority_table[RT_SCHED_PRIV(thread) + .current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= - ~RT_SCHED_PRIV(thread).high_mask; - if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) - { - rt_thread_ready_priority_group &= - ~RT_SCHED_PRIV(thread).number_mask; - } + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= + ~RT_SCHED_PRIV(thread).high_mask; + if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) { + rt_thread_ready_priority_group &= + ~RT_SCHED_PRIV(thread).number_mask; + } #else - rt_thread_ready_priority_group &= - ~RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group &= + ~RT_SCHED_PRIV(thread).number_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - } + } } /** @@ -280,156 +274,148 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) */ void rt_schedule(void) { - rt_base_t level; - /* need_insert_from_thread: need to insert from_thread to ready queue */ - rt_bool_t need_insert_from_thread; - /* using local variable to avoid unecessary function call */ - struct rt_thread *curr_thread; - struct rt_thread *to_thread; - struct rt_thread *from_thread; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - /* check the scheduler is enabled or not */ - if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) - { - need_insert_from_thread = RT_FALSE; - curr_thread = rt_thread_self(); - - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) - { - if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) - { - to_thread = curr_thread; - } - else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) - { - to_thread = curr_thread; - } - else - { - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); - need_insert_from_thread = RT_TRUE; - } - } - else - { - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); - } - - if (to_thread != curr_thread) - { - /* if the destination thread is not the same as current thread */ - rt_current_priority = - (rt_uint8_t)rt_thread_ready_highest_priority; - from_thread = curr_thread; - rt_cpu_self()->current_thread = to_thread; - - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, - (from_thread, to_thread)); - - if (need_insert_from_thread) - { - _rt_sched_remove_thread(from_thread); - _rt_sched_insert_thread(from_thread); - } - - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - RT_SCHED_CTX(from_thread).stat &= - ~RT_THREAD_STAT_YIELD_MASK; - } - - _rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = - RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); - - _scheduler_update_highest_priority(); - - /* switch to new thread */ - LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, - RT_NAME_MAX, to_thread->parent.name, - to_thread->sp, RT_NAME_MAX, - from_thread->parent.name, from_thread->sp); - - RT_SCHEDULER_STACK_CHECK(to_thread); - - if (rt_interrupt_nest == 0) - { - extern void rt_thread_handle_sig( - rt_bool_t clean_state); - - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, - (from_thread)); - - rt_hw_context_switch( - (rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp); - - /* enable interrupt */ - rt_hw_interrupt_enable(level); + rt_base_t level; + /* need_insert_from_thread: need to insert from_thread to ready queue */ + rt_bool_t need_insert_from_thread; + /* using local variable to avoid unecessary function call */ + struct rt_thread *curr_thread; + struct rt_thread *to_thread; + struct rt_thread *from_thread; + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + /* check the scheduler is enabled or not */ + if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { + need_insert_from_thread = RT_FALSE; + curr_thread = rt_thread_self(); + + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == + RT_THREAD_RUNNING) { + if (RT_SCHED_PRIV(curr_thread).current_priority < + rt_thread_ready_highest_priority) { + to_thread = curr_thread; + } else if (RT_SCHED_PRIV(curr_thread).current_priority == + rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & + RT_THREAD_STAT_YIELD_MASK) == 0) { + to_thread = curr_thread; + } else { + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); + need_insert_from_thread = RT_TRUE; + } + } else { + to_thread = _scheduler_get_priority_thread( + rt_thread_ready_highest_priority); + } + + if (to_thread != curr_thread) { + /* if the destination thread is not the same as current thread */ + rt_current_priority = + (rt_uint8_t)rt_thread_ready_highest_priority; + from_thread = curr_thread; + rt_cpu_self()->current_thread = to_thread; + + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, + (from_thread, to_thread)); + + if (need_insert_from_thread) { + _rt_sched_remove_thread(from_thread); + _rt_sched_insert_thread(from_thread); + } + + if ((RT_SCHED_CTX(from_thread).stat & + RT_THREAD_STAT_YIELD_MASK) != 0) { + RT_SCHED_CTX(from_thread).stat &= + ~RT_THREAD_STAT_YIELD_MASK; + } + + _rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = + RT_THREAD_RUNNING | + (RT_SCHED_CTX(to_thread).stat & + ~RT_THREAD_STAT_MASK); + + _scheduler_update_highest_priority(); + + /* switch to new thread */ + LOG_D("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, + RT_NAME_MAX, to_thread->parent.name, + to_thread->sp, RT_NAME_MAX, + from_thread->parent.name, from_thread->sp); + + RT_SCHEDULER_STACK_CHECK(to_thread); + + if (rt_interrupt_nest == 0) { + extern void rt_thread_handle_sig( + rt_bool_t clean_state); + + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, + (from_thread)); + + rt_hw_context_switch( + (rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp); + + /* enable interrupt */ + rt_hw_interrupt_enable(level); #ifdef RT_USING_SIGNALS - /* check stat of thread for signal */ - level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) - { - extern void rt_thread_handle_sig( - rt_bool_t clean_state); - - RT_SCHED_CTX(curr_thread).stat &= - ~RT_THREAD_STAT_SIGNAL_PENDING; - - rt_hw_interrupt_enable(level); - - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); - } - else - { - rt_hw_interrupt_enable(level); - } + /* check stat of thread for signal */ + level = rt_hw_interrupt_disable(); + if (RT_SCHED_CTX(curr_thread).stat & + RT_THREAD_STAT_SIGNAL_PENDING) { + extern void rt_thread_handle_sig( + rt_bool_t clean_state); + + RT_SCHED_CTX(curr_thread).stat &= + ~RT_THREAD_STAT_SIGNAL_PENDING; + + rt_hw_interrupt_enable(level); + + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } else { + rt_hw_interrupt_enable(level); + } #endif /* RT_USING_SIGNALS */ - goto __exit; - } - else - { - LOG_D("switch in interrupt"); - - rt_hw_context_switch_interrupt( - (rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp, - from_thread, to_thread); - } - } - } - /* enable interrupt */ - rt_hw_interrupt_enable(level); + goto __exit; + } else { + LOG_D("switch in interrupt"); + + rt_hw_context_switch_interrupt( + (rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); + } + } + } + /* enable interrupt */ + rt_hw_interrupt_enable(level); __exit: - return; + return; } /* Normally, there isn't anyone racing with us so this operation is lockless */ void rt_sched_thread_startup(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ - RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = - 1L - << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ + RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> + 3; /* 5bit */ + RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; + RT_SCHED_PRIV(thread).high_mask = + 1L + << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ #else - RT_SCHED_PRIV(thread).number_mask = - 1L << RT_SCHED_PRIV(thread).current_priority; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + RT_SCHED_PRIV(thread).number_mask = + 1L << RT_SCHED_PRIV(thread).current_priority; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - /* change thread stat, so we can resume it */ - RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; + /* change thread stat, so we can resume it */ + RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; } /** @@ -445,25 +431,26 @@ void rt_sched_thread_startup(struct rt_thread *thread) * - Initializes priority masks (number_mask, number, high_mask for >32 priorities) * - Sets initial and remaining time slice ticks */ -void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) +void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, + rt_uint8_t priority) { - rt_list_init(&RT_THREAD_LIST_NODE(thread)); + rt_list_init(&RT_THREAD_LIST_NODE(thread)); - /* priority init */ - RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); - RT_SCHED_PRIV(thread).init_priority = priority; - RT_SCHED_PRIV(thread).current_priority = priority; + /* priority init */ + RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); + RT_SCHED_PRIV(thread).init_priority = priority; + RT_SCHED_PRIV(thread).current_priority = priority; - /* don't add to scheduler queue as init thread */ - RT_SCHED_PRIV(thread).number_mask = 0; + /* don't add to scheduler queue as init thread */ + RT_SCHED_PRIV(thread).number_mask = 0; #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = 0; - RT_SCHED_PRIV(thread).high_mask = 0; + RT_SCHED_PRIV(thread).number = 0; + RT_SCHED_PRIV(thread).high_mask = 0; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - /* tick init */ - RT_SCHED_PRIV(thread).init_tick = tick; - RT_SCHED_PRIV(thread).remaining_tick = tick; + /* tick init */ + RT_SCHED_PRIV(thread).init_tick = tick; + RT_SCHED_PRIV(thread).remaining_tick = tick; } /** @@ -476,28 +463,28 @@ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_ui */ void rt_sched_insert_thread(struct rt_thread *thread) { - rt_base_t level; + rt_base_t level; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - /* it's current thread, it should be RUNNING thread */ - if (thread == rt_current_thread) - { - RT_SCHED_CTX(thread).stat = - RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - goto __exit; - } + /* it's current thread, it should be RUNNING thread */ + if (thread == rt_current_thread) { + RT_SCHED_CTX(thread).stat = + RT_THREAD_RUNNING | + (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + goto __exit; + } - _rt_sched_insert_thread(thread); + _rt_sched_insert_thread(thread); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); __exit: - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); } /** @@ -509,19 +496,19 @@ void rt_sched_insert_thread(struct rt_thread *thread) */ void rt_sched_remove_thread(struct rt_thread *thread) { - rt_base_t level; + rt_base_t level; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - _rt_sched_remove_thread(thread); + _rt_sched_remove_thread(thread); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); } #ifdef RT_DEBUGING_CRITICAL @@ -530,36 +517,35 @@ static volatile int _critical_error_occurred = 0; void rt_exit_critical_safe(rt_base_t critical_level) { - rt_base_t level; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - if (!_critical_error_occurred) - { - if (critical_level != rt_scheduler_lock_nest) - { - int dummy = 1; - _critical_error_occurred = 1; - - rt_kprintf("%s: un-compatible critical level\n" - "\tCurrent %d\n\tCaller %d\n", - __func__, rt_scheduler_lock_nest, - critical_level); - rt_backtrace(); - - while (dummy); - } - } - rt_hw_interrupt_enable(level); - - rt_exit_critical(); + rt_base_t level; + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (!_critical_error_occurred) { + if (critical_level != rt_scheduler_lock_nest) { + int dummy = 1; + _critical_error_occurred = 1; + + rt_kprintf("%s: un-compatible critical level\n" + "\tCurrent %d\n\tCaller %d\n", + __func__, rt_scheduler_lock_nest, + critical_level); + rt_backtrace(); + + while (dummy) + ; + } + } + rt_hw_interrupt_enable(level); + + rt_exit_critical(); } -#else /* !RT_DEBUGING_CRITICAL */ +#else /* !RT_DEBUGING_CRITICAL */ void rt_exit_critical_safe(rt_base_t critical_level) { - rt_exit_critical(); + rt_exit_critical(); } #endif /* RT_DEBUGING_CRITICAL */ @@ -570,22 +556,22 @@ RTM_EXPORT(rt_exit_critical_safe); */ rt_base_t rt_enter_critical(void) { - rt_base_t level; - rt_base_t critical_level; + rt_base_t level; + rt_base_t critical_level; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - /* + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + /* * the maximal number of nest is RT_UINT16_MAX, which is big * enough and does not check here */ - ++rt_scheduler_lock_nest; - critical_level = rt_scheduler_lock_nest; + ++rt_scheduler_lock_nest; + critical_level = rt_scheduler_lock_nest; - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); - return critical_level; + return critical_level; } RTM_EXPORT(rt_enter_critical); @@ -594,29 +580,25 @@ RTM_EXPORT(rt_enter_critical); */ void rt_exit_critical(void) { - rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - --rt_scheduler_lock_nest; - if (rt_scheduler_lock_nest <= 0) - { - rt_scheduler_lock_nest = 0; - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - if (rt_current_thread) - { - /* if scheduler is started, do a schedule */ - rt_schedule(); - } - } - else - { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } + rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + --rt_scheduler_lock_nest; + if (rt_scheduler_lock_nest <= 0) { + rt_scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + if (rt_current_thread) { + /* if scheduler is started, do a schedule */ + rt_schedule(); + } + } else { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } } RTM_EXPORT(rt_exit_critical); @@ -627,13 +609,13 @@ RTM_EXPORT(rt_exit_critical); */ rt_uint16_t rt_critical_level(void) { - return rt_scheduler_lock_nest; + return rt_scheduler_lock_nest; } RTM_EXPORT(rt_critical_level); rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu) { - return -RT_EINVAL; + return -RT_EINVAL; } /**@}*/ From 052ed7098e9b12caff0a1ce9a9ab7e27f1dd9f1f Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 21:02:01 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E5=8F=88=E9=87=8D=E6=96=B0=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 106 +++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 62 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index edee7e6f7dc..8e463a963f7 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -71,7 +71,7 @@ static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); * @param hook is the hook function. */ void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, - struct rt_thread *to)) + struct rt_thread *to)) { rt_scheduler_hook = hook; } @@ -106,8 +106,7 @@ rt_inline void _scheduler_update_highest_priority(void) rt_inline struct rt_thread *_scheduler_get_priority_thread(rt_ubase_t priority) { /* get highest ready priority thread */ - return RT_THREAD_LIST_NODE_ENTRY( - rt_thread_priority_table[priority].next); + return RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) @@ -172,8 +171,8 @@ void rt_system_scheduler_start(void) struct rt_thread *to_thread; _scheduler_update_highest_priority(); - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + to_thread = + _scheduler_get_priority_thread(rt_thread_ready_highest_priority); rt_cpu_self()->current_thread = to_thread; @@ -200,26 +199,23 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { /* READY thread, insert to ready queue */ RT_SCHED_CTX(thread).stat = - RT_THREAD_READY | - (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); /* there is no time slices left(YIELD), inserting thread before ready list*/ if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { rt_list_insert_before( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + &(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), &RT_THREAD_LIST_NODE(thread)); } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ else { rt_list_insert_after( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]), + &(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), &RT_THREAD_LIST_NODE(thread)); } LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 @@ -239,14 +235,13 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) { LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); + thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread) - .current_priority]))) { + if (rt_list_isempty(&( + rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; @@ -255,8 +250,7 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) ~RT_SCHED_PRIV(thread).number_mask; } #else - rt_thread_ready_priority_group &= - ~RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } } @@ -290,14 +284,14 @@ void rt_schedule(void) curr_thread = rt_thread_self(); if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == - RT_THREAD_RUNNING) { + RT_THREAD_RUNNING) { if (RT_SCHED_PRIV(curr_thread).current_priority < - rt_thread_ready_highest_priority) { + rt_thread_ready_highest_priority) { to_thread = curr_thread; } else if (RT_SCHED_PRIV(curr_thread).current_priority == - rt_thread_ready_highest_priority && - (RT_SCHED_CTX(curr_thread).stat & - RT_THREAD_STAT_YIELD_MASK) == 0) { + rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & + RT_THREAD_STAT_YIELD_MASK) == 0) { to_thread = curr_thread; } else { to_thread = _scheduler_get_priority_thread( @@ -311,54 +305,46 @@ void rt_schedule(void) if (to_thread != curr_thread) { /* if the destination thread is not the same as current thread */ - rt_current_priority = - (rt_uint8_t)rt_thread_ready_highest_priority; + rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; from_thread = curr_thread; rt_cpu_self()->current_thread = to_thread; - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, - (from_thread, to_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); if (need_insert_from_thread) { _rt_sched_remove_thread(from_thread); _rt_sched_insert_thread(from_thread); } - if ((RT_SCHED_CTX(from_thread).stat & - RT_THREAD_STAT_YIELD_MASK) != 0) { - RT_SCHED_CTX(from_thread).stat &= - ~RT_THREAD_STAT_YIELD_MASK; + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != + 0) { + RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; } _rt_sched_remove_thread(to_thread); RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | - (RT_SCHED_CTX(to_thread).stat & - ~RT_THREAD_STAT_MASK); + (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); _scheduler_update_highest_priority(); /* switch to new thread */ LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, - RT_NAME_MAX, to_thread->parent.name, - to_thread->sp, RT_NAME_MAX, - from_thread->parent.name, from_thread->sp); + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, + to_thread->parent.name, to_thread->sp, RT_NAME_MAX, + from_thread->parent.name, from_thread->sp); RT_SCHEDULER_STACK_CHECK(to_thread); if (rt_interrupt_nest == 0) { - extern void rt_thread_handle_sig( - rt_bool_t clean_state); + extern void rt_thread_handle_sig(rt_bool_t clean_state); - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, - (from_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); - rt_hw_context_switch( - (rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -367,9 +353,8 @@ void rt_schedule(void) /* check stat of thread for signal */ level = rt_hw_interrupt_disable(); if (RT_SCHED_CTX(curr_thread).stat & - RT_THREAD_STAT_SIGNAL_PENDING) { - extern void rt_thread_handle_sig( - rt_bool_t clean_state); + RT_THREAD_STAT_SIGNAL_PENDING) { + extern void rt_thread_handle_sig(rt_bool_t clean_state); RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; @@ -386,10 +371,9 @@ void rt_schedule(void) } else { LOG_D("switch in interrupt"); - rt_hw_context_switch_interrupt( - (rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp, - from_thread, to_thread); + rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, + (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); } } } @@ -404,11 +388,10 @@ void rt_sched_thread_startup(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> - 3; /* 5bit */ + 3; /* 5bit */ RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; RT_SCHED_PRIV(thread).high_mask = - 1L - << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ + 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ #else RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority; @@ -432,7 +415,7 @@ void rt_sched_thread_startup(struct rt_thread *thread) * - Sets initial and remaining time slice ticks */ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, - rt_uint8_t priority) + rt_uint8_t priority) { rt_list_init(&RT_THREAD_LIST_NODE(thread)); @@ -527,9 +510,8 @@ void rt_exit_critical_safe(rt_base_t critical_level) _critical_error_occurred = 1; rt_kprintf("%s: un-compatible critical level\n" - "\tCurrent %d\n\tCaller %d\n", - __func__, rt_scheduler_lock_nest, - critical_level); + "\tCurrent %d\n\tCaller %d\n", + __func__, rt_scheduler_lock_nest, critical_level); rt_backtrace(); while (dummy) From 5bf1ffb4e4f95a981c8b764600c90fb6abfde894 Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 21:09:46 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 101 +++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 67 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 8e463a963f7..a3c4d9773c6 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -70,8 +70,7 @@ static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); * * @param hook is the hook function. */ -void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, - struct rt_thread *to)) +void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) { rt_scheduler_hook = hook; } @@ -95,11 +94,9 @@ rt_inline void _scheduler_update_highest_priority(void) #if RT_THREAD_PRIORITY_MAX > 32 rt_ubase_t number; number = __rt_ffs(rt_thread_ready_priority_group) - 1; - rt_thread_ready_highest_priority = - (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; + rt_thread_ready_highest_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else - rt_thread_ready_highest_priority = - __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } @@ -171,8 +168,7 @@ void rt_system_scheduler_start(void) struct rt_thread *to_thread; _scheduler_update_highest_priority(); - to_thread = - _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); rt_cpu_self()->current_thread = to_thread; @@ -198,29 +194,24 @@ void rt_system_scheduler_start(void) rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { /* READY thread, insert to ready queue */ - RT_SCHED_CTX(thread).stat = - RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); /* there is no time slices left(YIELD), inserting thread before ready list*/ if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - rt_list_insert_before( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); + rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ else { - rt_list_insert_after( - &(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); + rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); } - LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, + LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority); /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= - RT_SCHED_PRIV(thread).high_mask; + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; } @@ -234,20 +225,16 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) */ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) { - LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, - thread->parent.name, + LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority); /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty(&( - rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { + if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= - ~RT_SCHED_PRIV(thread).high_mask; + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) { - rt_thread_ready_priority_group &= - ~RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; } #else rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; @@ -283,24 +270,18 @@ void rt_schedule(void) need_insert_from_thread = RT_FALSE; curr_thread = rt_thread_self(); - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == - RT_THREAD_RUNNING) { - if (RT_SCHED_PRIV(curr_thread).current_priority < - rt_thread_ready_highest_priority) { + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { + if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) { to_thread = curr_thread; - } else if (RT_SCHED_PRIV(curr_thread).current_priority == - rt_thread_ready_highest_priority && - (RT_SCHED_CTX(curr_thread).stat & - RT_THREAD_STAT_YIELD_MASK) == 0) { + } else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) { to_thread = curr_thread; } else { - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); need_insert_from_thread = RT_TRUE; } } else { - to_thread = _scheduler_get_priority_thread( - rt_thread_ready_highest_priority); + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); } if (to_thread != curr_thread) { @@ -316,15 +297,12 @@ void rt_schedule(void) _rt_sched_insert_thread(from_thread); } - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != - 0) { + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; } _rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = - RT_THREAD_RUNNING | - (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); _scheduler_update_highest_priority(); @@ -332,9 +310,8 @@ void rt_schedule(void) LOG_D("[%d]switch to priority#%d " "thread:%.*s(sp:0x%08x), " "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, - to_thread->parent.name, to_thread->sp, RT_NAME_MAX, - from_thread->parent.name, from_thread->sp); + rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, + RT_NAME_MAX, from_thread->parent.name, from_thread->sp); RT_SCHEDULER_STACK_CHECK(to_thread); @@ -343,8 +320,7 @@ void rt_schedule(void) RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); - rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -352,12 +328,10 @@ void rt_schedule(void) #ifdef RT_USING_SIGNALS /* check stat of thread for signal */ level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & - RT_THREAD_STAT_SIGNAL_PENDING) { + if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) { extern void rt_thread_handle_sig(rt_bool_t clean_state); - RT_SCHED_CTX(curr_thread).stat &= - ~RT_THREAD_STAT_SIGNAL_PENDING; + RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; rt_hw_interrupt_enable(level); @@ -371,8 +345,7 @@ void rt_schedule(void) } else { LOG_D("switch in interrupt"); - rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, - (rt_uintptr_t)&to_thread->sp, + rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, from_thread, to_thread); } } @@ -387,14 +360,11 @@ void rt_schedule(void) void rt_sched_thread_startup(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> - 3; /* 5bit */ + RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = - 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ + RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ #else - RT_SCHED_PRIV(thread).number_mask = - 1L << RT_SCHED_PRIV(thread).current_priority; + RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* change thread stat, so we can resume it */ @@ -414,8 +384,7 @@ void rt_sched_thread_startup(struct rt_thread *thread) * - Initializes priority masks (number_mask, number, high_mask for >32 priorities) * - Sets initial and remaining time slice ticks */ -void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, - rt_uint8_t priority) +void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) { rt_list_init(&RT_THREAD_LIST_NODE(thread)); @@ -455,9 +424,7 @@ void rt_sched_insert_thread(struct rt_thread *thread) /* it's current thread, it should be RUNNING thread */ if (thread == rt_current_thread) { - RT_SCHED_CTX(thread).stat = - RT_THREAD_RUNNING | - (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); goto __exit; } From ffdb2ca92373bf3a1ac81867be301d088def4b6b Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 21:16:51 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 510 ++++++++++++++++++++++----------------------- 1 file changed, 255 insertions(+), 255 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index a3c4d9773c6..1bd366433f9 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -72,7 +72,7 @@ static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); */ void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) { - rt_scheduler_hook = hook; + rt_scheduler_hook = hook; } /** @@ -83,7 +83,7 @@ void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread */ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) { - rt_scheduler_switch_hook = hook; + rt_scheduler_switch_hook = hook; } /**@}*/ @@ -92,48 +92,48 @@ void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) rt_inline void _scheduler_update_highest_priority(void) { #if RT_THREAD_PRIORITY_MAX > 32 - rt_ubase_t number; - number = __rt_ffs(rt_thread_ready_priority_group) - 1; - rt_thread_ready_highest_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; + rt_ubase_t number; + number = __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else - rt_thread_ready_highest_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; + rt_thread_ready_highest_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } rt_inline struct rt_thread *_scheduler_get_priority_thread(rt_ubase_t priority) { - /* get highest ready priority thread */ - return RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[priority].next); + /* get highest ready priority thread */ + return RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[priority].next); } rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl) { - rt_base_t level; - if (!plvl) - return -RT_EINVAL; + rt_base_t level; + if (!plvl) + return -RT_EINVAL; - level = rt_hw_interrupt_disable(); - *plvl = level; + level = rt_hw_interrupt_disable(); + *plvl = level; - return RT_EOK; + return RT_EOK; } rt_err_t rt_sched_unlock(rt_sched_lock_level_t level) { - rt_hw_interrupt_enable(level); + rt_hw_interrupt_enable(level); - return RT_EOK; + return RT_EOK; } rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) { - if (rt_thread_self()) { - /* if scheduler is available */ - rt_schedule(); - } - rt_hw_interrupt_enable(level); + if (rt_thread_self()) { + /* if scheduler is available */ + rt_schedule(); + } + rt_hw_interrupt_enable(level); - return RT_EOK; + return RT_EOK; } /** @@ -141,21 +141,21 @@ rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) */ void rt_system_scheduler_init(void) { - rt_base_t offset; - rt_scheduler_lock_nest = 0; + rt_base_t offset; + rt_scheduler_lock_nest = 0; - LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); + LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); - for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) { - rt_list_init(&rt_thread_priority_table[offset]); - } + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) { + rt_list_init(&rt_thread_priority_table[offset]); + } - /* initialize ready priority group */ - rt_thread_ready_priority_group = 0; + /* initialize ready priority group */ + rt_thread_ready_priority_group = 0; #if RT_THREAD_PRIORITY_MAX > 32 - /* initialize ready table */ - rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); + /* initialize ready table */ + rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } @@ -165,22 +165,22 @@ void rt_system_scheduler_init(void) */ void rt_system_scheduler_start(void) { - struct rt_thread *to_thread; + struct rt_thread *to_thread; - _scheduler_update_highest_priority(); - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + _scheduler_update_highest_priority(); + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - rt_cpu_self()->current_thread = to_thread; + rt_cpu_self()->current_thread = to_thread; - rt_sched_remove_thread(to_thread); + rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; - /* switch to new thread */ + /* switch to new thread */ - rt_hw_context_switch_to((rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch_to((rt_uintptr_t)&to_thread->sp); - /* never come back */ + /* never come back */ } /** @@ -193,27 +193,27 @@ void rt_system_scheduler_start(void) */ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) { - /* READY thread, insert to ready queue */ - RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - /* there is no time slices left(YIELD), inserting thread before ready list*/ - if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ - else { - rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } - - LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); - - /* set priority mask */ + /* READY thread, insert to ready queue */ + RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + /* there is no time slices left(YIELD), inserting thread before ready list*/ + if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { + rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ + else { + rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } + + LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); + + /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask; + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask; } /** @@ -225,21 +225,21 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) */ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) { - LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, - RT_SCHED_PRIV(rt_current_thread).current_priority); + LOG_D("remove thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, + RT_SCHED_PRIV(rt_current_thread).current_priority); - /* remove thread from ready list */ - rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { + /* remove thread from ready list */ + rt_list_remove(&RT_THREAD_LIST_NODE(thread)); + if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; - if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) { - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; - } + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; + if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) { + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; + } #else - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - } + } } /** @@ -255,120 +255,120 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) */ void rt_schedule(void) { - rt_base_t level; - /* need_insert_from_thread: need to insert from_thread to ready queue */ - rt_bool_t need_insert_from_thread; - /* using local variable to avoid unecessary function call */ - struct rt_thread *curr_thread; - struct rt_thread *to_thread; - struct rt_thread *from_thread; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - /* check the scheduler is enabled or not */ - if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { - need_insert_from_thread = RT_FALSE; - curr_thread = rt_thread_self(); - - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { - if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) { - to_thread = curr_thread; - } else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && - (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) { - to_thread = curr_thread; - } else { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - need_insert_from_thread = RT_TRUE; - } - } else { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - } - - if (to_thread != curr_thread) { - /* if the destination thread is not the same as current thread */ - rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; - from_thread = curr_thread; - rt_cpu_self()->current_thread = to_thread; - - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); - - if (need_insert_from_thread) { - _rt_sched_remove_thread(from_thread); - _rt_sched_insert_thread(from_thread); - } - - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; - } - - _rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); - - _scheduler_update_highest_priority(); - - /* switch to new thread */ - LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, - RT_NAME_MAX, from_thread->parent.name, from_thread->sp); - - RT_SCHEDULER_STACK_CHECK(to_thread); - - if (rt_interrupt_nest == 0) { - extern void rt_thread_handle_sig(rt_bool_t clean_state); - - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); - - rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); - - /* enable interrupt */ - rt_hw_interrupt_enable(level); + rt_base_t level; + /* need_insert_from_thread: need to insert from_thread to ready queue */ + rt_bool_t need_insert_from_thread; + /* using local variable to avoid unecessary function call */ + struct rt_thread *curr_thread; + struct rt_thread *to_thread; + struct rt_thread *from_thread; + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + /* check the scheduler is enabled or not */ + if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { + need_insert_from_thread = RT_FALSE; + curr_thread = rt_thread_self(); + + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { + if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) { + to_thread = curr_thread; + } else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) { + to_thread = curr_thread; + } else { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + need_insert_from_thread = RT_TRUE; + } + } else { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + } + + if (to_thread != curr_thread) { + /* if the destination thread is not the same as current thread */ + rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; + from_thread = curr_thread; + rt_cpu_self()->current_thread = to_thread; + + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); + + if (need_insert_from_thread) { + _rt_sched_remove_thread(from_thread); + _rt_sched_insert_thread(from_thread); + } + + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { + RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; + } + + _rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); + + _scheduler_update_highest_priority(); + + /* switch to new thread */ + LOG_D("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, + RT_NAME_MAX, from_thread->parent.name, from_thread->sp); + + RT_SCHEDULER_STACK_CHECK(to_thread); + + if (rt_interrupt_nest == 0) { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + + rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); + + /* enable interrupt */ + rt_hw_interrupt_enable(level); #ifdef RT_USING_SIGNALS - /* check stat of thread for signal */ - level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) { - extern void rt_thread_handle_sig(rt_bool_t clean_state); + /* check stat of thread for signal */ + level = rt_hw_interrupt_disable(); + if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) { + extern void rt_thread_handle_sig(rt_bool_t clean_state); - RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; - rt_hw_interrupt_enable(level); + rt_hw_interrupt_enable(level); - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); - } else { - rt_hw_interrupt_enable(level); - } + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } else { + rt_hw_interrupt_enable(level); + } #endif /* RT_USING_SIGNALS */ - goto __exit; - } else { - LOG_D("switch in interrupt"); - - rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, - from_thread, to_thread); - } - } - } - /* enable interrupt */ - rt_hw_interrupt_enable(level); + goto __exit; + } else { + LOG_D("switch in interrupt"); + + rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); + } + } + } + /* enable interrupt */ + rt_hw_interrupt_enable(level); __exit: - return; + return; } /* Normally, there isn't anyone racing with us so this operation is lockless */ void rt_sched_thread_startup(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ - RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ + RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ + RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number; + RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ #else - RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority; + RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - /* change thread stat, so we can resume it */ - RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; + /* change thread stat, so we can resume it */ + RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND; } /** @@ -386,23 +386,23 @@ void rt_sched_thread_startup(struct rt_thread *thread) */ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) { - rt_list_init(&RT_THREAD_LIST_NODE(thread)); + rt_list_init(&RT_THREAD_LIST_NODE(thread)); - /* priority init */ - RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); - RT_SCHED_PRIV(thread).init_priority = priority; - RT_SCHED_PRIV(thread).current_priority = priority; + /* priority init */ + RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); + RT_SCHED_PRIV(thread).init_priority = priority; + RT_SCHED_PRIV(thread).current_priority = priority; - /* don't add to scheduler queue as init thread */ - RT_SCHED_PRIV(thread).number_mask = 0; + /* don't add to scheduler queue as init thread */ + RT_SCHED_PRIV(thread).number_mask = 0; #if RT_THREAD_PRIORITY_MAX > 32 - RT_SCHED_PRIV(thread).number = 0; - RT_SCHED_PRIV(thread).high_mask = 0; + RT_SCHED_PRIV(thread).number = 0; + RT_SCHED_PRIV(thread).high_mask = 0; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - /* tick init */ - RT_SCHED_PRIV(thread).init_tick = tick; - RT_SCHED_PRIV(thread).remaining_tick = tick; + /* tick init */ + RT_SCHED_PRIV(thread).init_tick = tick; + RT_SCHED_PRIV(thread).remaining_tick = tick; } /** @@ -415,26 +415,26 @@ void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_ui */ void rt_sched_insert_thread(struct rt_thread *thread) { - rt_base_t level; + rt_base_t level; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - /* it's current thread, it should be RUNNING thread */ - if (thread == rt_current_thread) { - RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - goto __exit; - } + /* it's current thread, it should be RUNNING thread */ + if (thread == rt_current_thread) { + RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + goto __exit; + } - _rt_sched_insert_thread(thread); + _rt_sched_insert_thread(thread); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); __exit: - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); } /** @@ -446,19 +446,19 @@ void rt_sched_insert_thread(struct rt_thread *thread) */ void rt_sched_remove_thread(struct rt_thread *thread) { - rt_base_t level; + rt_base_t level; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - _rt_sched_remove_thread(thread); + _rt_sched_remove_thread(thread); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); } #ifdef RT_DEBUGING_CRITICAL @@ -467,34 +467,34 @@ static volatile int _critical_error_occurred = 0; void rt_exit_critical_safe(rt_base_t critical_level) { - rt_base_t level; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - if (!_critical_error_occurred) { - if (critical_level != rt_scheduler_lock_nest) { - int dummy = 1; - _critical_error_occurred = 1; - - rt_kprintf("%s: un-compatible critical level\n" - "\tCurrent %d\n\tCaller %d\n", - __func__, rt_scheduler_lock_nest, critical_level); - rt_backtrace(); - - while (dummy) - ; - } - } - rt_hw_interrupt_enable(level); - - rt_exit_critical(); + rt_base_t level; + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (!_critical_error_occurred) { + if (critical_level != rt_scheduler_lock_nest) { + int dummy = 1; + _critical_error_occurred = 1; + + rt_kprintf("%s: un-compatible critical level\n" + "\tCurrent %d\n\tCaller %d\n", + __func__, rt_scheduler_lock_nest, critical_level); + rt_backtrace(); + + while (dummy) + ; + } + } + rt_hw_interrupt_enable(level); + + rt_exit_critical(); } #else /* !RT_DEBUGING_CRITICAL */ void rt_exit_critical_safe(rt_base_t critical_level) { - rt_exit_critical(); + rt_exit_critical(); } #endif /* RT_DEBUGING_CRITICAL */ @@ -505,22 +505,22 @@ RTM_EXPORT(rt_exit_critical_safe); */ rt_base_t rt_enter_critical(void) { - rt_base_t level; - rt_base_t critical_level; + rt_base_t level; + rt_base_t critical_level; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - /* + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + /* * the maximal number of nest is RT_UINT16_MAX, which is big * enough and does not check here */ - ++rt_scheduler_lock_nest; - critical_level = rt_scheduler_lock_nest; + ++rt_scheduler_lock_nest; + critical_level = rt_scheduler_lock_nest; - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); - return critical_level; + return critical_level; } RTM_EXPORT(rt_enter_critical); @@ -529,25 +529,25 @@ RTM_EXPORT(rt_enter_critical); */ void rt_exit_critical(void) { - rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - --rt_scheduler_lock_nest; - if (rt_scheduler_lock_nest <= 0) { - rt_scheduler_lock_nest = 0; - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - if (rt_current_thread) { - /* if scheduler is started, do a schedule */ - rt_schedule(); - } - } else { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } + rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + --rt_scheduler_lock_nest; + if (rt_scheduler_lock_nest <= 0) { + rt_scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + if (rt_current_thread) { + /* if scheduler is started, do a schedule */ + rt_schedule(); + } + } else { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } } RTM_EXPORT(rt_exit_critical); @@ -558,13 +558,13 @@ RTM_EXPORT(rt_exit_critical); */ rt_uint16_t rt_critical_level(void) { - return rt_scheduler_lock_nest; + return rt_scheduler_lock_nest; } RTM_EXPORT(rt_critical_level); rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu) { - return -RT_EINVAL; + return -RT_EINVAL; } /**@}*/ From b0fb8093c269f6c297b43b1b7eeef41a815bf66a Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Tue, 10 Jun 2025 21:33:52 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E6=89=8B=E5=8A=A8=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index 1bd366433f9..eefa33e4c9a 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -310,7 +310,8 @@ void rt_schedule(void) LOG_D("[%d]switch to priority#%d " "thread:%.*s(sp:0x%08x), " "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, + rt_interrupt_nest, highest_ready_priority, + RT_NAME_MAX, to_thread->parent.name, to_thread->sp, RT_NAME_MAX, from_thread->parent.name, from_thread->sp); RT_SCHEDULER_STACK_CHECK(to_thread); From 97d49e93dca64169d52a345c063c058519873d2b Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Wed, 11 Jun 2025 13:02:22 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E5=9C=A8=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 267 +++++++++++++++++++++++++-------------------- 1 file changed, 149 insertions(+), 118 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index eefa33e4c9a..f0088d7c94f 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -127,10 +127,11 @@ rt_err_t rt_sched_unlock(rt_sched_lock_level_t level) rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) { - if (rt_thread_self()) { - /* if scheduler is available */ - rt_schedule(); - } + if (rt_thread_self()) + { + /* if scheduler is available */ + rt_schedule(); + } rt_hw_interrupt_enable(level); return RT_EOK; @@ -146,9 +147,10 @@ void rt_system_scheduler_init(void) LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); - for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) { - rt_list_init(&rt_thread_priority_table[offset]); - } + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) + { + rt_list_init(&rt_thread_priority_table[offset]); + } /* initialize ready priority group */ rt_thread_ready_priority_group = 0; @@ -196,15 +198,17 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) /* READY thread, insert to ready queue */ RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); /* there is no time slices left(YIELD), inserting thread before ready list*/ - if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } + if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) + { + rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ - else { - rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } + else + { + rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority); @@ -230,16 +234,18 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); - if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) { + if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) + { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; - if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) { - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; - } + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; + if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) + { + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; + } #else - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - } + } } /** @@ -266,91 +272,109 @@ void rt_schedule(void) level = rt_hw_interrupt_disable(); /* check the scheduler is enabled or not */ - if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) { - need_insert_from_thread = RT_FALSE; - curr_thread = rt_thread_self(); - - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { - if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) { - to_thread = curr_thread; - } else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && - (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) { - to_thread = curr_thread; - } else { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - need_insert_from_thread = RT_TRUE; - } - } else { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - } + if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) + { + need_insert_from_thread = RT_FALSE; + curr_thread = rt_thread_self(); + + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) + { + if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) + { + to_thread = curr_thread; + } + else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) + { + to_thread = curr_thread; + } + else + { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + need_insert_from_thread = RT_TRUE; + } + } + else + { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + } - if (to_thread != curr_thread) { - /* if the destination thread is not the same as current thread */ - rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; - from_thread = curr_thread; - rt_cpu_self()->current_thread = to_thread; + if (to_thread != curr_thread) + { + /* if the destination thread is not the same as current thread */ + rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; + from_thread = curr_thread; + rt_cpu_self()->current_thread = to_thread; - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); - if (need_insert_from_thread) { - _rt_sched_remove_thread(from_thread); - _rt_sched_insert_thread(from_thread); - } + if (need_insert_from_thread) + { + _rt_sched_remove_thread(from_thread); + _rt_sched_insert_thread(from_thread); + } - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) { - RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; - } + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) + { + RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; + } - _rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); + _rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | + (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); - /* switch to new thread */ - LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, - RT_NAME_MAX, to_thread->parent.name, to_thread->sp, - RT_NAME_MAX, from_thread->parent.name, from_thread->sp); + /* switch to new thread */ + LOG_D("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, + RT_NAME_MAX, from_thread->parent.name, from_thread->sp); - RT_SCHEDULER_STACK_CHECK(to_thread); + RT_SCHEDULER_STACK_CHECK(to_thread); - if (rt_interrupt_nest == 0) { - extern void rt_thread_handle_sig(rt_bool_t clean_state); + if (rt_interrupt_nest == 0) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); - rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); #ifdef RT_USING_SIGNALS - /* check stat of thread for signal */ - level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) { - extern void rt_thread_handle_sig(rt_bool_t clean_state); - - RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; - - rt_hw_interrupt_enable(level); - - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); - } else { - rt_hw_interrupt_enable(level); - } + /* check stat of thread for signal */ + level = rt_hw_interrupt_disable(); + if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + + rt_hw_interrupt_enable(level); + + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } + else + { + rt_hw_interrupt_enable(level); + } #endif /* RT_USING_SIGNALS */ - goto __exit; - } else { - LOG_D("switch in interrupt"); - - rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, - from_thread, to_thread); - } + goto __exit; + } + else + { + LOG_D("switch in interrupt"); + + rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); + } + } } - } /* enable interrupt */ rt_hw_interrupt_enable(level); __exit: @@ -424,10 +448,11 @@ void rt_sched_insert_thread(struct rt_thread *thread) level = rt_hw_interrupt_disable(); /* it's current thread, it should be RUNNING thread */ - if (thread == rt_current_thread) { - RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - goto __exit; - } + if (thread == rt_current_thread) + { + RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + goto __exit; + } _rt_sched_insert_thread(thread); @@ -472,20 +497,22 @@ void rt_exit_critical_safe(rt_base_t critical_level) /* disable interrupt */ level = rt_hw_interrupt_disable(); - if (!_critical_error_occurred) { - if (critical_level != rt_scheduler_lock_nest) { - int dummy = 1; - _critical_error_occurred = 1; + if (!_critical_error_occurred) + { + if (critical_level != rt_scheduler_lock_nest) + { + int dummy = 1; + _critical_error_occurred = 1; - rt_kprintf("%s: un-compatible critical level\n" - "\tCurrent %d\n\tCaller %d\n", - __func__, rt_scheduler_lock_nest, critical_level); - rt_backtrace(); + rt_kprintf("%s: un-compatible critical level\n" + "\tCurrent %d\n\tCaller %d\n", + __func__, rt_scheduler_lock_nest, critical_level); + rt_backtrace(); - while (dummy) - ; + while (dummy) + ; + } } - } rt_hw_interrupt_enable(level); rt_exit_critical(); @@ -536,19 +563,23 @@ void rt_exit_critical(void) level = rt_hw_interrupt_disable(); --rt_scheduler_lock_nest; - if (rt_scheduler_lock_nest <= 0) { - rt_scheduler_lock_nest = 0; - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - if (rt_current_thread) { - /* if scheduler is started, do a schedule */ - rt_schedule(); + if (rt_scheduler_lock_nest <= 0) + { + rt_scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + if (rt_current_thread) + { + /* if scheduler is started, do a schedule */ + rt_schedule(); + } + } + else + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); } - } else { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } } RTM_EXPORT(rt_exit_critical); From 52cab878a12cd0ac44a0e175376cf99f1dcecbfe Mon Sep 17 00:00:00 2001 From: yueling hu <502966985@qq.com> Date: Wed, 11 Jun 2025 13:06:28 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E5=9C=A8=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scheduler_up.c | 269 ++++++++++++++++++++++----------------------- 1 file changed, 134 insertions(+), 135 deletions(-) diff --git a/src/scheduler_up.c b/src/scheduler_up.c index f0088d7c94f..715982e9e2e 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -128,10 +128,10 @@ rt_err_t rt_sched_unlock(rt_sched_lock_level_t level) rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level) { if (rt_thread_self()) - { - /* if scheduler is available */ - rt_schedule(); - } + { + /* if scheduler is available */ + rt_schedule(); + } rt_hw_interrupt_enable(level); return RT_EOK; @@ -148,9 +148,9 @@ void rt_system_scheduler_init(void) LOG_D("start scheduler: max priority 0x%02x", RT_THREAD_PRIORITY_MAX); for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; ++offset) - { - rt_list_init(&rt_thread_priority_table[offset]); - } + { + rt_list_init(&rt_thread_priority_table[offset]); + } /* initialize ready priority group */ rt_thread_ready_priority_group = 0; @@ -199,16 +199,16 @@ rt_inline void _rt_sched_insert_thread(struct rt_thread *thread) RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); /* there is no time slices left(YIELD), inserting thread before ready list*/ if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } + { + rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/ else - { - rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), - &RT_THREAD_LIST_NODE(thread)); - } + { + rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]), + &RT_THREAD_LIST_NODE(thread)); + } LOG_D("insert thread[%.*s], the priority: %d", RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority); @@ -235,17 +235,17 @@ rt_inline void _rt_sched_remove_thread(struct rt_thread *thread) /* remove thread from ready list */ rt_list_remove(&RT_THREAD_LIST_NODE(thread)); if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]))) - { + { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; - if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) - { - rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; - } -#else + rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask; + if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0) + { rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ } +#else + rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + } } /** @@ -273,108 +273,107 @@ void rt_schedule(void) /* check the scheduler is enabled or not */ if (rt_scheduler_lock_nest == 0 && rt_thread_ready_priority_group) - { - need_insert_from_thread = RT_FALSE; - curr_thread = rt_thread_self(); + { + need_insert_from_thread = RT_FALSE; + curr_thread = rt_thread_self(); - if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) - { - if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) - { - to_thread = curr_thread; - } - else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && - (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) - { - to_thread = curr_thread; - } - else - { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - need_insert_from_thread = RT_TRUE; - } - } + if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) + { + if (RT_SCHED_PRIV(curr_thread).current_priority < rt_thread_ready_highest_priority) + { + to_thread = curr_thread; + } + else if (RT_SCHED_PRIV(curr_thread).current_priority == rt_thread_ready_highest_priority && + (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0) + { + to_thread = curr_thread; + } else - { - to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); - } + { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + need_insert_from_thread = RT_TRUE; + } + } + else + { + to_thread = _scheduler_get_priority_thread(rt_thread_ready_highest_priority); + } - if (to_thread != curr_thread) - { - /* if the destination thread is not the same as current thread */ - rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; - from_thread = curr_thread; - rt_cpu_self()->current_thread = to_thread; + if (to_thread != curr_thread) + { + /* if the destination thread is not the same as current thread */ + rt_current_priority = (rt_uint8_t)rt_thread_ready_highest_priority; + from_thread = curr_thread; + rt_cpu_self()->current_thread = to_thread; - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); - if (need_insert_from_thread) - { - _rt_sched_remove_thread(from_thread); - _rt_sched_insert_thread(from_thread); - } + if (need_insert_from_thread) + { + _rt_sched_remove_thread(from_thread); + _rt_sched_insert_thread(from_thread); + } - if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) - { - RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; - } + if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0) + { + RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK; + } - _rt_sched_remove_thread(to_thread); - RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | - (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); + _rt_sched_remove_thread(to_thread); + RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK); - _scheduler_update_highest_priority(); + _scheduler_update_highest_priority(); - /* switch to new thread */ - LOG_D("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)", - rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, - RT_NAME_MAX, from_thread->parent.name, from_thread->sp); + /* switch to new thread */ + LOG_D("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)", + rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->parent.name, to_thread->sp, + RT_NAME_MAX, from_thread->parent.name, from_thread->sp); - RT_SCHEDULER_STACK_CHECK(to_thread); + RT_SCHEDULER_STACK_CHECK(to_thread); - if (rt_interrupt_nest == 0) - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); + if (rt_interrupt_nest == 0) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); - RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); - rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); + rt_hw_context_switch((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); #ifdef RT_USING_SIGNALS - /* check stat of thread for signal */ - level = rt_hw_interrupt_disable(); - if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); - - RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; - - rt_hw_interrupt_enable(level); - - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); - } - else - { - rt_hw_interrupt_enable(level); - } -#endif /* RT_USING_SIGNALS */ - goto __exit; - } - else - { - LOG_D("switch in interrupt"); - - rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, - from_thread, to_thread); - } + /* check stat of thread for signal */ + level = rt_hw_interrupt_disable(); + if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + + rt_hw_interrupt_enable(level); + + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } + else + { + rt_hw_interrupt_enable(level); } +#endif /* RT_USING_SIGNALS */ + goto __exit; + } + else + { + LOG_D("switch in interrupt"); + + rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp, (rt_uintptr_t)&to_thread->sp, + from_thread, to_thread); + } } + } /* enable interrupt */ rt_hw_interrupt_enable(level); __exit: @@ -449,10 +448,10 @@ void rt_sched_insert_thread(struct rt_thread *thread) /* it's current thread, it should be RUNNING thread */ if (thread == rt_current_thread) - { - RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); - goto __exit; - } + { + RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); + goto __exit; + } _rt_sched_insert_thread(thread); @@ -498,21 +497,21 @@ void rt_exit_critical_safe(rt_base_t critical_level) level = rt_hw_interrupt_disable(); if (!_critical_error_occurred) + { + if (critical_level != rt_scheduler_lock_nest) { - if (critical_level != rt_scheduler_lock_nest) - { - int dummy = 1; - _critical_error_occurred = 1; + int dummy = 1; + _critical_error_occurred = 1; - rt_kprintf("%s: un-compatible critical level\n" - "\tCurrent %d\n\tCaller %d\n", - __func__, rt_scheduler_lock_nest, critical_level); - rt_backtrace(); + rt_kprintf("%s: un-compatible critical level\n" + "\tCurrent %d\n\tCaller %d\n", + __func__, rt_scheduler_lock_nest, critical_level); + rt_backtrace(); - while (dummy) - ; - } + while (dummy) + ; } + } rt_hw_interrupt_enable(level); rt_exit_critical(); @@ -564,22 +563,22 @@ void rt_exit_critical(void) --rt_scheduler_lock_nest; if (rt_scheduler_lock_nest <= 0) - { - rt_scheduler_lock_nest = 0; - /* enable interrupt */ - rt_hw_interrupt_enable(level); + { + rt_scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_interrupt_enable(level); - if (rt_current_thread) - { - /* if scheduler is started, do a schedule */ - rt_schedule(); - } - } - else + if (rt_current_thread) { - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* if scheduler is started, do a schedule */ + rt_schedule(); } + } + else + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } } RTM_EXPORT(rt_exit_critical);