diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chcond.c | 132 | ||||
-rw-r--r-- | src/chlists.c | 23 | ||||
-rw-r--r-- | src/chmtx.c | 10 | ||||
-rw-r--r-- | src/chschd.c | 9 | ||||
-rw-r--r-- | src/include/ch.h | 1 | ||||
-rw-r--r-- | src/include/condvars.h | 56 | ||||
-rw-r--r-- | src/include/lists.h | 5 | ||||
-rw-r--r-- | src/include/threads.h | 48 | ||||
-rw-r--r-- | src/kernel.mk | 2 |
9 files changed, 245 insertions, 41 deletions
diff --git a/src/chcond.c b/src/chcond.c new file mode 100644 index 000000000..adc1fc763 --- /dev/null +++ b/src/chcond.c @@ -0,0 +1,132 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT and Copyright (C) 2008 Leon Woestenberg.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @addtogroup Condition Variables
+ * @{
+ */
+
+#include <ch.h>
+
+#ifdef CH_USE_CONDVARS
+
+/**
+ * Initializes s \p CondVar structure.
+ * @param cp pointer to a \p CondVar structure
+ */
+void chCondInit(CondVar *cp) {
+
+ fifo_init(&cp->c_queue);
+}
+
+/**
+ * Signals one thread that is waiting on the condition variable.
+ *
+ * @param mp pointer to the \p CondVar structure
+ */
+void chCondSignal(CondVar *cp) {
+
+ chSysLock();
+ chCondSignalS(cp);
+ chSysUnlock();
+}
+
+/**
+ * Signals one thread that is waiting on the condition variable.
+ *
+ * @param cp pointer to the \p CondVar structure
+ * @note This function must be called within a \p chSysLock() / \p chSysUnlock()
+ * block.
+ */
+void chCondSignalS(CondVar *cp) {
+
+ if (notempty(&cp->c_queue))
+ /* wake up the first thread, if any */
+ chSchWakeupS(fifo_remove(&cp->c_queue), RDY_OK);
+}
+
+/**
+ * Signal all threads that are waiting on the condition variable.
+ *
+ * @param mp pointer to the \p CondVar structure
+ */
+void chCondBroadcast(CondVar *cp) {
+
+ chSysLock();
+
+ chCondBroadcastS(cp);
+ chSchRescheduleS();
+
+ chSysUnlock();
+}
+
+/**
+ * Signal all threads that are waiting on the condition variable.
+ *
+ * @param cp pointer to the \p CondVar structure
+ * @note This function must be called within a \p chSysLock() / \p chSysUnlock()
+ */
+void chCondBroadcastS(CondVar *cp) {
+
+ /* empties the condition variable queue and inserts all the Threads into the
+ * ready list in FIFO order. The wakeup message is set to \p RDY_RESET in
+ * order to make a chCondBroadcast() detectable from a chCondSignal(). */
+ while (cp->c_queue.p_next != (void *)&cp->c_queue)
+ chSchReadyI(fifo_remove(&cp->c_queue))->p_rdymsg = RDY_RESET;
+}
+
+/**
+ * Wait on the condition variable protected by the given mutex.
+ *
+ * Release the mutex, wait on the condition variable, and lock the mutex. This
+ * is done atomically.
+ *
+ * @param cp pointer to the \p CondVar structure
+ * @note This function must be called within a \p chSysLock() / \p chSysUnlock()
+ */
+msg_t chCondWait(CondVar *cp, Mutex *mp) {
+ msg_t msg;
+
+ chSysLock();
+
+ msg = chCondWaitS(cp, mp);
+
+ chSysUnlock();
+ return msg;
+}
+
+msg_t chCondWaitS(CondVar *cp, Mutex *mp)
+{
+ /* lock the mutex that protects access to the condition variable */
+ chMtxLockS(mp);
+ /* wait on the condition variable */
+ prio_insert(currp, &cp->c_queue);
+ /* Thread remembers the condition variable it waits on */
+ currp->p_wtcondp = cp;
+ /* sleep until signaled */
+ chSchGoSleepS(PRWTCOND);
+ /* lock the mutex that protects access to the condition variable */
+ chMtxLockS(mp);
+ currp->p_wtcondp = NULL;
+ return currp->p_rdymsg;
+}
+
+#endif /* CH_USE_CONDVARS */
+
+/** @} */
diff --git a/src/chlists.c b/src/chlists.c index 37db948b2..3b173c94d 100644 --- a/src/chlists.c +++ b/src/chlists.c @@ -26,6 +26,7 @@ #ifndef CH_OPTIMIZE_SPEED
/*
* Inserts a thread into a priority ordered queue.
+ *
* @param tp the pointer to the thread to be inserted in the list
* @param tqp the pointer to the threads list header
* @note the insertion is done by scanning the list from the highest priority
@@ -33,16 +34,21 @@ */
void prio_insert(Thread *tp, ThreadsQueue *tqp) {
+ /* cp iterates over the queue */
Thread *cp = tqp->p_next;
+ /* not end of queue? and cp has equal or higher priority than tp? */
while ((cp != (Thread *)tqp) && (cp->p_prio >= tp->p_prio))
+ /* iterate to next thread in queue */
cp = cp->p_next;
- /* Insertion on p_prev.*/
+ /* insert before cp, point tp to next and prev in queue */
tp->p_prev = (tp->p_next = cp)->p_prev;
+ /* make prev point to tp, and cp point back to tp */
tp->p_prev->p_next = cp->p_prev = tp;
}
/*
- * Inserts a thread into a FIFO queue.
+ * Inserts a Thread into a FIFO queue.
+ *
* @param tp the pointer to the thread to be inserted in the list
* @param tqp the pointer to the threads list header
*/
@@ -53,8 +59,10 @@ void fifo_insert(Thread *tp, ThreadsQueue *tqp) { }
/*
- * Removes a thread from a FIFO queue.
+ * Removes the first-out Thread from a FIFO queue and returns it.
+ *
* @param tqp the pointer to the threads list header
+ * @return the removed thread pointer
*/
Thread *fifo_remove(ThreadsQueue *tqp) {
Thread *tp = tqp->p_next;
@@ -65,8 +73,8 @@ Thread *fifo_remove(ThreadsQueue *tqp) { /*
* Removes a Thread from a FIFO list and returns it.
+ *
* @param tp the pointer to the thread to be removed from the list
- * @param tqp the pointer to the list header
* @return the removed thread pointer
*/
Thread *dequeue(Thread *tp) {
@@ -77,7 +85,8 @@ Thread *dequeue(Thread *tp) { }
/*
- * Inserts a Thread into a stack list.
+ * Pushes a Thread on top of a stack list.
+ *
* @param tp the pointer to the thread to be inserted in the list
* @param tlp the pointer to the threads list header
*/
@@ -87,9 +96,9 @@ void list_insert(Thread *tp, ThreadsList *tlp) { tlp->p_next = tp;
}
-
/*
- * Removes a Thread from a stack list and returns it.
+ * Pops a Thread from the top of a stack list and returns it.
+ *
* @param tlp the pointer to the threads list header
* @return the removed thread pointer
*/
diff --git a/src/chmtx.c b/src/chmtx.c index 2e95afca6..68e8d671b 100644 --- a/src/chmtx.c +++ b/src/chmtx.c @@ -95,10 +95,9 @@ void chMtxLockS(Mutex *mp) { } break; } - /* - * Goes to sleep on the mutex. - */ + /* sleep on the mutex */ prio_insert(currp, &mp->m_queue); + /* thread remembers the mutex where it is waiting on */ currp->p_wtmtxp = mp; chSchGoSleepS(PRWTMTX); chDbgAssert(mp->m_owner == NULL, "chmtx.c, chMtxLockS()"); @@ -159,11 +158,10 @@ void chMtxUnlock(void) { chDbgAssert((currp->p_mtxlist != NULL) && (currp->p_mtxlist->m_owner == currp), "chmtx.c, chMtxUnlock()"); - /* - * Removes the top Mutex from the owned mutexes list and marks it as not owned. - */ + /* remove the top Mutex from the Threads's owned mutexes list */ mp = currp->p_mtxlist; currp->p_mtxlist = mp->m_next; + /* mark the Mutex as not owned */ mp->m_owner = NULL; /* * If a thread is waiting on the mutex then the hard part begins. diff --git a/src/chschd.c b/src/chschd.c index c3e0fbaed..c97eea1e8 100644 --- a/src/chschd.c +++ b/src/chschd.c @@ -146,20 +146,21 @@ msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time) { */ void chSchWakeupS(Thread *ntp, msg_t msg) { ntp->p_rdymsg = msg; - /* the woken thread has equal or lower priority than the running thread? */ if (ntp->p_prio <= currp->p_prio) - /* put the woken thread on the ready queue */ + /* the woken thread has equal or lower priority than the running thread */ chSchReadyI(ntp); - /* the woken thread has higher priority than the running thread */ else { - /* put the running thread on the ready queue */ + /* the woken thread has higher priority than the running thread and thus + * preempts the currently running thread. */ Thread *otp = currp; chSchReadyI(otp); + /* change the to-be-run thread to running state */ (currp = ntp)->p_state = PRCURR; #ifdef CH_USE_ROUNDROBIN rlist.r_preempt = CH_TIME_QUANTUM; #endif #ifdef CH_USE_TRACE + /* trace the context switch */ chDbgTrace(otp, ntp); #endif /* switch the thread context */ diff --git a/src/include/ch.h b/src/include/ch.h index cafcf2f75..4cf9c157f 100644 --- a/src/include/ch.h +++ b/src/include/ch.h @@ -35,6 +35,7 @@ #include "scheduler.h"
#include "semaphores.h"
#include "mutexes.h"
+#include "condvars.h"
#include "events.h"
#include "messages.h"
#include "threads.h"
diff --git a/src/include/condvars.h b/src/include/condvars.h new file mode 100644 index 000000000..ec6c4589f --- /dev/null +++ b/src/include/condvars.h @@ -0,0 +1,56 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT and Copyright (C) 2008 Leon Woestenberg.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @addtogroup Conditional Variables
+ * @{
+ */
+
+#ifndef _CONDVARS_H_
+#define _CONDVARS_H_
+
+#ifdef CH_USE_CONDVARS
+
+typedef struct CondVar CondVar;
+
+struct CondVar {
+ /** Queue of the threads sleeping on this CondVar. */
+ ThreadsQueue c_queue;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void chCondInit(CondVar *cp);
+ void chCondSignal(CondVar *cp);
+ void chCondSignalS(CondVar *cp);
+ void chCondBroadcast(CondVar *cp);
+ void chCondBroadcastS(CondVar *cp);
+ msg_t chCondWait(CondVar *cp, Mutex *mp);
+ msg_t chCondWaitS(CondVar *cp, Mutex *mp);
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* CH_USE_CONDVARS */
+
+#endif /* _CONDVARS_H_ */
+
+/** @} */
diff --git a/src/include/lists.h b/src/include/lists.h index 08f574393..d98892600 100644 --- a/src/include/lists.h +++ b/src/include/lists.h @@ -35,9 +35,9 @@ typedef struct Thread Thread; * Generic threads queue header and element.
*/
typedef struct {
- /** First \p Thread in the queue.*/
+ /** First \p Thread in the queue, or \p ThreadQueue when empty. */
Thread *p_next;
- /** Last \p Thread in the queue.*/
+ /** Last \p Thread in the queue, or \p ThreadQueue when empty. */
Thread *p_prev;
} ThreadsQueue;
@@ -45,6 +45,7 @@ typedef struct { * Generic threads single link list, it works like a stack.
*/
typedef struct {
+ /** Last pushed \p Thread on the stack list, or \p ThreadList when empty. */
Thread *p_next;
} ThreadsList;
diff --git a/src/include/threads.h b/src/include/threads.h index b7b1a3e29..76e8d480e 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -67,6 +67,10 @@ struct Thread { /** Mutex where the thread is waiting on (only in \p PRWTMTX state). */ Mutex *p_wtmtxp; #endif +#ifdef CH_USE_CONDVARS + /** CondVar where the thread is waiting on (only in \p PRWTCOND state). */ + CondVar *p_wtcondp; +#endif #ifdef CH_USE_MESSAGES /** Destination thread for message send (only in \p PRSNDMSG state). */ Thread *p_wtthdp; @@ -85,11 +89,11 @@ struct Thread { * Start of the optional fields. */ #ifdef CH_USE_WAITEXIT - /** The list of the threads waiting for this thread termination.*/ + /** The list of the threads waiting for this thread termination. */ ThreadsList p_waiting; #endif #ifdef CH_USE_EXIT_EVENT - /** The thread termination \p EventSource.*/ + /** The thread termination \p EventSource. */ EventSource p_exitesource; #endif #ifdef CH_USE_MESSAGES @@ -97,39 +101,41 @@ struct Thread { msg_t p_msg; #endif #ifdef CH_USE_EVENTS - /** Pending events mask.*/ + /** Pending events mask. */ eventmask_t p_epending; #endif #ifdef CH_USE_MUTEXES - /** List of mutexes owned by this thread, \p NULL terminated.*/ + /** List of mutexes owned by this thread, \p NULL terminated. */ Mutex *p_mtxlist; - /** Thread's own, non-inherited, priority */ + /** Thread's own, non-inherited, priority. */ tprio_t p_realprio; #endif }; -/** Thread state: Thread in the ready list.*/ +/** Thread state: Ready to run, waiting on the ready list.*/ #define PRREADY 0 -/** Thread state: Current.*/ +/** Thread state: Currently running. */ #define PRCURR 1 -/** Thread state: Thread created in suspended state.*/ +/** Thread state: Thread created in suspended state. */ #define PRSUSPENDED 2 -/** Thread state: Waiting on a semaphore.*/ +/** Thread state: Waiting on a semaphore. */ #define PRWTSEM 3 -/** Thread state: Waiting on a mutex.*/ +/** Thread state: Waiting on a mutex. */ #define PRWTMTX 4 -/** Thread state: Waiting in \p chThdSleep() or \p chThdSleepUntil().*/ -#define PRSLEEP 5 -/** Thread state: Waiting in \p chThdWait().*/ -#define PRWAIT 6 -/** Thread state: Waiting in \p chEvtWait().*/ -#define PRWTEVENT 7 -/** Thread state: Waiting in \p chMsgSend().*/ -#define PRSNDMSG 8 -/** Thread state: Waiting in \p chMsgWait().*/ -#define PRWTMSG 9 +/** Thread state: Waiting on a condition variable. */ +#define PRWTCOND 5 +/** Thread state: Waiting in \p chThdSleep() or \p chThdSleepUntil(). */ +#define PRSLEEP 6 +/** Thread state: Waiting in \p chThdWait(). */ +#define PRWAIT 7 +/** Thread state: Waiting in \p chEvtWait(). */ +#define PRWTEVENT 8 +/** Thread state: Waiting in \p chMsgSend(). */ +#define PRSNDMSG 9 +/** Thread state: Waiting in \p chMsgWait(). */ +#define PRWTMSG 10 /** Thread state: After termination.*/ -#define PREXIT 10 +#define PREXIT 11 #ifdef CH_USE_TERMINATE /** Thread option: Termination requested flag.*/ diff --git a/src/kernel.mk b/src/kernel.mk index 54a9cffdd..d613e866f 100644 --- a/src/kernel.mk +++ b/src/kernel.mk @@ -6,4 +6,4 @@ KERNSRC = ../../src/chinit.c ../../src/chdebug.c \ ../../src/chsem.c ../../src/chmtx.c \
../../src/chevents.c ../../src/chmsg.c \
../../src/chsleep.c ../../src/chqueues.c \
- ../../src/chserial.c
+ ../../src/chcond.c ../../src/chserial.c
|