diff options
| author | wycwyhwyq <[email protected]> | 2025-04-10 10:20:21 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2025-04-13 15:25:42 +0800 |
| commit | 12de72a462d79c8d715a8fcd1620460abfa546fb (patch) | |
| tree | a165402e87e871f0ce7c7668fe0ae57bb12dc04c /src | |
| parent | da1c5c788279dab423458dafa79a85c29a9e31f6 (diff) | |
add RT_THREAD_CTRL_SET_PRIORITY
Diffstat (limited to 'src')
| -rw-r--r-- | src/scheduler_comm.c | 61 | ||||
| -rw-r--r-- | src/thread.c | 6 |
2 files changed, 21 insertions, 46 deletions
diff --git a/src/scheduler_comm.c b/src/scheduler_comm.c index 889c6070cf..c4e3e7a166 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 475ed9d5b2..ed9dca13b6 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; } |
