Skip to content

Commit cedd901

Browse files
committed
add thread mq test code
1 parent 1719383 commit cedd901

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-07-03 rcitach test case for messagequeue
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rthw.h>
13+
#include <rtdevice.h>
14+
#include <utest.h>
15+
#include <utest_assert.h>
16+
#include <perf_tc.h>
17+
18+
static rt_sem_t sem1 = RT_NULL;
19+
static rt_sem_t complete_sem = RT_NULL;
20+
static rt_mq_t perf_thread_mq = RT_NULL;
21+
22+
static void perf_thread_mq1(void *parameter)
23+
{
24+
char recv = 0;
25+
rt_perf_t *perf = (rt_perf_t *)parameter;
26+
while (1)
27+
{
28+
rt_mq_recv(perf_thread_mq, &recv, 1, RT_WAITING_FOREVER);
29+
rt_perf_stop(perf);
30+
if (recv != 'A')
31+
{
32+
LOG_E("mq recv value error!");
33+
rt_mq_delete(perf_thread_mq);
34+
return;
35+
}
36+
37+
if (perf->count >= UTEST_SYS_PERF_TC_COUNT)
38+
{
39+
rt_mq_delete(perf_thread_mq);
40+
rt_sem_delete(sem1);
41+
return;
42+
}
43+
rt_sem_release(sem1);
44+
}
45+
}
46+
47+
static void perf_thread_mq2(void *parameter)
48+
{
49+
rt_perf_t *perf = (rt_perf_t *)parameter;
50+
char send = 'A';
51+
while (1)
52+
{
53+
if (perf->count >= UTEST_SYS_PERF_TC_COUNT)
54+
{
55+
rt_sem_release(complete_sem);
56+
return;
57+
}
58+
rt_sem_take(sem1, RT_WAITING_FOREVER);
59+
rt_perf_start(perf);
60+
rt_mq_send(perf_thread_mq, &send, 1);
61+
}
62+
}
63+
64+
rt_err_t rt_perf_thread_mq(rt_perf_t *perf)
65+
{
66+
rt_thread_t thread1 = RT_NULL;
67+
rt_thread_t thread2 = RT_NULL;
68+
69+
# if __STDC_VERSION__ >= 199901L
70+
rt_strcpy(perf->name,__func__);
71+
#else
72+
rt_strcpy(perf->name,"rt_perf_thread_mq");
73+
#endif
74+
75+
perf_thread_mq = rt_mq_create("perf_thread_mq", 1, 1, RT_IPC_FLAG_PRIO);
76+
if (perf_thread_mq == RT_NULL)
77+
{
78+
LOG_E("perf_thread_mq create failed.");
79+
return -RT_ERROR;
80+
}
81+
82+
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
83+
complete_sem = rt_sem_create("complete", 0, RT_IPC_FLAG_FIFO);
84+
85+
thread1 = rt_thread_create("perf_thread_mq1", perf_thread_mq1, perf,
86+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
87+
if (thread1 == RT_NULL)
88+
{
89+
LOG_E("perf_thread_mq1 create failed.");
90+
return -RT_ERROR;
91+
}
92+
93+
thread2 = rt_thread_create("perf_thread_mq2", perf_thread_mq2, perf,
94+
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
95+
if (thread2 == RT_NULL)
96+
{
97+
LOG_E("perf_thread_mq2 create failed.");
98+
return -RT_ERROR;
99+
}
100+
101+
rt_thread_startup(thread1);
102+
rt_thread_startup(thread2);
103+
104+
rt_sem_take(complete_sem, RT_WAITING_FOREVER);
105+
rt_perf_dump(perf);
106+
rt_sem_delete(complete_sem);
107+
return RT_EOK;
108+
}
109+

0 commit comments

Comments
 (0)