priority_inversion.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  10. /*
  11. * 程序清单:互斥量使用例程
  12. *
  13. * 这个例子将创建 3 个动态线程以检查持有互斥量时,持有的线程优先级是否
  14. * 被调整到等待线程优先级中的最高优先级。
  15. *
  16. * 线程 1,2,3 的优先级从高到低分别被创建,
  17. * 线程 3 先持有互斥量,而后线程 2 试图持有互斥量,此时线程 3 的优先级应该
  18. * 被提升为和线程 2 的优先级相同。线程 1 用于检查线程 3 的优先级是否被提升
  19. * 为与线程 2的优先级相同。
  20. */
  21. #include <rtthread.h>
  22. /* 指向线程控制块的指针 */
  23. static rt_thread_t tid1 = RT_NULL;
  24. static rt_thread_t tid2 = RT_NULL;
  25. static rt_thread_t tid3 = RT_NULL;
  26. static rt_mutex_t mutex = RT_NULL;
  27. #define THREAD_PRIORITY 10
  28. #define THREAD_STACK_SIZE 512
  29. #define THREAD_TIMESLICE 5
  30. /* 线程 1 入口 */
  31. static void thread1_entry(void *parameter)
  32. {
  33. /* 先让低优先级线程运行 */
  34. rt_thread_mdelay(100);
  35. /* 此时 thread3 持有 mutex,并且 thread2 等待持有 mutex */
  36. /* 检查 rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]); 与 thread3 的优先级情况 */
  37. if (tid2->current_priority != tid3->current_priority)
  38. {
  39. /* 优先级不相同,测试失败 */
  40. rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority);
  41. rt_kprintf("the priority of thread3 is: %d\n", 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", tid2->current_priority);
  48. rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority);
  49. rt_kprintf("test OK.\n");
  50. }
  51. }
  52. /* 线程 2 入口 */
  53. static void thread2_entry(void *parameter)
  54. {
  55. rt_err_t result;
  56. rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority);
  57. /* 先让低优先级线程运行 */
  58. rt_thread_mdelay(50);
  59. /*
  60. * 试图持有互斥锁,此时 thread3 持有,应把 thread3 的优先级提升
  61. * 到 thread2 相同的优先级
  62. */
  63. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  64. if (result == RT_EOK)
  65. {
  66. /* 释放互斥锁 */
  67. rt_mutex_release(mutex);
  68. }
  69. }
  70. /* 线程 3 入口 */
  71. static void thread3_entry(void *parameter)
  72. {
  73. rt_tick_t tick;
  74. rt_err_t result;
  75. rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority);
  76. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  77. if (result != RT_EOK)
  78. {
  79. rt_kprintf("thread3 take a mutex, failed.\n");
  80. }
  81. /* 做一个长时间的循环,500ms */
  82. tick = rt_tick_get();
  83. while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ;
  84. rt_mutex_release(mutex);
  85. }
  86. int pri_inversion(void)
  87. {
  88. /* 创建互斥锁 */
  89. mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
  90. if (mutex == RT_NULL)
  91. {
  92. rt_kprintf("create dynamic mutex failed.\n");
  93. return -1;
  94. }
  95. /* 创建线程 1 */
  96. tid1 = rt_thread_create("thread1",
  97. thread1_entry,
  98. RT_NULL,
  99. THREAD_STACK_SIZE,
  100. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  101. if (tid1 != RT_NULL)
  102. rt_thread_startup(tid1);
  103. /* 创建线程 2 */
  104. tid2 = rt_thread_create("thread2",
  105. thread2_entry,
  106. RT_NULL,
  107. THREAD_STACK_SIZE,
  108. THREAD_PRIORITY, THREAD_TIMESLICE);
  109. if (tid2 != RT_NULL)
  110. rt_thread_startup(tid2);
  111. /* 创建线程 3 */
  112. tid3 = rt_thread_create("thread3",
  113. thread3_entry,
  114. RT_NULL,
  115. THREAD_STACK_SIZE,
  116. THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  117. if (tid3 != RT_NULL)
  118. rt_thread_startup(tid3);
  119. return 0;
  120. }
  121. /* 导出到 msh 命令列表中 */
  122. MSH_CMD_EXPORT(pri_inversion, pri_inversion sample);