ChibiOS  0.0.0
chobjfifos.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 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  */
80 typedef 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
100 extern "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 size of objects
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 objealign 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  */
128 static inline void chFifoObjectInit(objects_fifo_t *ofp, size_t objsize,
129  size_t objn, unsigned objalign,
130  void *objbuf, msg_t *msgbuf) {
131 
132  chGuardedPoolObjectInitAligned(&ofp->free, objsize, objalign);
133  chGuardedPoolLoadArray(&ofp->free, objbuf, objn);
134  chMBObjectInit(&ofp->mbx, msgbuf, objn);
135 }
136 
137 /**
138  * @brief Allocates a free object.
139  *
140  * @param[in] ofp pointer to a @p objects_fifo_t structure
141  * @return The pointer to the allocated object.
142  * @retval NULL if an object is not immediately available.
143  *
144  * @iclass
145  */
146 static inline void *chFifoTakeObjectI(objects_fifo_t *ofp) {
147 
148  return chGuardedPoolAllocI(&ofp->free);
149 }
150 
151 /**
152  * @brief Allocates a free object.
153  *
154  * @param[in] ofp pointer to a @p objects_fifo_t structure
155  * @param[in] timeout the number of ticks before the operation timeouts,
156  * the following special values are allowed:
157  * - @a TIME_IMMEDIATE immediate timeout.
158  * - @a TIME_INFINITE no timeout.
159  * .
160  * @return The pointer to the allocated object.
161  * @retval NULL if an object is not available within the specified
162  * timeout.
163  *
164  * @sclass
165  */
166 static inline void *chFifoTakeObjectTimeoutS(objects_fifo_t *ofp,
167  sysinterval_t timeout) {
168 
169  return chGuardedPoolAllocTimeoutS(&ofp->free, timeout);
170 }
171 
172 /**
173  * @brief Allocates a free object.
174  *
175  * @param[in] ofp pointer to a @p objects_fifo_t structure
176  * @param[in] timeout the number of ticks before the operation timeouts,
177  * the following special values are allowed:
178  * - @a TIME_IMMEDIATE immediate timeout.
179  * - @a TIME_INFINITE no timeout.
180  * .
181  * @return The pointer to the allocated object.
182  * @retval NULL if an object is not available within the specified
183  * timeout.
184  *
185  * @api
186  */
187 static inline void *chFifoTakeObjectTimeout(objects_fifo_t *ofp,
188  sysinterval_t timeout) {
189 
190  return chGuardedPoolAllocTimeout(&ofp->free, timeout);
191 }
192 
193 /**
194  * @brief Releases a fetched object.
195  *
196  * @param[in] ofp pointer to a @p objects_fifo_t structure
197  * @param[in] objp pointer to the object to be released
198  *
199  * @iclass
200  */
201 static inline void chFifoReturnObjectI(objects_fifo_t *ofp,
202  void *objp) {
203 
204  chGuardedPoolFreeI(&ofp->free, objp);
205 }
206 
207 /**
208  * @brief Releases a fetched object.
209  *
210  * @param[in] ofp pointer to a @p objects_fifo_t structure
211  * @param[in] objp pointer to the object to be released
212  *
213  * @api
214  */
215 static inline void chFifoReturnObject(objects_fifo_t *ofp,
216  void *objp) {
217 
218  chGuardedPoolFree(&ofp->free, objp);
219 }
220 
221 /**
222  * @brief Posts an object.
223  * @note By design the object can be always immediately posted.
224  *
225  * @param[in] ofp pointer to a @p objects_fifo_t structure
226  * @param[in] objp pointer to the object to be posted
227  *
228  * @iclass
229  */
230 static inline void chFifoSendObjectI(objects_fifo_t *ofp,
231  void *objp) {
232  msg_t msg;
233 
234  msg = chMBPostI(&ofp->mbx, (msg_t)objp);
235  chDbgAssert(msg == MSG_OK, "post failed");
236 }
237 
238 /**
239  * @brief Posts an object.
240  * @note By design the object can be always immediately posted.
241  *
242  * @param[in] ofp pointer to a @p objects_fifo_t structure
243  * @param[in] objp pointer to the object to be posted
244  *
245  * @sclass
246  */
247 static inline void chFifoSendObjectS(objects_fifo_t *ofp,
248  void *objp) {
249  msg_t msg;
250 
251  msg = chMBPostTimeoutS(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
252  chDbgAssert(msg == MSG_OK, "post failed");
253 }
254 
255 /**
256  * @brief Posts an object.
257  * @note By design the object can be always immediately posted.
258  *
259  * @param[in] ofp pointer to a @p objects_fifo_t structure
260  * @param[in] objp pointer to the object to be released
261  *
262  * @api
263  */
264 static inline void chFifoSendObject(objects_fifo_t *ofp, void *objp) {
265 
266  msg_t msg;
267 
268  msg = chMBPostTimeout(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
269  chDbgAssert(msg == MSG_OK, "post failed");
270 }
271 
272 /**
273  * @brief Fetches an object.
274  *
275  * @param[in] ofp pointer to a @p objects_fifo_t structure
276  * @param[in] objpp pointer to the fetched object reference
277  * @return The operation status.
278  * @retval MSG_OK if an object has been correctly fetched.
279  * @retval MSG_TIMEOUT if the FIFO is empty and a message cannot be fetched.
280  *
281  * @iclass
282  */
284  void **objpp) {
285 
286  return chMBFetchI(&ofp->mbx, (msg_t *)objpp);
287 }
288 
289 /**
290  * @brief Fetches an object.
291  *
292  * @param[in] ofp pointer to a @p objects_fifo_t structure
293  * @param[in] objpp pointer to the fetched object reference
294  * @param[in] timeout the number of ticks before the operation timeouts,
295  * the following special values are allowed:
296  * - @a TIME_IMMEDIATE immediate timeout.
297  * - @a TIME_INFINITE no timeout.
298  * .
299  * @return The operation status.
300  * @retval MSG_OK if an object has been correctly fetched.
301  * @retval MSG_TIMEOUT if the operation has timed out.
302  *
303  * @sclass
304  */
306  void **objpp,
307  sysinterval_t timeout) {
308 
309  return chMBFetchTimeoutS(&ofp->mbx, (msg_t *)objpp, timeout);
310 }
311 
312 /**
313  * @brief Fetches an object.
314  *
315  * @param[in] ofp pointer to a @p objects_fifo_t structure
316  * @param[in] objpp pointer to the fetched object reference
317  * @param[in] timeout the number of ticks before the operation timeouts,
318  * the following special values are allowed:
319  * - @a TIME_IMMEDIATE immediate timeout.
320  * - @a TIME_INFINITE no timeout.
321  * .
322  * @return The operation status.
323  * @retval MSG_OK if an object has been correctly fetched.
324  * @retval MSG_TIMEOUT if the operation has timed out.
325  *
326  * @api
327  */
329  void **objpp,
330  sysinterval_t timeout) {
331 
332  return chMBFetchTimeout(&ofp->mbx, (msg_t *)objpp, timeout);
333 }
334 #endif /* CH_CFG_USE_OBJ_FIFOS == TRUE */
335 
336 #endif /* CHOBJFIFOS_H */
337 
338 /** @} */
mailbox_t mbx
Mailbox of the sent objects.
Definition: chobjfifos.h:88
static void chFifoReturnObjectI(objects_fifo_t *ofp, void *objp)
Releases a fetched object.
Definition: chobjfifos.h:201
msg_t chMBPostI(mailbox_t *mbp, msg_t msg)
Posts a message into a mailbox.
Definition: chmboxes.c:243
static void chFifoSendObjectI(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition: chobjfifos.h:230
static msg_t chFifoReceiveObjectI(objects_fifo_t *ofp, void **objpp)
Fetches an object.
Definition: chobjfifos.h:283
void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition: chmempools.c:338
static void chFifoSendObject(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition: chobjfifos.h:264
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
static void * chFifoTakeObjectTimeout(objects_fifo_t *ofp, sysinterval_t timeout)
Allocates a free object.
Definition: chobjfifos.h:187
void * chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition: chmempools.c:297
void chMBObjectInit(mailbox_t *mbp, msg_t *buf, size_t n)
Initializes a mailbox_t object.
Definition: chmboxes.c:87
static void * chGuardedPoolAllocI(guarded_memory_pool_t *gmp)
Allocates an object from a guarded memory pool.
Definition: chmempools.h:302
msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp)
Retrieves a message from a mailbox.
Definition: chmboxes.c:493
void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition: chmempools.c:320
msg_t chMBPostTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition: chmboxes.c:194
Type of an objects FIFO.
Definition: chobjfifos.h:80
static void * chFifoTakeObjectTimeoutS(objects_fifo_t *ofp, sysinterval_t timeout)
Allocates a free object.
Definition: chobjfifos.h:166
static void * chFifoTakeObjectI(objects_fifo_t *ofp)
Allocates a free object.
Definition: chobjfifos.h:146
static msg_t chFifoReceiveObjectTimeout(objects_fifo_t *ofp, void **objpp, sysinterval_t timeout)
Fetches an object.
Definition: chobjfifos.h:328
msg_t chMBFetchTimeout(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition: chmboxes.c:415
#define MSG_OK
Normal wakeup message.
Definition: chschd.h:39
static void chFifoSendObjectS(objects_fifo_t *ofp, void *objp)
Posts an object.
Definition: chobjfifos.h:247
void * chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition: chmempools.c:270
struct ch_objects_fifo objects_fifo_t
Type of an objects FIFO.
#define TIME_IMMEDIATE
Zero interval specification for some functions with a timeout specification.
Definition: chtime.h:47
static msg_t chFifoReceiveObjectTimeoutS(objects_fifo_t *ofp, void **objpp, sysinterval_t timeout)
Fetches an object.
Definition: chobjfifos.h:305
#define chDbgAssert(c, r)
Condition assertion.
Definition: chdebug.h:127
guarded_memory_pool_t free
Pool of the free objects.
Definition: chobjfifos.h:84
void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, size_t size, unsigned align)
Initializes an empty guarded memory pool.
Definition: chmempools.c:221
Guarded memory pool descriptor.
Definition: chmempools.h:77
msg_t chMBPostTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition: chmboxes.c:165
static void chFifoObjectInit(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
Structure representing a mailbox object.
Definition: chmboxes.h:52
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:242
msg_t chMBFetchTimeoutS(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition: chmboxes.c:444
static void chFifoReturnObject(objects_fifo_t *ofp, void *objp)
Releases a fetched object.
Definition: chobjfifos.h:215
int32_t msg_t
Definition: chtypes.h:51