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