thread_static_simple.c 2.6 KB

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