semaphore_static.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /*
  10. * 程序清单:静态信号量
  11. *
  12. * 这个例子中将创建一个静态信号量(初始值为0 )及一个静态线程,在这个静态线程中
  13. * 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
  14. * 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
  15. */
  16. #include <rtthread.h>
  17. #include "tc_comm.h"
  18. /* 线程控制块及栈 */
  19. static struct rt_thread thread;
  20. static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
  21. /* 信号量控制块 */
  22. static struct rt_semaphore sem;
  23. /* 线程入口 */
  24. static void thread_entry(void* parameter)
  25. {
  26. rt_err_t result;
  27. rt_tick_t tick;
  28. /* 获得当前的OS Tick */
  29. tick = rt_tick_get();
  30. /* 试图持有信号量,最大等待10个OS Tick后返回 */
  31. result = rt_sem_take(&sem, 10);
  32. if (result == -RT_ETIMEOUT)
  33. {
  34. rt_tick_t new_tick = rt_tick_get();
  35. /* 可以有两个 tick 的误差 */
  36. if (new_tick - tick >= 12)
  37. {
  38. rt_kprintf("tick error to large: expect: 10, get %d\n",
  39. new_tick - tick);
  40. tc_done(TC_STAT_FAILED);
  41. rt_sem_detach(&sem);
  42. return;
  43. }
  44. rt_kprintf("take semaphore timeout\n");
  45. }
  46. else
  47. {
  48. /* 因为没有其他地方是否信号量,所以不应该成功持有信号量,否则测试失败 */
  49. tc_done(TC_STAT_FAILED);
  50. rt_sem_detach(&sem);
  51. return;
  52. }
  53. /* 释放一次信号量 */
  54. rt_sem_release(&sem);
  55. /* 永久等待方式持有信号量 */
  56. result = rt_sem_take(&sem, RT_WAITING_FOREVER);
  57. if (result != RT_EOK)
  58. {
  59. /* 不成功则测试失败 */
  60. tc_done(TC_STAT_FAILED);
  61. rt_sem_detach(&sem);
  62. return;
  63. }
  64. /* 测试通过 */
  65. tc_done(TC_STAT_PASSED);
  66. /* 脱离信号量对象 */
  67. rt_sem_detach(&sem);
  68. }
  69. int semaphore_static_init(void)
  70. {
  71. rt_err_t result;
  72. /* 初始化信号量,初始值是0 */
  73. result = rt_sem_init(&sem, "sem", 0, RT_IPC_FLAG_PRIO);
  74. if (result != RT_EOK)
  75. {
  76. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  77. return 0;
  78. }
  79. /* 初始化线程1 */
  80. result = rt_thread_init(&thread, "thread", /* 线程名:thread */
  81. thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是RT_NULL*/
  82. &thread_stack[0], sizeof(thread_stack), /* 线程栈是thread_stack */
  83. THREAD_PRIORITY, 10);
  84. if (result == RT_EOK) /* 如果返回正确,启动线程1 */
  85. rt_thread_startup(&thread);
  86. else
  87. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  88. return 0;
  89. }
  90. #ifdef RT_USING_TC
  91. static void _tc_cleanup(void)
  92. {
  93. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  94. rt_enter_critical();
  95. /* 执行线程脱离 */
  96. if (thread.stat != RT_THREAD_CLOSE)
  97. {
  98. rt_thread_detach(&thread);
  99. /* 执行信号量对象脱离 */
  100. rt_sem_detach(&sem);
  101. }
  102. /* 调度器解锁 */
  103. rt_exit_critical();
  104. /* 设置TestCase状态 */
  105. tc_done(TC_STAT_PASSED);
  106. }
  107. int _tc_semaphore_static(void)
  108. {
  109. /* 设置TestCase清理回调函数 */
  110. tc_cleanup(_tc_cleanup);
  111. semaphore_static_init();
  112. /* 返回TestCase运行的最长时间 */
  113. return 100;
  114. }
  115. /* 输出函数命令到finsh shell中 */
  116. FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example);
  117. #else
  118. /* 用户应用入口 */
  119. int rt_application_init(void)
  120. {
  121. semaphore_static_init();
  122. return 0;
  123. }
  124. #endif