aboutsummaryrefslogtreecommitdiffstats
path: root/os
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2011-02-23 18:59:39 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2011-02-23 18:59:39 +0000
commit6f6e1a6401eda000dce198150937c7919b4c9855 (patch)
treeb85e95a84dc9618c4abdf9150fdb9ea6070afe4f /os
parentb3b1028036a2f18327fb97f2126192a2ace62bb2 (diff)
downloadChibiOS-6f6e1a6401eda000dce198150937c7919b4c9855.tar.gz
ChibiOS-6f6e1a6401eda000dce198150937c7919b4c9855.tar.bz2
ChibiOS-6f6e1a6401eda000dce198150937c7919b4c9855.zip
Improved messages subsystem.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2759 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os')
-rw-r--r--os/kernel/include/chmsg.h41
-rw-r--r--os/kernel/include/chthreads.h22
-rw-r--r--os/kernel/src/chmsg.c59
-rw-r--r--os/kernel/src/chmtx.c2
4 files changed, 65 insertions, 59 deletions
diff --git a/os/kernel/include/chmsg.h b/os/kernel/include/chmsg.h
index 438da021f..b2e8ef51e 100644
--- a/os/kernel/include/chmsg.h
+++ b/os/kernel/include/chmsg.h
@@ -39,20 +39,47 @@
((tp)->p_msgqueue.p_next != (Thread *)&(tp)->p_msgqueue)
/**
- * @brief Returns the first message in the queue.
+ * @brief Returns the message carried by the specified thread.
+ * @pre This function must be invoked immediately after exiting a call
+ * to @p chMsgWait().
*
- * @iclass
+ * @param[in] tp pointer to the thread
+ * @return The message carried by the sender.
+ *
+ * @api
+ */
+#define chMsgGet(tp) ((tp)->p_msg)
+
+/**
+ * @brief Returns the message carried by the specified thread.
+ * @pre This function must be invoked immediately after exiting a call
+ * to @p chMsgWait().
+ *
+ * @param[in] tp pointer to the thread
+ * @return The message carried by the sender.
+ *
+ * @sclass
+ */
+#define chMsgGetS(tp) ((tp)->p_msg)
+
+/**
+ * @brief Releases the thread waiting on top of the messages queue.
+ * @pre Invoke this function only after a message has been received
+ * using @p chMsgWait().
+ *
+ * @param[in] tp pointer to the thread
+ * @param[in] msg message to be returned to the sender
+ *
+ * @sclass
*/
-#define chMsgGetI(tp) \
- ((tp)->p_msgqueue.p_next->p_msg)
+#define chMsgReleaseS(tp, msg) chSchWakeupS(tp, msg)
#ifdef __cplusplus
extern "C" {
#endif
msg_t chMsgSend(Thread *tp, msg_t msg);
- msg_t chMsgWait(void);
- msg_t chMsgGet(void);
- void chMsgRelease(msg_t msg);
+ Thread * chMsgWait(void);
+ void chMsgRelease(Thread *tp, msg_t msg);
#ifdef __cplusplus
}
#endif
diff --git a/os/kernel/include/chthreads.h b/os/kernel/include/chthreads.h
index c22255ad0..f6ed23d1d 100644
--- a/os/kernel/include/chthreads.h
+++ b/os/kernel/include/chthreads.h
@@ -176,12 +176,14 @@ struct Thread {
#define THD_STATE_WTOREVT 8
/** @brief Thread state: Waiting in @p chEvtWaitAllTimeout().*/
#define THD_STATE_WTANDEVT 9
-/** @brief Thread state: Waiting in @p chMsgSend().*/
-#define THD_STATE_SNDMSG 10
+/** @brief Thread state: Waiting in @p chMsgSend() (queued).*/
+#define THD_STATE_SNDMSGQ 10
+/** @brief Thread state: Waiting in @p chMsgSend() (not queued).*/
+#define THD_STATE_SNDMSG 11
/** @brief Thread state: Waiting in @p chMsgWait().*/
-#define THD_STATE_WTMSG 11
+#define THD_STATE_WTMSG 12
/** @brief Thread state: After termination.*/
-#define THD_STATE_FINAL 12
+#define THD_STATE_FINAL 13
/*
* Various flags into the thread p_flags field.
@@ -242,7 +244,7 @@ extern "C" {
* @note This function is only available when the
* @p CH_DBG_THREADS_PROFILING configuration option is enabled.
*
- * @param[in] tp the pointer to the thread
+ * @param[in] tp pointer to the thread
*
* @api
*/
@@ -258,7 +260,7 @@ extern "C" {
/**
* @brief Verifies if the specified thread is in the @p THD_STATE_FINAL state.
*
- * @param[in] tp the pointer to the thread
+ * @param[in] tp pointer to the thread
* @retval TRUE thread terminated.
* @retval FALSE thread not terminated.
*
@@ -279,7 +281,7 @@ extern "C" {
/**
* @brief Resumes a thread created with @p chThdInit().
*
- * @param[in] tp the pointer to the thread
+ * @param[in] tp pointer to the thread
*
* @iclass
*/
@@ -305,7 +307,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
- * @param[in] sec the time in seconds
+ * @param[in] sec time in seconds
*
* @api
*/
@@ -318,7 +320,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
- * @param[in] msec the time in milliseconds
+ * @param[in] msec time in milliseconds
*
* @api
*/
@@ -331,7 +333,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
- * @param[in] usec the time in microseconds
+ * @param[in] usec time in microseconds
*
* @api
*/
diff --git a/os/kernel/src/chmsg.c b/os/kernel/src/chmsg.c
index c3b4848b0..d4199bbb0 100644
--- a/os/kernel/src/chmsg.c
+++ b/os/kernel/src/chmsg.c
@@ -75,7 +75,7 @@ msg_t chMsgSend(Thread *tp, msg_t msg) {
msg_insert(ctp, &tp->p_msgqueue);
if (tp->p_state == THD_STATE_WTMSG)
chSchReadyI(tp);
- chSchGoSleepS(THD_STATE_SNDMSG);
+ chSchGoSleepS(THD_STATE_SNDMSGQ);
msg = ctp->p_u.rdymsg;
chSysUnlock();
return msg;
@@ -83,69 +83,46 @@ msg_t chMsgSend(Thread *tp, msg_t msg) {
/**
* @brief Suspends the thread and waits for an incoming message.
- * @post After receiving a message the function @p chMsgRelease() must be
- * invoked in order to acknowledge the reception and send the answer.
+ * @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.
*
- * @return The message.
+ * @return A reference to the thread carrying the message.
*
* @api
*/
-msg_t chMsgWait(void) {
- msg_t msg;
+Thread *chMsgWait(void) {
+ Thread *tp;
chSysLock();
if (!chMsgIsPendingI(currp))
chSchGoSleepS(THD_STATE_WTMSG);
-#if defined(CH_ARCHITECTURE_STM8)
- msg = chMsgGetI((volatile Thread *)currp); /* Temporary hack.*/
-#else
- msg = chMsgGetI(currp);
-#endif
+ tp = fifo_remove(&currp->p_msgqueue);
+ tp->p_state = THD_STATE_SNDMSG;
chSysUnlock();
- return msg;
-}
-
-/**
- * @brief Returns the next message in the queue.
- * @post After receiving a message the function @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.
- *
- * @return The message.
- * @retval 0 if the queue is empty.
- *
- * @api
- */
-msg_t chMsgGet(void) {
- msg_t msg;
-
- chSysLock();
- msg = chMsgIsPendingI(currp) ? chMsgGetI(currp) : (msg_t)NULL;
- chSysUnlock();
- return msg;
+ return tp;
}
/**
* @brief Releases the thread waiting on top of the messages queue.
* @pre Invoke this function only after a message has been received
- * using @p chMsgWait() or @p chMsgGet().
+ * using @p chMsgWait().
*
- * @param[in] msg the message returned to the message sender
+ * @param[in] tp pointer to the thread
+ * @param[in] msg message to be returned to the sender
*
* @api
*/
-void chMsgRelease(msg_t msg) {
+void chMsgRelease(Thread *tp, msg_t msg) {
chSysLock();
- chDbgAssert(chMsgIsPendingI(currp),
- "chMsgRelease(), #1",
- "no message pending");
- chSchWakeupS(fifo_remove(&currp->p_msgqueue), msg);
+ chDbgAssert(tp->p_state == THD_STATE_SNDMSG,
+ "chMsgRelease(), #1", "invalid state");
+ chMsgReleaseS(tp, msg);
chSysUnlock();
}
diff --git a/os/kernel/src/chmtx.c b/os/kernel/src/chmtx.c
index b84c4d57e..826ef27fe 100644
--- a/os/kernel/src/chmtx.c
+++ b/os/kernel/src/chmtx.c
@@ -141,7 +141,7 @@ void chMtxLockS(Mutex *mp) {
case THD_STATE_WTSEM:
#endif
#if CH_USE_MESSAGES_PRIORITY
- case THD_STATE_SNDMSG:
+ case THD_STATE_SNDMSGQ:
#endif
/* Re-enqueues tp with its new priority on the queue.*/
prio_insert(dequeue(tp), (ThreadsQueue *)tp->p_u.wtobjp);