1
0

thread_static_simple.c 2.7 KB

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