Skip to content

Commit 12efa02

Browse files
authored
Refactor thread suspend state handling
Refactor thread suspension logic to improve clarity and maintainability.
1 parent 1297b89 commit 12efa02

File tree

1 file changed

+16
-32
lines changed

1 file changed

+16
-32
lines changed

src/thread.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -885,26 +885,27 @@ RTM_EXPORT(rt_thread_control);
885885
#include <lwp_signal.h>
886886
#endif
887887

888-
static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
888+
/* Convert suspend_flag to corresponding thread suspend state value */
889+
static rt_uint8_t _thread_get_suspend_state(int suspend_flag)
889890
{
890-
rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
891-
892-
RT_ASSERT(thread != RT_NULL);
893891
switch (suspend_flag)
894892
{
895893
case RT_INTERRUPTIBLE:
896-
stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
897-
break;
894+
return RT_THREAD_SUSPEND_INTERRUPTIBLE;
898895
case RT_KILLABLE:
899-
stat = RT_THREAD_SUSPEND_KILLABLE;
900-
break;
896+
return RT_THREAD_SUSPEND_KILLABLE;
901897
case RT_UNINTERRUPTIBLE:
902-
stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
903-
break;
904898
default:
905-
RT_ASSERT(0);
906-
break;
899+
return RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
907900
}
901+
}
902+
903+
static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
904+
{
905+
rt_uint8_t stat;
906+
907+
RT_ASSERT(thread != RT_NULL);
908+
stat = _thread_get_suspend_state(suspend_flag);
908909
RT_SCHED_CTX(thread).stat = stat | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
909910
}
910911

@@ -948,7 +949,6 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
948949
rt_sched_lock(&slvl);
949950

950951
stat = rt_sched_thread_get_stat(thread);
951-
/* Already suspended, just set the status to success. */
952952
if (stat & RT_THREAD_SUSPEND_MASK)
953953
{
954954
if (RT_SCHED_CTX(thread).sched_flag_ttmr_set == 1)
@@ -957,29 +957,13 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
957957
LOG_D("Thread [%s]'s timer has been halted.\n", thread->parent.name);
958958
rt_sched_thread_timer_stop(thread);
959959
}
960-
/* Map suspend_flag to corresponding thread suspend state value */
961-
rt_uint8_t new_suspend_state;
962-
switch (suspend_flag)
963-
{
964-
case RT_INTERRUPTIBLE:
965-
new_suspend_state = RT_THREAD_SUSPEND_INTERRUPTIBLE;
966-
break;
967-
case RT_KILLABLE:
968-
new_suspend_state = RT_THREAD_SUSPEND_KILLABLE;
969-
break;
970-
case RT_UNINTERRUPTIBLE:
971-
default:
972-
new_suspend_state = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
973-
break;
974-
}
975-
/* Compare the suspend state portion of stat with the new suspend state */
976-
if (stat < new_suspend_state)
960+
/* Upgrade suspend state if new state is stricter */
961+
if (stat < _thread_get_suspend_state(suspend_flag))
977962
{
978-
/* Update if suspend_flag is stricter */
979963
_thread_set_suspend_state(thread, suspend_flag);
980964
}
981-
982965
rt_sched_unlock(slvl);
966+
/* Already suspended, just set the status to success. */
983967
return RT_EOK;
984968
}
985969
else if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))

0 commit comments

Comments
 (0)