thread_static_simple.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <rtthread.h>
  2. #include "tc_comm.h"
  3. /*
  4. * This is an example for static thread
  5. */
  6. static struct rt_thread thread1;
  7. static struct rt_thread thread2;
  8. static char thread1_stack[THREAD_STACK_SIZE];
  9. static char thread2_stack[THREAD_STACK_SIZE];
  10. static void thread_entry(void* parameter)
  11. {
  12. rt_uint32_t count = 0;
  13. rt_uint32_t no = (rt_uint32_t) parameter;
  14. while (1)
  15. {
  16. rt_kprintf("thread%d count: %d\n", no, count ++);
  17. rt_thread_delay(10);
  18. }
  19. }
  20. rt_err_t thread_static_simple_init()
  21. {
  22. rt_err_t result;
  23. result = rt_thread_init(&thread1,
  24. "t1",
  25. thread_entry, (void*)1,
  26. &thread1_stack[0], sizeof(thread1_stack),
  27. THREAD_PRIORITY, 10);
  28. if (result == RT_EOK)
  29. rt_thread_startup(&thread1);
  30. else
  31. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  32. result = rt_thread_init(&thread2,
  33. "t2",
  34. thread_entry, (void*)2,
  35. &thread2_stack[0], sizeof(thread2_stack),
  36. THREAD_PRIORITY + 1, 10);
  37. if (result == RT_EOK)
  38. rt_thread_startup(&thread2);
  39. else
  40. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  41. return result;
  42. }
  43. #ifdef RT_USING_TC
  44. static void _tc_cleanup()
  45. {
  46. /* lock scheduler */
  47. rt_enter_critical();
  48. if (thread1.stat != RT_THREAD_CLOSE)
  49. rt_thread_detach(&thread1);
  50. if (thread2.stat != RT_THREAD_CLOSE)
  51. rt_thread_detach(&thread2);
  52. /* unlock scheduler */
  53. rt_exit_critical();
  54. }
  55. int _tc_thread_static_simple()
  56. {
  57. /* set tc cleanup */
  58. tc_cleanup(_tc_cleanup);
  59. thread_static_simple_init();
  60. return 20;
  61. }
  62. FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example);
  63. #else
  64. int rt_application_init()
  65. {
  66. thread_static_simple_init();
  67. return 0;
  68. }
  69. #endif