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