mbox_simple.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * 程序清单:邮箱例程
  3. *
  4. * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
  5. * 一个线程往邮箱中收取邮件。
  6. */
  7. #include <rtthread.h>
  8. #include "tc_comm.h"
  9. /* 指向线程控制块的指针 */
  10. static rt_thread_t tid1 = RT_NULL;
  11. static rt_thread_t tid2 = RT_NULL;
  12. static struct rt_mailbox mb;
  13. static char mb_pool[128];
  14. static char mb_str1[] = "I'm a mail!";
  15. static char mb_str2[] = "this is another mail!";
  16. /* 线程1入口 */
  17. static void thread1_entry(void* parameter)
  18. {
  19. unsigned char* str;
  20. while (1)
  21. {
  22. rt_kprintf("thread1: try to recv a mail\n");
  23. if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
  24. {
  25. rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
  26. rt_thread_delay(100);
  27. }
  28. }
  29. }
  30. /* 线程2入口 */
  31. static void thread2_entry(void* parameter)
  32. {
  33. rt_uint8_t count;
  34. count = 0;
  35. while (1)
  36. {
  37. count ++;
  38. if (count & 0x1)
  39. {
  40. rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
  41. }
  42. else
  43. {
  44. rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
  45. }
  46. rt_thread_delay(200);
  47. }
  48. }
  49. int mbox_simple_init()
  50. {
  51. /* 初始化一个mailbox */
  52. rt_mb_init(&mb, "mbt", &mb_pool[0], 128 / 4, RT_IPC_FLAG_FIFO);
  53. /* 创建线程1 */
  54. tid1 = rt_thread_create("t1",
  55. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  56. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  57. if (tid1 != RT_NULL)
  58. rt_thread_startup(tid1);
  59. else
  60. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  61. /* 创建线程2 */
  62. tid2 = rt_thread_create("t2",
  63. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  64. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  65. if (tid2 != RT_NULL)
  66. rt_thread_startup(tid2);
  67. else
  68. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  69. return 0;
  70. }
  71. #ifdef RT_USING_TC
  72. static void _tc_cleanup()
  73. {
  74. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  75. rt_enter_critical();
  76. /* 删除线程 */
  77. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  78. rt_thread_delete(tid1);
  79. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  80. rt_thread_delete(tid2);
  81. /* 调度器解锁 */
  82. rt_exit_critical();
  83. /* 设置TestCase状态 */
  84. tc_done(TC_STAT_PASSED);
  85. }
  86. int _tc_mbox_simple()
  87. {
  88. /* 设置TestCase清理回调函数 */
  89. tc_cleanup(_tc_cleanup);
  90. mbox_simple_init();
  91. /* 返回TestCase运行的最长时间 */
  92. return 100;
  93. }
  94. /* 输出函数命令到finsh shell中 */
  95. FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
  96. #else
  97. /* 用户应用入口 */
  98. int rt_application_init()
  99. {
  100. mbox_simple_init();
  101. return 0;
  102. }
  103. #endif