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