ChibiOS 21.11.5
nil/src/chmsg.c
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 nil/src/chmsg.c
21 * @brief Nil RTOS synchronous messages source file.
22 *
23 * @addtogroup NIL_MESSAGES
24 * @{
25 */
26
27#include "ch.h"
28
29#if (CH_CFG_USE_MESSAGES == TRUE) || defined(__DOXYGEN__)
30
31/*===========================================================================*/
32/* Module local definitions. */
33/*===========================================================================*/
34
35/*===========================================================================*/
36/* Module exported variables. */
37/*===========================================================================*/
38
39/*===========================================================================*/
40/* Module local variables. */
41/*===========================================================================*/
42
43/*===========================================================================*/
44/* Module local functions. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Module interrupt handlers. */
49/*===========================================================================*/
50
51/*===========================================================================*/
52/* Module exported functions. */
53/*===========================================================================*/
54
55/**
56 * @brief Sends a message to the specified thread.
57 * @details The sender is stopped until the receiver executes a
58 * @p chMsgRelease()after receiving the message.
59 *
60 * @param[in] tp the pointer to the thread
61 * @param[in] msg the message
62 * @return The answer message from @p chMsgRelease().
63 *
64 * @api
65 */
67 thread_t *ctp = nil.current;
68
69 chDbgCheck(tp != NULL);
70
71 chSysLock();
72 ctp->sntmsg = msg;
73 ctp->u1.tp = tp;
74 if (NIL_THD_IS_WTMSG(tp)) {
75 (void) chSchReadyI(tp, (msg_t)ctp);
76 }
79
80 return msg;
81}
82
83/**
84 * @brief Suspends the thread and waits for an incoming message.
85 * @post After receiving a message the function @p chMsgGet() must be
86 * called in order to retrieve the message and then @p chMsgRelease()
87 * must be invoked in order to acknowledge the reception and send
88 * the answer.
89 * @note If the message is a pointer then you can assume that the data
90 * pointed by the message is stable until you invoke @p chMsgRelease()
91 * because the sending thread is suspended until then.
92 * @note The reference counter of the sender thread is not increased, the
93 * returned pointer is a temporary reference.
94 *
95 * @return A pointer to the thread carrying the message.
96 *
97 * @api
98 */
100 thread_t *tp;
101
102 chSysLock();
103 tp = chMsgWaitS();
104 chSysUnlock();
105
106 return tp;
107}
108
109/**
110 * @brief Suspends the thread and waits for an incoming message or a
111 * timeout to occur.
112 * @post After receiving a message the function @p chMsgGet() must be
113 * called in order to retrieve the message and then @p chMsgRelease()
114 * must be invoked in order to acknowledge the reception and send
115 * the answer.
116 * @note If the message is a pointer then you can assume that the data
117 * pointed by the message is stable until you invoke @p chMsgRelease()
118 * because the sending thread is suspended until then.
119 * @note The reference counter of the sender thread is not increased, the
120 * returned pointer is a temporary reference.
121 *
122 * @param[in] timeout the number of ticks before the operation timeouts,
123 * the following special values are allowed:
124 * - @a TIME_IMMEDIATE immediate timeout.
125 * - @a TIME_INFINITE no timeout.
126 * .
127 * @return A pointer to the thread carrying the message.
128 * @retval NULL if a timeout occurred.
129 *
130 * @api
131 */
133 thread_t *tp;
134
135 chSysLock();
136 tp = chMsgWaitTimeoutS(timeout);
137 chSysUnlock();
138
139 return tp;
140}
141
142/**
143 * @brief Suspends the thread and waits for an incoming message or a
144 * timeout to occur.
145 * @post After receiving a message the function @p chMsgGet() must be
146 * called in order to retrieve the message and then @p chMsgRelease()
147 * must be invoked in order to acknowledge the reception and send
148 * the answer.
149 * @note If the message is a pointer then you can assume that the data
150 * pointed by the message is stable until you invoke @p chMsgRelease()
151 * because the sending thread is suspended until then.
152 * @note The reference counter of the sender thread is not increased, the
153 * returned pointer is a temporary reference.
154 *
155 * @param[in] timeout the number of ticks before the operation timeouts,
156 * the following special values are allowed:
157 * - @a TIME_INFINITE no timeout.
158 * .
159 * @return A pointer to the thread carrying the message.
160 * @retval NULL if a timeout occurred.
161 *
162 * @sclass
163 */
165 thread_t *tp;
166
168
170 if (tp == NULL) {
172 if (msg != MSG_TIMEOUT) {
173 return (thread_t *)msg;
174 }
175 }
176
177 return tp;
178}
179
180/**
181 * @brief Releases a sender thread specifying a response message.
182 * @pre Invoke this function only after a message has been received
183 * using @p chMsgWait().
184 *
185 * @param[in] tp pointer to the thread
186 * @param[in] msg message to be returned to the sender
187 *
188 * @api
189 */
191
192 chSysLock();
193 chDbgAssert(tp->state == NIL_STATE_SNDMSGQ, "invalid state");
194 chMsgReleaseS(tp, msg);
195 chSysUnlock();
196}
197
198#endif /* CH_CFG_USE_MESSAGES == TRUE */
199
200/** @} */
#define chSysUnlock()
Leaves the kernel lock state.
os_instance_t nil
System data structures.
Definition ch.c:40
#define NIL_STATE_WTMSG
Waiting for a message.
#define chSysLock()
Enters the kernel lock state.
#define NIL_THD_IS_WTMSG(tp)
thread_t * nil_find_thread(tstate_t state, void *p)
Retrieves the highest priority thread in the specified state and associated to the specified object.
Definition ch.c:69
#define NIL_STATE_SNDMSGQ
Sending a message, in queue.
thread_t * chMsgWaitTimeoutS(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
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.
#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.
void chMsgRelease(thread_t *tp, msg_t msg)
Releases a sender thread specifying a response message.
thread_t * chMsgWaitTimeout(sysinterval_t timeout)
Suspends the thread and waits for an incoming message or a timeout to occur.
#define chDbgAssert(c, r)
Condition assertion.
Definition chdebug.h:143
#define chDbgCheck(c)
Function parameters check.
Definition chdebug.h:117
#define chDbgCheckClassS()
Definition chdebug.h:99
int32_t msg_t
Definition chearly.h:87
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
thread_t * chSchReadyI(thread_t *tp)
Inserts a thread in the Ready List placing it behind its peers.
Definition chschd.c:279
#define MSG_TIMEOUT
Wakeup caused by a timeout condition.
Definition chschd.h:39
msg_t chSchGoSleepTimeoutS(tstate_t newstate, sysinterval_t timeout)
Puts the current thread to sleep into the specified state with timeout specification.
Definition chschd.c:358
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
#define TIME_INFINITE
Infinite interval specification for all functions with a timeout specification.
Definition chtime.h:54
tstate_t state
Current thread state.
Definition chobjects.h:203