messageq_simple.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * 程序清单:动态线程
  3. *
  4. * 这个程序会初始化2个动态线程,它们拥有共同的入口函数,但参数不相同
  5. */
  6. #include <rtthread.h>
  7. #include "tc_comm.h"
  8. /* 指向线程控制块的指针 */
  9. static rt_thread_t tid1 = RT_NULL;
  10. static rt_thread_t tid2 = RT_NULL;
  11. static rt_thread_t tid3 = RT_NULL;
  12. static struct rt_messagequeue mq;
  13. static char msg_pool[2048];
  14. static void thread1_entry(void* parameter)
  15. {
  16. char buf[128];
  17. while (1)
  18. {
  19. rt_memset(&buf[0], 0, sizeof(buf));
  20. if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  21. {
  22. rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
  23. }
  24. rt_thread_delay(100);
  25. }
  26. }
  27. static void thread2_entry(void* parameter)
  28. {
  29. int i, result;
  30. char buf[] = "this is message No.x";
  31. while (1)
  32. {
  33. for (i = 0; i < 10; i++)
  34. {
  35. buf[sizeof(buf) - 2] = '0' + i;
  36. rt_kprintf("thread2: send message - %s\n", buf);
  37. result = rt_mq_send(&mq, &buf[0], sizeof(buf));
  38. if ( result == -RT_EFULL);
  39. {
  40. rt_kprintf("message queue full, delay 10s\n");
  41. rt_thread_delay(1000);
  42. }
  43. }
  44. rt_thread_delay(100);
  45. }
  46. }
  47. static void thread3_entry(void* parameter)
  48. {
  49. char buf[] = "this is an urgent message!";
  50. while (1)
  51. {
  52. rt_kprintf("thread3: send an urgent message\n");
  53. rt_mq_urgent(&mq, &buf[0], sizeof(buf));
  54. rt_thread_delay(250);
  55. }
  56. }
  57. int messageq_simple_init()
  58. {
  59. rt_mq_init(&mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
  60. /* 创建线程1 */
  61. tid1 = rt_thread_create("t1",
  62. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  63. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  64. if (tid1 != RT_NULL)
  65. rt_thread_startup(tid1);
  66. else
  67. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  68. /* 创建线程2 */
  69. tid2 = rt_thread_create("t2",
  70. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  71. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  72. if (tid2 != RT_NULL)
  73. rt_thread_startup(tid2);
  74. else
  75. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  76. /* 创建线程3 */
  77. tid3 = rt_thread_create("t3",
  78. thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  79. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  80. if (tid3 != RT_NULL)
  81. rt_thread_startup(tid3);
  82. else
  83. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  84. return 0;
  85. }
  86. #ifdef RT_USING_TC
  87. static void _tc_cleanup()
  88. {
  89. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  90. rt_enter_critical();
  91. /* 删除线程 */
  92. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  93. rt_thread_delete(tid1);
  94. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  95. rt_thread_delete(tid2);
  96. if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
  97. rt_thread_delete(tid3);
  98. /* 执行消息队列脱离 */
  99. rt_mq_detach(&mq);
  100. /* 调度器解锁 */
  101. rt_exit_critical();
  102. /* 设置TestCase状态 */
  103. tc_done(TC_STAT_PASSED);
  104. }
  105. int _tc_messageq_simple()
  106. {
  107. /* 设置TestCase清理回调函数 */
  108. tc_cleanup(_tc_cleanup);
  109. messageq_simple_init();
  110. /* 返回TestCase运行的最长时间 */
  111. return 100;
  112. }
  113. /* 输出函数命令到finsh shell中 */
  114. FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example);
  115. #else
  116. /* 用户应用入口 */
  117. int rt_application_init()
  118. {
  119. messageq_simple_init();
  120. return 0;
  121. }
  122. #endif