ChibiOS/RT  6.1.4
chmsg.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3  2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4 
5  This file is part of ChibiOS.
6 
7  ChibiOS is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 3 of the License.
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 rt/include/chmsg.h
22  * @brief Messages macros and structures.
23  *
24  * @addtogroup messages
25  * @{
26  */
27 
28 #ifndef CHMSG_H
29 #define CHMSG_H
30 
31 #if (CH_CFG_USE_MESSAGES == TRUE) || defined(__DOXYGEN__)
32 
33 /*===========================================================================*/
34 /* Module constants. */
35 /*===========================================================================*/
36 
37 /*===========================================================================*/
38 /* Module pre-compile time settings. */
39 /*===========================================================================*/
40 
41 /*===========================================================================*/
42 /* Derived constants and error checks. */
43 /*===========================================================================*/
44 
45 /*===========================================================================*/
46 /* Module data structures and types. */
47 /*===========================================================================*/
48 
49 /*===========================================================================*/
50 /* Module macros. */
51 /*===========================================================================*/
52 
53 /*===========================================================================*/
54 /* External declarations. */
55 /*===========================================================================*/
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60  msg_t chMsgSend(thread_t *tp, msg_t msg);
61  thread_t *chMsgWaitS(void);
63  thread_t *chMsgPollS(void);
64  void chMsgRelease(thread_t *tp, msg_t msg);
65 #ifdef __cplusplus
66 }
67 #endif
68 
69 /*===========================================================================*/
70 /* External declarations. */
71 /*===========================================================================*/
72 
73 /**
74  * @brief Suspends the thread and waits for an incoming message.
75  * @post After receiving a message the function @p chMsgGet() must be
76  * called in order to retrieve the message and then @p chMsgRelease()
77  * must be invoked in order to acknowledge the reception and send
78  * the answer.
79  * @note If the message is a pointer then you can assume that the data
80  * pointed by the message is stable until you invoke @p chMsgRelease()
81  * because the sending thread is suspended until then.
82  * @note The reference counter of the sender thread is not increased, the
83  * returned pointer is a temporary reference.
84  *
85  * @return A pointer to the thread carrying the message.
86  *
87  * @api
88  */
89 static inline thread_t *chMsgWait(void) {
90  thread_t *tp;
91 
92  chSysLock();
93  tp = chMsgWaitS();
94  chSysUnlock();
95 
96  return tp;
97 }
98 
99 /**
100  * @brief Suspends the thread and waits for an incoming message or a
101  * timeout to occur.
102  * @post After receiving a message the function @p chMsgGet() must be
103  * called in order to retrieve the message and then @p chMsgRelease()
104  * must be invoked in order to acknowledge the reception and send
105  * the answer.
106  * @note If the message is a pointer then you can assume that the data
107  * pointed by the message is stable until you invoke @p chMsgRelease()
108  * because the sending thread is suspended until then.
109  * @note The reference counter of the sender thread is not increased, the
110  * returned pointer is a temporary reference.
111  *
112  * @param[in] timeout the number of ticks before the operation timeouts,
113  * the following special values are allowed:
114  * - @a TIME_IMMEDIATE immediate timeout.
115  * - @a TIME_INFINITE no timeout.
116  * .
117  * @return A pointer to the thread carrying the message.
118  * @retval NULL if a timeout occurred.
119  *
120  * @api
121  */
122 static inline thread_t *chMsgWaitTimeout(sysinterval_t timeout) {
123  thread_t *tp;
124 
125  chSysLock();
126  tp = chMsgWaitTimeoutS(timeout);
127  chSysUnlock();
128 
129  return tp;
130 }
131 
132 /**
133  * @brief Poll to check for an incoming message.
134  * @post If a message is available the function @p chMsgGet() must be
135  * called in order to retrieve the message and then @p chMsgRelease()
136  * must be invoked in order to acknowledge the reception and send
137  * the answer.
138  * @note If the message is a pointer then you can assume that the data
139  * pointed by the message is stable until you invoke @p chMsgRelease()
140  * because the sending thread is suspended until then.
141  * @note The reference counter of the sender thread is not increased, the
142  * returned pointer is a temporary reference.
143  *
144  * @return A pointer to the thread carrying the message.
145  * @retval NULL if no incoming message waiting.
146  *
147  * @api
148  */
149 static inline thread_t *chMsgPoll(void) {
150  thread_t *tp;
151 
152  chSysLock();
153  tp = chMsgPollS();
154  chSysUnlock();
155 
156  return tp;
157 }
158 
159 /**
160  * @brief Evaluates to @p true if the thread has pending messages.
161  *
162  * @param[in] tp pointer to the thread
163  * @return The pending messages status.
164  *
165  * @iclass
166  */
167 static inline bool chMsgIsPendingI(thread_t *tp) {
168 
170 
171  return (bool)(tp->msgqueue.next != &tp->msgqueue);
172 }
173 
174 /**
175  * @brief Returns the message carried by the specified thread.
176  * @pre This function must be invoked immediately after exiting a call
177  * to @p chMsgWait().
178  *
179  * @param[in] tp pointer to the thread
180  * @return The message carried by the sender.
181  *
182  * @api
183  */
184 static inline msg_t chMsgGet(thread_t *tp) {
185 
186  chDbgAssert(tp->state == CH_STATE_SNDMSG, "invalid state");
187 
188  return tp->u.sentmsg;
189 }
190 
191 /**
192  * @brief Releases the thread waiting on top of the messages queue.
193  * @pre Invoke this function only after a message has been received
194  * using @p chMsgWait().
195  *
196  * @param[in] tp pointer to the thread
197  * @param[in] msg message to be returned to the sender
198  *
199  * @sclass
200  */
201 static inline void chMsgReleaseS(thread_t *tp, msg_t msg) {
202 
204 
205  chSchWakeupS(tp, msg);
206 }
207 
208 #endif /* CH_CFG_USE_MESSAGES == TRUE */
209 
210 #endif /* CHMSG_H */
211 
212 /** @} */
ch_queue::next
ch_queue_t * next
Next in the list/queue.
Definition: chlists.h:70
chSysLock
static void chSysLock(void)
Enters the kernel lock state.
Definition: chsys.h:355
chMsgPollS
thread_t * chMsgPollS(void)
Poll to check for an incoming message.
Definition: chmsg.c:190
chDbgAssert
#define chDbgAssert(c, r)
Condition assertion.
Definition: chdebug.h:127
chMsgReleaseS
static void chMsgReleaseS(thread_t *tp, msg_t msg)
Releases the thread waiting on top of the messages queue.
Definition: chmsg.h:201
chMsgWaitS
thread_t * chMsgWaitS(void)
Suspends the thread and waits for an incoming message.
Definition: chmsg.c:121
chMsgGet
static msg_t chMsgGet(thread_t *tp)
Returns the message carried by the specified thread.
Definition: chmsg.h:184
ch_thread::sentmsg
msg_t sentmsg
Thread sent message.
Definition: chschd.h:227
ch_thread
Structure representing a thread.
Definition: chschd.h:134
chMsgSend
msg_t chMsgSend(thread_t *tp, msg_t msg)
Sends a message to the specified thread.
Definition: chmsg.c:87
chMsgWait
static thread_t * chMsgWait(void)
Suspends the thread and waits for an incoming message.
Definition: chmsg.h:89
ch_thread::msgqueue
ch_queue_t msgqueue
Messages queue.
Definition: chschd.h:266
ch_thread::u
union ch_thread::@1 u
State-specific fields.
CH_STATE_SNDMSG
#define CH_STATE_SNDMSG
Sent a message, waiting answer.
Definition: chschd.h:77
chDbgCheckClassI
void chDbgCheckClassI(void)
I-class functions context check.
Definition: chdebug.c:233
chMsgIsPendingI
static bool chMsgIsPendingI(thread_t *tp)
Evaluates to true if the thread has pending messages.
Definition: chmsg.h:167
chMsgPoll
static thread_t * chMsgPoll(void)
Poll to check for an incoming message.
Definition: chmsg.h:149
chMsgRelease
void chMsgRelease(thread_t *tp, msg_t msg)
Releases a sender thread specifying a response message.
Definition: chmsg.c:211
sysinterval_t
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
ch_thread::state
tstate_t state
Current thread state.
Definition: chschd.h:165
chSysUnlock
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition: chsys.h:367
chMsgWaitTimeout
static thread_t * chMsgWaitTimeout(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
Definition: chmsg.h:122
chDbgCheckClassS
void chDbgCheckClassS(void)
S-class functions context check.
Definition: chdebug.c:248
chMsgWaitTimeoutS
thread_t * chMsgWaitTimeoutS(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
Definition: chmsg.c:157
chSchWakeupS
void chSchWakeupS(thread_t *ntp, msg_t msg)
Wakes up a thread.
Definition: chschd.c:295