mailbox_sample.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  10. /*
  11. * 程序清单:邮箱例程
  12. *
  13. * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
  14. * 一个线程往邮箱中收取邮件。
  15. */
  16. #include <rtthread.h>
  17. #define THREAD_PRIORITY 10
  18. #define THREAD_TIMESLICE 5
  19. /* 邮箱控制块 */
  20. static struct rt_mailbox mb;
  21. /* 用于放邮件的内存池 */
  22. static char mb_pool[128];
  23. static char mb_str1[] = "I'm a mail!";
  24. static char mb_str2[] = "this is another mail!";
  25. static char mb_str3[] = "over";
  26. ALIGN(RT_ALIGN_SIZE)
  27. static char thread1_stack[1024];
  28. static struct rt_thread thread1;
  29. /* 线程1入口 */
  30. static void thread1_entry(void *parameter)
  31. {
  32. char *str;
  33. while (1)
  34. {
  35. rt_kprintf("thread1: try to recv a mail\n");
  36. /* 从邮箱中收取邮件 */
  37. if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK)
  38. {
  39. rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
  40. if (str == mb_str3)
  41. break;
  42. /* 延时100ms */
  43. rt_thread_mdelay(100);
  44. }
  45. }
  46. /* 执行邮箱对象脱离 */
  47. rt_mb_detach(&mb);
  48. }
  49. ALIGN(RT_ALIGN_SIZE)
  50. static char thread2_stack[1024];
  51. static struct rt_thread thread2;
  52. /* 线程2入口 */
  53. static void thread2_entry(void *parameter)
  54. {
  55. rt_uint8_t count;
  56. count = 0;
  57. while (count < 10)
  58. {
  59. count ++;
  60. if (count & 0x1)
  61. {
  62. /* 发送mb_str1地址到邮箱中 */
  63. rt_mb_send(&mb, (rt_uint32_t)&mb_str1);
  64. }
  65. else
  66. {
  67. /* 发送mb_str2地址到邮箱中 */
  68. rt_mb_send(&mb, (rt_uint32_t)&mb_str2);
  69. }
  70. /* 延时200ms */
  71. rt_thread_mdelay(200);
  72. }
  73. /* 发送邮件告诉线程1,线程2已经运行结束 */
  74. rt_mb_send(&mb, (rt_uint32_t)&mb_str3);
  75. }
  76. int mailbox_sample(void)
  77. {
  78. rt_err_t result;
  79. /* 初始化一个mailbox */
  80. result = rt_mb_init(&mb,
  81. "mbt", /* 名称是mbt */
  82. &mb_pool[0], /* 邮箱用到的内存池是mb_pool */
  83. sizeof(mb_pool) / 4, /* 邮箱中的邮件数目,因为一封邮件占4字节 */
  84. RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
  85. if (result != RT_EOK)
  86. {
  87. rt_kprintf("init mailbox failed.\n");
  88. return -1;
  89. }
  90. rt_thread_init(&thread1,
  91. "thread1",
  92. thread1_entry,
  93. RT_NULL,
  94. &thread1_stack[0],
  95. sizeof(thread1_stack),
  96. THREAD_PRIORITY, THREAD_TIMESLICE);
  97. rt_thread_startup(&thread1);
  98. rt_thread_init(&thread2,
  99. "thread2",
  100. thread2_entry,
  101. RT_NULL,
  102. &thread2_stack[0],
  103. sizeof(thread2_stack),
  104. THREAD_PRIORITY, THREAD_TIMESLICE);
  105. rt_thread_startup(&thread2);
  106. return 0;
  107. }
  108. /* 导出到 msh 命令列表中 */
  109. MSH_CMD_EXPORT(mailbox_sample, mailbox sample);