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: 384
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 180 times
Been thanked: 130 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.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Add chMsgPoll()

Postby Giovanni » Sun Jan 06, 2019 5:13 pm

bump

User avatar
FXCoder
Posts: 384
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 180 times
Been thanked: 130 times

Re: Add chMsgPoll()

Postby FXCoder » Mon Jan 07, 2019 5:07 am

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


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 6 guests