priority_inversion.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: prevent priority inversions
  13. *
  14. * This demo creates 3 threads and 1 mutex to
  15. * demonstrate how the mutex prevents priority inversions.
  16. *
  17. * read more:
  18. * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#mutex
  19. */
  20. #include <rtthread.h>
  21. /* thread(s) handler */
  22. static rt_thread_t tid1 = RT_NULL;
  23. static rt_thread_t tid2 = RT_NULL;
  24. static rt_thread_t tid3 = RT_NULL;
  25. static rt_mutex_t mutex = RT_NULL;
  26. #define THREAD_PRIORITY 10
  27. #define THREAD_STACK_SIZE 512
  28. #define THREAD_TIMESLICE 5
  29. /* thread #1 entry function */
  30. static void thread1_entry(void *parameter)
  31. {
  32. /* let the lower priority thread run first */
  33. rt_thread_mdelay(100);
  34. /* at this point, thread #3 holds the mutex and thread #2 is pending on holding the mutex */
  35. /* check the priority level of thread #2 and thread #3 */
  36. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
  37. if (RT_SCHED_PRIV(tid2).current_priority != RT_SCHED_PRIV(tid3).current_priority)
  38. {
  39. /* failure */
  40. rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority);
  41. rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority);
  42. rt_kprintf("test failed.\n");
  43. return;
  44. }
  45. else
  46. {
  47. rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority);
  48. rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority);
  49. rt_kprintf("test OK.\n");
  50. }
  51. #else
  52. if (tid2->current_priority != tid3->current_priority)
  53. {
  54. /* failure */
  55. rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority);
  56. rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority);
  57. rt_kprintf("test failed.\n");
  58. return;
  59. }
  60. else
  61. {
  62. rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority);
  63. rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority);
  64. rt_kprintf("test OK.\n");
  65. }
  66. #endif
  67. }
  68. /* thread #2 entry function */
  69. static void thread2_entry(void *parameter)
  70. {
  71. rt_err_t result;
  72. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
  73. rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority);
  74. #else
  75. rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority);
  76. #endif
  77. /* let the lower priority thread run first */
  78. rt_thread_mdelay(50);
  79. /*
  80. * pending the mutex
  81. * At this time, kernel raises the priority of thread #3 to same priority
  82. * as thread #2
  83. */
  84. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  85. if (result == RT_EOK)
  86. {
  87. /* release the mutex */
  88. rt_mutex_release(mutex);
  89. }
  90. }
  91. /* thread #3 entry function */
  92. static void thread3_entry(void *parameter)
  93. {
  94. rt_tick_t tick;
  95. rt_err_t result;
  96. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
  97. rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority);
  98. #else
  99. rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority);
  100. #endif
  101. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  102. if (result != RT_EOK)
  103. {
  104. rt_kprintf("thread3 take a mutex, failed.\n");
  105. }
  106. /* delay for 500ms without scheduling */
  107. tick = rt_tick_get();
  108. while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ;
  109. /* release the mutex */
  110. rt_mutex_release(mutex);
  111. }
  112. int pri_inversion(void)
  113. {
  114. /* create mutex */
  115. mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO);
  116. if (mutex == RT_NULL)
  117. {
  118. rt_kprintf("create dynamic mutex failed.\n");
  119. return -1;
  120. }
  121. /* create thread #1 */
  122. tid1 = rt_thread_create("thread1",
  123. thread1_entry,
  124. RT_NULL,
  125. THREAD_STACK_SIZE,
  126. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  127. #ifdef RT_USING_SMP
  128. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  129. rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  130. #endif
  131. if (tid1 != RT_NULL)
  132. rt_thread_startup(tid1);
  133. /* create thread #2 */
  134. tid2 = rt_thread_create("thread2",
  135. thread2_entry,
  136. RT_NULL,
  137. THREAD_STACK_SIZE,
  138. THREAD_PRIORITY, THREAD_TIMESLICE);
  139. #ifdef RT_USING_SMP
  140. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  141. rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  142. #endif
  143. if (tid2 != RT_NULL)
  144. rt_thread_startup(tid2);
  145. /* create thread #3 */
  146. tid3 = rt_thread_create("thread3",
  147. thread3_entry,
  148. RT_NULL,
  149. THREAD_STACK_SIZE,
  150. THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  151. #ifdef RT_USING_SMP
  152. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  153. rt_thread_control(tid3, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  154. #endif
  155. if (tid3 != RT_NULL)
  156. rt_thread_startup(tid3);
  157. return 0;
  158. }
  159. /* export the msh command */
  160. MSH_CMD_EXPORT(pri_inversion, pri_inversion sample);