semaphore_priority.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <rtthread.h>
  2. #include "tc_comm.h"
  3. static rt_sem_t sem;
  4. static rt_uint8_t t1_count, t2_count;
  5. static rt_thread_t t1, t2, worker;
  6. static void thread1_entry(void* parameter)
  7. {
  8. rt_err_t result;
  9. while (1)
  10. {
  11. result = rt_sem_take(sem, RT_WAITING_FOREVER);
  12. if (result != RT_EOK)
  13. {
  14. tc_done(TC_STAT_FAILED);
  15. return;
  16. }
  17. t1_count ++;
  18. rt_kprintf("thread1: got semaphore, count: %d\n", t1_count);
  19. }
  20. }
  21. static void thread2_entry(void* parameter)
  22. {
  23. rt_err_t result;
  24. while (1)
  25. {
  26. result = rt_sem_take(sem, RT_WAITING_FOREVER);
  27. if (result != RT_EOK)
  28. {
  29. tc_done(TC_STAT_FAILED);
  30. return;
  31. }
  32. t2_count ++;
  33. rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
  34. }
  35. }
  36. static void worker_thread_entry(void* parameter)
  37. {
  38. rt_thread_delay(10);
  39. while (1)
  40. {
  41. rt_sem_release(sem);
  42. rt_thread_delay(5);
  43. }
  44. }
  45. int semaphore_priority_init()
  46. {
  47. sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO);
  48. if (sem == RT_NULL)
  49. {
  50. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  51. return 0;
  52. }
  53. t1_count = t2_count = 0;
  54. t1 = rt_thread_create("t1",
  55. thread1_entry, RT_NULL,
  56. THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  57. if (t1 != RT_NULL)
  58. rt_thread_startup(t1);
  59. else
  60. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  61. t2 = rt_thread_create("t2",
  62. thread2_entry, RT_NULL,
  63. THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  64. if (t2 != RT_NULL)
  65. rt_thread_startup(t2);
  66. else
  67. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  68. worker = rt_thread_create("worker",
  69. worker_thread_entry, RT_NULL,
  70. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  71. if (worker != RT_NULL)
  72. rt_thread_startup(worker);
  73. else
  74. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  75. return 0;
  76. }
  77. #ifdef RT_USING_TC
  78. static void _tc_cleanup()
  79. {
  80. /* lock scheduler */
  81. rt_enter_critical();
  82. /* delete t1, t2 and worker thread */
  83. rt_thread_delete(t1);
  84. rt_thread_delete(t2);
  85. rt_thread_delete(worker);
  86. if (sem)
  87. {
  88. rt_sem_delete(sem);
  89. sem = RT_NULL;
  90. }
  91. if (t1_count > t2_count)
  92. tc_done(TC_STAT_FAILED);
  93. else
  94. tc_done(TC_STAT_PASSED);
  95. /* unlock scheduler */
  96. rt_exit_critical();
  97. }
  98. int _tc_semaphore_priority()
  99. {
  100. /* set tc cleanup */
  101. tc_cleanup(_tc_cleanup);
  102. semaphore_priority_init();
  103. return 50;
  104. }
  105. FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test);
  106. #else
  107. int rt_application_init()
  108. {
  109. semaphore_priority_init();
  110. return 0;
  111. }
  112. #endif