semaphore_priority.c 2.9 KB

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