ChibiOS 21.11.5
chevt.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file nil/include/chevt.h
21 * @brief Nil RTOS events header file.
22 *
23 * @addtogroup NIL_EVENTS
24 * @{
25 */
26
27#ifndef CHEVT_H
28#define CHEVT_H
29
30#if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Module pre-compile time settings. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Derived constants and error checks. */
42/*===========================================================================*/
43
44/*===========================================================================*/
45/* Module data structures and types. */
46/*===========================================================================*/
47
49
50/**
51 * @brief Event Listener structure.
52 */
53struct event_listener {
54 event_listener_t *next; /**< @brief Next Event Listener
55 registered on the event
56 source. */
57 thread_t *listener; /**< @brief Thread interested in the
58 event source. */
59 eventmask_t events; /**< @brief Events to be set in
60 the listening thread. */
61 eventflags_t flags; /**< @brief Flags added to the listener
62 by the event source. */
63 eventflags_t wflags; /**< @brief Flags that this listener
64 interested in. */
65};
66
67/**
68 * @brief Event Source structure.
69 */
70typedef struct event_source {
71 event_listener_t *next; /**< @brief First Event Listener
72 registered on the Event
73 Source. */
75
76/**
77 * @brief Event Handler callback function.
78 */
79typedef void (*evhandler_t)(eventid_t id);
80
81/*===========================================================================*/
82/* Module macros. */
83/*===========================================================================*/
84
85/**
86 * @brief All events allowed mask.
87 */
88#define ALL_EVENTS ((eventmask_t)-1)
89
90/**
91 * @brief Returns an event mask from an event identifier.
92 */
93#define EVENT_MASK(eid) ((eventmask_t)1 << (eventmask_t)(eid))
94
95/**
96 * @brief Data part of a static event source initializer.
97 * @details This macro should be used when statically initializing an event
98 * source that is part of a bigger structure.
99 * @param name the name of the event source variable
100 */
101#define _EVENTSOURCE_DATA(name) {(event_listener_t *)(&name)}
102
103/**
104 * @brief Static event source initializer.
105 * @details Statically initialized event sources require no explicit
106 * initialization using @p chEvtInit().
107 *
108 * @param name the name of the event source variable
109 */
110#define EVENTSOURCE_DECL(name) event_source_t name = _EVENTSOURCE_DATA(name)
111
112/**
113 * @name Macro Functions
114 * @{
115 */
116/**
117 * @brief Initializes an Event Source.
118 * @note This function can be invoked before the kernel is initialized
119 * because it just prepares a @p event_source_t structure.
120 *
121 * @param[in] esp pointer to the @p event_source_t structure
122 *
123 * @init
124 */
125#define chEvtObjectInit(esp) do { \
126 (esp)->next = (event_listener_t *)(esp); \
127} while (0)
128
129/**
130 * @brief Registers an Event Listener on an Event Source.
131 * @details Once a thread has registered as listener on an event source it
132 * will be notified of all events broadcasted there.
133 * @note Multiple Event Listeners can specify the same bits to be ORed to
134 * different threads.
135 *
136 * @param[in] esp pointer to the @p event_source_t structure
137 * @param[out] elp pointer to the @p event_listener_t structure
138 * @param[in] events the mask of events to be ORed to the thread when
139 * the event source is broadcasted
140 *
141 * @api
142 */
143#define chEvtRegisterMask(esp, elp, events) \
144 chEvtRegisterMaskWithFlags(esp, elp, events, (eventflags_t)-1)
145
146/**
147 * @brief Registers an Event Listener on an Event Source.
148 * @note Multiple Event Listeners can use the same event identifier, the
149 * listener will share the callback function.
150 *
151 * @param[in] esp pointer to the @p event_source_t structure
152 * @param[out] elp pointer to the @p event_listener_t structure
153 * @param[in] event numeric identifier assigned to the Event Listener.
154 * The value must range between zero and the size, in bit,
155 * of the @p eventmask_t type minus one.
156 *
157 * @api
158 */
159#define chEvtRegister(esp, elp, event) \
160 chEvtRegisterMask(esp, elp, EVENT_MASK(event))
161
162/**
163 * @brief Verifies if there is at least one @p event_listener_t registered.
164 *
165 * @param[in] esp pointer to the @p event_source_t structure
166 * @return The event source status.
167 *
168 * @iclass
169 */
170#define chEvtIsListeningI(esp) (bool)((esp) != (event_source_t *)(esp)->next)
171
172/**
173 * @brief Signals all the Event Listeners registered on the specified Event
174 * Source.
175 *
176 * @param[in] esp pointer to the @p event_source_t structure
177 *
178 * @api
179 */
180#define chEvtBroadcast(esp) chEvtBroadcastFlags(esp, (eventflags_t)0)
181
182/**
183 * @brief Signals all the Event Listeners registered on the specified Event
184 * Source.
185 * @post This function does not reschedule so a call to a rescheduling
186 * function must be performed before unlocking the kernel. Note that
187 * interrupt handlers always reschedule on exit so an explicit
188 * reschedule must not be performed in ISRs.
189 *
190 * @param[in] esp pointer to the @p event_source_t structure
191 *
192 * @iclass
193 */
194#define chEvtBroadcastI(esp) chEvtBroadcastFlagsI(esp, (eventflags_t)0)
195
196/**
197 * @brief Adds (OR) a set of events to the current thread, this is
198 * @b much faster than using @p chEvtBroadcast() or @p chEvtSignal().
199 *
200 * @param[in] events the events to be added
201 * @return The mask of currently pending events.
202 *
203 * @iclass
204 */
205#define chEvtAddEventsI(events) (nil.current->epmask |= events)
206
207/**
208 * @brief Returns the events mask.
209 * @details The pending events mask is returned but not altered in any way.
210 *
211 * @return The pending events mask.
212 *
213 * @api
214 */
215#define chEvtGetEventsX(void) (nil.current->epmask)
216
217/**
218 * @brief Waits for exactly one of the specified events.
219 * @details The function waits for one event among those specified in
220 * @p events to become pending then the event is cleared and returned.
221 * @note One and only one event is served in the function, the one with the
222 * lowest event id. The function is meant to be invoked into a loop
223 * in order to serve all the pending events.<br>
224 * This means that Event Listeners with a lower event identifier have
225 * an higher priority.
226 *
227 * @param[in] events events that the function should wait
228 * for, @p ALL_EVENTS enables all the events
229 * @return The mask of the lowest event id served and cleared.
230 * @retval 0 if the operation has timed out.
231 *
232 * @api
233 */
234#define chEvtWaitOne(events) chEvtWaitOneTimeout(events, TIME_INFINITE)
235
236/**
237 * @brief Waits for any of the specified events.
238 * @details The function waits for any event among those specified in
239 * @p mask to become pending then the events are cleared and
240 * returned.
241 *
242 * @param[in] events events that the function should wait
243 * for, @p ALL_EVENTS enables all the events
244 * @return The mask of the served and cleared events.
245 * @retval 0 if the operation has timed out.
246 *
247 * @api
248 */
249#define chEvtWaitAny(events) chEvtWaitAnyTimeout(events, TIME_INFINITE)
250
251/**
252 * @brief Waits for all the specified events.
253 * @details The function waits for all the events specified in @p mask to
254 * become pending then the events are cleared and returned.
255 *
256 * @param[in] events events that the function should wait
257 * for, @p ALL_EVENTS enables all the events
258 * @return The mask of the served and cleared events.
259 * @retval 0 if the operation has timed out.
260 *
261 * @api
262 */
263#define chEvtWaitAll(events) chEvtWaitAllTimeout(events, TIME_INFINITE)
264
265/** @} */
266
267/*===========================================================================*/
268/* External declarations. */
269/*===========================================================================*/
270
271#ifdef __cplusplus
272extern "C" {
273#endif
275 event_listener_t *elp,
276 eventmask_t events,
277 eventflags_t wflags);
284 void chEvtSignal(thread_t *tp, eventmask_t events);
285 void chEvtSignalI(thread_t *tp, eventmask_t events);
288 void chEvtDispatch(const evhandler_t *handlers, eventmask_t events);
292#ifdef __cplusplus
293}
294#endif
295
296#endif /* CH_CFG_USE_EVENTS == TRUE */
297
298#endif /* CHEVT_H */
299
300/** @} */
void(* evhandler_t)(eventid_t id)
Event Handler callback function.
Definition chevents.h:82
void chEvtSignalI(thread_t *tp, eventmask_t events)
Adds a set of event flags directly to the specified thread_t.
Definition chevents.c:294
struct event_source event_source_t
Event Source structure.
void chEvtUnregister(event_source_t *esp, event_listener_t *elp)
Unregisters an Event Listener from its Event Source.
Definition chevents.c:157
void chEvtSignal(thread_t *tp, eventmask_t events)
Adds a set of event flags directly to the specified thread_t.
Definition chevents.c:318
eventmask_t chEvtWaitAllTimeout(eventmask_t events, sysinterval_t timeout)
Waits for all the specified events.
Definition chevents.c:611
void chEvtBroadcastFlagsI(event_source_t *esp, eventflags_t flags)
Signals all the Event Listeners registered on the specified Event Source.
Definition chevents.c:345
eventmask_t chEvtGetAndClearEvents(eventmask_t events)
Clears the pending events specified in the events mask.
Definition chevents.c:206
eventmask_t chEvtWaitOneTimeout(eventmask_t events, sysinterval_t timeout)
Waits for exactly one of the specified events.
Definition chevents.c:527
eventmask_t chEvtWaitAnyTimeout(eventmask_t events, sysinterval_t timeout)
Waits for any of the specified events.
Definition chevents.c:570
eventflags_t chEvtGetAndClearFlags(event_listener_t *elp)
Returns the flags associated to an event_listener_t.
Definition chevents.c:269
struct event_listener event_listener_t
Definition chevents.h:51
void chEvtDispatch(const evhandler_t *handlers, eventmask_t events)
Invokes the event handlers associated to an event flags mask.
Definition chevents.c:396
eventmask_t chEvtGetAndClearEventsI(eventmask_t events)
Clears the pending events specified in the events mask.
Definition chevents.c:186
void chEvtRegisterMaskWithFlags(event_source_t *esp, event_listener_t *elp, eventmask_t events, eventflags_t wflags)
Registers an Event Listener on an Event Source.
Definition chevents.c:134
eventmask_t chEvtAddEvents(eventmask_t events)
Adds (OR) a set of events to the current thread, this is much faster than using chEvtBroadcast() or c...
Definition chevents.c:225
void chEvtBroadcastFlags(event_source_t *esp, eventflags_t flags)
Signals all the Event Listeners registered on the specified Event Source.
Definition chevents.c:379
eventflags_t chEvtGetAndClearFlagsI(event_listener_t *elp)
Returns the unmasked flags associated to an event_listener_t.
Definition chevents.c:246
uint32_t eventflags_t
Definition chearly.h:90
uint32_t eventmask_t
Definition chearly.h:89
int32_t eventid_t
Definition chearly.h:88
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
Event Listener structure.
Definition chevents.h:56
eventmask_t events
Events to be set in the listening thread.
Definition chevents.h:62
eventflags_t wflags
Flags that this listener interested in.
Definition chevents.h:66
thread_t * listener
Thread interested in the event source.
Definition chevents.h:60
eventflags_t flags
Flags added to the listener by the event source.
Definition chevents.h:64
event_listener_t * next
Next Event Listener registered on the event source.
Definition chevents.h:57
Event Source structure.
Definition chevents.h:73
event_listener_t * next
First Event Listener registered on the Event Source.
Definition chevents.h:74