thread_priority.c 2.1 KB

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