From b1db8a9f7fe3bc2cd48c52e7c2c50e0e118f8889 Mon Sep 17 00:00:00 2001
From: gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>
Date: Mon, 7 Jan 2008 14:06:46 +0000
Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@166
 35acf78f-673a-0410-8e92-d51de3d6d3f4

---
 demos/ARM7-LPC214x-GCC-minimal/chconf.h |  5 +++++
 demos/ARM7-LPC214x-GCC/chconf.h         |  5 +++++
 demos/AVR-AT90CANx-GCC/chconf.h         |  5 +++++
 demos/Win32-MSVS/chconf.h               |  5 +++++
 demos/Win32-MinGW/chconf.h              |  5 +++++
 docs/Doxyfile                           |  3 ++-
 docs/ch.txt                             |  5 ++++-
 docs/index.html                         |  2 +-
 readme.txt                              | 11 +++++++++++
 src/chmsg.c                             | 21 +++++++++++++++++++++
 src/include/threads.h                   |  2 ++
 src/templates/chconf.h                  |  5 +++++
 12 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/demos/ARM7-LPC214x-GCC-minimal/chconf.h b/demos/ARM7-LPC214x-GCC-minimal/chconf.h
index 11081acc4..7db7c3618 100644
--- a/demos/ARM7-LPC214x-GCC-minimal/chconf.h
+++ b/demos/ARM7-LPC214x-GCC-minimal/chconf.h
@@ -107,6 +107,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 //#define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+//#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
diff --git a/demos/ARM7-LPC214x-GCC/chconf.h b/demos/ARM7-LPC214x-GCC/chconf.h
index 28db4b351..4889fb590 100644
--- a/demos/ARM7-LPC214x-GCC/chconf.h
+++ b/demos/ARM7-LPC214x-GCC/chconf.h
@@ -107,6 +107,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 #define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
diff --git a/demos/AVR-AT90CANx-GCC/chconf.h b/demos/AVR-AT90CANx-GCC/chconf.h
index b4ddeeb05..d3f4491b2 100644
--- a/demos/AVR-AT90CANx-GCC/chconf.h
+++ b/demos/AVR-AT90CANx-GCC/chconf.h
@@ -108,6 +108,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 #define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
diff --git a/demos/Win32-MSVS/chconf.h b/demos/Win32-MSVS/chconf.h
index 037e3d8a0..e4710e575 100644
--- a/demos/Win32-MSVS/chconf.h
+++ b/demos/Win32-MSVS/chconf.h
@@ -112,6 +112,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 #define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
diff --git a/demos/Win32-MinGW/chconf.h b/demos/Win32-MinGW/chconf.h
index 3e741bb8c..115db4325 100644
--- a/demos/Win32-MinGW/chconf.h
+++ b/demos/Win32-MinGW/chconf.h
@@ -112,6 +112,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 #define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
diff --git a/docs/Doxyfile b/docs/Doxyfile
index 24ab33ea9..1672eaa7c 100644
--- a/docs/Doxyfile
+++ b/docs/Doxyfile
@@ -4,7 +4,7 @@
 # Project related configuration options
 #---------------------------------------------------------------------------
 PROJECT_NAME           = ChibiOS/RT
-PROJECT_NUMBER         = "0.5.0 beta"
+PROJECT_NUMBER         = "0.5.1 beta"
 OUTPUT_DIRECTORY       = .
 CREATE_SUBDIRS         = NO
 OUTPUT_LANGUAGE        = English
@@ -243,6 +243,7 @@ PREDEFINED             = __JUST_STUBS__ \
                          CH_USE_MESSAGES \
                          CH_USE_MESSAGES_TIMEOUT \
                          CH_USE_MESSAGES_EVENT \
+                         CH_USE_MESSAGES_PRIORITY \
                          CH_USE_SEMSW \
                          CH_USE_DEBUG \
                          CH_USE_TRACE
diff --git a/docs/ch.txt b/docs/ch.txt
index 48995a155..257d3d89e 100644
--- a/docs/ch.txt
+++ b/docs/ch.txt
@@ -305,7 +305,10 @@
  * be carryed in both directions. Data is not copyed between the client and
  * server threads but just a pointer passed so the exchange is very time
  * efficient.<br>
- * Messages are always processed in FIFO order.<br>
+ * Messages are usually processed in FIFO order but it is possible to process
+ * them in priority order by specifying \p P_MSGBYPRIO when creating a server
+ * thread, \p CH_USE_MESSAGES_PRIORITY must also be specified in \p chconf.h
+ * in order to enable the feature.<br>
  * Threads do not need to allocate space for message queues, the mechanism
  * just requires two extra pointers in the \p Thread structure (the message
  * queue header).<br>
diff --git a/docs/index.html b/docs/index.html
index a4d9e32c2..ae0284cae 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -13,7 +13,7 @@ Homepage</h2>
     </tr>
     <tr>
       <td style="text-align: center; vertical-align: top; width: 150px;">Current
