1 /*
2 * �����嵥����Ϣ��������
3 *
4 * �������ᴴ��3����̬�̣߳�һ���̻߳����Ϣ��������ȡ��Ϣ��һ���̻߳ᶨʱ����
5 * Ϣ���з�����Ϣ��һ���̻߳ᶨʱ����Ϣ���з��ͽ�����Ϣ��
6 */
7 #include <rtthread.h>
8 #include "tc_comm.h"
9
10 /* ָ���߳̿��ƿ��ָ�� */
11 static rt_thread_t tid = RT_NULL;
12
13 /* ��Ϣ���п��ƿ� */
14 static struct rt_messagequeue mq;
15 /* ��Ϣ�������õ��ķ�����Ϣ���ڴ�� */
16 static char msg_pool[2048];
17
18 /* ��ʱ���Ŀ��ƿ� */
19 static struct rt_timer timer;
20 static rt_uint16_t no = 0;
timer_timeout(void * parameter)21 static void timer_timeout(void* parameter)
22 {
23 char buf[32];
24 rt_uint32_t length;
25
26 length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
27 rt_mq_send(&mq, &buf[0], length);
28 }
29
30 /* �߳���ں��� */
thread_entry(void * parameter)31 static void thread_entry(void* parameter)
32 {
33 char buf[64];
34 rt_err_t result;
35
36 /* ��ʼ����ʱ�� */
37 rt_timer_init(&timer, "timer", /* ��ʱ�������� timer1 */
38 timer_timeout, /* ��ʱʱ�ص��Ĵ����� */
39 RT_NULL, /* ��ʱ��������ڲ��� */
40 1, /* ��ʱ���ȣ���OS TickΪ��λ����1��OS Tick */
41 RT_TIMER_FLAG_PERIODIC); /* �����Զ�ʱ�� */
42
43 while (1)
44 {
45 rt_memset(&buf[0], 0, sizeof(buf));
46
47 /* ����Ϣ�����н�����Ϣ */
48 result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
49 if (result == RT_EOK)
50 {
51 rt_kprintf("recv msg: %s\n", buf);
52 }
53 else if (result == -RT_ETIMEOUT)
54 {
55 rt_kprintf("recv msg timeout\n");
56 }
57 }
58 }
59
timer_timeout_init()60 int timer_timeout_init()
61 {
62 /* ��ʼ����Ϣ���� */
63 rt_mq_init(&mq, "mqt",
64 &msg_pool[0], /* �ڴ��ָ��msg_pool */
65 128 - sizeof(void*), /* ÿ����Ϣ�Ĵ�С�� 128 - void* */
66 sizeof(msg_pool), /* �ڴ�صĴ�С��msg_pool�Ĵ�С */
67 RT_IPC_FLAG_FIFO); /* ����ж���̵߳ȴ������������ȵõ��ķ���������Ϣ */
68
69 /* �����߳� */
70 tid = rt_thread_create("t",
71 thread_entry, RT_NULL, /* �߳������thread_entry, ��ڲ�����RT_NULL */
72 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
73 if (tid != RT_NULL)
74 rt_thread_startup(tid);
75 else
76 tc_stat(TC_STAT_END | TC_STAT_FAILED);
77
78 return 0;
79 }
80
81 #ifdef RT_USING_TC
_tc_cleanup()82 static void _tc_cleanup()
83 {
84 /* �����������������������л��������̣߳�����Ӧ�ж� */
85 rt_enter_critical();
86
87 /* ɾ���߳� */
88 if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
89 rt_thread_delete(tid);
90
91 /* ִ����Ϣ���ж������� */
92 rt_mq_detach(&mq);
93 /* ִ�ж�ʱ������ */
94 rt_timer_detach(&timer);
95
96 /* ���������� */
97 rt_exit_critical();
98
99 /* ����TestCase״̬ */
100 tc_done(TC_STAT_PASSED);
101 }
102
_tc_timer_timeout()103 int _tc_timer_timeout()
104 {
105 /* ����TestCase����ص����� */
106 tc_cleanup(_tc_cleanup);
107 timer_timeout_init();
108
109 /* ����TestCase���е��ʱ�� */
110 return 100;
111 }
112 /* ����������finsh shell�� */
113 FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
114 #else
115 /* �û�Ӧ����� */
rt_application_init()116 int rt_application_init()
117 {
118 timer_timeout_init();
119
120 return 0;
121 }
122 #endif
123