Page 1 of 1

Add chMsgPoll()

Posted: Fri Jun 29, 2018 2:45 am
by FXCoder
A small addition to chmsg.c
Although it is possible to use chMsgPendingI() followed by chMsgWait() to implement a polled wait I thought a specific function would be cleaner.

Code: Select all

Index: chmsg.c
===================================================================
--- chmsg.c   (revision 12120)
+++ chmsg.c   (working copy)
@@ -133,6 +133,37 @@
 }
 
 /**
+ * @brief   Poll to check for an incoming message.
+ * @post    If a message is available 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  Result of the poll.
+ * @retval  A pointer to the thread carrying the message.
+ * @retval  NULL if no incoming message waiting.
+ *
+ * @api
+ */
+thread_t *chMsgPoll(void) {
+  thread_t *tp = NULL;
+
+  chSysLock();
+  if (chMsgIsPendingI(currp)) {
+    tp = queue_fifo_remove(&currp->msgqueue);
+    tp->state = CH_STATE_SNDMSG;
+  }
+  chSysUnlock();
+
+  return tp;
+}
+
+/**
  * @brief   Releases a sender thread specifying a response message.
  * @pre     Invoke this function only after a message has been received
  *          using @p chMsgWait().


I use the above added function in a thread sweeper (idle or other as required) which enables a "chThdExitandCleanup()" type function to be used in terminating threads.

Re: Add chMsgPoll()

Posted: Sun Jan 06, 2019 5:13 pm
by Giovanni
bump

Re: Add chMsgPoll()

Posted: Mon Jan 07, 2019 5:07 am
by FXCoder
Hi,
Yes would still propose adding this helper function...

Code: Select all

Index: include/chmsg.h
===================================================================
--- include/chmsg.h   (revision 12537)
+++ include/chmsg.h   (working copy)
@@ -59,6 +59,7 @@
 #endif
   msg_t chMsgSend(thread_t *tp, msg_t msg);
   thread_t * chMsgWait(void);
+  thread_t * chMsgPoll(void);
   void chMsgRelease(thread_t *tp, msg_t msg);
 #ifdef __cplusplus
 }
Index: src/chmsg.c
===================================================================
--- src/chmsg.c   (revision 12537)
+++ src/chmsg.c   (working copy)
@@ -133,6 +133,37 @@
 }
 
 /**
+ * @brief   Poll to check for an incoming message.
+ * @post    If a message is available 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  Result of the poll.
+ * @retval  A pointer to the thread carrying the message.
+ * @retval  NULL if no incoming message waiting.
+ *
+ * @api
+ */
+thread_t *chMsgPoll(void) {
+  thread_t *tp = NULL;
+
+  chSysLock();
+  if (chMsgIsPendingI(currp)) {
+    tp = queue_fifo_remove(&currp->msgqueue);
+    tp->state = CH_STATE_SNDMSG;
+  }
+  chSysUnlock();
+
+  return tp;
+}
+
+/**
  * @brief   Releases a sender thread specifying a response message.
  * @pre     Invoke this function only after a message has been received
  *          using @p chMsgWait().



--
Bob