xref: /nrf52832-nimble/rt-thread/examples/kernel/thread_static_simple.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * 程序清单:静态线程
3  *
4  * 这个程序会初始化2个静态线程,它们拥有共同的入口函数,但参数不相同
5  */
6 #include <rtthread.h>
7 #include "tc_comm.h"
8 
9 /* 线程1控制块 */
10 static struct rt_thread thread1;
11 /* 线程1栈 */
12 static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
13 /* 线程2控制块 */
14 static struct rt_thread thread2;
15 /* 线程2栈 */
16 static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
17 
18 /* 线程入口 */
thread_entry(void * parameter)19 static void thread_entry(void* parameter)
20 {
21     rt_uint32_t count = 0;
22     rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
23 
24     while (1)
25     {
26         /* 打印线程计数值输出 */
27         rt_kprintf("thread%d count: %d\n", no, count ++);
28 
29         /* 休眠10个OS Tick */
30         rt_thread_delay(10);
31     }
32 }
33 
thread_static_simple_init()34 int thread_static_simple_init()
35 {
36     rt_err_t result;
37 
38     /* 初始化线程1 */
39     result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */
40         thread_entry, (void*)1, /* 线程的入口是thread_entry,入口参数是1 */
41         &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */
42         THREAD_PRIORITY, 10);
43     if (result == RT_EOK) /* 如果返回正确,启动线程1 */
44         rt_thread_startup(&thread1);
45     else
46         tc_stat(TC_STAT_END | TC_STAT_FAILED);
47 
48     /* 初始化线程2 */
49     result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */
50         thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是2 */
51         &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */
52         THREAD_PRIORITY + 1, 10);
53     if (result == RT_EOK) /* 如果返回正确,启动线程2 */
54         rt_thread_startup(&thread2);
55     else
56         tc_stat(TC_STAT_END | TC_STAT_FAILED);
57 
58     return 0;
59 }
60 
61 #ifdef RT_USING_TC
_tc_cleanup()62 static void _tc_cleanup()
63 {
64     /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
65     rt_enter_critical();
66 
67     /* 执行线程脱离 */
68     if (thread1.stat != RT_THREAD_CLOSE)
69         rt_thread_detach(&thread1);
70     if (thread2.stat != RT_THREAD_CLOSE)
71         rt_thread_detach(&thread2);
72 
73     /* 调度器解锁 */
74     rt_exit_critical();
75 
76     /* 设置TestCase状态 */
77     tc_done(TC_STAT_PASSED);
78 }
79 
_tc_thread_static_simple()80 int _tc_thread_static_simple()
81 {
82     /* 设置TestCase清理回调函数 */
83     tc_cleanup(_tc_cleanup);
84     thread_static_simple_init();
85 
86     /* 返回TestCase运行的最长时间 */
87     return 100;
88 }
89 /* 输出函数命令到finsh shell中 */
90 FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example);
91 #else
92 /* 用户应用入口 */
rt_application_init()93 int rt_application_init()
94 {
95     thread_static_simple_init();
96 
97     return 0;
98 }
99 #endif
100