Skip to content

Commit d072554

Browse files
committed
update change prio func
1 parent 9a84c13 commit d072554

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

include/rtsched.h

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ rt_err_t rt_sched_thread_close(struct rt_thread *thread);
168168
rt_err_t rt_sched_thread_ready(struct rt_thread *thread);
169169
rt_err_t rt_sched_thread_suspend(struct rt_thread *thread, rt_sched_lock_level_t level);
170170
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority);
171+
rt_err_t rt_sched_thread_change_curr_priority(struct rt_thread *thread, rt_uint8_t priority);
171172
rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu);
172173
rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread);
173174
rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread);

src/ipc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ rt_inline void _thread_update_priority(struct rt_thread *thread, rt_uint8_t prio
873873
LOG_D("thread:%s priority -> %d", thread->parent.name, priority);
874874

875875
/* change priority of the thread */
876-
ret = rt_sched_thread_change_priority(thread, priority);
876+
ret = rt_sched_thread_change_curr_priority(thread, priority);
877877

878878
while ((ret == RT_EOK) && rt_sched_thread_is_suspended(thread))
879879
{
@@ -904,7 +904,7 @@ rt_inline void _thread_update_priority(struct rt_thread *thread, rt_uint8_t prio
904904
{
905905
thread = pending_mutex->owner;
906906

907-
ret = rt_sched_thread_change_priority(thread, mutex_priority);
907+
ret = rt_sched_thread_change_curr_priority(thread, mutex_priority);
908908
}
909909
else
910910
{
@@ -931,7 +931,7 @@ static rt_bool_t _check_and_update_prio(rt_thread_t thread, rt_mutex_t mutex)
931931
/* get the highest priority in the taken list of thread */
932932
priority = _thread_get_mutex_priority(thread);
933933

934-
rt_sched_thread_change_priority(thread, priority);
934+
rt_sched_thread_change_curr_priority(thread, priority);
935935

936936
/**
937937
* notify a pending reschedule. Since scheduler is locked, we will not

src/scheduler_comm.c

+55-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick)
180180
/**
181181
* @brief Update priority of the target thread
182182
*/
183-
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority)
183+
rt_err_t rt_sched_thread_change_curr_priority(struct rt_thread *thread, rt_uint8_t priority)
184184
{
185185
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
186186
RT_SCHED_DEBUG_IS_LOCKED;
@@ -224,6 +224,60 @@ rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t pr
224224
return RT_EOK;
225225
}
226226

227+
/**
228+
* @brief Update priority of the target thread
229+
*/
230+
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority)
231+
{
232+
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
233+
RT_SCHED_DEBUG_IS_LOCKED;
234+
235+
/* change thread init priority */
236+
RT_SCHED_PRIV(thread).init_priority = priority;
237+
238+
/* if this thread takes mutex, its current prio is controlled by mutex */
239+
if (rt_list_isempty(&thread->taken_object_list) && (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
240+
{
241+
/* for ready thread, change queue; otherwise simply update the priority */
242+
if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
243+
{
244+
/* remove thread from schedule queue first */
245+
rt_sched_remove_thread(thread);
246+
247+
/* change thread priority */
248+
RT_SCHED_PRIV(thread).current_priority = priority;
249+
250+
/* recalculate priority attribute */
251+
#if RT_THREAD_PRIORITY_MAX > 32
252+
RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
253+
RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).number;
254+
RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
255+
#else
256+
RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority;
257+
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
258+
RT_SCHED_CTX(thread).stat = RT_THREAD_INIT;
259+
260+
/* insert thread to schedule queue again */
261+
rt_sched_insert_thread(thread);
262+
}
263+
else
264+
{
265+
RT_SCHED_PRIV(thread).current_priority = priority;
266+
267+
/* recalculate priority attribute */
268+
#if RT_THREAD_PRIORITY_MAX > 32
269+
RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
270+
RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).number;
271+
RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
272+
#else
273+
RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority;
274+
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
275+
}
276+
}
277+
278+
return RT_EOK;
279+
}
280+
227281
#ifdef RT_USING_OVERFLOW_CHECK
228282
void rt_scheduler_stack_check(struct rt_thread *thread)
229283
{

0 commit comments

Comments
 (0)