ChibiOS 21.11.5
rt/include/chmsg.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 rt/include/chmsg.h
21 * @brief Messages macros and structures.
22 *
23 * @addtogroup messages
24 * @{
25 */
26
27#ifndef CHMSG_H
28#define CHMSG_H
29
30#if (CH_CFG_USE_MESSAGES == 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/* Module macros. */
50/*===========================================================================*/
51
52#if CH_CFG_USE_MESSAGES_PRIORITY == TRUE
53#define __ch_msg_insert(qp, tp) ch_sch_prio_insert(qp, &tp->hdr.queue)
54#else
55#define __ch_msg_insert(qp, tp) ch_queue_insert(qp, &tp->hdr.queue)
56#endif
57
58/*===========================================================================*/
59/* External declarations. */
60/*===========================================================================*/
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65 msg_t chMsgSend(thread_t *tp, msg_t msg);
66 thread_t *chMsgWaitS(void);
68 thread_t *chMsgPollS(void);
69 void chMsgRelease(thread_t *tp, msg_t msg);
70#ifdef __cplusplus
71}
72#endif
73
74/*===========================================================================*/
75/* External declarations. */
76/*===========================================================================*/
77
78/**
79 * @brief Suspends the thread and waits for an incoming message.
80 * @post After receiving a message the function @p chMsgGet() must be
81 * called in order to retrieve the message and then @p chMsgRelease()
82 * must be invoked in order to acknowledge the reception and send
83 * the answer.
84 * @note If the message is a pointer then you can assume that the data
85 * pointed by the message is stable until you invoke @p chMsgRelease()
86 * because the sending thread is suspended until then.
87 * @note The reference counter of the sender thread is not increased, the
88 * returned pointer is a temporary reference.
89 *
90 * @return A pointer to the thread carrying the message.
91 *
92 * @api
93 */
94static inline thread_t *chMsgWait(void) {
95 thread_t *tp;
96
97 chSysLock();
98 tp = chMsgWaitS();
100
101 return tp;
102}
103
104/**
105 * @brief Suspends the thread and waits for an incoming message or a
106 * timeout to occur.
107 * @post After receiving a message the function @p chMsgGet() must be
108 * called in order to retrieve the message and then @p chMsgRelease()
109 * must be invoked in order to acknowledge the reception and send
110 * the answer.
111 * @note If the message is a pointer then you can assume that the data
112 * pointed by the message is stable until you invoke @p chMsgRelease()
113 * because the sending thread is suspended until then.
114 * @note The reference counter of the sender thread is not increased, the
115 * returned pointer is a temporary reference.
116 *
117 * @param[in] timeout the number of ticks before the operation timeouts,
118 * the following special values are handled:
119 * - @a TIME_INFINITE no timeout.
120 * - @a TIME_IMMEDIATE this value is not allowed.
121 * @return A pointer to the thread carrying the message.
122 * @retval NULL if a timeout occurred.
123 *
124 * @api
125 */
126static inline thread_t *chMsgWaitTimeout(sysinterval_t timeout) {
127 thread_t *tp;
128
129 chSysLock();
130 tp = chMsgWaitTimeoutS(timeout);
131 chSysUnlock();
132
133 return tp;
134}
135
136/**
137 * @brief Poll to check for an incoming message.
138 * @post If a message is available the function @p chMsgGet() must be
139 * called in order to retrieve the message and then @p chMsgRelease()
140 * must be invoked in order to acknowledge the reception and send
141 * the answer.
142 * @note If the message is a pointer then you can assume that the data
143 * pointed by the message is stable until you invoke @p chMsgRelease()
144 * because the sending thread is suspended until then.
145 * @note The reference counter of the sender thread is not increased, the
146 * returned pointer is a temporary reference.
147 *
148 * @return A pointer to the thread carrying the message.
149 * @retval NULL if no incoming message waiting.
150 *
151 * @api
152 */
153static inline thread_t *chMsgPoll(void) {
154 thread_t *tp;
155
156 chSysLock();
157 tp = chMsgPollS();
158 chSysUnlock();
159
160 return tp;
161}
162
163/**
164 * @brief Evaluates to @p true if the thread has pending messages.
165 *
166 * @param[in] tp pointer to the thread
167 * @return The pending messages status.
168 *
169 * @iclass
170 */
171static inline bool chMsgIsPendingI(thread_t *tp) {
172
174
175 return (bool)(tp->msgqueue.next != &tp->msgqueue);
176}
177
178/**
179 * @brief Returns the message carried by the specified thread.
180 * @pre This function must be invoked immediately after exiting a call
181 * to @p chMsgWait().
182 *
183 * @param[in] tp pointer to the thread
184 * @return The message carried by the sender.
185 *
186 * @api
187 */
188static inline msg_t chMsgGet(thread_t *tp) {
189
190 chDbgAssert(tp->state == CH_STATE_SNDMSG, "invalid state");
191
192 return tp->sentmsg;
193}
194
195/**
196 * @brief Releases the thread waiting on top of the messages queue.
197 * @pre Invoke this function only after a message has been received
198 * using @p chMsgWait().
199 *
200 * @param[in] tp pointer to the thread
201 * @param[in] msg message to be returned to the sender
202 *
203 * @sclass
204 */
205static inline void chMsgReleaseS(thread_t *tp, msg_t msg) {
206
208
209 chSchWakeupS(tp, msg);
210}
211
212#endif /* CH_CFG_USE_MESSAGES == TRUE */
213
214#endif /* CHMSG_H */
215
216/** @} */
#define chSysUnlock()
Leaves the kernel lock state.
#define chSchWakeupS(ntp, msg)
Wakes up a thread.
#define chSysLock()
Enters the kernel lock state.
#define chMsgGet(tp)
Returns the message carried by the specified thread.
#define chMsgReleaseS(tp, msg)
Releases the thread waiting on top of the messages queue.
#define chMsgWaitS()
Suspends the thread and waits for an incoming message.
#define chDbgAssert(c, r)
Condition assertion.
Definition chdebug.h:143
#define chDbgCheckClassS()
Definition chdebug.h:99
#define chDbgCheckClassI()
Definition chdebug.h:98
static thread_t * chMsgPoll(void)
Poll to check for an incoming message.
thread_t * chMsgWaitTimeoutS(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
static thread_t * chMsgWait(void)
Suspends the thread and waits for an incoming message.
msg_t chMsgSend(thread_t *tp, msg_t msg)
Sends a message to the specified thread.
thread_t * chMsgPollS(void)
Poll to check for an incoming message.
static bool chMsgIsPendingI(thread_t *tp)
Evaluates to true if the thread has pending messages.
void chMsgRelease(thread_t *tp, msg_t msg)
Releases a sender thread specifying a response message.
static thread_t * chMsgWaitTimeout(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
int32_t msg_t
Definition chearly.h:87
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
#define CH_STATE_SNDMSG
Sent a message, waiting answer.
Definition chschd.h:76
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
ch_queue_t * next
Next in the list/queue.
Definition chlists.h:69
msg_t sentmsg
Sent message.
Definition chobjects.h:316
tstate_t state
Current thread state.
Definition chobjects.h:203
ch_queue_t msgqueue
Messages queue.
Definition chobjects.h:298