Skip to content

Commit 321277e

Browse files
authored
Enhance thread suspend function with stricter checks
Refactor thread suspension logic to improve clarity and correctness.
1 parent 6ab057b commit 321277e

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/thread.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -943,30 +943,58 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
943943
RT_ASSERT(thread != RT_NULL);
944944
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
945945

946-
LOG_D("thread suspend: %s", thread->parent.name);
946+
LOG_D("thread suspend: %s", thread->parent.name);
947947

948948
rt_sched_lock(&slvl);
949949

950950
stat = rt_sched_thread_get_stat(thread);
951-
if (stat == RT_THREAD_SUSPEND)
951+
/* Already suspended, just set the status to success. */
952+
if (stat & RT_THREAD_SUSPEND_MASK)
952953
{
954+
if (RT_SCHED_CTX(thread).sched_flag_ttmr_set == 1)
955+
{
956+
/* The new suspend operation will halt the tick timer. */
957+
LOG_D("Thread [%s]'s timer has been halted.\n", thread->parent.name);
958+
rt_sched_thread_timer_stop(thread);
959+
960+
}
961+
/* Map suspend_flag to corresponding thread suspend state value */
962+
rt_uint8_t new_suspend_state;
963+
switch (suspend_flag)
964+
{
965+
case RT_INTERRUPTIBLE:
966+
new_suspend_state = RT_THREAD_SUSPEND_INTERRUPTIBLE;
967+
break;
968+
case RT_KILLABLE:
969+
new_suspend_state = RT_THREAD_SUSPEND_KILLABLE;
970+
break;
971+
case RT_UNINTERRUPTIBLE:
972+
default:
973+
new_suspend_state = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
974+
break;
975+
}
976+
/* Compare the suspend state portion of stat with the new suspend state */
977+
if (stat < new_suspend_state)
978+
{
979+
/* Update if suspend_flag is stricter */
980+
_thread_set_suspend_state(thread, suspend_flag);
981+
}
982+
953983
rt_sched_unlock(slvl);
954-
/* Already suspended, just set status to success. */
955984
return RT_EOK;
956985
}
957986
else if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
958987
{
959-
LOG_D("thread suspend: thread disorder, 0x%2x", RT_SCHED_CTX(thread).stat);
988+
LOG_W("thread suspend: thread disorder, 0x%02x", RT_SCHED_CTX(thread).stat);
960989
rt_sched_unlock(slvl);
961990
return -RT_ERROR;
962991
}
963992

964993
if (stat == RT_THREAD_RUNNING)
965994
{
966-
/* not suspend running status thread on other core */
967995
RT_ASSERT(thread == rt_thread_self());
968996
}
969-
997+
970998
#ifdef RT_USING_SMART
971999
if (thread->lwp)
9721000
{

0 commit comments

Comments
 (0)