Skip to content

Commit 97effe2

Browse files
committed
sched/nxevent: add support of kernel event group
Events groups are synchronization primitives that allow tasks to wait for multiple conditions to be met before proceeding. They are particularly useful in scenarios where a task needs to wait for several events to occur simultaneously. Signed-off-by: chao an <anchao@lixiang.com>
1 parent cd4fdf2 commit 97effe2

File tree

13 files changed

+959
-0
lines changed

13 files changed

+959
-0
lines changed

Documentation/reference/os/events.rst

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
==============
2+
Events
3+
==============
4+
5+
Events groups are synchronization primitives that allow tasks to wait
6+
for multiple conditions to be met before proceeding. They are particularly
7+
useful in scenarios where a task needs to wait for several events to occur
8+
simultaneously.
9+
This concept can be particularly powerful in real-time operating systems (RTOS).
10+
11+
Overview
12+
=========================
13+
14+
An event group consists of a set of binary flags, each representing a
15+
specific event. Tasks can set, clear, and wait on these flags. When a
16+
task waits on an event group, it can specify which flags it is interested
17+
in and whether it wants to wait for all specified flags to be set or just
18+
any one of them.
19+
20+
Configuration Options
21+
=====================
22+
23+
``CONFIG_SCHED_EVENTS``
24+
This option enables event objects. Threads may wait on event
25+
objects for specific events, but both threads and ISRs may deliver
26+
events to event objects.
27+
28+
Common Events Interfaces
29+
================================
30+
31+
Events Types
32+
--------------------
33+
34+
- ``event_t``. Defines one event group entry.
35+
- ``event_mask_t``. Defines one events mask value.
36+
37+
Notifier Chain Interfaces
38+
-------------------------
39+
40+
.. c:function:: int nxevent_init(FAR event_t *event, event_mask_t events)
41+
42+
Initializes an event object, Set of default events to post
43+
to event.
44+
45+
:param event: Address of the event object
46+
:param events: Set of events to event
47+
48+
.. c:function:: int nxevent_destroy(FAR event_t *event)
49+
50+
This function is used to destroy the event.
51+
52+
:param event: Address of the event object
53+
54+
.. c:function:: int nxevent_reset(FAR event_t *event, event_mask_t events)
55+
56+
Reset events mask to a specific value.
57+
58+
:param event: Address of the event object
59+
:param events: Set of events to event
60+
61+
.. c:function:: int nxevent_post(FAR event_t *event, event_mask_t events)
62+
63+
Post one or more events to an event object.
64+
65+
This routine posts one or more events to an event object. All tasks
66+
waiting on the event object event whose waiting conditions become
67+
met by this posting immediately unpend.
68+
69+
Posting differs from setting in that posted events are merged together
70+
with the current set of events tracked by the event object.
71+
72+
:param event: Address of the event object
73+
:param events: Set of events to post to event, 0 will ignored the waiting events and wake up the waiting thread
74+
75+
.. c:function:: event_mask_t nxevent_wait(FAR event_t *event, event_mask_t events)
76+
77+
Wait for all of the specified events.
78+
79+
This routine waits on event object event until any of the specified
80+
events have been delivered to the event object, or the maximum wait time
81+
timeout has expired. A thread may wait on up to 32 distinctly numbered
82+
events that are expressed as bits in a single 32-bit word.
83+
84+
:param event: Address of the event object
85+
:param events: Set of events to post to event, 0 will indicate wait from any events
86+
87+
.. c:function:: event_mask_t nxevent_tickwait(FAR event_t *event, event_mask_t events, uint32_t delay)
88+
89+
Wait for all of the specified events from the specified tick time.
90+
91+
This routine waits on event object event until any of the specified
92+
events have been delivered to the event object, or the maximum wait time
93+
timeout has expired. A thread may wait on up to 32 distinctly numbered
94+
events that are expressed as bits in a single 32-bit word.
95+
96+
:param event: Address of the event object.
97+
:param events: Set of events to post to event, 0 will indicate wait from any events
98+
:param delay: Ticks to wait from the start time until the event is posted,
99+
If ticks is zero, then this function is equivalent to nxevent_trywait().
100+
101+
.. c:function:: event_mask_t nxevent_trywait(FAR event_t *event, event_mask_t events)
102+
103+
Try wait for all of the specified events.
104+
105+
This routine waits on event object event until any of the specified
106+
events have been delivered to the event object, or the maximum wait time
107+
timeout has expired. A thread may wait on up to 32 distinctly numbered
108+
events that are expressed as bits in a single 32-bit word.
109+
110+
:param event: Address of the event object
111+
:param events: Set of events to post to event, 0 will indicate wait from any events
112+

