Add chMsgPoll()

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
User avatar
FXCoder
Posts: 126
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 32 times
Been thanked: 32 times

Add chMsgPoll()

Postby FXCoder » Fri Jun 29, 2018 2:45 am

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.

Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 1 guest