From e9b8e0ba06d23302c04b2d21eec61fde5de67c34 Mon Sep 17 00:00:00 2001 From: wycwyhwyq <5f20.6d9b@gmail.com> Date: Thu, 27 Feb 2025 17:46:32 +0800 Subject: [PATCH 1/3] [src] fix mutex bug --- include/rtsched.h | 1 + src/scheduler_comm.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/thread.c | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/include/rtsched.h b/include/rtsched.h index 01951a2611e..5aab9624a31 100644 --- a/include/rtsched.h +++ b/include/rtsched.h @@ -128,6 +128,7 @@ rt_err_t rt_sched_thread_yield(struct rt_thread *thread); rt_err_t rt_sched_thread_close(struct rt_thread *thread); rt_err_t rt_sched_thread_ready(struct rt_thread *thread); rt_err_t rt_sched_thread_suspend(struct rt_thread *thread, rt_sched_lock_level_t level); +rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t priority); rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority); rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu); rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread); diff --git a/src/scheduler_comm.c b/src/scheduler_comm.c index 957f1a132e8..889c6070cf1 100644 --- a/src/scheduler_comm.c +++ b/src/scheduler_comm.c @@ -177,6 +177,55 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick) return RT_EOK; } +/** + * @brief Set priority of the target thread + */ +rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t priority) +{ + RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); + RT_SCHED_DEBUG_IS_LOCKED; + + /* for ready thread, change queue; otherwise simply update the priority */ + if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + { + /* remove thread from schedule queue first */ + rt_sched_remove_thread(thread); + + /* change thread priority */ + RT_SCHED_PRIV(thread).init_priority = priority; + RT_SCHED_PRIV(thread).current_priority = priority; + + /* recalculate priority attribute */ +#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 = 1 << RT_SCHED_PRIV(thread).number; + RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ +#else + RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + RT_SCHED_CTX(thread).stat = RT_THREAD_INIT; + + /* insert thread to schedule queue again */ + rt_sched_insert_thread(thread); + } + else + { + RT_SCHED_PRIV(thread).init_priority = priority; + RT_SCHED_PRIV(thread).current_priority = priority; + + /* recalculate priority attribute */ +#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 = 1 << RT_SCHED_PRIV(thread).number; + RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ +#else + RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ + } + + return RT_EOK; +} + /** * @brief Update priority of the target thread */ diff --git a/src/thread.c b/src/thread.c index 4a913eec92d..76d75ccb1f3 100644 --- a/src/thread.c +++ b/src/thread.c @@ -797,7 +797,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) rt_err_t error; rt_sched_lock_level_t slvl; rt_sched_lock(&slvl); - error = rt_sched_thread_change_priority(thread, *(rt_uint8_t *)arg); + error = rt_sched_thread_set_priority(thread, *(rt_uint8_t *)arg); rt_sched_unlock(slvl); return error; } From 0a6da69aa2c3f76d9ac2e5b4b138971d20187641 Mon Sep 17 00:00:00 2001 From: wycwyhwyq <5f20.6d9b@gmail.com> Date: Wed, 12 Mar 2025 18:17:35 +0800 Subject: [PATCH 2/3] add RT_THREAD_CTRL_SET_PRIORITY --- components/lwp/lwp_syscall.c | 6 +++--- include/rtdef.h | 1 + src/thread.c | 12 ++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 429861f50e5..4e171ce07d5 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -1634,7 +1634,7 @@ sysret_t sys_setpriority(int which, id_t who, int prio) for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next) { thread = rt_list_entry(list, struct rt_thread, sibling); - rt_thread_control(thread, RT_THREAD_CTRL_CHANGE_PRIORITY, &prio); + rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, &prio); } lwp_pid_lock_release(); return 0; @@ -8789,7 +8789,7 @@ sysret_t sys_sched_setparam(pid_t tid, void *param) if (thread) { - ret = rt_thread_control(thread, RT_THREAD_CTRL_CHANGE_PRIORITY, (void *)&sched_param->sched_priority); + ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority); } lwp_tid_dec_ref(thread); @@ -8959,7 +8959,7 @@ sysret_t sys_sched_setscheduler(int tid, int policy, void *param) } thread = lwp_tid_get_thread_and_inc_ref(tid); - ret = rt_thread_control(thread, RT_THREAD_CTRL_CHANGE_PRIORITY, (void *)&sched_param->sched_priority); + ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority); lwp_tid_dec_ref(thread); kmem_put(sched_param); diff --git a/include/rtdef.h b/include/rtdef.h index b1f8e48c6f9..45d1c16227f 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -643,6 +643,7 @@ enum #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ #define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */ +#define RT_THREAD_CTRL_SET_PRIORITY 0x05 /**< Set thread priority. */ /** * CPU usage statistics data diff --git a/src/thread.c b/src/thread.c index 76d75ccb1f3..fb4d2af3d39 100644 --- a/src/thread.c +++ b/src/thread.c @@ -779,6 +779,8 @@ RTM_EXPORT(rt_thread_mdelay); * * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. * + * RT_THREAD_CTRL_SET_PRIORITY for set priority level of thread. + * * @param arg is the argument of control command. * * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. @@ -793,6 +795,16 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) switch (cmd) { case RT_THREAD_CTRL_CHANGE_PRIORITY: + { + rt_err_t error; + rt_sched_lock_level_t slvl; + rt_sched_lock(&slvl); + error = rt_sched_thread_change_priority(thread, *(rt_uint8_t *)arg); + rt_sched_unlock(slvl); + return error; + } + + case RT_THREAD_CTRL_SET_PRIORITY: { rt_err_t error; rt_sched_lock_level_t slvl; From b1aeea9f85b1909e8c7600c42c92877f26d38aa8 Mon Sep 17 00:00:00 2001 From: wycwyhwyq <5f20.6d9b@gmail.com> Date: Thu, 10 Apr 2025 10:20:21 +0800 Subject: [PATCH 3/3] add RT_THREAD_CTRL_SET_PRIORITY --- components/lwp/lwp_syscall.c | 6 ++-- include/rtdef.h | 2 +- include/rtsched.h | 2 +- src/scheduler_comm.c | 61 +++++++++++------------------------- src/thread.c | 6 ++-- 5 files changed, 26 insertions(+), 51 deletions(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 4e171ce07d5..6efc3a0e163 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -1634,7 +1634,7 @@ sysret_t sys_setpriority(int which, id_t who, int prio) for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next) { thread = rt_list_entry(list, struct rt_thread, sibling); - rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, &prio); + rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, &prio); } lwp_pid_lock_release(); return 0; @@ -8789,7 +8789,7 @@ sysret_t sys_sched_setparam(pid_t tid, void *param) if (thread) { - ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority); + ret = rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, (void *)&sched_param->sched_priority); } lwp_tid_dec_ref(thread); @@ -8959,7 +8959,7 @@ sysret_t sys_sched_setscheduler(int tid, int policy, void *param) } thread = lwp_tid_get_thread_and_inc_ref(tid); - ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority); + ret = rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, (void *)&sched_param->sched_priority); lwp_tid_dec_ref(thread); kmem_put(sched_param); diff --git a/include/rtdef.h b/include/rtdef.h index 45d1c16227f..1cad582286a 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -643,7 +643,7 @@ enum #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ #define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */ -#define RT_THREAD_CTRL_SET_PRIORITY 0x05 /**< Set thread priority. */ +#define RT_THREAD_CTRL_RESET_PRIORITY 0x05 /**< Reset thread priority. */ /** * CPU usage statistics data diff --git a/include/rtsched.h b/include/rtsched.h index 5aab9624a31..5bafea93eb2 100644 --- a/include/rtsched.h +++ b/include/rtsched.h @@ -128,8 +128,8 @@ rt_err_t rt_sched_thread_yield(struct rt_thread *thread); rt_err_t rt_sched_thread_close(struct rt_thread *thread); rt_err_t rt_sched_thread_ready(struct rt_thread *thread); rt_err_t rt_sched_thread_suspend(struct rt_thread *thread, rt_sched_lock_level_t level); -rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t priority); rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority); +rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t priority); rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu); rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread); rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread); diff --git a/src/scheduler_comm.c b/src/scheduler_comm.c index 889c6070cf1..c4e3e7a1660 100644 --- a/src/scheduler_comm.c +++ b/src/scheduler_comm.c @@ -178,9 +178,9 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick) } /** - * @brief Set priority of the target thread + * @brief Update priority of the target thread */ -rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t priority) +static rt_err_t _rt_sched_update_priority(struct rt_thread *thread, rt_uint8_t priority, rt_bool_t update_init_prio) { RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); RT_SCHED_DEBUG_IS_LOCKED; @@ -192,7 +192,10 @@ rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t prior rt_sched_remove_thread(thread); /* change thread priority */ - RT_SCHED_PRIV(thread).init_priority = priority; + if (update_init_prio) + { + RT_SCHED_PRIV(thread).init_priority = priority; + } RT_SCHED_PRIV(thread).current_priority = priority; /* recalculate priority attribute */ @@ -210,7 +213,10 @@ rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t prior } else { - RT_SCHED_PRIV(thread).init_priority = priority; + if (update_init_prio) + { + RT_SCHED_PRIV(thread).init_priority = priority; + } RT_SCHED_PRIV(thread).current_priority = priority; /* recalculate priority attribute */ @@ -231,46 +237,15 @@ rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t prior */ rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority) { - RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); - RT_SCHED_DEBUG_IS_LOCKED; - - /* for ready thread, change queue; otherwise simply update the priority */ - if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) - { - /* remove thread from schedule queue first */ - rt_sched_remove_thread(thread); - - /* change thread priority */ - RT_SCHED_PRIV(thread).current_priority = priority; - - /* recalculate priority attribute */ -#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 = 1 << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ -#else - RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - RT_SCHED_CTX(thread).stat = RT_THREAD_INIT; - - /* insert thread to schedule queue again */ - rt_sched_insert_thread(thread); - } - else - { - RT_SCHED_PRIV(thread).current_priority = priority; - - /* recalculate priority attribute */ -#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 = 1 << RT_SCHED_PRIV(thread).number; - RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ -#else - RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - } + return _rt_sched_update_priority(thread, priority, RT_FALSE); +} - return RT_EOK; +/** + * @brief Reset priority of the target thread + */ +rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t priority) +{ + return _rt_sched_update_priority(thread, priority, RT_TRUE); } #ifdef RT_USING_OVERFLOW_CHECK diff --git a/src/thread.c b/src/thread.c index fb4d2af3d39..f7a21330121 100644 --- a/src/thread.c +++ b/src/thread.c @@ -779,7 +779,7 @@ RTM_EXPORT(rt_thread_mdelay); * * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. * - * RT_THREAD_CTRL_SET_PRIORITY for set priority level of thread. + * RT_THREAD_CTRL_RESET_PRIORITY for reset priority level of thread. * * @param arg is the argument of control command. * @@ -804,12 +804,12 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) return error; } - case RT_THREAD_CTRL_SET_PRIORITY: + case RT_THREAD_CTRL_RESET_PRIORITY: { rt_err_t error; rt_sched_lock_level_t slvl; rt_sched_lock(&slvl); - error = rt_sched_thread_set_priority(thread, *(rt_uint8_t *)arg); + error = rt_sched_thread_reset_priority(thread, *(rt_uint8_t *)arg); rt_sched_unlock(slvl); return error; }