thread_same_priority.c 2.0 KB

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