semaphore_static.c 3.2 KB

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