@@ -177,6 +177,55 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick)
177177 return RT_EOK ;
178178}
179179
180+ /**
181+ * @brief Set priority of the target thread
182+ */
183+ rt_err_t rt_sched_thread_set_priority (struct rt_thread * thread , rt_uint8_t priority )
184+ {
185+ RT_ASSERT (priority < RT_THREAD_PRIORITY_MAX );
186+ RT_SCHED_DEBUG_IS_LOCKED ;
187+
188+ /* for ready thread, change queue; otherwise simply update the priority */
189+ if ((RT_SCHED_CTX (thread ).stat & RT_THREAD_STAT_MASK ) == RT_THREAD_READY )
190+ {
191+ /* remove thread from schedule queue first */
192+ rt_sched_remove_thread (thread );
193+
194+ /* change thread priority */
195+ RT_SCHED_PRIV (thread ).init_priority = priority ;
196+ RT_SCHED_PRIV (thread ).current_priority = priority ;
197+
198+ /* recalculate priority attribute */
199+ #if RT_THREAD_PRIORITY_MAX > 32
200+ RT_SCHED_PRIV (thread ).number = RT_SCHED_PRIV (thread ).current_priority >> 3 ; /* 5bit */
201+ RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).number ;
202+ RT_SCHED_PRIV (thread ).high_mask = 1 << (RT_SCHED_PRIV (thread ).current_priority & 0x07 ); /* 3bit */
203+ #else
204+ RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).current_priority ;
205+ #endif /* RT_THREAD_PRIORITY_MAX > 32 */
206+ RT_SCHED_CTX (thread ).stat = RT_THREAD_INIT ;
207+
208+ /* insert thread to schedule queue again */
209+ rt_sched_insert_thread (thread );
210+ }
211+ else
212+ {
213+ RT_SCHED_PRIV (thread ).init_priority = priority ;
214+ RT_SCHED_PRIV (thread ).current_priority = priority ;
215+
216+ /* recalculate priority attribute */
217+ #if RT_THREAD_PRIORITY_MAX > 32
218+ RT_SCHED_PRIV (thread ).number = RT_SCHED_PRIV (thread ).current_priority >> 3 ; /* 5bit */
219+ RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).number ;
220+ RT_SCHED_PRIV (thread ).high_mask = 1 << (RT_SCHED_PRIV (thread ).current_priority & 0x07 ); /* 3bit */
221+ #else
222+ RT_SCHED_PRIV (thread ).number_mask = 1 << RT_SCHED_PRIV (thread ).current_priority ;
223+ #endif /* RT_THREAD_PRIORITY_MAX > 32 */
224+ }
225+
226+ return RT_EOK ;
227+ }
228+
180229/**
181230 * @brief Update priority of the target thread
182231 */
0 commit comments