From 321277e2ffd4e6da46f12f860d84858f719da6d0 Mon Sep 17 00:00:00 2001 From: Kurngsy <154585910+Kurngsy@users.noreply.github.com> Date: Fri, 21 Nov 2025 17:45:53 +0800 Subject: [PATCH 1/5] Enhance thread suspend function with stricter checks Refactor thread suspension logic to improve clarity and correctness. --- src/thread.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/thread.c b/src/thread.c index 481dfebeef2..17bfdce08ba 100644 --- a/src/thread.c +++ b/src/thread.c @@ -943,30 +943,58 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); - LOG_D("thread suspend: %s", thread->parent.name); + LOG_D("thread suspend: %s", thread->parent.name); rt_sched_lock(&slvl); stat = rt_sched_thread_get_stat(thread); - if (stat == RT_THREAD_SUSPEND) + /* Already suspended, just set the status to success. */ + if (stat & RT_THREAD_SUSPEND_MASK) { + if (RT_SCHED_CTX(thread).sched_flag_ttmr_set == 1) + { + /* The new suspend operation will halt the tick timer. */ + LOG_D("Thread [%s]'s timer has been halted.\n", thread->parent.name); + rt_sched_thread_timer_stop(thread); + + } + /* Map suspend_flag to corresponding thread suspend state value */ + rt_uint8_t new_suspend_state; + switch (suspend_flag) + { + case RT_INTERRUPTIBLE: + new_suspend_state = RT_THREAD_SUSPEND_INTERRUPTIBLE; + break; + case RT_KILLABLE: + new_suspend_state = RT_THREAD_SUSPEND_KILLABLE; + break; + case RT_UNINTERRUPTIBLE: + default: + new_suspend_state = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; + break; + } + /* Compare the suspend state portion of stat with the new suspend state */ + if (stat < new_suspend_state) + { + /* Update if suspend_flag is stricter */ + _thread_set_suspend_state(thread, suspend_flag); + } + rt_sched_unlock(slvl); - /* Already suspended, just set status to success. */ return RT_EOK; } else if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING)) { - LOG_D("thread suspend: thread disorder, 0x%2x", RT_SCHED_CTX(thread).stat); + LOG_W("thread suspend: thread disorder, 0x%02x", RT_SCHED_CTX(thread).stat); rt_sched_unlock(slvl); return -RT_ERROR; } if (stat == RT_THREAD_RUNNING) { - /* not suspend running status thread on other core */ RT_ASSERT(thread == rt_thread_self()); } - + #ifdef RT_USING_SMART if (thread->lwp) { From 1297b89f43b39a6c453b688ea87fae7c4d2b1842 Mon Sep 17 00:00:00 2001 From: Kurngsy <154585910+Kurngsy@users.noreply.github.com> Date: Fri, 28 Nov 2025 17:54:14 +0800 Subject: [PATCH 2/5] Clean up formatting in thread.c Removed unnecessary blank line in thread.c. --- src/thread.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/thread.c b/src/thread.c index 17bfdce08ba..77679581397 100644 --- a/src/thread.c +++ b/src/thread.c @@ -956,7 +956,6 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int /* The new suspend operation will halt the tick timer. */ LOG_D("Thread [%s]'s timer has been halted.\n", thread->parent.name); rt_sched_thread_timer_stop(thread); - } /* Map suspend_flag to corresponding thread suspend state value */ rt_uint8_t new_suspend_state; @@ -994,7 +993,7 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int { RT_ASSERT(thread == rt_thread_self()); } - + #ifdef RT_USING_SMART if (thread->lwp) { From 12efa026e31b9e081b8a9635f8590387233a0f12 Mon Sep 17 00:00:00 2001 From: Kurngsy <154585910+Kurngsy@users.noreply.github.com> Date: Tue, 9 Dec 2025 18:26:05 +0800 Subject: [PATCH 3/5] Refactor thread suspend state handling Refactor thread suspension logic to improve clarity and maintainability. --- src/thread.c | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/src/thread.c b/src/thread.c index 77679581397..0e5c40c3c99 100644 --- a/src/thread.c +++ b/src/thread.c @@ -885,26 +885,27 @@ RTM_EXPORT(rt_thread_control); #include #endif -static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag) +/* Convert suspend_flag to corresponding thread suspend state value */ +static rt_uint8_t _thread_get_suspend_state(int suspend_flag) { - rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; - - RT_ASSERT(thread != RT_NULL); switch (suspend_flag) { case RT_INTERRUPTIBLE: - stat = RT_THREAD_SUSPEND_INTERRUPTIBLE; - break; + return RT_THREAD_SUSPEND_INTERRUPTIBLE; case RT_KILLABLE: - stat = RT_THREAD_SUSPEND_KILLABLE; - break; + return RT_THREAD_SUSPEND_KILLABLE; case RT_UNINTERRUPTIBLE: - stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; - break; default: - RT_ASSERT(0); - break; + return RT_THREAD_SUSPEND_UNINTERRUPTIBLE; } +} + +static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag) +{ + rt_uint8_t stat; + + RT_ASSERT(thread != RT_NULL); + stat = _thread_get_suspend_state(suspend_flag); RT_SCHED_CTX(thread).stat = stat | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK); } @@ -948,7 +949,6 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int rt_sched_lock(&slvl); stat = rt_sched_thread_get_stat(thread); - /* Already suspended, just set the status to success. */ if (stat & RT_THREAD_SUSPEND_MASK) { 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 LOG_D("Thread [%s]'s timer has been halted.\n", thread->parent.name); rt_sched_thread_timer_stop(thread); } - /* Map suspend_flag to corresponding thread suspend state value */ - rt_uint8_t new_suspend_state; - switch (suspend_flag) - { - case RT_INTERRUPTIBLE: - new_suspend_state = RT_THREAD_SUSPEND_INTERRUPTIBLE; - break; - case RT_KILLABLE: - new_suspend_state = RT_THREAD_SUSPEND_KILLABLE; - break; - case RT_UNINTERRUPTIBLE: - default: - new_suspend_state = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; - break; - } - /* Compare the suspend state portion of stat with the new suspend state */ - if (stat < new_suspend_state) + /* Upgrade suspend state if new state is stricter */ + if (stat < _thread_get_suspend_state(suspend_flag)) { - /* Update if suspend_flag is stricter */ _thread_set_suspend_state(thread, suspend_flag); } - rt_sched_unlock(slvl); + /* Already suspended, just set the status to success. */ return RT_EOK; } else if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING)) From ac4527b4eacf6d1f39a13d15a86ad02864de337c Mon Sep 17 00:00:00 2001 From: Kurngsy <154585910+Kurngsy@users.noreply.github.com> Date: Tue, 9 Dec 2025 18:28:26 +0800 Subject: [PATCH 4/5] Update thread.c --- src/thread.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/thread.c b/src/thread.c index 0e5c40c3c99..55fa4015bb3 100644 --- a/src/thread.c +++ b/src/thread.c @@ -975,6 +975,7 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int if (stat == RT_THREAD_RUNNING) { + /* not suspend running status thread on other core */ RT_ASSERT(thread == rt_thread_self()); } From 92de13113db391e96cda648bb44e4a61a1bffd6c Mon Sep 17 00:00:00 2001 From: Kurngsy <154585910+Kurngsy@users.noreply.github.com> Date: Wed, 10 Dec 2025 21:39:14 +0800 Subject: [PATCH 5/5] Fix indentation for RT_THREAD_SUSPEND_KILLABLE case --- src/thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thread.c b/src/thread.c index 55fa4015bb3..15e4ee45e85 100644 --- a/src/thread.c +++ b/src/thread.c @@ -893,7 +893,7 @@ static rt_uint8_t _thread_get_suspend_state(int suspend_flag) case RT_INTERRUPTIBLE: return RT_THREAD_SUSPEND_INTERRUPTIBLE; case RT_KILLABLE: - return RT_THREAD_SUSPEND_KILLABLE; + return RT_THREAD_SUSPEND_KILLABLE; case RT_UNINTERRUPTIBLE: default: return RT_THREAD_SUSPEND_UNINTERRUPTIBLE;