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.
Post Reply
User avatar
FXCoder
Posts: 403
Joined: Sun Jun 12, 2016 4:10 am
Has thanked: 188 times
Been thanked: 134 times

Add chMsgPoll()

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Add chMsgPoll()

Post by Giovanni »

bump
User avatar
FXCoder
Posts: 403
Joined: Sun Jun 12, 2016 4:10 am
Has thanked: 188 times
Been thanked: 134 times

Re: Add chMsgPoll()

Post 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
Post Reply