1 /* 2 * 程序清单:消息队列例程 3 * 4 * 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消 5 * 息队列发送消息;一个线程会定时给消息队列发送紧急消息。 6 */ 7 #include <rtthread.h> 8 #include "tc_comm.h" 9 10 /* 指向线程控制块的指针 */ 11 static rt_thread_t tid1 = RT_NULL; 12 static rt_thread_t tid2 = RT_NULL; 13 static rt_thread_t tid3 = RT_NULL; 14 15 /* 消息队列控制块 */ 16 static struct rt_messagequeue mq; 17 /* 消息队列中用到的放置消息的内存池 */ 18 static char msg_pool[2048]; 19 20 /* 线程1入口函数 */ 21 static void thread1_entry(void* parameter) 22 { 23 char buf[128]; 24 25 while (1) 26 { 27 rt_memset(&buf[0], 0, sizeof(buf)); 28 29 /* 从消息队列中接收消息 */ 30 if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK) 31 { 32 rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf); 33 } 34 35 /* 延迟10个OS Tick */ 36 rt_thread_delay(10); 37 } 38 } 39 40 /* 线程2入口函数 */ 41 static void thread2_entry(void* parameter) 42 { 43 int i, result; 44 char buf[] = "this is message No.x"; 45 46 while (1) 47 { 48 for (i = 0; i < 10; i++) 49 { 50 buf[sizeof(buf) - 2] = '0' + i; 51 52 rt_kprintf("thread2: send message - %s\n", buf); 53 /* 发送消息到消息队列中 */ 54 result = rt_mq_send(&mq, &buf[0], sizeof(buf)); 55 if ( result == -RT_EFULL) 56 { 57 /* 消息队列满, 延迟1s时间 */ 58 rt_kprintf("message queue full, delay 1s\n"); 59 rt_thread_delay(100); 60 } 61 } 62 63 /* 延时10个OS Tick */ 64 rt_thread_delay(10); 65 } 66 } 67 68 /* 线程3入口函数 */ 69 static void thread3_entry(void* parameter) 70 { 71 char buf[] = "this is an urgent message!"; 72 73 while (1) 74 { 75 rt_kprintf("thread3: send an urgent message\n"); 76 77 /* 发送紧急消息到消息队列中 */ 78 rt_mq_urgent(&mq, &buf[0], sizeof(buf)); 79 80 /* 延时25个OS Tick */ 81 rt_thread_delay(25); 82 } 83 } 84 85 int messageq_simple_init() 86 { 87 /* 初始化消息队列 */ 88 rt_mq_init(&mq, "mqt", 89 &msg_pool[0], /* 内存池指向msg_pool */ 90 128 - sizeof(void*), /* 每个消息的大小是 128 - void* */ 91 sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */ 92 RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ 93 94 /* 创建线程1 */ 95 tid1 = rt_thread_create("t1", 96 thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */ 97 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); 98 if (tid1 != RT_NULL) 99 rt_thread_startup(tid1); 100 else 101 tc_stat(TC_STAT_END | TC_STAT_FAILED); 102 103 /* 创建线程2 */ 104 tid2 = rt_thread_create("t2", 105 thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */ 106 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); 107 if (tid2 != RT_NULL) 108 rt_thread_startup(tid2); 109 else 110 tc_stat(TC_STAT_END | TC_STAT_FAILED); 111 112 /* 创建线程3 */ 113 tid3 = rt_thread_create("t3", 114 thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */ 115 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); 116 if (tid3 != RT_NULL) 117 rt_thread_startup(tid3); 118 else 119 tc_stat(TC_STAT_END | TC_STAT_FAILED); 120 121 return 0; 122 } 123 124 #ifdef RT_USING_TC 125 static void _tc_cleanup() 126 { 127 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ 128 rt_enter_critical(); 129 130 /* 删除线程 */ 131 if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE) 132 rt_thread_delete(tid1); 133 if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE) 134 rt_thread_delete(tid2); 135 if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE) 136 rt_thread_delete(tid3); 137 138 /* 执行消息队列对象脱离 */ 139 rt_mq_detach(&mq); 140 141 /* 调度器解锁 */ 142 rt_exit_critical(); 143 144 /* 设置TestCase状态 */ 145 tc_done(TC_STAT_PASSED); 146 } 147 148 int _tc_messageq_simple() 149 { 150 /* 设置TestCase清理回调函数 */ 151 tc_cleanup(_tc_cleanup); 152 messageq_simple_init(); 153 154 /* 返回TestCase运行的最长时间 */ 155 return 100; 156 } 157 /* 输出函数命令到finsh shell中 */ 158 FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example); 159 #else 160 /* 用户应用入口 */ 161 int rt_application_init() 162 { 163 messageq_simple_init(); 164 165 return 0; 166 } 167 #endif 168