chmboxes.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 chmboxes.h
  17. * @brief Mailboxes macros and structures.
  18. *
  19. * @addtogroup oslib_mailboxes
  20. * @{
  21. */
  22. #ifndef CHMBOXES_H
  23. #define CHMBOXES_H
  24. #if (CH_CFG_USE_MAILBOXES == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Module pre-compile time settings. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Derived constants and error checks. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Module data structures and types. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Structure representing a mailbox object.
  39. */
  40. typedef struct {
  41. msg_t *buffer; /**< @brief Pointer to the mailbox
  42. buffer. */
  43. msg_t *top; /**< @brief Pointer to the location
  44. after the buffer. */
  45. msg_t *wrptr; /**< @brief Write pointer. */
  46. msg_t *rdptr; /**< @brief Read pointer. */
  47. size_t cnt; /**< @brief Messages in queue. */
  48. bool reset; /**< @brief True in reset state. */
  49. threads_queue_t qw; /**< @brief Queued writers. */
  50. threads_queue_t qr; /**< @brief Queued readers. */
  51. } mailbox_t;
  52. /*===========================================================================*/
  53. /* Module macros. */
  54. /*===========================================================================*/
  55. /**
  56. * @brief Data part of a static mailbox initializer.
  57. * @details This macro should be used when statically initializing a
  58. * mailbox that is part of a bigger structure.
  59. *
  60. * @param[in] name the name of the mailbox variable
  61. * @param[in] buffer pointer to the mailbox buffer array of @p msg_t
  62. * @param[in] size number of @p msg_t elements in the buffer array
  63. */
  64. #define _MAILBOX_DATA(name, buffer, size) { \
  65. (msg_t *)(buffer), \
  66. (msg_t *)(buffer) + size, \
  67. (msg_t *)(buffer), \
  68. (msg_t *)(buffer), \
  69. (size_t)0, \
  70. false, \
  71. _THREADS_QUEUE_DATA(name.qw), \
  72. _THREADS_QUEUE_DATA(name.qr), \
  73. }
  74. /**
  75. * @brief Static mailbox initializer.
  76. * @details Statically initialized mailboxes require no explicit
  77. * initialization using @p chMBObjectInit().
  78. *
  79. * @param[in] name the name of the mailbox variable
  80. * @param[in] buffer pointer to the mailbox buffer array of @p msg_t
  81. * @param[in] size number of @p msg_t elements in the buffer array
  82. */
  83. #define MAILBOX_DECL(name, buffer, size) \
  84. mailbox_t name = _MAILBOX_DATA(name, buffer, size)
  85. /*===========================================================================*/
  86. /* External declarations. */
  87. /*===========================================================================*/
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91. void chMBObjectInit(mailbox_t *mbp, msg_t *buf, size_t n);
  92. void chMBReset(mailbox_t *mbp);
  93. void chMBResetI(mailbox_t *mbp);
  94. msg_t chMBPostTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout);
  95. msg_t chMBPostTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout);
  96. msg_t chMBPostI(mailbox_t *mbp, msg_t msg);
  97. msg_t chMBPostAheadTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout);
  98. msg_t chMBPostAheadTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout);
  99. msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg);
  100. msg_t chMBFetchTimeout(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout);
  101. msg_t chMBFetchTimeoutS(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout);
  102. msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. /*===========================================================================*/
  107. /* Module inline functions. */
  108. /*===========================================================================*/
  109. /**
  110. * @brief Returns the mailbox buffer size as number of messages.
  111. *
  112. * @param[in] mbp the pointer to an initialized @p mailbox_t object
  113. * @return The size of the mailbox.
  114. *
  115. * @iclass
  116. */
  117. static inline size_t chMBGetSizeI(const mailbox_t *mbp) {
  118. /*lint -save -e9033 [10.8] Perfectly safe pointers
  119. arithmetic.*/
  120. return (size_t)(mbp->top - mbp->buffer);
  121. /*lint -restore*/
  122. }
  123. /**
  124. * @brief Returns the number of used message slots into a mailbox.
  125. *
  126. * @param[in] mbp the pointer to an initialized @p mailbox_t object
  127. * @return The number of queued messages.
  128. *
  129. * @iclass
  130. */
  131. static inline size_t chMBGetUsedCountI(const mailbox_t *mbp) {
  132. chDbgCheckClassI();
  133. return mbp->cnt;
  134. }
  135. /**
  136. * @brief Returns the number of free message slots into a mailbox.
  137. *
  138. * @param[in] mbp the pointer to an initialized @p mailbox_t object
  139. * @return The number of empty message slots.
  140. *
  141. * @iclass
  142. */
  143. static inline size_t chMBGetFreeCountI(const mailbox_t *mbp) {
  144. chDbgCheckClassI();
  145. return chMBGetSizeI(mbp) - chMBGetUsedCountI(mbp);
  146. }
  147. /**
  148. * @brief Returns the next message in the queue without removing it.
  149. * @pre A message must be waiting in the queue for this function to work
  150. * or it would return garbage. The correct way to use this macro is
  151. * to use @p chMBGetUsedCountI() and then use this macro, all within
  152. * a lock state.
  153. *
  154. * @param[in] mbp the pointer to an initialized @p mailbox_t object
  155. * @return The next message in queue.
  156. *
  157. * @iclass
  158. */
  159. static inline msg_t chMBPeekI(const mailbox_t *mbp) {
  160. chDbgCheckClassI();
  161. return *mbp->rdptr;
  162. }
  163. /**
  164. * @brief Terminates the reset state.
  165. *
  166. * @param[in] mbp the pointer to an initialized @p mailbox_t object
  167. *
  168. * @xclass
  169. */
  170. static inline void chMBResumeX(mailbox_t *mbp) {
  171. mbp->reset = false;
  172. }
  173. #endif /* CH_CFG_USE_MAILBOXES == TRUE */
  174. #endif /* CHMBOXES_H */
  175. /** @} */