ChibiOS 21.11.5
chmboxes.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 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 version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file oslib/include/chmboxes.h
21 * @brief Mailboxes macros and structures.
22 *
23 * @addtogroup oslib_mailboxes
24 * @{
25 */
26
27#ifndef CHMBOXES_H
28#define CHMBOXES_H
29
30#if (CH_CFG_USE_MAILBOXES == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Module pre-compile time settings. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Derived constants and error checks. */
42/*===========================================================================*/
43
44/*===========================================================================*/
45/* Module data structures and types. */
46/*===========================================================================*/
47
48/**
49 * @brief Structure representing a mailbox object.
50 */
51typedef struct {
52 msg_t *buffer; /**< @brief Pointer to the mailbox
53 buffer. */
54 msg_t *top; /**< @brief Pointer to the location
55 after the buffer. */
56 msg_t *wrptr; /**< @brief Write pointer. */
57 msg_t *rdptr; /**< @brief Read pointer. */
58 size_t cnt; /**< @brief Messages in queue. */
59 bool reset; /**< @brief True in reset state. */
60 threads_queue_t qw; /**< @brief Queued writers. */
61 threads_queue_t qr; /**< @brief Queued readers. */
62} mailbox_t;
63
64/*===========================================================================*/
65/* Module macros. */
66/*===========================================================================*/
67
68/**
69 * @brief Data part of a static mailbox initializer.
70 * @details This macro should be used when statically initializing a
71 * mailbox that is part of a bigger structure.
72 *
73 * @param[in] name the name of the mailbox variable
74 * @param[in] buffer pointer to the mailbox buffer array of @p msg_t
75 * @param[in] size number of @p msg_t elements in the buffer array
76 */
77#define __MAILBOX_DATA(name, buffer, size) { \
78 (msg_t *)(buffer), \
79 (msg_t *)(buffer) + size, \
80 (msg_t *)(buffer), \
81 (msg_t *)(buffer), \
82 (size_t)0, \
83 false, \
84 __THREADS_QUEUE_DATA(name.qw), \
85 __THREADS_QUEUE_DATA(name.qr), \
86}
87
88/**
89 * @brief Static mailbox initializer.
90 * @details Statically initialized mailboxes require no explicit
91 * initialization using @p chMBObjectInit().
92 *
93 * @param[in] name the name of the mailbox variable
94 * @param[in] buffer pointer to the mailbox buffer array of @p msg_t
95 * @param[in] size number of @p msg_t elements in the buffer array
96 */
97#define MAILBOX_DECL(name, buffer, size) \
98 mailbox_t name = __MAILBOX_DATA(name, buffer, size)
99
100/*===========================================================================*/
101/* External declarations. */
102/*===========================================================================*/
103
104#ifdef __cplusplus
105extern "C" {
106#endif
107 void chMBObjectInit(mailbox_t *mbp, msg_t *buf, size_t n);
108 void chMBReset(mailbox_t *mbp);
109 void chMBResetI(mailbox_t *mbp);
112 msg_t chMBPostI(mailbox_t *mbp, msg_t msg);
116 msg_t chMBFetchTimeout(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout);
118 msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp);
119#ifdef __cplusplus
120}
121#endif
122
123/*===========================================================================*/
124/* Module inline functions. */
125/*===========================================================================*/
126
127/**
128 * @brief Returns the mailbox buffer size as number of messages.
129 *
130 * @param[in] mbp the pointer to an initialized @p mailbox_t object
131 * @return The size of the mailbox.
132 *
133 * @iclass
134 */
135static inline size_t chMBGetSizeI(const mailbox_t *mbp) {
136
137 /*lint -save -e9033 [10.8] Perfectly safe pointers
138 arithmetic.*/
139 return (size_t)(mbp->top - mbp->buffer);
140 /*lint -restore*/
141}
142
143/**
144 * @brief Returns the number of used message slots into a mailbox.
145 *
146 * @param[in] mbp the pointer to an initialized @p mailbox_t object
147 * @return The number of queued messages.
148 *
149 * @iclass
150 */
151static inline size_t chMBGetUsedCountI(const mailbox_t *mbp) {
152
154
155 return mbp->cnt;
156}
157
158/**
159 * @brief Returns the number of free message slots into a mailbox.
160 *
161 * @param[in] mbp the pointer to an initialized @p mailbox_t object
162 * @return The number of empty message slots.
163 *
164 * @iclass
165 */
166static inline size_t chMBGetFreeCountI(const mailbox_t *mbp) {
167
169
170 return chMBGetSizeI(mbp) - chMBGetUsedCountI(mbp);
171}
172
173/**
174 * @brief Returns the next message in the queue without removing it.
175 * @pre A message must be waiting in the queue for this function to work
176 * or it would return garbage. The correct way to use this macro is
177 * to use @p chMBGetUsedCountI() and then use this macro, all within
178 * a lock state.
179 *
180 * @param[in] mbp the pointer to an initialized @p mailbox_t object
181 * @return The next message in queue.
182 *
183 * @iclass
184 */
185static inline msg_t chMBPeekI(const mailbox_t *mbp) {
186
188
189 return *mbp->rdptr;
190}
191
192/**
193 * @brief Terminates the reset state.
194 *
195 * @param[in] mbp the pointer to an initialized @p mailbox_t object
196 *
197 * @xclass
198 */
199static inline void chMBResumeX(mailbox_t *mbp) {
200
201 mbp->reset = false;
202}
203
204#endif /* CH_CFG_USE_MAILBOXES == TRUE */
205
206#endif /* CHMBOXES_H */
207
208/** @} */
#define chDbgCheckClassI()
Definition chdebug.h:98
int32_t msg_t
Definition chearly.h:87
void chMBObjectInit(mailbox_t *mbp, msg_t *buf, size_t n)
Initializes a mailbox_t object.
Definition chmboxes.c:86
msg_t chMBFetchTimeout(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition chmboxes.c:414
static size_t chMBGetSizeI(const mailbox_t *mbp)
Returns the mailbox buffer size as number of messages.
Definition chmboxes.h:135
static msg_t chMBPeekI(const mailbox_t *mbp)
Returns the next message in the queue without removing it.
Definition chmboxes.h:185
msg_t chMBPostTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition chmboxes.c:164
static void chMBResumeX(mailbox_t *mbp)
Terminates the reset state.
Definition chmboxes.h:199
msg_t chMBPostI(mailbox_t *mbp, msg_t msg)
Posts a message into a mailbox.
Definition chmboxes.c:242
static size_t chMBGetUsedCountI(const mailbox_t *mbp)
Returns the number of used message slots into a mailbox.
Definition chmboxes.h:151
msg_t chMBPostAheadTimeout(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts an high priority message into a mailbox.
Definition chmboxes.c:289
static size_t chMBGetFreeCountI(const mailbox_t *mbp)
Returns the number of free message slots into a mailbox.
Definition chmboxes.h:166
void chMBResetI(mailbox_t *mbp)
Resets a mailbox_t object.
Definition chmboxes.c:132
msg_t chMBPostTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts a message into a mailbox.
Definition chmboxes.c:193
msg_t chMBPostAheadTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout)
Posts an high priority message into a mailbox.
Definition chmboxes.c:318
msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp)
Retrieves a message from a mailbox.
Definition chmboxes.c:492
msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg)
Posts an high priority message into a mailbox.
Definition chmboxes.c:367
msg_t chMBFetchTimeoutS(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout)
Retrieves a message from a mailbox.
Definition chmboxes.c:443
void chMBReset(mailbox_t *mbp)
Resets a mailbox_t object.
Definition chmboxes.c:112
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
Structure representing a mailbox object.
Definition chmboxes.h:51
threads_queue_t qw
Queued writers.
Definition chmboxes.h:60
msg_t * wrptr
Write pointer.
Definition chmboxes.h:56
threads_queue_t qr
Queued readers.
Definition chmboxes.h:61
bool reset
True in reset state.
Definition chmboxes.h:59
msg_t * buffer
Pointer to the mailbox buffer.
Definition chmboxes.h:52
size_t cnt
Messages in queue.
Definition chmboxes.h:58
msg_t * rdptr
Read pointer.
Definition chmboxes.h:57
msg_t * top
Pointer to the location after the buffer.
Definition chmboxes.h:54
Type of a thread queue.
Definition osal.h:238