Skip to content

Commit 12de72a

Browse files
wycwyhwyqRbb666
authored andcommitted
add RT_THREAD_CTRL_SET_PRIORITY
1 parent da1c5c7 commit 12de72a

File tree

5 files changed

+26
-51
lines changed

5 files changed

+26
-51
lines changed

components/lwp/lwp_syscall.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ sysret_t sys_setpriority(int which, id_t who, int prio)
16341634
for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
16351635
{
16361636
thread = rt_list_entry(list, struct rt_thread, sibling);
1637-
rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, &prio);
1637+
rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, &prio);
16381638
}
16391639
lwp_pid_lock_release();
16401640
return 0;
@@ -8789,7 +8789,7 @@ sysret_t sys_sched_setparam(pid_t tid, void *param)
87898789

87908790
if (thread)
87918791
{
8792-
ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority);
8792+
ret = rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, (void *)&sched_param->sched_priority);
87938793
}
87948794

87958795
lwp_tid_dec_ref(thread);
@@ -8959,7 +8959,7 @@ sysret_t sys_sched_setscheduler(int tid, int policy, void *param)
89598959
}
89608960

89618961
thread = lwp_tid_get_thread_and_inc_ref(tid);
8962-
ret = rt_thread_control(thread, RT_THREAD_CTRL_SET_PRIORITY, (void *)&sched_param->sched_priority);
8962+
ret = rt_thread_control(thread, RT_THREAD_CTRL_RESET_PRIORITY, (void *)&sched_param->sched_priority);
89638963
lwp_tid_dec_ref(thread);
89648964

89658965
kmem_put(sched_param);

include/rtdef.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ enum
643643
#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
644644
#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
645645
#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */
646-
#define RT_THREAD_CTRL_SET_PRIORITY 0x05 /**< Set thread priority. */
646+
#define RT_THREAD_CTRL_RESET_PRIORITY 0x05 /**< Reset thread priority. */
647647

648648
/**
649649
* CPU usage statistics data

include/rtsched.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ 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);
132131
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority);
132+
rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t priority);
133133
rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu);
134134
rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread);
135135
rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread);

src/scheduler_comm.c

+18-43
Original file line numberDiff line numberDiff line change
@@ -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
*/
232238
rt_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

src/thread.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ RTM_EXPORT(rt_thread_mdelay);
779779
*
780780
* RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
781781
*
782-
* RT_THREAD_CTRL_SET_PRIORITY for set priority level of thread.
782+
* RT_THREAD_CTRL_RESET_PRIORITY for reset priority level of thread.
783783
*
784784
* @param arg is the argument of control command.
785785
*
@@ -804,12 +804,12 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
804804
return error;
805805
}
806806

807-
case RT_THREAD_CTRL_SET_PRIORITY:
807+
case RT_THREAD_CTRL_RESET_PRIORITY:
808808
{
809809
rt_err_t error;
810810
rt_sched_lock_level_t slvl;
811811
rt_sched_lock(&slvl);
812-
error = rt_sched_thread_set_priority(thread, *(rt_uint8_t *)arg);
812+
error = rt_sched_thread_reset_priority(thread, *(rt_uint8_t *)arg);
813813
rt_sched_unlock(slvl);
814814
return error;
815815
}

0 commit comments

Comments
 (0)