1
0

messageq_simple.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
  13. * 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
  14. */
  15. #include <rtthread.h>
  16. #include "tc_comm.h"
  17. /* 指向线程控制块的指针 */
  18. static rt_thread_t tid1 = RT_NULL;
  19. static rt_thread_t tid2 = RT_NULL;
  20. static rt_thread_t tid3 = RT_NULL;
  21. /* 消息队列控制块 */
  22. static struct rt_messagequeue mq;
  23. /* 消息队列中用到的放置消息的内存池 */
  24. static char msg_pool[2048];
  25. /* 线程1入口函数 */
  26. static void thread1_entry(void* parameter)
  27. {
  28. char buf[128];
  29. while (1)
  30. {
  31. rt_memset(&buf[0], 0, sizeof(buf));
  32. /* 从消息队列中接收消息 */
  33. if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  34. {
  35. rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
  36. }
  37. /* 延迟10个OS Tick */
  38. rt_thread_delay(10);
  39. }
  40. }
  41. /* 线程2入口函数 */
  42. static void thread2_entry(void* parameter)
  43. {
  44. int i, result;
  45. char buf[] = "this is message No.x";
  46. while (1)
  47. {
  48. for (i = 0; i < 10; i++)
  49. {
  50. buf[sizeof(buf) - 2] = '0' + i;
  51. rt_kprintf("thread2: send message - %s\n", buf);
  52. /* 发送消息到消息队列中 */
  53. result = rt_mq_send(&mq, &buf[0], sizeof(buf));
  54. if ( result == -RT_EFULL)
  55. {
  56. /* 消息队列满, 延迟1s时间 */
  57. rt_kprintf("message queue full, delay 1s\n");
  58. rt_thread_delay(100);
  59. }
  60. }
  61. /* 延时10个OS Tick */
  62. rt_thread_delay(10);
  63. }
  64. }
  65. /* 线程3入口函数 */
  66. static void thread3_entry(void* parameter)
  67. {
  68. char buf[] = "this is an urgent message!";
  69. while (1)
  70. {
  71. rt_kprintf("thread3: send an urgent message\n");
  72. /* 发送紧急消息到消息队列中 */
  73. rt_mq_urgent(&mq, &buf[0], sizeof(buf));
  74. /* 延时25个OS Tick */
  75. rt_thread_delay(25);
  76. }
  77. }
  78. int messageq_simple_init()
  79. {
  80. /* 初始化消息队列 */
  81. rt_mq_init(&mq, "mqt",
  82. &msg_pool[0], /* 内存池指向msg_pool */
  83. 128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
  84. sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
  85. RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照优先级由高到低的方法分配消息 */
  86. /* 创建线程1 */
  87. tid1 = rt_thread_create("t1",
  88. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  89. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  90. if (tid1 != RT_NULL)
  91. rt_thread_startup(tid1);
  92. else
  93. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  94. /* 创建线程2 */
  95. tid2 = rt_thread_create("t2",
  96. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  97. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  98. if (tid2 != RT_NULL)
  99. rt_thread_startup(tid2);
  100. else
  101. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  102. /* 创建线程3 */
  103. tid3 = rt_thread_create("t3",
  104. thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  105. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  106. if (tid3 != RT_NULL)
  107. rt_thread_startup(tid3);
  108. else
  109. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  110. return 0;
  111. }
  112. #ifdef RT_USING_TC
  113. static void _tc_cleanup()
  114. {
  115. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  116. rt_enter_critical();
  117. /* 删除线程 */
  118. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  119. rt_thread_delete(tid1);
  120. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  121. rt_thread_delete(tid2);
  122. if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
  123. rt_thread_delete(tid3);
  124. /* 执行消息队列对象脱离 */
  125. rt_mq_detach(&mq);
  126. /* 调度器解锁 */
  127. rt_exit_critical();
  128. /* 设置TestCase状态 */
  129. tc_done(TC_STAT_PASSED);
  130. }
  131. int _tc_messageq_simple()
  132. {
  133. /* 设置TestCase清理回调函数 */
  134. tc_cleanup(_tc_cleanup);
  135. messageq_simple_init();
  136. /* 返回TestCase运行的最长时间 */
  137. return 100;
  138. }
  139. /* 输出函数命令到finsh shell中 */
  140. FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example);
  141. #else
  142. /* 用户应用入口 */
  143. int rt_application_init()
  144. {
  145. messageq_simple_init();
  146. return 0;
  147. }
  148. #endif