mutex_simple.c 3.7 KB

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