From f13193ff92e47c39436e0254062927a39450e04a Mon Sep 17 00:00:00 2001 From: wycwyhwyq <5f20.6d9b@gmail.com> Date: Thu, 27 Feb 2025 17:46:32 +0800 Subject: [PATCH] [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 01951a2611..5aab9624a3 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 957f1a132e..889c6070cf 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 789077d082..09cba18131 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; }