chobjfifos.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file chobjfifos.h
  17. * @brief Objects FIFO structures and macros.
  18. * @details This module implements a generic FIFO queue of objects by
  19. * coupling a Guarded Memory Pool (for objects storage) and
  20. * a MailBox.<br>
  21. * On the sender side free objects are taken from the pool, filled
  22. * and then sent to the receiver, on the receiver side objects are
  23. * fetched, used and then returned to the pool.
  24. * Operations defined for object FIFOs:
  25. * - <b>Take</b>: An object is taken from the pool of the free
  26. * objects, can be blocking.
  27. * - <b>Return</b>: An object is returned to the pool of the
  28. * free objects, it is guaranteed to be non-blocking.
  29. * - <b>Send</b>: An object is sent through the mailbox, it is
  30. * guaranteed to be non-blocking
  31. * - <b>Receive</b>: An object is received from the mailbox,
  32. * can be blocking.
  33. * .
  34. *
  35. * @addtogroup oslib_objects_fifos
  36. * @{
  37. */
  38. #ifndef CHOBJFIFOS_H
  39. #define CHOBJFIFOS_H
  40. #if (CH_CFG_USE_OBJ_FIFOS == TRUE) || defined(__DOXYGEN__)
  41. /*===========================================================================*/
  42. /* Module constants. */
  43. /*===========================================================================*/
  44. /*===========================================================================*/
  45. /* Module pre-compile time settings. */
  46. /*===========================================================================*/
  47. /*===========================================================================*/
  48. /* Derived constants and error checks. */
  49. /*===========================================================================*/
  50. #if CH_CFG_USE_MEMPOOLS == FALSE
  51. #error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_MEMPOOLS"
  52. #endif
  53. #if CH_CFG_USE_SEMAPHORES == FALSE
  54. #error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_SEMAPHORES"
  55. #endif
  56. #if CH_CFG_USE_MAILBOXES == FALSE
  57. #error "CH_CFG_USE_OBJ_FIFOS requires CH_CFG_USE_MAILBOXES"
  58. #endif
  59. /*===========================================================================*/
  60. /* Module data structures and types. */
  61. /*===========================================================================*/
  62. /**
  63. * @brief Type of an objects FIFO.
  64. */
  65. typedef struct ch_objects_fifo {
  66. /**
  67. * @brief Pool of the free objects.
  68. */
  69. guarded_memory_pool_t free;
  70. /**
  71. * @brief Mailbox of the sent objects.
  72. */
  73. mailbox_t mbx;
  74. } objects_fifo_t;
  75. /*===========================================================================*/
  76. /* Module macros. */
  77. /*===========================================================================*/
  78. /*===========================================================================*/
  79. /* External declarations. */
  80. /*===========================================================================*/
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. /*===========================================================================*/
  88. /* Module inline functions. */
  89. /*===========================================================================*/
  90. /**
  91. * @brief Initializes a FIFO object.
  92. * @pre The messages size must be a multiple of the alignment
  93. * requirement.
  94. *
  95. * @param[out] ofp pointer to a @p objects_fifo_t structure
  96. * @param[in] objsize size of objects
  97. * @param[in] objn number of objects available
  98. * @param[in] objalign required objects alignment
  99. * @param[in] objbuf pointer to the buffer of objects, it must be able
  100. * to hold @p objn objects of @p objsize size with
  101. * @p objealign alignment
  102. * @param[in] msgbuf pointer to the buffer of messages, it must be able
  103. * to hold @p objn messages
  104. *
  105. * @init
  106. */
  107. static inline void chFifoObjectInitAligned(objects_fifo_t *ofp, size_t objsize,
  108. size_t objn, unsigned objalign,
  109. void *objbuf, msg_t *msgbuf) {
  110. chDbgCheck((objsize >= objalign) && ((objsize % objalign) == 0U));
  111. chGuardedPoolObjectInitAligned(&ofp->free, objsize, objalign);
  112. chGuardedPoolLoadArray(&ofp->free, objbuf, objn);
  113. chMBObjectInit(&ofp->mbx, msgbuf, objn);
  114. }
  115. /**
  116. * @brief Initializes a FIFO object.
  117. * @pre The messages size must be a multiple of the alignment
  118. * requirement.
  119. *
  120. * @param[out] ofp pointer to a @p objects_fifo_t structure
  121. * @param[in] objsize size of objects
  122. * @param[in] objn number of objects available
  123. * @param[in] objbuf pointer to the buffer of objects, it must be able
  124. * to hold @p objn objects of @p objsize size with
  125. * @p objealign alignment
  126. * @param[in] msgbuf pointer to the buffer of messages, it must be able
  127. * to hold @p objn messages
  128. *
  129. * @init
  130. */
  131. static inline void chFifoObjectInit(objects_fifo_t *ofp, size_t objsize,
  132. size_t objn, void *objbuf,
  133. msg_t *msgbuf) {
  134. chFifoObjectInitAligned(ofp, objsize, objn,
  135. PORT_NATURAL_ALIGN,
  136. objbuf, msgbuf);
  137. }
  138. /**
  139. * @brief Allocates a free object.
  140. *
  141. * @param[in] ofp pointer to a @p objects_fifo_t structure
  142. * @return The pointer to the allocated object.
  143. * @retval NULL if an object is not immediately available.
  144. *
  145. * @iclass
  146. */
  147. static inline void *chFifoTakeObjectI(objects_fifo_t *ofp) {
  148. return chGuardedPoolAllocI(&ofp->free);
  149. }
  150. /**
  151. * @brief Allocates a free object.
  152. *
  153. * @param[in] ofp pointer to a @p objects_fifo_t structure
  154. * @param[in] timeout the number of ticks before the operation timeouts,
  155. * the following special values are allowed:
  156. * - @a TIME_IMMEDIATE immediate timeout.
  157. * - @a TIME_INFINITE no timeout.
  158. * .
  159. * @return The pointer to the allocated object.
  160. * @retval NULL if an object is not available within the specified
  161. * timeout.
  162. *
  163. * @sclass
  164. */
  165. static inline void *chFifoTakeObjectTimeoutS(objects_fifo_t *ofp,
  166. sysinterval_t timeout) {
  167. return chGuardedPoolAllocTimeoutS(&ofp->free, timeout);
  168. }
  169. /**
  170. * @brief Allocates a free object.
  171. *
  172. * @param[in] ofp pointer to a @p objects_fifo_t structure
  173. * @param[in] timeout the number of ticks before the operation timeouts,
  174. * the following special values are allowed:
  175. * - @a TIME_IMMEDIATE immediate timeout.
  176. * - @a TIME_INFINITE no timeout.
  177. * .
  178. * @return The pointer to the allocated object.
  179. * @retval NULL if an object is not available within the specified
  180. * timeout.
  181. *
  182. * @api
  183. */
  184. static inline void *chFifoTakeObjectTimeout(objects_fifo_t *ofp,
  185. sysinterval_t timeout) {
  186. return chGuardedPoolAllocTimeout(&ofp->free, timeout);
  187. }
  188. /**
  189. * @brief Releases a fetched object.
  190. *
  191. * @param[in] ofp pointer to a @p objects_fifo_t structure
  192. * @param[in] objp pointer to the object to be released
  193. *
  194. * @iclass
  195. */
  196. static inline void chFifoReturnObjectI(objects_fifo_t *ofp,
  197. void *objp) {
  198. chGuardedPoolFreeI(&ofp->free, objp);
  199. }
  200. /**
  201. * @brief Releases a fetched object.
  202. *
  203. * @param[in] ofp pointer to a @p objects_fifo_t structure
  204. * @param[in] objp pointer to the object to be released
  205. *
  206. * @sclass
  207. */
  208. static inline void chFifoReturnObjectS(objects_fifo_t *ofp,
  209. void *objp) {
  210. chGuardedPoolFreeS(&ofp->free, objp);
  211. }
  212. /**
  213. * @brief Releases a fetched object.
  214. *
  215. * @param[in] ofp pointer to a @p objects_fifo_t structure
  216. * @param[in] objp pointer to the object to be released
  217. *
  218. * @api
  219. */
  220. static inline void chFifoReturnObject(objects_fifo_t *ofp,
  221. void *objp) {
  222. chGuardedPoolFree(&ofp->free, objp);
  223. }
  224. /**
  225. * @brief Posts an object.
  226. * @note By design the object can be always immediately posted.
  227. *
  228. * @param[in] ofp pointer to a @p objects_fifo_t structure
  229. * @param[in] objp pointer to the object to be posted
  230. *
  231. * @iclass
  232. */
  233. static inline void chFifoSendObjectI(objects_fifo_t *ofp,
  234. void *objp) {
  235. msg_t msg;
  236. msg = chMBPostI(&ofp->mbx, (msg_t)objp);
  237. chDbgAssert(msg == MSG_OK, "post failed");
  238. }
  239. /**
  240. * @brief Posts an object.
  241. * @note By design the object can be always immediately posted.
  242. *
  243. * @param[in] ofp pointer to a @p objects_fifo_t structure
  244. * @param[in] objp pointer to the object to be posted
  245. *
  246. * @sclass
  247. */
  248. static inline void chFifoSendObjectS(objects_fifo_t *ofp,
  249. void *objp) {
  250. msg_t msg;
  251. msg = chMBPostTimeoutS(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
  252. chDbgAssert(msg == MSG_OK, "post failed");
  253. }
  254. /**
  255. * @brief Posts an object.
  256. * @note By design the object can be always immediately posted.
  257. *
  258. * @param[in] ofp pointer to a @p objects_fifo_t structure
  259. * @param[in] objp pointer to the object to be released
  260. *
  261. * @api
  262. */
  263. static inline void chFifoSendObject(objects_fifo_t *ofp, void *objp) {
  264. msg_t msg;
  265. msg = chMBPostTimeout(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
  266. chDbgAssert(msg == MSG_OK, "post failed");
  267. }
  268. /**
  269. * @brief Posts an high priority object.
  270. * @note By design the object can be always immediately posted.
  271. *
  272. * @param[in] ofp pointer to a @p objects_fifo_t structure
  273. * @param[in] objp pointer to the object to be posted
  274. *
  275. * @iclass
  276. */
  277. static inline void chFifoSendObjectAheadI(objects_fifo_t *ofp,
  278. void *objp) {
  279. msg_t msg;
  280. msg = chMBPostAheadI(&ofp->mbx, (msg_t)objp);
  281. chDbgAssert(msg == MSG_OK, "post failed");
  282. }
  283. /**
  284. * @brief Posts an high priority object.
  285. * @note By design the object can be always immediately posted.
  286. *
  287. * @param[in] ofp pointer to a @p objects_fifo_t structure
  288. * @param[in] objp pointer to the object to be posted
  289. *
  290. * @sclass
  291. */
  292. static inline void chFifoSendObjectAheadS(objects_fifo_t *ofp,
  293. void *objp) {
  294. msg_t msg;
  295. msg = chMBPostAheadTimeoutS(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
  296. chDbgAssert(msg == MSG_OK, "post failed");
  297. }
  298. /**
  299. * @brief Posts an high priority object.
  300. * @note By design the object can be always immediately posted.
  301. *
  302. * @param[in] ofp pointer to a @p objects_fifo_t structure
  303. * @param[in] objp pointer to the object to be released
  304. *
  305. * @api
  306. */
  307. static inline void chFifoSendObjectAhead(objects_fifo_t *ofp, void *objp) {
  308. msg_t msg;
  309. msg = chMBPostAheadTimeout(&ofp->mbx, (msg_t)objp, TIME_IMMEDIATE);
  310. chDbgAssert(msg == MSG_OK, "post failed");
  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. * @return The operation status.
  318. * @retval MSG_OK if an object has been correctly fetched.
  319. * @retval MSG_TIMEOUT if the FIFO is empty and a message cannot be fetched.
  320. *
  321. * @iclass
  322. */
  323. static inline msg_t chFifoReceiveObjectI(objects_fifo_t *ofp,
  324. void **objpp) {
  325. return chMBFetchI(&ofp->mbx, (msg_t *)objpp);
  326. }
  327. /**
  328. * @brief Fetches an object.
  329. *
  330. * @param[in] ofp pointer to a @p objects_fifo_t structure
  331. * @param[in] objpp pointer to the fetched object reference
  332. * @param[in] timeout the number of ticks before the operation timeouts,
  333. * the following special values are allowed:
  334. * - @a TIME_IMMEDIATE immediate timeout.
  335. * - @a TIME_INFINITE no timeout.
  336. * .
  337. * @return The operation status.
  338. * @retval MSG_OK if an object has been correctly fetched.
  339. * @retval MSG_TIMEOUT if the operation has timed out.
  340. *
  341. * @sclass
  342. */
  343. static inline msg_t chFifoReceiveObjectTimeoutS(objects_fifo_t *ofp,
  344. void **objpp,
  345. sysinterval_t timeout) {
  346. return chMBFetchTimeoutS(&ofp->mbx, (msg_t *)objpp, timeout);
  347. }
  348. /**
  349. * @brief Fetches an object.
  350. *
  351. * @param[in] ofp pointer to a @p objects_fifo_t structure
  352. * @param[in] objpp pointer to the fetched object reference
  353. * @param[in] timeout the number of ticks before the operation timeouts,
  354. * the following special values are allowed:
  355. * - @a TIME_IMMEDIATE immediate timeout.
  356. * - @a TIME_INFINITE no timeout.
  357. * .
  358. * @return The operation status.
  359. * @retval MSG_OK if an object has been correctly fetched.
  360. * @retval MSG_TIMEOUT if the operation has timed out.
  361. *
  362. * @api
  363. */
  364. static inline msg_t chFifoReceiveObjectTimeout(objects_fifo_t *ofp,
  365. void **objpp,
  366. sysinterval_t timeout) {
  367. return chMBFetchTimeout(&ofp->mbx, (msg_t *)objpp, timeout);
  368. }
  369. #endif /* CH_CFG_USE_OBJ_FIFOS == TRUE */
  370. #endif /* CHOBJFIFOS_H */
  371. /** @} */