Skip to content

Commit 4734b32

Browse files
committed
add thread event test code
1 parent 435bbc4 commit 4734b32

File tree

1 file changed

+187
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)