From 9ab62baa10d5522f0185b7579806913d73b649bf Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Tue, 19 Nov 2019 14:02:56 +0000 Subject: [PATCH] Improvements to messages, new functions chMsgWaitS(), chMsgWaitTimeoutS(), chMsgWaitTimeout() (NIL). git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@13187 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/nil/include/chmsg.h | 20 ++++++++++++ os/nil/src/chmsg.c | 72 ++++++++++++++++++++++++++++++++++++++++-- os/rt/src/chmsg.c | 2 ++ readme.txt | 5 +-- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/os/nil/include/chmsg.h b/os/nil/include/chmsg.h index 19ed088ba..abe1de75e 100644 --- a/os/nil/include/chmsg.h +++ b/os/nil/include/chmsg.h @@ -54,6 +54,24 @@ * @name Macro Functions * @{ */ +/** + * @brief Suspends the thread and waits for an incoming message. + * @post After receiving a message the function @p chMsgGet() must be + * called in order to retrieve the message and then @p chMsgRelease() + * must be invoked in order to acknowledge the reception and send + * the answer. + * @note If the message is a pointer then you can assume that the data + * pointed by the message is stable until you invoke @p chMsgRelease() + * because the sending thread is suspended until then. + * @note The reference counter of the sender thread is not increased, the + * returned pointer is a temporary reference. + * + * @return A pointer to the thread carrying the message. + * + * @sclass + */ +#define chMsgWaitS() chMsgWaitTimeoutS(TIME_INFINITE) + /** * @brief Returns the message carried by the specified thread. * @pre This function must be invoked immediately after exiting a call @@ -91,6 +109,8 @@ extern "C" { #endif msg_t chMsgSend(thread_t *tp, msg_t msg); thread_t *chMsgWait(void); + thread_t *chMsgWaitTimeout(sysinterval_t timeout); + thread_t *chMsgWaitTimeoutS(sysinterval_t timeout); void chMsgRelease(thread_t *tp, msg_t msg); #ifdef __cplusplus } diff --git a/os/nil/src/chmsg.c b/os/nil/src/chmsg.c index 08a6a9a71..ccefcc587 100644 --- a/os/nil/src/chmsg.c +++ b/os/nil/src/chmsg.c @@ -101,11 +101,79 @@ thread_t *chMsgWait(void) { thread_t *tp; chSysLock(); + tp = chMsgWaitS(); + chSysUnlock(); + + return tp; +} + +/** + * @brief Suspends the thread and waits for an incoming message or a + * timeout to occur. + * @post After receiving a message the function @p chMsgGet() must be + * called in order to retrieve the message and then @p chMsgRelease() + * must be invoked in order to acknowledge the reception and send + * the answer. + * @note If the message is a pointer then you can assume that the data + * pointed by the message is stable until you invoke @p chMsgRelease() + * because the sending thread is suspended until then. + * @note The reference counter of the sender thread is not increased, the + * returned pointer is a temporary reference. + * + * @param[in] timeout the number of ticks before the operation timeouts, + * the following special values are allowed: + * - @a TIME_IMMEDIATE immediate timeout. + * - @a TIME_INFINITE no timeout. + * . + * @return A pointer to the thread carrying the message. + * @retval NULL if a timeout occurred. + * + * @api + */ +thread_t *chMsgWaitTimeout(sysinterval_t timeout) { + thread_t *tp; + + chSysLock(); + tp = chMsgWaitTimeoutS(timeout); + chSysUnlock(); + + return tp; +} + +/** + * @brief Suspends the thread and waits for an incoming message or a + * timeout to occur. + * @post After receiving a message the function @p chMsgGet() must be + * called in order to retrieve the message and then @p chMsgRelease() + * must be invoked in order to acknowledge the reception and send + * the answer. + * @note If the message is a pointer then you can assume that the data + * pointed by the message is stable until you invoke @p chMsgRelease() + * because the sending thread is suspended until then. + * @note The reference counter of the sender thread is not increased, the + * returned pointer is a temporary reference. + * + * @param[in] timeout the number of ticks before the operation timeouts, + * the following special values are allowed: + * - @a TIME_INFINITE no timeout. + * . + * @return A pointer to the thread carrying the message. + * @retval NULL if a timeout occurred. + * + * @sclass + */ +thread_t *chMsgWaitTimeoutS(sysinterval_t timeout) { + thread_t *tp; + + chDbgCheckClassS(); + tp = nil_find_thread(NIL_STATE_SNDMSGQ, nil.current); if (tp == NULL) { - tp = (thread_t *)chSchGoSleepTimeoutS(NIL_STATE_WTMSG, TIME_INFINITE); + msg_t msg = chSchGoSleepTimeoutS(NIL_STATE_WTMSG, timeout); + if (msg != MSG_TIMEOUT) { + return (thread_t *)msg; + } } - chSysUnlock(); return tp; } diff --git a/os/rt/src/chmsg.c b/os/rt/src/chmsg.c index 8a97e8a4a..48bdab022 100644 --- a/os/rt/src/chmsg.c +++ b/os/rt/src/chmsg.c @@ -121,6 +121,8 @@ msg_t chMsgSend(thread_t *tp, msg_t msg) { thread_t *chMsgWaitS(void) { thread_t *tp; + chDbgCheckClassS(); + if (!chMsgIsPendingI(currp)) { chSchGoSleepS(CH_STATE_WTMSG); } diff --git a/readme.txt b/readme.txt index 5b7fef4fb..b5029545b 100644 --- a/readme.txt +++ b/readme.txt @@ -75,9 +75,10 @@ *** Next *** - LIB: Added support for delegate threads to OSLIB. +- NIL: Improvements to messages, new functions chMsgWaitS(), + chMsgWaitTimeoutS(), chMsgWaitTimeout(). - RT: Improvements to messages, new functions chMsgWaitS(), - chMsgWaitTimeoutS(), chMsgWaitTimeout(), chMsgWaitPollS(), - chMsgWaitPoll(). + chMsgWaitTimeoutS(), chMsgWaitTimeout(), chMsgPollS(), chMsgPoll(). - HAL: TRNG support added to STM32F7xx, STM32G0xx, STM32G4xx, STM32H7xx and STM32L0xx. - NEW: Added support for .cc files extensions in makefiles.