ChibiOS 21.11.5
nil/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 nil/include/chmsg.h
21 * @brief Nil RTOS synchronous messages header file.
22 *
23 * @addtogroup NIL_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/**
53 * @name Macro Functions
54 * @{
55 */
56/**
57 * @brief Suspends the thread and waits for an incoming message.
58 * @post After receiving a message the function @p chMsgGet() must be
59 * called in order to retrieve the message and then @p chMsgRelease()
60 * must be invoked in order to acknowledge the reception and send
61 * the answer.
62 * @note If the message is a pointer then you can assume that the data
63 * pointed by the message is stable until you invoke @p chMsgRelease()
64 * because the sending thread is suspended until then.
65 * @note The reference counter of the sender thread is not increased, the
66 * returned pointer is a temporary reference.
67 *
68 * @return A pointer to the thread carrying the message.
69 *
70 * @sclass
71 */
72#define chMsgWaitS() chMsgWaitTimeoutS(TIME_INFINITE)
73
74/**
75 * @brief Returns the message carried by the specified thread.
76 * @pre This function must be invoked immediately after exiting a call
77 * to @p chMsgWait().
78 *
79 * @param[in] tp pointer to the thread
80 * @return The message carried by the sender.
81 *
82 * @api
83 */
84#define chMsgGet(tp) ((tp)->sntmsg)
85
86/**
87 * @brief Releases the thread waiting on top of the messages queue.
88 * @pre Invoke this function only after a message has been received
89 * using @p chMsgWait().
90 *
91 * @param[in] tp pointer to the thread
92 * @param[in] msg message to be returned to the sender
93 *
94 * @sclass
95 */
96#define chMsgReleaseS(tp, msg) do { \
97 (void) chSchReadyI(tp, msg); \
98 chSchRescheduleS(); \
99 } while (false)
100/** @} */
101
102/*===========================================================================*/
103/* External declarations. */
104/*===========================================================================*/
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109 msg_t chMsgSend(thread_t *tp, msg_t msg);
110 thread_t *chMsgWait(void);
113 void chMsgRelease(thread_t *tp, msg_t msg);
114#ifdef __cplusplus
115}
116#endif
117
118#endif /* CH_CFG_USE_MESSAGES == TRUE */
119
120#endif /* CHMSG_H */
121
122/** @} */
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.
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
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118