1 /*
2 * 程序清单:挂起线程
3 *
4 * 这个例子中将创建两个动态线程,高优先级线程将在一定时刻后挂起低优先级线程。
5 */
6 #include <rtthread.h>
7 #include "tc_comm.h"
8
9 /* 指向线程控制块的指针 */
10 static rt_thread_t tid1 = RT_NULL;
11 static rt_thread_t tid2 = RT_NULL;
12 /* 线程1入口 */
thread1_entry(void * parameter)13 static void thread1_entry(void* parameter)
14 {
15 rt_uint32_t count = 0;
16
17 while (1)
18 {
19 /* 线程1采用低优先级运行,一直打印计数值 */
20 rt_kprintf("thread count: %d\n", count ++);
21 }
22 }
23
24 /* 线程2入口 */
thread2_entry(void * parameter)25 static void thread2_entry(void* parameter)
26 {
27 /* 延时10个OS Tick */
28 rt_thread_delay(10);
29
30 /* 挂起线程1 */
31 rt_thread_suspend(tid1);
32
33 /* 延时10个OS Tick */
34 rt_thread_delay(10);
35
36 /* 线程2自动退出 */
37 tid2 = RT_NULL;
38 }
39
thread_suspend_init()40 int thread_suspend_init()
41 {
42 /* 创建线程1 */
43 tid1 = rt_thread_create("thread",
44 thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
45 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
46 if (tid1 != RT_NULL)
47 rt_thread_startup(tid1);
48 else
49 tc_stat(TC_STAT_END | TC_STAT_FAILED);
50
51 /* 创建线程2 */
52 tid2 = rt_thread_create("thread",
53 thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
54 THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
55 if (tid2 != RT_NULL)
56 rt_thread_startup(tid2);
57 else
58 tc_stat(TC_STAT_END | TC_STAT_FAILED);
59
60 return 0;
61 }
62
63 #ifdef RT_USING_TC
_tc_cleanup()64 static void _tc_cleanup()
65 {
66 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
67 rt_enter_critical();
68
69 /* 删除线程 */
70 if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
71 rt_thread_delete(tid1);
72 if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
73 rt_thread_delete(tid2);
74
75 /* 调度器解锁 */
76 rt_exit_critical();
77
78 /* 设置TestCase状态 */
79 tc_done(TC_STAT_PASSED);
80 }
81
_tc_thread_suspend()82 int _tc_thread_suspend()
83 {
84 /* 设置TestCase清理回调函数 */
85 tc_cleanup(_tc_cleanup);
86 thread_suspend_init();
87
88 /* 返回TestCase运行的最长时间 */
89 return 100;
90 }
91 /* 输出函数命令到finsh shell中 */
92 FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example);
93 #else
94 /* 用户应用入口 */
rt_application_init()95 int rt_application_init()
96 {
97 thread_suspend_init();
98
99 return 0;
100 }
101 #endif
102