1
0

mutex_simple.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /* 指向线程控制块的指针 */
  12. static rt_thread_t tid1 = RT_NULL;
  13. static rt_thread_t tid2 = RT_NULL;
  14. static rt_thread_t tid3 = RT_NULL;
  15. static rt_mutex_t mutex = RT_NULL;
  16. /* 线程1入口 */
  17. static void thread1_entry(void* parameter)
  18. {
  19. /* 先让低优先级线程运行 */
  20. rt_thread_delay(10);
  21. /* 此时thread3持有mutex,并且thread2等待持有mutex */
  22. /* 检查thread2与thread3的优先级情况 */
  23. if (tid2->current_priority != tid3->current_priority)
  24. {
  25. /* 优先级不相同,测试失败 */
  26. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  27. return;
  28. }
  29. }
  30. /* 线程2入口 */
  31. static void thread2_entry(void* parameter)
  32. {
  33. rt_err_t result;
  34. /* 先让低优先级线程运行 */
  35. rt_thread_delay(5);
  36. while (1)
  37. {
  38. /*
  39. * 试图持有互斥锁,此时thread3持有,应把thread3的优先级提升到thread2相同
  40. * 的优先级
  41. */
  42. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  43. if (result == RT_EOK)
  44. {
  45. /* 释放互斥锁 */
  46. rt_mutex_release(mutex);
  47. }
  48. }
  49. }
  50. /* 线程3入口 */
  51. static void thread3_entry(void* parameter)
  52. {
  53. rt_tick_t tick;
  54. rt_err_t result;
  55. while (1)
  56. {
  57. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  58. if (result != RT_EOK)
  59. {
  60. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  61. }
  62. result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
  63. if (result != RT_EOK)
  64. {
  65. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  66. }
  67. /* 做一个长时间的循环,总共50个OS Tick */
  68. tick = rt_tick_get();
  69. while (rt_tick_get() - tick < 50) ;
  70. rt_mutex_release(mutex);
  71. rt_mutex_release(mutex);
  72. }
  73. }
  74. int mutex_simple_init()
  75. {
  76. /* 创建互斥锁 */
  77. mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO);
  78. if (mutex == RT_NULL)
  79. {
  80. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  81. return 0;
  82. }
  83. /* 创建线程1 */
  84. tid1 = rt_thread_create("t1",
  85. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  86. THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  87. if (tid1 != RT_NULL)
  88. rt_thread_startup(tid1);
  89. else
  90. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  91. /* 创建线程2 */
  92. tid2 = rt_thread_create("t2",
  93. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  94. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  95. if (tid2 != RT_NULL)
  96. rt_thread_startup(tid2);
  97. else
  98. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  99. /* 创建线程3 */
  100. tid3 = rt_thread_create("t3",
  101. thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
  102. THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  103. if (tid3 != RT_NULL)
  104. rt_thread_startup(tid3);
  105. else
  106. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  107. return 0;
  108. }
  109. #ifdef RT_USING_TC
  110. static void _tc_cleanup()
  111. {
  112. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  113. rt_enter_critical();
  114. /* 删除线程 */
  115. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  116. rt_thread_delete(tid1);
  117. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  118. rt_thread_delete(tid2);
  119. if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
  120. rt_thread_delete(tid3);
  121. if (mutex != RT_NULL)
  122. {
  123. rt_mutex_delete(mutex);
  124. }
  125. /* 调度器解锁 */
  126. rt_exit_critical();
  127. /* 设置TestCase状态 */
  128. tc_done(TC_STAT_PASSED);
  129. }
  130. int _tc_mutex_simple()
  131. {
  132. /* 设置TestCase清理回调函数 */
  133. tc_cleanup(_tc_cleanup);
  134. mutex_simple_init();
  135. /* 返回TestCase运行的最长时间 */
  136. return 100;
  137. }
  138. /* 输出函数命令到finsh shell中 */
  139. FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example);
  140. #else
  141. /* 用户应用入口 */
  142. int rt_application_init()
  143. {
  144. mutex_simple_init();
  145. return 0;
  146. }
  147. #endif