mailbox_sample.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo:
  13. *
  14. * This demo creates two threads and one boxmail (static):
  15. * 1) thread #1: receive mails
  16. * 2) thread #2: send mails
  17. *
  18. * read more:
  19. * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#mailbox
  20. */
  21. #include <rtthread.h>
  22. #define THREAD_PRIORITY 10
  23. #define THREAD_TIMESLICE 5
  24. /* mailbox control block */
  25. static struct rt_mailbox mb;
  26. /* memory pool for mails storage */
  27. static char mb_pool[128];
  28. static char mb_str1[] = "I'm a mail!";
  29. static char mb_str2[] = "this is another mail!";
  30. static char mb_str3[] = "over";
  31. #ifdef rt_align
  32. rt_align(RT_ALIGN_SIZE)
  33. #else
  34. ALIGN(RT_ALIGN_SIZE)
  35. #endif
  36. static char thread1_stack[1024];
  37. static struct rt_thread thread1;
  38. /* thread #1 entry function */
  39. static void thread1_entry(void *parameter)
  40. {
  41. char *str;
  42. while (1)
  43. {
  44. rt_kprintf("thread1: try to recv a mail\n");
  45. /* pend and receive mail(s) from mailbox */
  46. if (rt_mb_recv(&mb, (rt_ubase_t *)&str, RT_WAITING_FOREVER) == RT_EOK)
  47. {
  48. rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
  49. if (str == mb_str3)
  50. break;
  51. /* delay for 100ms */
  52. rt_thread_mdelay(100);
  53. }
  54. }
  55. /* detach mailbox */
  56. rt_mb_detach(&mb);
  57. }
  58. #ifdef rt_align
  59. rt_align(RT_ALIGN_SIZE)
  60. #else
  61. ALIGN(RT_ALIGN_SIZE)
  62. #endif
  63. static char thread2_stack[1024];
  64. static struct rt_thread thread2;
  65. /* thread #2 entry function */
  66. static void thread2_entry(void *parameter)
  67. {
  68. rt_uint8_t count;
  69. count = 0;
  70. while (count < 10)
  71. {
  72. count ++;
  73. if (count & 0x1)
  74. {
  75. /* send the 'mb_str1' variable's address to the mailbox */
  76. rt_mb_send(&mb, (rt_uint32_t)&mb_str1);
  77. }
  78. else
  79. {
  80. /* send the 'mb_str2' variable's address to the mailbox */
  81. rt_mb_send(&mb, (rt_uint32_t)&mb_str2);
  82. }
  83. /* delay for 200ms */
  84. rt_thread_mdelay(200);
  85. }
  86. /* send the 'mb_str3' variable's address to the mailbox */
  87. /* to inform thread #1 that thread #2 has finished running */
  88. rt_mb_send(&mb, (rt_uint32_t)&mb_str3);
  89. }
  90. /* mailbox(s) demo */
  91. int mailbox_sample(void)
  92. {
  93. rt_err_t result;
  94. /* initiate a mailbox */
  95. result = rt_mb_init(&mb,
  96. "mbt",
  97. &mb_pool[0],
  98. sizeof(mb_pool) / sizeof(rt_ubase_t), /* size of mails */
  99. RT_IPC_FLAG_PRIO);
  100. if (result != RT_EOK)
  101. {
  102. rt_kprintf("init mailbox failed.\n");
  103. return -1;
  104. }
  105. /* initiate thread #1 */
  106. rt_thread_init(&thread1,
  107. "thread1",
  108. thread1_entry,
  109. RT_NULL,
  110. &thread1_stack[0],
  111. sizeof(thread1_stack),
  112. THREAD_PRIORITY, THREAD_TIMESLICE);
  113. #ifdef RT_USING_SMP
  114. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  115. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  116. #endif
  117. rt_thread_startup(&thread1); /* start thread #1 */
  118. /*initiate thread #2 */
  119. rt_thread_init(&thread2,
  120. "thread2",
  121. thread2_entry,
  122. RT_NULL,
  123. &thread2_stack[0],
  124. sizeof(thread2_stack),
  125. THREAD_PRIORITY, THREAD_TIMESLICE);
  126. #ifdef RT_USING_SMP
  127. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  128. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  129. #endif
  130. rt_thread_startup(&thread2); /* start thread #2 */
  131. return 0;
  132. }
  133. /* export the msh command */
  134. MSH_CMD_EXPORT(mailbox_sample, mailbox sample);