ChibiOS/RT 7.0.6
chevents.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 Concepts and parts of this file have been contributed by Scott (skute).
20 */
21
22/**
23 * @file rt/include/chevents.h
24 * @brief Events macros and structures.
25 *
26 * @addtogroup events
27 * @{
28 */
29
30#ifndef CHEVENTS_H
31#define CHEVENTS_H
32
33#if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
34
35/*===========================================================================*/
36/* Module constants. */
37/*===========================================================================*/
38
39/*===========================================================================*/
40/* Module pre-compile time settings. */
41/*===========================================================================*/
42
43/*===========================================================================*/
44/* Derived constants and error checks. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Module data structures and types. */
49/*===========================================================================*/
50
52
53/**
54 * @brief Event Listener structure.
55 */
57 event_listener_t *next; /**< @brief Next Event Listener
58 registered on the event
59 source. */
60 thread_t *listener; /**< @brief Thread interested in the
61 event source. */
62 eventmask_t events; /**< @brief Events to be set in
63 the listening thread. */
64 eventflags_t flags; /**< @brief Flags added to the listener
65 by the event source. */
66 eventflags_t wflags; /**< @brief Flags that this listener
67 interested in. */
68};
69
70/**
71 * @brief Event Source structure.
72 */
73typedef struct event_source {
74 event_listener_t *next; /**< @brief First Event Listener
75 registered on the Event
76 Source. */
78
79/**
80 * @brief Event Handler callback function.
81 */
82typedef void (*evhandler_t)(eventid_t id);
83
84/*===========================================================================*/
85/* Module macros. */
86/*===========================================================================*/
87
88/**
89 * @brief All events allowed mask.
90 */
91#define ALL_EVENTS ((eventmask_t)-1)
92
93/**
94 * @brief Returns an event mask from an event identifier.
95 */
96#define EVENT_MASK(eid) ((eventmask_t)1 << (eventmask_t)(eid))
97
98/**
99 * @brief Data part of a static event source initializer.
100 * @details This macro should be used when statically initializing an event
101 * source that is part of a bigger structure.
102 * @param name the name of the event source variable
103 */
104#define __EVENTSOURCE_DATA(name) {(event_listener_t *)(&name)}
105
106/**
107 * @brief Static event source initializer.
108 * @details Statically initialized event sources require no explicit
109 * initialization using @p chEvtInit().
110 *
111 * @param name the name of the event source variable
112 */
113#define EVENTSOURCE_DECL(name) event_source_t name = __EVENTSOURCE_DATA(name)
114
115/*===========================================================================*/
116/* External declarations. */
117/*===========================================================================*/
118
119#ifdef __cplusplus
120extern "C" {
121#endif
123 event_listener_t *elp,
124 eventmask_t events,
125 eventflags_t wflags);
127 event_listener_t *elp,
128 eventmask_t events,
129 eventflags_t wflags);
136 void chEvtSignal(thread_t *tp, eventmask_t events);
137 void chEvtSignalI(thread_t *tp, eventmask_t events);
140 void chEvtDispatch(const evhandler_t *handlers, eventmask_t events);
141#if (CH_CFG_OPTIMIZE_SPEED == TRUE) || (CH_CFG_USE_EVENTS_TIMEOUT == FALSE)
145#endif
146#if CH_CFG_USE_EVENTS_TIMEOUT == TRUE
150#endif
151#ifdef __cplusplus
152}
153#endif
154
155#if (CH_CFG_OPTIMIZE_SPEED == FALSE) && (CH_CFG_USE_EVENTS_TIMEOUT == TRUE)
156#define chEvtWaitOne(mask) chEvtWaitOneTimeout(mask, TIME_INFINITE)
157#define chEvtWaitAny(mask) chEvtWaitAnyTimeout(mask, TIME_INFINITE)
158#define chEvtWaitAll(mask) chEvtWaitAllTimeout(mask, TIME_INFINITE)
159#endif
160
161/*===========================================================================*/
162/* Module inline functions. */
163/*===========================================================================*/
164
165/**
166 * @brief Initializes an Event Source.
167 * @note This function can be invoked before the kernel is initialized
168 * because it just prepares a @p event_source_t structure.
169 *
170 * @param[in] esp pointer to the @p event_source_t structure
171 *
172 * @init
173 */
174static inline void chEvtObjectInit(event_source_t *esp) {
175
176 esp->next = (event_listener_t *)esp;
177}
178
179/**
180 * @brief Registers an Event Listener on an Event Source.
181 * @details Once a thread has registered as listener on an event source it
182 * will be notified of all events broadcasted there.
183 * @note Multiple Event Listeners can specify the same bits to be ORed to
184 * different threads.
185 *
186 * @param[in] esp pointer to the @p event_source_t structure
187 * @param[out] elp pointer to the @p event_listener_t structure
188 * @param[in] events the mask of events to be ORed to the thread when
189 * the event source is broadcasted
190 *
191 * @api
192 */
193static inline void chEvtRegisterMask(event_source_t *esp,
194 event_listener_t *elp,
195 eventmask_t events) {
196
197 chEvtRegisterMaskWithFlags(esp, elp, events, (eventflags_t)-1);
198}
199
200/**
201 * @brief Registers an Event Listener on an Event Source.
202 * @note Multiple Event Listeners can use the same event identifier, the
203 * listener will share the callback function.
204 *
205 * @param[in] esp pointer to the @p event_source_t structure
206 * @param[out] elp pointer to the @p event_listener_t structure
207 * @param[in] event numeric identifier assigned to the Event Listener.
208 * The value must range between zero and the size, in bit,
209 * of the @p eventmask_t type minus one.
210 *
211 * @api
212 */
213static inline void chEvtRegister(event_source_t *esp,
214 event_listener_t *elp,
215 eventid_t event) {
216
217 chEvtRegisterMask(esp, elp, EVENT_MASK(event));
218}
219
220/**
221 * @brief Verifies if there is at least one @p event_listener_t registered.
222 *
223 * @param[in] esp pointer to the @p event_source_t structure
224 * @return The event source status.
225 *
226 * @iclass
227 */
228static inline bool chEvtIsListeningI(event_source_t *esp) {
229
230 return (bool)(esp != (event_source_t *)esp->next);
231}
232
233/**
234 * @brief Signals all the Event Listeners registered on the specified Event
235 * Source.
236 *
237 * @param[in] esp pointer to the @p event_source_t structure
238 *
239 * @api
240 */
241static inline void chEvtBroadcast(event_source_t *esp) {
242
244}
245
246/**
247 * @brief Signals all the Event Listeners registered on the specified Event
248 * Source.
249 * @post This function does not reschedule so a call to a rescheduling
250 * function must be performed before unlocking the kernel. Note that
251 * interrupt handlers always reschedule on exit so an explicit
252 * reschedule must not be performed in ISRs.
253 *
254 * @param[in] esp pointer to the @p event_source_t structure
255 *
256 * @iclass
257 */
258static inline void chEvtBroadcastI(event_source_t *esp) {
259
261}
262
263/**
264 * @brief Adds (OR) a set of events to the current thread, this is
265 * @b much faster than using @p chEvtBroadcast() or @p chEvtSignal().
266 *
267 * @param[in] events the events to be added
268 * @return The mask of currently pending events.
269 *
270 * @iclass
271 */
273
274 return __sch_get_currthread()->epending |= events;
275}
276
277/**
278 * @brief Returns the events mask.
279 * @details The pending events mask is returned but not altered in any way.
280 *
281 * @return The pending events mask.
282 *
283 * @api
284 */
285static inline eventmask_t chEvtGetEventsX(void) {
286
287 return __sch_get_currthread()->epending;
288}
289
290#endif /* CH_CFG_USE_EVENTS == TRUE */
291
292#endif /* CHEVENTS_H */
293
294/** @} */
void(* evhandler_t)(eventid_t id)
Event Handler callback function.
Definition chevents.h:82
static void chEvtObjectInit(event_source_t *esp)
Initializes an Event Source.
Definition chevents.h:174
static eventmask_t chEvtGetEventsX(void)
Returns the events mask.
Definition chevents.h:285
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 chEvtRegisterMaskWithFlagsI(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:102
static void chEvtBroadcastI(event_source_t *esp)
Signals all the Event Listeners registered on the specified Event Source.
Definition chevents.h:258
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
#define chEvtWaitOne(mask)
Definition chevents.h:156
eventmask_t chEvtGetAndClearEvents(eventmask_t events)
Clears the pending events specified in the events mask.
Definition chevents.c:206
static eventmask_t chEvtAddEventsI(eventmask_t events)
Adds (OR) a set of events to the current thread, this is much faster than using chEvtBroadcast() or c...
Definition chevents.h:272
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
static void chEvtBroadcast(event_source_t *esp)
Signals all the Event Listeners registered on the specified Event Source.
Definition chevents.h:241
eventflags_t chEvtGetAndClearFlags(event_listener_t *elp)
Returns the flags associated to an event_listener_t.
Definition chevents.c:269
#define EVENT_MASK(eid)
Returns an event mask from an event identifier.
Definition chevents.h:96
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
static void chEvtRegister(event_source_t *esp, event_listener_t *elp, eventid_t event)
Registers an Event Listener on an Event Source.
Definition chevents.h:213
static bool chEvtIsListeningI(event_source_t *esp)
Verifies if there is at least one event_listener_t registered.
Definition chevents.h:228
static void chEvtRegisterMask(event_source_t *esp, event_listener_t *elp, eventmask_t events)
Registers an Event Listener on an Event Source.
Definition chevents.h:193
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
#define chEvtWaitAll(mask)
Definition chevents.h:158
#define chEvtWaitAny(mask)
Definition chevents.h:157
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
#define __sch_get_currthread()
Current thread pointer get macro.
Definition chschd.h:136
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
event_listener_t * next
Next Event Listener registered on the event source.
Definition chevents.h:57
eventflags_t wflags
Flags that this listener interested in.
Definition chevents.h:66
eventflags_t flags
Flags added to the listener by the event source.
Definition chevents.h:64
thread_t * listener
Thread interested in the event source.
Definition chevents.h:60
Event Source structure.
Definition chevents.h:73
event_listener_t * next
First Event Listener registered on the Event Source.
Definition chevents.h:74