thread_same_priority.c 1.9 KB

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