msgq_sample.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2018, 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. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: message queue
  13. *
  14. * This demo creates two threads and one message queue:
  15. * 1) thread #1: receive message(s) from message queue
  16. * 2) thread #2: send normal and urgent messages to message queue
  17. *
  18. * read more:
  19. * https://www.rt-thread.io/document/site/thread-comm/thread-comm/#message-queue
  20. */
  21. #include <rtthread.h>
  22. #define THREAD_PRIORITY 25
  23. #define THREAD_TIMESLICE 5
  24. /* message queue control block */
  25. static struct rt_messagequeue mq;
  26. /* the memory pool used to place messages in the message queue */
  27. static rt_uint8_t msg_pool[2048];
  28. ALIGN(RT_ALIGN_SIZE)
  29. static char thread1_stack[1024];
  30. static struct rt_thread thread1;
  31. /* thread #1 entry function */
  32. static void thread1_entry(void *parameter)
  33. {
  34. char buf = 0;
  35. rt_uint8_t cnt = 0;
  36. while (1)
  37. {
  38. /* pend and receive message(s) from message queue */
  39. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  40. {
  41. rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf);
  42. if (cnt == 19)
  43. {
  44. break;
  45. }
  46. }
  47. cnt++;
  48. /* delay for 50ms */
  49. rt_thread_mdelay(50);
  50. }
  51. rt_kprintf("thread1: detach mq \n");
  52. rt_mq_detach(&mq);
  53. }
  54. ALIGN(RT_ALIGN_SIZE)
  55. static char thread2_stack[1024];
  56. static struct rt_thread thread2;
  57. /* thread #2 entry function */
  58. static void thread2_entry(void *parameter)
  59. {
  60. int result;
  61. char buf = 'A';
  62. rt_uint8_t cnt = 0;
  63. while (1)
  64. {
  65. if (cnt == 8)
  66. {
  67. /* send one URGENT message to the message queue */
  68. result = rt_mq_urgent(&mq, &buf, 1);
  69. if (result != RT_EOK)
  70. {
  71. rt_kprintf("rt_mq_urgent ERR\n");
  72. }
  73. else
  74. {
  75. rt_kprintf("thread2: send urgent message - %c\n", buf);
  76. }
  77. }
  78. else if (cnt >= 20) /* exit */
  79. {
  80. rt_kprintf("message queue stop send, thread2 quit\n");
  81. break;
  82. }
  83. else
  84. {
  85. /* send one message to the message queue */
  86. result = rt_mq_send(&mq, &buf, 1);
  87. if (result != RT_EOK)
  88. {
  89. rt_kprintf("rt_mq_send ERR\n");
  90. }
  91. rt_kprintf("thread2: send message - %c\n", buf);
  92. }
  93. buf++;
  94. cnt++;
  95. /* delay for 5ms */
  96. rt_thread_mdelay(5);
  97. }
  98. }
  99. int msgq_sample(void)
  100. {
  101. rt_err_t result;
  102. /* initiate a message queue */
  103. result = rt_mq_init(&mq,
  104. "mqt",
  105. &msg_pool[0], /* msg_pool's address */
  106. 1, /* the size of each message is 1 byte */
  107. sizeof(msg_pool), /* The size of the memory pool is the size of msg_pool */
  108. RT_IPC_FLAG_FIFO);
  109. if (result != RT_EOK)
  110. {
  111. rt_kprintf("init message queue failed.\n");
  112. return -1;
  113. }
  114. rt_thread_init(&thread1,
  115. "thread1",
  116. thread1_entry,
  117. RT_NULL,
  118. &thread1_stack[0],
  119. sizeof(thread1_stack),
  120. THREAD_PRIORITY, THREAD_TIMESLICE);
  121. rt_thread_startup(&thread1);
  122. rt_thread_init(&thread2,
  123. "thread2",
  124. thread2_entry,
  125. RT_NULL,
  126. &thread2_stack[0],
  127. sizeof(thread2_stack),
  128. THREAD_PRIORITY, THREAD_TIMESLICE);
  129. rt_thread_startup(&thread2);
  130. return 0;
  131. }
  132. /* export the msh command */
  133. MSH_CMD_EXPORT(msgq_sample, msgq sample);