-Version 0.5.0<br>
+Version 0.5.1<br>
 -<br>
       <a href="http://sourceforge.net/projects/chibios/" rel="me" target="_top">Project on SourceForge</a><br>
       <a href="html/index.html" target="_top" rel="me">Documentation</a><br>
diff --git a/readme.txt b/readme.txt
index b00db6498..47e4c16e7 100644
--- a/readme.txt
+++ b/readme.txt
@@ -40,6 +40,17 @@ AVR-AT90CANx-GCC       - Port on AVR AT90CAN128, not complete yet.
 *****************************************************************************
 
 *** 0.5.1 ***
+- NEW: Priority enqueing for messages can be optionally enabled by specifying
+  the P_MSGBYPRIO option when creating a server thread.
+  This change allows the implementation of a priority ceiling protocol into
+  message servers threads. Threads serving messages by priority and threads
+  serving messages in FIFO orded can exist at the same time in the system.
+  This feature can be enabled or disabled by removing the option
+  CH_USE_MESSAGES_PRIORITY into the chconf.h file.
+  Note: This option brings a small overhead when sending a message regardless
+  if in FIFO or priority order, if you dont need priority ordering for your
+  messages it is better disable the feature in chconf.h. It also saves some
+  space.
 - Added to the ARM demos load scripts the capability to load code in RAM
   instead flash, the function must be marked as:
     __attribute__((section(".ramtext")))
diff --git a/src/chmsg.c b/src/chmsg.c
index 61546d7c1..20a59904e 100644
--- a/src/chmsg.c
+++ b/src/chmsg.c
@@ -36,7 +36,14 @@ t_msg chMsgSend(Thread *tp, t_msg msg) {
 
   chSysLock();
 
+#ifdef CH_USE_MESSAGES_PRIORITY
+  if (tp->p_flags & P_MSGBYPRIO)
+    prio_insert(currp, &tp->p_msgqueue);
+  else
+    fifo_insert(currp, &tp->p_msgqueue);
+#else
   fifo_insert(currp, &tp->p_msgqueue);
+#endif
   currp->p_msg = msg;
   if (tp->p_state == PRWTMSG)
     chSchReadyI(tp, RDY_OK);
@@ -67,7 +74,14 @@ t_msg chMsgSendWithEvent(Thread *tp, t_msg msg, EventSource *esp) {
   chSysLock();
 
   chDbgAssert(tp->p_state != PRWTMSG, "chmsg.c, chMsgSendWithEvent()");
+#ifdef CH_USE_MESSAGES_PRIORITY
+  if (tp->p_flags & P_MSGBYPRIO)
+    prio_insert(currp, &tp->p_msgqueue);
+  else
+    fifo_insert(currp, &tp->p_msgqueue);
+#else
   fifo_insert(currp, &tp->p_msgqueue);
+#endif
   chEvtSendI(esp);
   currp->p_msg = msg;
   chSchGoSleepS(PRSNDMSG);
@@ -107,7 +121,14 @@ t_msg chMsgSendTimeout(Thread *tp, t_msg msg, t_time time) {
   chSysLock();
 
   chVTSetI(&vt, time, wakeup, currp);
+#ifdef CH_USE_MESSAGES_PRIORITY
+  if (tp->p_flags & P_MSGBYPRIO)
+    prio_insert(currp, &tp->p_msgqueue);
+  else
+    fifo_insert(currp, &tp->p_msgqueue);
+#else
   fifo_insert(currp, &tp->p_msgqueue);
+#endif
   if (tp->p_state == PRWTMSG)
     chSchReadyI(tp, RDY_OK);
   currp->p_msg = msg;
diff --git a/src/include/threads.h b/src/include/threads.h
index 50468e063..f451278cf 100644
--- a/src/include/threads.h
+++ b/src/include/threads.h
@@ -135,6 +135,8 @@ struct Thread {
 #define P_TERMINATE 1
 /** Thread option: Create suspended thread.*/
 #define P_SUSPENDED 2
+/** Thread option: Serve messages by priority instead of FIFO order.*/
+#define P_MSGBYPRIO 4
 
 /** Pseudo priority used by the ready list header, do not use.*/
 #define NOPRIO      0
diff --git a/src/templates/chconf.h b/src/templates/chconf.h
index fb8ae6a69..d8e5f88da 100644
--- a/src/templates/chconf.h
+++ b/src/templates/chconf.h
@@ -108,6 +108,11 @@
  * @note requires \p CH_USE_VIRTUAL_TIMERS.*/
 #define CH_USE_MESSAGES_EVENT
 
+/** Configuration option: If enabled then the threads have an option to serve
+ *  messages by priority instead of FIFO order.
+ * @note requires \p CH_USE_MESSAGES.*/
+#define CH_USE_MESSAGES_PRIORITY
+
 /** Configuration option: if specified then the
  *  \p chThdGetExitEventSource() function is included in the kernel.
  * @note requires \p CH_USE_MESSAGES.
-- 
cgit v1.2.3