Skip to content

Commit f13193f

Browse files
wycwyhwyqRbb666
authored andcommitted
[src] fix mutex bug
1 parent 2db2bf0 commit f13193f

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

include/rtsched.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ rt_err_t rt_sched_thread_yield(struct rt_thread *thread);
128128
rt_err_t rt_sched_thread_close(struct rt_thread *thread);
129129
rt_err_t rt_sched_thread_ready(struct rt_thread *thread);
130130
rt_err_t rt_sched_thread_suspend(struct rt_thread *thread, rt_sched_lock_level_t level);
131+
rt_err_t rt_sched_thread_set_priority(struct rt_thread *thread, rt_uint8_t priority);
131132
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority);
132133
rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu);
133134
rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread);

src/scheduler_comm.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

src/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
797797
rt_err_t error;
798798
rt_sched_lock_level_t slvl;
799799
rt_sched_lock(&slvl);
800-
error = rt_sched_thread_change_priority(thread, *(rt_uint8_t *)arg);
800+
error = rt_sched_thread_set_priority(thread, *(rt_uint8_t *)arg);
801801
rt_sched_unlock(slvl);
802802
return error;
803803
}

0 commit comments

Comments
 (0)