From ac6d1e202cea7e7dfa0f7f12045040587dd10d44 Mon Sep 17 00:00:00 2001 From: Tm-C-mT <490534897@qq.com> Date: Tue, 2 Dec 2025 18:16:14 +0800 Subject: [PATCH 1/2] [utest]:Modify the execution logic of smp_thread_preemptions Currently, the print information of this test case fails to demonstrate that the high-priority thread has preempted the low-priority thread. This is because when the high-priority thread prints the thread list, there is no information about the low-priority thread (tlow), as tlow has already completed execution and been destroyed. Therefore, the current execution logic cannot confirm the successful completion of the preemption operation. Solution: After the low-priority thread (tlow) releases the lock, add a busy-wait loop while(!finish_flag);. At this point, when the high-priority thread (thigh) prints the thread list information, tlow can be observed in the ready state, indicating that it has been preempted by thigh. Signed-off-by: Mengchen Teng --- src/utest/smp/smp_thread_preemption_tc.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utest/smp/smp_thread_preemption_tc.c b/src/utest/smp/smp_thread_preemption_tc.c index 699498a2b8f..f975217427b 100644 --- a/src/utest/smp/smp_thread_preemption_tc.c +++ b/src/utest/smp/smp_thread_preemption_tc.c @@ -17,6 +17,7 @@ * @note Create multiple threads, low-priority threads run first, * high-priority threads preempt low-priority threads, and * print the current status of each core in the thread's entry function. + * */ #define THREAD_PRIORITY_HIGH 21 @@ -24,6 +25,7 @@ #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE static rt_thread_t threads[2]; +static volatile rt_uint8_t finish_flag = 0; static struct rt_spinlock lock; /* High Priority Thread */ @@ -35,6 +37,7 @@ static void thread_high_entry(void *parameter) extern long list_thread(void); list_thread(); rt_spin_unlock(&lock); + finish_flag = 1; } /* Low Priority Thread */ @@ -46,6 +49,7 @@ static void thread_low_entry(void *parameter) extern long list_thread(void); list_thread(); rt_spin_unlock(&lock); + while (!finish_flag); } static void thread_preemptions_tc(void) @@ -72,6 +76,7 @@ static void thread_preemptions_tc(void) static rt_err_t utest_tc_init(void) { rt_spin_lock_init(&lock); + finish_flag = 0; return RT_EOK; } From 90b8b7f96642a5bbea1133c4dc480ee2f1d0407f Mon Sep 17 00:00:00 2001 From: Tm-C-mT <490534897@qq.com> Date: Tue, 9 Dec 2025 21:57:04 +0800 Subject: [PATCH 2/2] [docs][utest]:Add comments for smp_thread_preemption Add comments for smp_thread_preemption. Signed-off-by: Mengchen Teng --- src/utest/smp/smp_thread_preemption_tc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/utest/smp/smp_thread_preemption_tc.c b/src/utest/smp/smp_thread_preemption_tc.c index f975217427b..b8493d9be20 100644 --- a/src/utest/smp/smp_thread_preemption_tc.c +++ b/src/utest/smp/smp_thread_preemption_tc.c @@ -18,6 +18,28 @@ * high-priority threads preempt low-priority threads, and * print the current status of each core in the thread's entry function. * + * Test Case Name: [smp_thread_preemptions] + * + * Test Objectives: + * - Test the thread preemption logic under the SMP architecture + * + * Test Scenarios: + * - A high-priority thread thigh and a low-priority thread tlow are created. Since tlow is created + * - prior to thigh, it prints the thread list information first and then waits for the finish_flag + * - bit. Once thigh becomes ready, it preempts tlow, prints the thread list, and then sets the + * - finish_flag bit. After that, tlow exits the busy-wait loop. + * + * Verification Metrics: + * - First, the message "Low priority thread is running" will be displayed, followed immediately by + * - a thread_list information list. Subsequently, the message "High priority thread is running" will + * - appear, and in the thread_list that follows, the tlow thread can be observed in the ready state. + * + * Dependencies: + * - Enable RT_USING_SMP, set RT_THREAD_PRIORITY_MAX = 256. + * + * Expected Results: + * - You will see the message "[ PASSED ] [ result ] testcase (core.smp_thread_preemptions)", and observe + * - the tlow thread in the ready state in the thread_list printed by the thigh thread. */ #define THREAD_PRIORITY_HIGH 21