Skip to content

Commit 923b654

Browse files
committed
add thread event test code
1 parent 43ea76c commit 923b654

File tree

1 file changed

+186
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)