thread_same_priority.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. static rt_uint32_t t1_count = 0;
  8. 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. if (t1_count / t2_count != 2)
  58. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  59. }
  60. int _tc_thread_same_priority()
  61. {
  62. t1_count = 0;
  63. t2_count = 0;
  64. /* set tc cleanup */
  65. tc_cleanup(_tc_cleanup);
  66. thread_same_priority_init();
  67. return 100;
  68. }
  69. FINSH_FUNCTION_EXPORT(_tc_thread_same_priority, a same priority thread test);
  70. #else
  71. int rt_application_init()
  72. {
  73. thread_same_priority_init();
  74. return 0;
  75. }
  76. #endif