1 /*
2  * Copyright (c) 2006-2024, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-08-10     RV           the first version
9  */
10 
11 #include <rtthread.h>
12 #include "utest.h"
13 
14 /**
15  * @brief   Thread Preemption Test with Different Priorities.
16  *
17  * @note    Create multiple threads, low-priority threads run first,
18  *          high-priority threads preempt low-priority threads, and
19  *          print the current status of each core in the thread's entry function.
20  */
21 
22 #define THREAD_PRIORITY_HIGH 21
23 #define THREAD_PRIORITY_LOW  30
24 #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
25 
26 static rt_thread_t   threads[2];
27 static struct rt_spinlock lock;
28 
29 /* High Priority Thread */
thread_high_entry(void * parameter)30 static void thread_high_entry(void *parameter)
31 {
32     uassert_true(1);
33     rt_spin_lock(&lock);
34     rt_kprintf("High priority thread is running\n");
35     extern long list_thread(void);
36     list_thread();
37     rt_spin_unlock(&lock);
38 }
39 
40 /* Low Priority Thread */
thread_low_entry(void * parameter)41 static void thread_low_entry(void *parameter)
42 {
43     uassert_true(1);
44     rt_spin_lock(&lock);
45     rt_kprintf("Low priority thread is running\n");
46     extern long list_thread(void);
47     list_thread();
48     rt_spin_unlock(&lock);
49 }
50 
thread_preemptions_tc(void)51 static void thread_preemptions_tc(void)
52 {
53     /* Creating low-priority thread */
54     threads[0] = rt_thread_create("tlow", thread_low_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY_LOW, 10);
55     if (threads[0]  != RT_NULL)
56     {
57         uassert_true(1);
58         rt_thread_startup(threads[0] );
59     }
60 
61     rt_thread_delay(5);
62     /* Creating high-priority thread */
63     threads[1]  = rt_thread_create("thigh", thread_high_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY_HIGH, 10);
64     if (threads[1] != RT_NULL)
65     {
66         uassert_true(1);
67         rt_thread_startup(threads[1]);
68     }
69     rt_thread_delay(50);
70 }
71 
utest_tc_init(void)72 static rt_err_t utest_tc_init(void)
73 {
74     rt_spin_lock_init(&lock);
75     return RT_EOK;
76 }
77 
utest_tc_cleanup(void)78 static rt_err_t utest_tc_cleanup(void)
79 {
80     return RT_EOK;
81 }
82 
testcase(void)83 static void testcase(void)
84 {
85     UTEST_UNIT_RUN(thread_preemptions_tc);
86 }
87 UTEST_TC_EXPORT(testcase, "testcases.smp.thread_preemptions_tc", utest_tc_init, utest_tc_cleanup, 10);
88