@@ -178,9 +178,9 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick)
178178}
179179
180180/**
181- * @brief Set priority of the target thread
181+ * @brief Update priority of the target thread
182182 */
183- rt_err_t rt_sched_thread_set_priority (struct rt_thread * thread , rt_uint8_t priority )
183+ static rt_err_t _rt_sched_update_priority (struct rt_thread * thread , rt_uint8_t priority , rt_bool_t update_init_prio )
184184{
185185 RT_ASSERT (priority < RT_THREAD_PRIORITY_MAX );
186186 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
192192 rt_sched_remove_thread (thread );
193193
194194 /* change thread priority */
195- RT_SCHED_PRIV (thread ).init_priority = priority ;
195+ if (update_init_prio )
196+ {
197+ RT_SCHED_PRIV (thread ).init_priority = priority ;
198+ }
196199 RT_SCHED_PRIV (thread ).current_priority = priority ;
197200
198201 /* recalculate priority attribute */
@@ -210,7 +213,10 @@ rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t prior
210213 }
211214 else
212215 {
213- RT_SCHED_PRIV (thread ).init_priority = priority ;
216+ if (update_init_prio )
217+ {
218+ RT_SCHED_PRIV (thread ).init_priority = priority ;
219+ }
214220 RT_SCHED_PRIV (thread ).current_priority = priority ;
215221
216222 /* recalculate priority attribute */
@@ -231,46 +237,15 @@ rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t prior
231237 */
232238rt_err_t rt_sched_thread_change_priority (struct rt_thread * thread , rt_uint8_t priority )
233239{
234- RT_ASSERT (priority < RT_THREAD_PRIORITY_MAX );
235- RT_SCHED_DEBUG_IS_LOCKED ;
236-
237- /* for ready thread, change queue; otherwise simply update the priority */
238- if ((RT_SCHED_CTX (thread ).stat & RT_THREAD_STAT_MASK ) == RT_THREAD_READY )
239- {
240- /* remove thread from schedule queue first */
241- rt_sched_remove_thread (thread );
242-
243- /* change thread priority */
244- RT_SCHED_PRIV (thread ).current_priority = priority ;
245-
246- /* recalculate priority attribute */
247- #if RT_THREAD_PRIORITY_MAX > 32
248- RT_SCHED_PRIV (thread ).number = RT_SCHED_PRIV (thread ).current_priority >> 3 ; /* 5bit */
249- RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).number ;
250- RT_SCHED_PRIV (thread ).high_mask = 1 << (RT_SCHED_PRIV (thread ).current_priority & 0x07 ); /* 3bit */
251- #else
252- RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).current_priority ;
253- #endif /* RT_THREAD_PRIORITY_MAX > 32 */
254- RT_SCHED_CTX (thread ).stat = RT_THREAD_INIT ;
255-
256- /* insert thread to schedule queue again */
257- rt_sched_insert_thread (thread );
258- }
259- else
260- {
261- RT_SCHED_PRIV (thread ).current_priority = priority ;
262-
263- /* recalculate priority attribute */
264- #if RT_THREAD_PRIORITY_MAX > 32
265- RT_SCHED_PRIV (thread ).number = RT_SCHED_PRIV (thread ).current_priority >> 3 ; /* 5bit */
266- RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).number ;
267- RT_SCHED_PRIV (thread ).high_mask = 1 << (RT_SCHED_PRIV (thread ).current_priority & 0x07 ); /* 3bit */
268- #else
269- RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).current_priority ;
270- #endif /* RT_THREAD_PRIORITY_MAX > 32 */
271- }
240+ return _rt_sched_update_priority (thread , priority , RT_FALSE );
241+ }
272242
273- return RT_EOK ;
243+ /**
244+ * @brief Reset priority of the target thread
245+ */
246+ rt_err_t rt_sched_thread_reset_priority (struct rt_thread * thread , rt_uint8_t priority )
247+ {
248+ return _rt_sched_update_priority (thread , priority , RT_TRUE );
274249}
275250
276251#ifdef RT_USING_OVERFLOW_CHECK
0 commit comments