mailbox_sample.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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:
  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/thread-comm/thread-comm/#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. ALIGN(RT_ALIGN_SIZE)
  32. static char thread1_stack[1024];
  33. static struct rt_thread thread1;
  34. /* thread #1 entry function */
  35. static void thread1_entry(void *parameter)
  36. {
  37. char *str;
  38. while (1)
  39. {
  40. rt_kprintf("thread1: try to recv a mail\n");
  41. /* pend and receive mail(s) from mailbox */
  42. if (rt_mb_recv(&mb, (rt_ubase_t *)&str, RT_WAITING_FOREVER) == RT_EOK)
  43. {
  44. rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
  45. if (str == mb_str3)
  46. break;
  47. /* delay for 100ms */
  48. rt_thread_mdelay(100);
  49. }
  50. }
  51. /* detach mailbox */
  52. rt_mb_detach(&mb);
  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. rt_uint8_t count;
  61. count = 0;
  62. while (count < 10)
  63. {
  64. count ++;
  65. if (count & 0x1)
  66. {
  67. /* send the 'mb_str1' variable's address to the mailbox */
  68. rt_mb_send(&mb, (rt_uint32_t)&mb_str1);
  69. }
  70. else
  71. {
  72. /* send the 'mb_str2' variable's address to the mailbox */
  73. rt_mb_send(&mb, (rt_uint32_t)&mb_str2);
  74. }
  75. /* delay for 200ms */
  76. rt_thread_mdelay(200);
  77. }
  78. /* send the 'mb_str3' variable's address to the mailbox */
  79. /* to inform thread #1 that thread #2 has finished running */
  80. rt_mb_send(&mb, (rt_uint32_t)&mb_str3);
  81. }
  82. /* mailbox(s) demo */
  83. int mailbox_sample(void)
  84. {
  85. rt_err_t result;
  86. /* initiate a mailbox */
  87. result = rt_mb_init(&mb,
  88. "mbt",
  89. &mb_pool[0],
  90. sizeof(mb_pool) / sizeof(rt_ubase_t), /* size of mails */
  91. RT_IPC_FLAG_FIFO);
  92. if (result != RT_EOK)
  93. {
  94. rt_kprintf("init mailbox failed.\n");
  95. return -1;
  96. }
  97. /* initiate thread #1 */
  98. rt_thread_init(&thread1,
  99. "thread1",
  100. thread1_entry,
  101. RT_NULL,
  102. &thread1_stack[0],
  103. sizeof(thread1_stack),
  104. THREAD_PRIORITY, THREAD_TIMESLICE);
  105. rt_thread_startup(&thread1); /* start thread #1 */
  106. /*initiate thread #2 */
  107. rt_thread_init(&thread2,
  108. "thread2",
  109. thread2_entry,
  110. RT_NULL,
  111. &thread2_stack[0],
  112. sizeof(thread2_stack),
  113. THREAD_PRIORITY, THREAD_TIMESLICE);
  114. rt_thread_startup(&thread2); /* start thread #2 */
  115. return 0;
  116. }
  117. /* export the msh command */
  118. MSH_CMD_EXPORT(mailbox_sample, mailbox sample);