Skip to content

Commit bb7b344

Browse files
committed
add thread mbox test code
1 parent 3af2f2b commit bb7b344

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 mbox
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rthw.h>
13+
#include <rtdevice.h>
14+
#include <utest.h>
15+
#include <utest_assert.h>
16+
17+
#define PERF_COUNT 1000
18+
static rt_sem_t sem1 = RT_NULL;
19+
static rt_mailbox_t perf_thread_mbox = RT_NULL;
20+
volatile static uint16_t thread_event_count = 0;
21+
#define THREAD_STACK_SIZE 1024
22+
#define THREAD_PRIORITY 10
23+
#define THREAD_TIMESLICE 5
24+
25+
struct perf_time
26+
{
27+
rt_uint32_t start_time;
28+
rt_uint32_t end_time;
29+
rt_uint32_t use_time;
30+
};
31+
static struct perf_time use_time[PERF_COUNT];
32+
33+
#ifdef USING_HWTIME_AS_TIME_REF
34+
static rt_device_t hw_dev = RT_NULL;
35+
static rt_hwtimerval_t timer_val;
36+
37+
static rt_uint32_t get_timer_us(void)
38+
{
39+
if (hw_dev && rt_device_read(hw_dev, 0, &timer_val, sizeof(rt_hwtimerval_t)))
40+
{
41+
return (rt_uint32_t)(timer_val.sec * 1000000 + timer_val.usec);
42+
}
43+
return 0;
44+
}
45+
#endif
46+
47+
static void perf_thread_mbox1(void *parameter)
48+
{
49+
rt_ubase_t recv = 0;
50+
rt_uint32_t total_time = 0;
51+
while (1)
52+
{
53+
rt_mb_recv(perf_thread_mbox, &recv, RT_WAITING_FOREVER);
54+
#ifdef USING_HWTIME_AS_TIME_REF
55+
use_time[thread_event_count].end_time = get_timer_us();
56+
use_time[thread_event_count].use_time = use_time[thread_event_count].end_time - use_time[thread_event_count].start_time;
57+
#else
58+
use_time[thread_event_count].end_time = rt_tick_get();
59+
use_time[thread_event_count].use_time = (use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) / RT_TICK_PER_SECOND;
60+
use_time[thread_event_count].use_time += ((use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) % RT_TICK_PER_SECOND) * (1000000u / RT_TICK_PER_SECOND);
61+
#endif
62+
if (recv != 1)
63+
{
64+
LOG_E("mbox recv value error!\n");
65+
rt_mb_delete(perf_thread_mbox);
66+
return;
67+
}
68+
69+
recv = 0;
70+
total_time += use_time[thread_event_count].use_time;
71+
thread_event_count ++;
72+
if (thread_event_count >= PERF_COUNT)
73+
{
74+
LOG_I("Mbox test completed: %d iterations, total time = %lu us, average = %lu us per mbox",
75+
PERF_COUNT, total_time, total_time / PERF_COUNT);
76+
77+
rt_mb_delete(perf_thread_mbox);
78+
rt_sem_delete(sem1);
79+
return;
80+
}
81+
rt_sem_release(sem1);
82+
}
83+
}
84+
85+
static void perf_thread_mbox2(void *parameter)
86+
{
87+
while (1)
88+
{
89+
if (thread_event_count >= PERF_COUNT)
90+
{
91+
return;
92+
}
93+
rt_sem_take(sem1, RT_WAITING_FOREVER);
94+
#ifdef USING_HWTIME_AS_TIME_REF
95+
use_time[thread_event_count].start_time = get_timer_us();
96+
#else
97+
use_time[thread_event_count].start_time = rt_tick_get();
98+
#endif
99+
rt_mb_send(perf_thread_mbox, 1);
100+
}
101+
}
102+
103+
static void rt_perf_thread_mbox(void)
104+
{
105+
rt_thread_t thread1 = RT_NULL;
106+
rt_thread_t thread2 = RT_NULL;
107+
108+
perf_thread_mbox = rt_mb_create("perf_thread_mbox", 1, RT_IPC_FLAG_PRIO);
109+
if (perf_thread_mbox == RT_NULL)
110+
{
111+
LOG_E("perf thread mbox create failed.");
112+
return;
113+
}
114+
115+
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
116+
117+
thread1 = rt_thread_create("perf_thread_mbox1", perf_thread_mbox1, RT_NULL,
118+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
119+
if (thread1 == RT_NULL)
120+
{
121+
LOG_E("perf_thread_mbox1 create failed.");
122+
return;
123+
}
124+
125+
thread2 = rt_thread_create("perf_thread_mbox2", perf_thread_mbox2, RT_NULL,
126+
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
127+
if (thread2 == RT_NULL)
128+
{
129+
LOG_E("perf_thread_mbox2 create failed.");
130+
return;
131+
}
132+
133+
rt_thread_startup(thread1);
134+
rt_thread_startup(thread2);
135+
}
136+
137+
static rt_err_t utest_tc_init(void)
138+
{
139+
#ifdef USING_HWTIME_AS_TIME_REF
140+
rt_hwtimerval_t timeout_s;
141+
int ret = RT_EOK;
142+
hw_dev = rt_device_find(UTEST_HWTIMER_DEV_NAME);
143+
if (hw_dev == RT_NULL)
144+
{
145+
ret = RT_ERROR;
146+
LOG_E("hwtimer sample run failed! can't find %s device!", UTEST_HWTIMER_DEV_NAME);
147+
return ret;
148+
}
149+
150+
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
151+
if (ret != RT_EOK)
152+
{
153+
LOG_E("open %s device failed!", UTEST_HWTIMER_DEV_NAME);
154+
return ret;
155+
}
156+
157+
timeout_s.sec = 10; /* s */
158+
timeout_s.usec = 0; /* us */
159+
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(rt_hwtimerval_t)) != sizeof(rt_hwtimerval_t))
160+
{
161+
ret = RT_ERROR;
162+
LOG_E("set timeout value failed");
163+
return ret;
164+
}
165+
#endif
166+
167+
return RT_EOK;
168+
}
169+
170+
static rt_err_t utest_tc_cleanup(void)
171+
{
172+
#ifdef USING_HWTIME_AS_TIME_REF
173+
if (hw_dev) rt_device_close(hw_dev);
174+
#endif
175+
return RT_EOK;
176+
}
177+
178+
static void testcase(void)
179+
{
180+
UTEST_UNIT_RUN(rt_perf_thread_mbox);
181+
}
182+
183+
UTEST_TC_EXPORT(testcase, "testcase.pref.mbox", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)