1 /* 2 * 程序清单: 3 */ 4 #include <rtthread.h> 5 #include "tc_comm.h" 6 7 /* 指向线程控制块的指针 */ 8 static rt_thread_t tid1 = RT_NULL; 9 static rt_thread_t tid2 = RT_NULL; 10 /* 线程1入口 */ 11 static void thread1_entry(void* parameter) 12 { 13 rt_uint32_t count = 0; 14 15 while (1) 16 { 17 /* 打印线程1的输出 */ 18 rt_kprintf("thread1: count = %d\n", count ++); 19 20 /* 执行yield后应该切换到thread2执行 */ 21 rt_thread_yield(); 22 } 23 } 24 25 /* 线程2入口 */ 26 static void thread2_entry(void* parameter) 27 { 28 rt_uint32_t count = 0; 29 30 while (1) 31 { 32 /* 打印线程2的输出 */ 33 rt_kprintf("thread2: count = %d\n", count ++); 34 35 /* 执行yield后应该切换到thread1执行 */ 36 rt_thread_yield(); 37 } 38 } 39 40 int thread_yield_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, 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 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 82 int _tc_thread_yield() 83 { 84 /* 设置TestCase清理回调函数 */ 85 tc_cleanup(_tc_cleanup); 86 thread_yield_init(); 87 88 /* 返回TestCase运行的最长时间 */ 89 return 30; 90 } 91 /* 输出函数命令到finsh shell中 */ 92 FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example); 93 #else 94 /* 用户应用入口 */ 95 int rt_application_init() 96 { 97 thread_yield_init(); 98 99 return 0; 100 } 101 #endif 102