Skip to content

Commit

Permalink
update change prio func
Browse files Browse the repository at this point in the history
  • Loading branch information
zmshahaha committed Aug 28, 2024
1 parent 9a84c13 commit d072554
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/rtsched.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down
56 changes: 55 additions & 1 deletion src/scheduler_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,6 +224,60 @@ 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 */
if (rt_list_isempty(&thread->taken_object_list) && (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
{
/* 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_EOK;
}

#ifdef RT_USING_OVERFLOW_CHECK
void rt_scheduler_stack_check(struct rt_thread *thread)
{
Expand Down

0 comments on commit d072554

Please sign in to comment.