ChibiOS/RT 7.0.6
chobjfifos.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 oslib/include/chobjfifos.h
21 * @brief Objects FIFO structures and macros.
22 * @details This module implements a generic FIFO queue of objects by
23 * coupling a Guarded Memory Pool (for objects storage) and
24 * a MailBox.<br>
25 * On the sender side free objects are taken from the pool, filled
26 * and then sent to the receiver, on the receiver side objects are
27 * fetched, used and then returned to the pool.
28 * Operations defined for object FIFOs:
29 * - <b>Take</b>: An object is taken from the pool of the free
30 * objects, can be blocking.
31 * - <b>Return</b>: An object is returned to the pool of the
32 * free objects, it is guaranteed to be non-blocking.
33 * - <b>Send</b>: An object is sent through the mailbox, it is
34 * guaranteed to be non-blocking
35 * - <b>Receive</b>: An object is received from the mailbox,
36 * can be blocking.
37 * .
38 *
39 * @addtogroup oslib_objects_fifos
40 * @{
41 */
42
43#ifndef CHOBJFIFOS_H
44#define CHOBJFIFOS_H
45
46#if (CH_CFG_USE_OBJ_FIFOS == TRUE) || defined(__DOXYGEN__)
47
48/*===========================================================================*/
49/* Module constants. */
50/*===========================================================================*/
51
52/*===========================================================================*/
53/* Module pre-compile time settings. */
54/*===========================================================================*/
55
56/*===========================================================================*/
57/* Derived constants and error checks. */
58/*===========================================================================*/
59
60#if CH_CFG_USE_MEMPOOLS == FALSE
61#error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_MEMPOOLS"
62#endif
63
64#if CH_CFG_USE_SEMAPHORES == FALSE
65#error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_SEMAPHORES"
66#endif
67
68#if CH_CFG_USE_MAILBOXES == FALSE
69#error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_MAILBOXES"
70#endif
71
72/*===========================================================================*/
73/* Module data structures and types. */
74/*===========================================================================*/
75
76/**
77 * @brief Type of an objects FIFO.
78 */
79typedef struct ch_objects_fifo {
80 /**
81 * @brief Pool of the free objects.
82 */
84 /**
85 * @brief Mailbox of the sent objects.
86 */
89
90/*===========================================================================*/
91/* Module macros. */
92/*===========================================================================*/
93
94/*===========================================================================*/
95/* External declarations. */
96/*===========================================================================*/
97
98#ifdef __cplusplus
99extern "C" {
100#endif
101
102#ifdef __cplusplus
103}
104#endif
105
106/*===========================================================================*/
107/* Module inline functions. */
108/*===========================================================================*/
109
110/**
111 * @brief Initializes a FIFO object.
112 * @pre The objects size must be a multiple of the alignment
113 * requirement.
114 *
115 * @param[out] ofp pointer to a @p objects_fifo_t structure
116 * @param[in] objsize object size
117 * @param[in] objn number of objects available
118 * @param[in] objalign required objects alignment
119 * @param[in] objbuf pointer to the buffer of objects, it must be able
120 * to hold @p objn objects of @p objsize size with
121 * @p objalign alignment
122 * @param[in] msgbuf pointer to the buffer of messages, it must be able
123 * to hold @p objn messages
124 *
125 * @init
126 */
127static inline void chFifoObjectInitAligned(objects_fifo_t *ofp, size_t objsize,
128 size_t objn, unsigned objalign,
129 void *objbuf, msg_t *msgbuf) {
130
131 chDbgCheck((objsize >= objalign) && ((objsize % objalign) == 0U));
132
133 chGuardedPoolObjectInitAligned(&ofp->free, objsize, objalign);
134 chGuardedPoolLoadArray(&ofp->free, objbuf, objn);
135 chMBObjectInit(&ofp->mbx, msgbuf, objn);
136}
137
138/**
139 * @brief Initializes a FIFO object.
140 * @pre The objects size must be a multiple of the alignment
141 * requirement.
142 *
143 * @param[out] ofp pointer to a @p objects_fifo_t structure
144 * @param[in] objsize object size
145 * @param[in] objn number of objects available
146 * @param[in] objbuf pointer to the buffer of objects, it must be able
147 * to hold @p objn objects of @p objsize size
148 * @param[in] msgbuf pointer to the buffer of messages, it must be able
149 * to hold @p objn messages
150 *
151 * @init
152 */
153static inline void chFifoObjectInit(objects_fifo_t *ofp, size_t objsize,
154 size_t objn, void *objbuf,
155 msg_t *msgbuf) {
156
157 chFifoObjectInitAligned(ofp, objsize, objn,
158 PORT_NATURAL_ALIGN,
159 objbuf, msgbuf);
160}
161
162/**
163 * @brief Allocates a free object.
164 *
165 * @param[in] ofp pointer to a @p objects_fifo_t structure
166 * @return The pointer to the allocated object.
167 * @retval NULL if an object is not immediately available.
168 *
169 * @iclass
170 */
171static inline void *chFifoTakeObjectI(objects_fifo_t *ofp) {
172
173 return chGuardedPoolAllocI(&ofp->free);
174}
175
176/**
177 * @brief Allocates a free object.
178 *
179 * @param[in] ofp pointer to a @p objects_fifo_t structure
180 * @param[in] timeout the number of ticks before the operation timeouts,
181 * the following special values are allowed:
182 * - @a TIME_IMMEDIATE immediate timeout.
183 * - @a TIME_INFINITE no timeout.
184 * .
185 * @return The pointer to the allocated object.
186 * @retval NULL if an object is not available within the specified
187 * timeout.
188 *
189 * @sclass
190 */
192 sysinterval_t timeout) {
193
194 return chGuardedPoolAllocTimeoutS(&ofp->free, timeout);
195}
196
197/**
198 * @brief Allocates a free object.
199 *
200 * @param[in] ofp pointer to a @p objects_fifo_t structure
201 * @param[in] timeout the number of ticks before the operation timeouts,
202 * the following special values are allowed:
203 * - @a TIME_IMMEDIATE immediate timeout.
204 * - @a TIME_INFINITE no timeout.
205 * .
206 * @return The pointer to the allocated object.
207 * @retval NULL if an object is not available within the specified
208 * timeout.
209 *
210 * @api
211 */
213 sysinterval_t timeout) {
214
215 return chGuardedPoolAllocTimeout(&ofp->free, timeout);
216}
217
218/**
219 * @brief Releases a fetched object.
220 *
221 * @param[in] ofp pointer to a @p objects_fifo_t structure
222 * @param[in] objp pointer to the object to be released
223 *
224 * @iclass
225 */
226static inline void chFifoReturnObjectI(objects_fifo_t *ofp,
227 void *objp) {
228
229 chGuardedPoolFreeI(&ofp->free, objp);
230}
231
232/**
233 * @brief Releases a fetched object.
234 *
235 * @param[in] ofp pointer to a @p objects_fifo_t structure
236 * @param[in] objp pointer to the object to be released
237 *
238 * @sclass
239 */
240static inline void chFifoReturnObjectS(objects_fifo_t *ofp,
241 void *objp) {
242
243 chGuardedPoolFreeS(&ofp->free, objp);
244}
245
246/**
247 * @brief Releases a fetched object.
248 *
249 * @param[in] ofp pointer to a @p objects_fifo_t structure
250 * @param[in] objp pointer to the object to be released
251 *
252 * @api
253 */
254static inline void chFifoReturnObject(objects_fifo_t *ofp,
255 void *objp) {
256
257 chGuardedPoolFree(&ofp->free, objp);
258}
259
260/**
261 * @brief Posts an object.
262 * @note By design the object can be always immediately posted.
263 *
264 * @param[in] ofp pointer to a @p objects_fifo_t structure
265 * @param[in] objp pointer to the object to be posted
266 *
267 * @iclass
268 */
269static inline void chFifoSendObjectI(objects_fifo_t *ofp,
270 void *objp) {
271 msg_t msg;
272
273 msg = chMBPostI(&ofp->mbx, (msg_t)objp);
274 chDbgAssert(msg == MSG_OK, "post failed");
275}
276
277/**
278 * @brief Posts an object.
279 * @note By design the object can be always immediately posted.
280 *
281 * @param[in] ofp pointer to a @p objects_fifo_t structure
282 * @param[in] objp pointer to the object to be posted
283 *
284 * @sclass
285 */
286static inline void chFifoSendObjectS(objects_fifo_t *ofp,
287 void *objp) {
288 msg_t msg;
289
290 msg = chMBPostTimeoutS(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
291 chDbgAssert(msg == MSG_OK, "post failed");
292}
293
294/**
295 * @brief Posts an object.
296 * @note By design the object can be always immediately posted.
297 *
298 * @param[in] ofp pointer to a @p objects_fifo_t structure
299 * @param[in] objp pointer to the object to be released
300 *
301 * @api
302 */
303static inline void chFifoSendObject(objects_fifo_t *ofp, void *objp) {
304
305 msg_t msg;
306
307 msg = chMBPostTimeout(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
308 chDbgAssert(msg == MSG_OK, "post failed");
309}
310
311/**
312 * @brief Posts an high priority object.
313 * @note By design the object can be always immediately posted.
314 *
315 * @param[in] ofp pointer to a @p objects_fifo_t structure
316 * @param[in] objp pointer to the object to be posted
317 *
318 * @iclass
319 */
321 void *objp) {
322 msg_t msg;
323
324 msg = chMBPostAheadI(&ofp->mbx, (msg_t)objp);
325 chDbgAssert(msg == MSG_OK, "post failed");
326}
327
328/**
329 * @brief Posts an high priority object.
330 * @note By design the object can be always immediately posted.
331 *
332 * @param[in] ofp pointer to a @p objects_fifo_t structure
333 * @param[in] objp pointer to the object to be posted
334 *
335 * @sclass
336 */
338 void *objp) {
339 msg_t msg;
340
341 msg = chMBPostAheadTimeoutS(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
342 chDbgAssert(msg == MSG_OK, "post failed");
343}
344
345/**
346 * @brief Posts an high priority object.
347 * @note By design the object can be always immediately posted.
348 *
349 * @param[in] ofp pointer to a @p objects_fifo_t structure
350 * @param[in] objp pointer to the object to be released
351 *
352 * @api
353 */
354static inline void chFifoSendObjectAhead(objects_fifo_t *ofp, void *objp) {
355
356 msg_t msg;
357
358 msg = chMBPostAheadTimeout(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
359 chDbgAssert(msg == MSG_OK, "post failed");
360}
361
362/**
363 * @brief Fetches an object.
364 *
365 * @param[in] ofp pointer to a @p objects_fifo_t structure
366 * @param[in] objpp pointer to the fetched object reference
367 * @return The operation status.
368 * @retval MSG_OK if an object has been correctly fetched.
369 * @retval MSG_TIMEOUT if the FIFO is empty and a message cannot be fetched.
370 *
371 * @iclass
372 */
374 void **objpp) {
375
376 return chMBFetchI(&ofp->mbx, (msg_t *)objpp);
377}
378
379/**
380 * @brief Fetches an object.
381 *
382 * @param[in] ofp pointer to a @p objects_fifo_t structure
383 * @param[in] objpp pointer to the fetched object reference
384 * @param[in] timeout the number of ticks before the operation timeouts,
385 * the following special values are allowed:
386 * - @a TIME_IMMEDIATE immediate timeout.
387 * - @a TIME_INFINITE no timeout.
388 * .
389 * @return The operation status.
390 * @retval MSG_OK if an object has been correctly fetched.
391 * @retval MSG_TIMEOUT if the operation has timed out.
392 *
393 * @sclass
394 */
396 void **objpp,
397 sysinterval_t timeout) {
398
399 return chMBFetchTimeoutS(&ofp->mbx, (msg_t *)objpp, timeout);
400}
401
402/**
403 * @brief Fetches an object.
404 *
405 * @param[in] ofp pointer to a @p objects_fifo_t structure
406 * @param[in] objpp pointer to the fetched object reference
407 * @param[in] timeout the number of ticks before the operation timeouts,
408 * the following special values are allowed:
409 * - @a TIME_IMMEDIATE immediate timeout.
410 * - @a TIME_INFINITE no timeout.
411 * .
412 * @return The operation status.
413 * @retval MSG_OK if an object has been correctly fetched.
414 * @retval MSG_TIMEOUT if the operation has timed out.
415 *
416 * @api
417 */
419 void **objpp,
420 sysinterval_t timeout) {
421
422 return chMBFetchTimeout(&ofp->mbx, (msg_t *)objpp, timeout);
423}
424
425#endif /* CH_CFG_USE_OBJ_FIFOS == TRUE */
426
427#endif /* CHOBJFIFOS_H */
428
429/** @} */
#define chDbgAssert(c, r)
Condition assertion.
Definition chdebug.h:143
#define chDbgCheck(c)
Function parameters check.
Definition chdebug.h:117
int32_t msg_t
Definition chearly.h:87
void chMBObjectInit(mailbox_t *mbp, msg_t *buf, size_t n)
Initializes a mailbox_t object.
Definition chmboxes.c:86
msg_t chMBFetchTimeout(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition chmboxes.c:414
msg_t chMBPostTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition chmboxes.c:164
msg_t chMBPostI(mailbox_t *mbp, msg_t msg)
Posts a message into a mailbox.
Definition chmboxes.c:242
msg_t chMBPostAheadTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts an high priority message into a mailbox.
Definition chmboxes.c:289
msg_t chMBPostTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition chmboxes.c:193
msg_t chMBPostAheadTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts an high priority message into a mailbox.
Definition chmboxes.c:318
msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp)
Retrieves a message from a mailbox.
Definition chmboxes.c:492
msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg)
Posts an high priority message into a mailbox.
Definition chmboxes.c:367
msg_t chMBFetchTimeoutS(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition chmboxes.c:443
void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, size_t size, unsigned align)
Initializes an empty guarded memory pool.
Definition chmempools.c:225
static void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.h:301
static void chGuardedPoolFreeS(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.h:319
void * chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition chmempools.c:301
void * chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition chmempools.c:274
void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n)
Loads a guarded memory pool with an array of static objects.
Definition chmempools.c:246
void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.c:324
static void * chGuardedPoolAllocI(guarded_memory_pool_t *gmp)
Allocates an object from a guarded memory pool.
Definition chmempools.h:274
static void chFifoSendObjectAheadI(objects_fifo_t *ofp, void *objp)
Posts an high priority object.
Definition chobjfifos.h:320
static msg_t chFifoReceiveObjectI(objects_fifo_t *ofp, void **objpp)
Fetches an object.
Definition chobjfifos.h:373
static void chFifoSendObjectAhead(objects_fifo_t *ofp, void *objp)
Posts an high priority object.
Definition chobjfifos.h:354
static void chFifoSendObjectAheadS(objects_fifo_t *ofp, void *objp)
Posts an high priority object.
Definition chobjfifos.h:337
static void chFifoReturnObject(objects_fifo_t *ofp, void *objp)
Releases a fetched object.
Definition chobjfifos.h:254
static void * chFifoTakeObjectTimeout(objects_fifo_t *ofp, sysinterval_t timeout)
Allocates a free object.
Definition chobjfifos.h:212
static void chFifoReturnObjectI(objects_fifo_t *ofp, void *objp)
Releases a fetched object.
Definition chobjfifos.h:226
static void * chFifoTakeObjectI(objects_fifo_t *ofp)
Allocates a free object.
Definition chobjfifos.h:171
static void * chFifoTakeObjectTimeoutS(objects_fifo_t *ofp, sysinterval_t timeout)
Allocates a free object.
Definition chobjfifos.h:191
static void chFifoSendObject(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition chobjfifos.h:303
static void chFifoObjectInitAligned(objects_fifo_t *ofp, size_t objsize, size_t objn, unsigned objalign, void *objbuf, msg_t *msgbuf)
Initializes a FIFO object.
Definition chobjfifos.h:127
static void chFifoSendObjectI(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition chobjfifos.h:269
static void chFifoReturnObjectS(objects_fifo_t *ofp, void *objp)
Releases a fetched object.
Definition chobjfifos.h:240
struct ch_objects_fifo objects_fifo_t
Type of an objects FIFO.
static void chFifoObjectInit(objects_fifo_t *ofp, size_t objsize, size_t objn, void *objbuf, msg_t *msgbuf)
Initializes a FIFO object.
Definition chobjfifos.h:153
static void chFifoSendObjectS(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition chobjfifos.h:286
static msg_t chFifoReceiveObjectTimeoutS(objects_fifo_t *ofp, void **objpp, sysinterval_t timeout)
Fetches an object.
Definition chobjfifos.h:395
static msg_t chFifoReceiveObjectTimeout(objects_fifo_t *ofp, void **objpp, sysinterval_t timeout)
Fetches an object.
Definition chobjfifos.h:418
#define MSG_OK
Normal wakeup message.
Definition chschd.h:38
#define TIME_IMMEDIATE
Zero interval specification for some functions with a timeout specification.
Definition chtime.h:46
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
Type of an objects FIFO.
Definition chobjfifos.h:79
mailbox_t mbx
Mailbox of the sent objects.
Definition chobjfifos.h:87
guarded_memory_pool_t free
Pool of the free objects.
Definition chobjfifos.h:83
Guarded memory pool descriptor.
Definition chmempools.h:76
Structure representing a mailbox object.
Definition chmboxes.h:51