From b1b54629dc5bfc08b0b54380cb675c5d34850ee5 Mon Sep 17 00:00:00 2001 From: zhao maosheng Date: Wed, 28 Aug 2024 16:04:07 +0800 Subject: [PATCH] update change prio func --- include/rtsched.h | 1 + src/ipc.c | 6 +++--- src/scheduler_comm.c | 22 +++++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/rtsched.h b/include/rtsched.h index d650e09e379..38b6f5d3e4b 100644 --- a/include/rtsched.h +++ b/include/rtsched.h @@ -168,6 +168,7 @@ 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_change_priority(struct rt_thread *thread, rt_uint8_t priority); +rt_err_t rt_sched_thread_change_curr_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/ipc.c b/src/ipc.c index 5ffcf017c2c..190a940e031 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -873,7 +873,7 @@ rt_inline void _thread_update_priority(struct rt_thread *thread, rt_uint8_t prio LOG_D("thread:%s priority -> %d", thread->parent.name, priority); /* change priority of the thread */ - ret = rt_sched_thread_change_priority(thread, priority); + ret = rt_sched_thread_change_curr_priority(thread, priority); while ((ret == RT_EOK) && rt_sched_thread_is_suspended(thread)) { @@ -904,7 +904,7 @@ rt_inline void _thread_update_priority(struct rt_thread *thread, rt_uint8_t prio { thread = pending_mutex->owner; - ret = rt_sched_thread_change_priority(thread, mutex_priority); + ret = rt_sched_thread_change_curr_priority(thread, mutex_priority); } else { @@ -931,7 +931,7 @@ static rt_bool_t _check_and_update_prio(rt_thread_t thread, rt_mutex_t mutex) /* get the highest priority in the taken list of thread */ priority = _thread_get_mutex_priority(thread); - rt_sched_thread_change_priority(thread, priority); + rt_sched_thread_change_curr_priority(thread, priority); /** * notify a pending reschedule. Since scheduler is locked, we will not diff --git a/src/scheduler_comm.c b/src/scheduler_comm.c index 4e5495e727c..b0d73818c3b 100644 --- a/src/scheduler_comm.c +++ b/src/scheduler_comm.c @@ -180,7 +180,7 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick) /** * @brief Update priority of the target thread */ -rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority) +rt_err_t rt_sched_thread_change_curr_priority(struct rt_thread *thread, rt_uint8_t priority) { RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); RT_SCHED_DEBUG_IS_LOCKED; @@ -224,6 +224,26 @@ rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t pr return RT_EOK; } +/** + * @brief Update priority of the target thread + */ +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; + + /* change thread init priority */ + RT_SCHED_PRIV(thread).init_priority = priority; + + /* if this thread takes mutex, its current prio is controlled by mutex PI protocol */ + if (rt_list_isempty(&thread->taken_object_list) && !(thread->pending_object && (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))) + { + rt_sched_thread_change_curr_priority(thread, priority); + } + + return RT_EOK; +} + #ifdef RT_USING_OVERFLOW_CHECK void rt_scheduler_stack_check(struct rt_thread *thread) {