Documentation/reference/os/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ in other header files.
2525
smp.rst
2626
time_clock.rst
2727
wqueue.rst
28+
events.rst

include/nuttx/event.h

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/****************************************************************************
2+
* include/nuttx/event.h
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
#ifndef __INCLUDE_NUTTX_EVENT_H
22+
#define __INCLUDE_NUTTX_EVENT_H
23+
24+
/****************************************************************************
25+
* Included Files
26+
****************************************************************************/
27+
28+
#include <nuttx/config.h>
29+
30+
#include <errno.h>
31+
#include <stdint.h>
32+
#include <semaphore.h>
33+
34+
#include <nuttx/irq.h>
35+
#include <nuttx/semaphore.h>
36+
#include <nuttx/clock.h>
37+
38+
/****************************************************************************
39+
* Pre-processor Definitions
40+
****************************************************************************/
41+
42+
/* Initializers */
43+
44+
#define NXEVENT_INITIALIZER(e) {SEM_INITIALIZER(0), e}
45+
46+
/****************************************************************************
47+
* Public Type Definitions
48+
****************************************************************************/
49+
50+
typedef struct event_s event_t;
51+
typedef uint32_t event_mask_t;
52+
53+
struct event_s
54+
{
55+
sem_t sem; /* Event Semaphore */
56+
volatile event_mask_t events; /* Pending Events */
57+
};
58+
59+
/****************************************************************************
60+
* Public Data
61+
****************************************************************************/
62+
63+
#ifndef __ASSEMBLY__
64+
65+
#ifdef __cplusplus
66+
#define EXTERN extern "C"
67+
extern "C"
68+
{
69+
#else
70+
#define EXTERN extern
71+
#endif
72+
73+
/****************************************************************************
74+
* Public Function Prototypes
75+
****************************************************************************/
76+
77+
/****************************************************************************
78+
* Name: nxevent_init
79+
*
80+
* Description:
81+
* This routine initializes an event object, Set of default events to post
82+
* to event.
83+
*
84+
* Input Parameters:
85+
* event - Address of the event object
86+
* events - Set of events to post to event
87+
*
88+
* Returned Value:
89+
* This is an internal OS interface and should not be used by applications.
90+
* It follows the NuttX internal error return policy: Zero (OK) is
91+
* returned on success. A negated errno value is returned on failure.
92+
*
93+
****************************************************************************/
94+
95+
int nxevent_init(FAR event_t *event, event_mask_t events);
96+
97+
/****************************************************************************
98+
* Name: nxevent_destroy
99+
*
100+
* Description:
101+
* This function is used to destroy the event.
102+
*
103+
* Input Parameters:
104+
* event - Address of the event object
105+
*
106+
* Returned Value:
107+
* This is an internal OS interface and should not be used by applications.
108+
* It follows the NuttX internal error return policy: Zero (OK) is
109+
* returned on success. A negated errno value is returned on failure.
110+
*
111+
****************************************************************************/
112+
113+
int nxevent_destroy(FAR event_t *event);
114+
115+
/****************************************************************************
116+
* Name: nxevent_reset
117+
*
118+
* Description:
119+
* Reset events mask to a specific value.
120+
*
121+
* Input Parameters:
122+
* event - Address of the event object
123+
* events - Set of events to post to event
124+
*
125+
* Returned Value:
126+
* This is an internal OS interface, not available to applications, and
127+
* hence follows the NuttX internal error return policy: Zero (OK) is
128+
* returned on success. A negated errno value is returned on failure.
129+
*
130+
****************************************************************************/
131+
132+
int nxevent_reset(FAR event_t *event, event_mask_t events);
133+
134+
/****************************************************************************
135+
* Name: nxevent_post
136+
*
137+
* Description:
138+
* Post one or more events to an event object.
139+
*
140+
* This routine posts one or more events to an event object. All tasks
141+
* waiting on the event object event whose waiting conditions become
142+
* met by this posting immediately unpend.
143+
*
144+
* Posting differs from setting in that posted events are merged together
145+
* with the current set of events tracked by the event object.
146+
*
147+
* Input Parameters:
148+
* event - Address of the event object
149+
* events - Set of events to post to event
150+
* - Set events to 0 will ignored the waiting events and
151+
* wake up the waiting thread.
152+
*
153+
* Returned Value:
154+
* This is an internal OS interface and should not be used by applications.
155+
* It follows the NuttX internal error return policy: Zero (OK) is
156+
* returned on success. A negated errno value is returned on failure.
157+
*
158+
* Assumptions:
159+
* This function may be called from an interrupt handler.
160+
*
161+
****************************************************************************/
162+
163+
int nxevent_post(FAR event_t *event, event_mask_t events);
164+
165+
/****************************************************************************
166+
* Name: nxevent_wait
167+
*
168+
* Description:
169+
* Wait for all of the specified events.
170+
*
171+
* This routine waits on event object event until any of the specified
172+
* events have been delivered to the event object, or the maximum wait time
173+
* timeout has expired. A thread may wait on up to 32 distinctly numbered
174+
* events that are expressed as bits in a single 32-bit word.
175+
*
176+
* Input Parameters:
177+
* event - Address of the event object
178+
* events - Set of events to post to event
179+
* - Set events to 0 will indicate wait from any events
180+
*
181+
* Returned Value:
182+
* This is an internal OS interface and should not be used by applications.
183+
* Return of matching events upon success.
184+
* 0 if matching events were not received.
185+
*
186+
****************************************************************************/
187+
188+
event_mask_t nxevent_wait(FAR event_t *event, event_mask_t events);
189+
190+
/****************************************************************************
191+
* Name: nxevent_tickwait
192+
*
193+
* Description:
194+
* Wait for all of the specified events from the specified tick time.
195+
*
196+
* This routine waits on event object event until any of the specified
197+
* events have been delivered to the event object, or the maximum wait time
198+
* timeout has expired. A thread may wait on up to 32 distinctly numbered
199+
* events that are expressed as bits in a single 32-bit word.
200+
*
201+
* Input Parameters:
202+
* event - Address of the event object
203+
* events - Set of events to post to event
204+
* - Set events to 0 will indicate wait from any events
205+
* delay - Ticks to wait from the start time until the event is
206+
* posted. If ticks is zero, then this function is equivalent
207+
* to nxevent_trywait().
208+
*
209+
* Returned Value:
210+
* This is an internal OS interface and should not be used by applications.
211+
* Return of matching events upon success.
212+
* 0 if matching events were not received within the specified time.
213+
*
214+
****************************************************************************/
215+
216+
event_mask_t nxevent_tickwait(FAR event_t *event,
217+
event_mask_t events, uint32_t delay);
218+
219+
/****************************************************************************
220+
* Name: nxevent_trywait
221+
*
222+
* Description:
223+
* Try wait for all of the specified events.
224+
*
225+
* This routine waits on event object event until any of the specified
226+
* events have been delivered to the event object, or the maximum wait time
227+
* timeout has expired. A thread may wait on up to 32 distinctly numbered
228+
* events that are expressed as bits in a single 32-bit word.
229+
*
230+
* Input Parameters:
231+
* event - Address of the event object
232+
* events - Set of events to post to event
233+
* - Set events to 0 will indicate wait from any events
234+
*
235+
* Returned Value:
236+
* This is an internal OS interface and should not be used by applications.
237+
* Return of matching events upon success.
238+
* 0 if matching events were not received.
239+
*
240+
****************************************************************************/
241+
242+
event_mask_t nxevent_trywait(FAR event_t *event, event_mask_t events);
243+
244+
#undef EXTERN
245+
#ifdef __cplusplus
246+
}
247+
#endif
248+
249+
#endif /* __ASSEMBLY__ */
250+
#endif /* __INCLUDE_NUTTX_EVENT_H */

include/nuttx/sched.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <nuttx/addrenv.h>
3939
#include <nuttx/clock.h>
40+
#include <nuttx/event.h>
4041
#include <nuttx/irq.h>
4142
#include <nuttx/mutex.h>
4243
#include <nuttx/semaphore.h>
@@ -632,6 +633,12 @@ struct tcb_s
632633

633634
FAR void *waitobj; /* Object thread waiting on */
634635

636+
/* Schedule Event Group support *******************************************/
637+
638+
#ifdef CONFIG_SCHED_EVENTS
639+
event_mask_t events; /* Schedule Events */
640+
#endif
641+
635642
/* POSIX Signal Control Fields ********************************************/
636643

637644
sigset_t sigprocmask; /* Signals that are blocked */

0 commit comments

Comments
 (0)