thread_priority.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <rtthread.h>
  10. #include "tc_comm.h"
  11. struct rt_thread thread1;
  12. struct rt_thread thread2;
  13. static char thread1_stack[THREAD_STACK_SIZE];
  14. static char thread2_stack[THREAD_STACK_SIZE];
  15. static rt_uint32_t count = 0;
  16. /*
  17. * the priority of thread1 > the priority of thread2
  18. */
  19. static void thread1_entry(void* parameter)
  20. {
  21. while (1)
  22. {
  23. count ++;
  24. rt_kprintf("count = %d\n", count);
  25. rt_thread_delay(10);
  26. }
  27. }
  28. static void thread2_entry(void* parameter)
  29. {
  30. rt_tick_t tick;
  31. tick = rt_tick_get();
  32. while (1)
  33. {
  34. if (rt_tick_get() - tick >= 50)
  35. {
  36. if (count == 0)
  37. tc_done(TC_STAT_FAILED);
  38. else
  39. tc_done(TC_STAT_PASSED);
  40. break;
  41. }
  42. }
  43. }
  44. int thread_priority_init()
  45. {
  46. rt_err_t result;
  47. result = rt_thread_init(&thread1,
  48. "t1",
  49. thread1_entry, RT_NULL,
  50. &thread1_stack[0], sizeof(thread1_stack),
  51. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  52. if (result == RT_EOK)
  53. rt_thread_startup(&thread1);
  54. else
  55. tc_stat(TC_STAT_FAILED);
  56. rt_thread_init(&thread2,
  57. "t2",
  58. thread2_entry, RT_NULL,
  59. &thread2_stack[0], sizeof(thread2_stack),
  60. THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  61. if (result == RT_EOK)
  62. rt_thread_startup(&thread2);
  63. else
  64. tc_stat(TC_STAT_FAILED);
  65. return 0;
  66. }
  67. #ifdef RT_USING_TC
  68. static void _tc_cleanup()
  69. {
  70. /* lock scheduler */
  71. rt_enter_critical();
  72. if (thread1.stat != RT_THREAD_CLOSE)
  73. rt_thread_detach(&thread1);
  74. if (thread2.stat != RT_THREAD_CLOSE)
  75. rt_thread_detach(&thread2);
  76. /* unlock scheduler */
  77. rt_exit_critical();
  78. }
  79. int _tc_thread_priority()
  80. {
  81. count = 0;
  82. /* set tc cleanup */
  83. tc_cleanup(_tc_cleanup);
  84. thread_priority_init();
  85. return RT_TICK_PER_SECOND;
  86. }
  87. FINSH_FUNCTION_EXPORT(_tc_thread_priority, a priority thread test);
  88. #else
  89. int rt_application_init()
  90. {
  91. thread_priority_init();
  92. return 0;
  93. }
  94. #endif