msgq_sample.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * 程序清单:消息队列例程
  12. *
  13. * 这个程序会创建2个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
  14. * 息队列发送 普通消息和紧急消息。
  15. */
  16. #include <rtthread.h>
  17. #define THREAD_PRIORITY 25
  18. #define THREAD_TIMESLICE 5
  19. /* 消息队列控制块 */
  20. static struct rt_messagequeue mq;
  21. /* 消息队列中用到的放置消息的内存池 */
  22. static rt_uint8_t msg_pool[2048];
  23. #ifdef rt_align
  24. rt_align(RT_ALIGN_SIZE)
  25. #else
  26. ALIGN(RT_ALIGN_SIZE)
  27. #endif
  28. static char thread1_stack[1024];
  29. static struct rt_thread thread1;
  30. /* 线程1入口函数 */
  31. static void thread1_entry(void *parameter)
  32. {
  33. char buf = 0;
  34. rt_uint8_t cnt = 0;
  35. while (1)
  36. {
  37. /* 从消息队列中接收消息 */
  38. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1))
  39. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0)
  40. #else
  41. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  42. #endif
  43. {
  44. rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf);
  45. if (cnt == 19)
  46. {
  47. break;
  48. }
  49. }
  50. /* 延时50ms */
  51. cnt++;
  52. rt_thread_mdelay(50);
  53. }
  54. rt_kprintf("thread1: detach mq \n");
  55. rt_mq_detach(&mq);
  56. }
  57. #ifdef rt_align
  58. rt_align(RT_ALIGN_SIZE)
  59. #else
  60. ALIGN(RT_ALIGN_SIZE)
  61. #endif
  62. static char thread2_stack[1024];
  63. static struct rt_thread thread2;
  64. /* 线程2入口 */
  65. static void thread2_entry(void *parameter)
  66. {
  67. int result;
  68. char buf = 'A';
  69. rt_uint8_t cnt = 0;
  70. while (1)
  71. {
  72. if (cnt == 8)
  73. {
  74. /* 发送紧急消息到消息队列中 */
  75. result = rt_mq_urgent(&mq, &buf, 1);
  76. if (result != RT_EOK)
  77. {
  78. rt_kprintf("rt_mq_urgent ERR\n");
  79. }
  80. else
  81. {
  82. rt_kprintf("thread2: send urgent message - %c\n", buf);
  83. }
  84. }
  85. else if (cnt >= 20)/* 发送20次消息之后退出 */
  86. {
  87. rt_kprintf("message queue stop send, thread2 quit\n");
  88. break;
  89. }
  90. else
  91. {
  92. /* 发送消息到消息队列中 */
  93. result = rt_mq_send(&mq, &buf, 1);
  94. if (result != RT_EOK)
  95. {
  96. rt_kprintf("rt_mq_send ERR\n");
  97. }
  98. rt_kprintf("thread2: send message - %c\n", buf);
  99. }
  100. buf++;
  101. cnt++;
  102. /* 延时5ms */
  103. rt_thread_mdelay(5);
  104. }
  105. }
  106. /* 消息队列示例的初始化 */
  107. int msgq_sample(void)
  108. {
  109. rt_err_t result;
  110. /* 初始化消息队列 */
  111. result = rt_mq_init(&mq,
  112. "mqt",
  113. &msg_pool[0], /* 内存池指向msg_pool */
  114. 1, /* 每个消息的大小是 1 字节 */
  115. sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
  116. RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  117. if (result != RT_EOK)
  118. {
  119. rt_kprintf("init message queue failed.\n");
  120. return -1;
  121. }
  122. rt_thread_init(&thread1,
  123. "thread1",
  124. thread1_entry,
  125. RT_NULL,
  126. &thread1_stack[0],
  127. sizeof(thread1_stack),
  128. THREAD_PRIORITY, THREAD_TIMESLICE);
  129. #ifdef RT_USING_SMP
  130. /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
  131. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  132. #endif
  133. rt_thread_startup(&thread1);
  134. rt_thread_init(&thread2,
  135. "thread2",
  136. thread2_entry,
  137. RT_NULL,
  138. &thread2_stack[0],
  139. sizeof(thread2_stack),
  140. THREAD_PRIORITY, THREAD_TIMESLICE);
  141. #ifdef RT_USING_SMP
  142. /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
  143. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  144. #endif
  145. rt_thread_startup(&thread2);
  146. return 0;
  147. }
  148. /* 导出到 msh 命令列表中 */
  149. MSH_CMD_EXPORT(msgq_sample, msgq sample);