diff --git a/demos/ARM7-LPC214x-GCC/chconf.h b/demos/ARM7-LPC214x-GCC/chconf.h index 513c352a6..ddf733411 100644 --- a/demos/ARM7-LPC214x-GCC/chconf.h +++ b/demos/ARM7-LPC214x-GCC/chconf.h @@ -55,6 +55,16 @@ * the kernel.*/ #define CH_USE_MUTEXES +/** Configuration option: if specified then the Conditional Variables APIs are + * included in the kernel. + * @note requires \p CH_USE_MUTEXES.*/ +#define CH_USE_CONDVARS + +/** Configuration option: if specified then the Conditional Variables APIs are + * included in the kernel. + * @note requires \p CH_USE_CONDVARS and \p CH_USE_MUTEXES.*/ +#define CH_USE_CONDVARS_TIMEOUT + /** Configuration option: if specified then the Events APIs are included in * the kernel.*/ #define CH_USE_EVENTS diff --git a/readme.txt b/readme.txt index 92444beab..d76a9e3eb 100644 --- a/readme.txt +++ b/readme.txt @@ -74,6 +74,7 @@ Win32-MinGW - ChibiOS/RT simulator and demo into a WIN32 process, ***************************************************************************** *** 0.8.0 *** +- NEW: Added condvars mechanism on top of the mutexes subsystem. - NEW: Improved events subsystems, now it is also possible to use it just as "event flags" without have to use event handler callbacks. Some new APIs were introduced: @@ -90,6 +91,7 @@ Win32-MinGW - ChibiOS/RT simulator and demo into a WIN32 process, CH_USE_DEBUG is disabled, it produces no code but allows to check the optional code without have to compile twice. - FIX: Fixed a warning generated by the chEvtIsListening() macro. +- Added new test cases to the test suite about the new events APIs. - Added a new benchmark to the test suite (timers set/reset performance). - Renamed the macro fifo_init() to queue_init() because it is used to init both FIFO queues and priority queues. diff --git a/src/chcond.c b/src/chcond.c new file mode 100644 index 000000000..27e746d39 --- /dev/null +++ b/src/chcond.c @@ -0,0 +1,216 @@ +/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + 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 . +*/ +/* + Concepts and parts of this file are contributed by and Copyright (C) 2008 + of Leon Woestenberg + */ + +/** + * @addtogroup CondVars + * @{ + */ + +#include + +#if defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) + +/** + * Initializes s \p CondVar structure. + * @param cp pointer to a \p CondVar structure + */ +void chCondInit(CondVar *cp) { + + queue_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(); + + if (notempty(&cp->c_queue)) /* any thread ? */ + chSchWakeupS(fifo_remove(&cp->c_queue), RDY_OK); + + 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 chCondSignalI(CondVar *cp) { + + if (notempty(&cp->c_queue)) /* any thread ? */ + chSchReadyI(fifo_remove(&cp->c_queue))->p_rdymsg = 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(); + + chCondBroadcastI(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 chCondBroadcastI(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 releasing the mutex lock. + * + * Releases the mutex, waits on the condition variable, and finally acquires + * the mutex again. This is done atomically. + * + * The thread MUST already have locked the mutex when calling chCondWait(). + * + * @param cp pointer to the \p CondVar structure + * @return The wakep mode. + * @retval RDY_OK if the condvar was signaled using chCondSignal(). + * @retval RDY_RESET if the condvar was signaled using chCondBroadcast(). + */ +msg_t chCondWait(CondVar *cp) { + msg_t msg; + + chSysLock(); + + msg = chCondWaitS(cp); + + chSysUnlock(); + return msg; +} + +/** + * Wait on the condition variable releasing the mutex lock. + * + * Releases the mutex, waits on the condition variable, and finally acquires + * the mutex again. This is done atomically. + * + * The thread MUST already have locked the mutex when calling chCondWait(). + * + * @param cp pointer to the \p CondVar structure + * @return The wakep mode. + * @retval RDY_OK if the condvar was signaled using chCondSignal(). + * @retval RDY_RESET if the condvar was signaled using chCondBroadcast(). + * @note This function must be called within a \p chSysLock() / \p chSysUnlock() + */ +msg_t chCondWaitS(CondVar *cp) { + Mutex *mp; + msg_t msg; + + chDbgAssert(currp->p_mtxlist != NULL, "chcond.c, chCondWaitS()"); + + mp = chMtxUnlockS(); /* unlocks the condvar mutex */ + prio_insert(currp, &cp->c_queue); /* enters the condvar queue */ + currp->p_wtcondp = cp; /* needed by the tracer */ + chSchGoSleepS(PRWTCOND); /* waits on the condvar */ + msg = currp->p_rdymsg; /* fetches the wakeup message */ + chMtxLockS(mp); /* atomically relocks the mutex */ + return msg; /* returns the wakeup message */ +} + +#ifdef CH_USE_CONDVARS_TIMEOUT +/** + * Wait on the condition variable releasing the mutex lock. + * + * Releases the mutex, waits on the condition variable, and finally acquires + * the mutex again. This is done atomically. + * + * The thread MUST already have locked the mutex when calling chCondWait(). + * + * @param cp pointer to the \p CondVar structure + * @param time the number of ticks before the operation fails + * @return The wakep mode. + * @retval RDY_OK if the condvar was signaled using chCondSignal(). + * @retval RDY_RESET if the condvar was signaled using chCondBroadcast(). + * @retval RDY_TIMEOUT if the condvar was not signaled within the specified + * timeout. + */ +msg_t chCondWaitTimeout(CondVar *cp, systime_t time) { + msg_t msg; + + chSysLock(); + + msg = chCondWaitTimeoutS(cp, time); + + chSysUnlock(); + return msg; +} + +/** + * Wait on the condition variable releasing the mutex lock. + * + * Releases the mutex, waits on the condition variable, and finally acquires + * the mutex again. This is done atomically. + * + * The thread MUST already have locked the mutex when calling chCondWait(). + * + * @param cp pointer to the \p CondVar structure + * @param time the number of ticks before the operation fails + * @return The wakep mode. + * @retval RDY_OK if the condvar was signaled using chCondSignal(). + * @retval RDY_RESET if the condvar was signaled using chCondBroadcast(). + * @retval RDY_TIMEOUT if the condvar was not signaled within the specified + * timeout. + * @note This function must be called within a \p chSysLock() / \p chSysUnlock() + */ +msg_t chCondWaitTimeoutS(CondVar *cp, systime_t time) { + Mutex *mp; + msg_t msg; + + chDbgAssert(currp->p_mtxlist != NULL, "chcond.c, chCondWaitS()"); + + mp = chMtxUnlockS(); /* unlocks the condvar mutex */ + prio_insert(currp, &cp->c_queue); /* enters the condvar queue */ + currp->p_wtcondp = cp; /* needed by the tracer */ + chSchGoSleepTimeoutS(PRWTCOND, time); /* waits on the condvar */ + msg = currp->p_rdymsg; /* fetches the wakeup message */ + chMtxLockS(mp); /* atomically relocks the mutex */ + return msg; /* returns the wakeup message */ +} +#endif /* CH_USE_CONDVARS_TIMEOUT */ + +#endif /* defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) */ + +/** @} */ diff --git a/src/include/ch.h b/src/include/ch.h index 28a3b1bd8..aa6c04cc0 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 "heap.h" diff --git a/src/include/condvars.h b/src/include/condvars.h new file mode 100644 index 000000000..53bbe4a83 --- /dev/null +++ b/src/include/condvars.h @@ -0,0 +1,63 @@ +/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + 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 . +*/ +/* + Concepts and parts of this file are contributed by and Copyright (C) 2008 + of Leon Woestenberg + */ + +/** + * @addtogroup Conditional Variables + * @{ + */ + +#ifndef _CONDVARS_H_ +#define _CONDVARS_H_ + +#if defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) + +/** + * CondVar structure. + */ +typedef struct { + ThreadsQueue c_queue; +} CondVar; + +#ifdef __cplusplus +extern "C" { +#endif + void chCondInit(CondVar *cp); + void chCondSignal(CondVar *cp); + void chCondSignalI(CondVar *cp); + void chCondBroadcast(CondVar *cp); + void chCondBroadcastI(CondVar *cp); + msg_t chCondWait(CondVar *cp); + msg_t chCondWaitS(CondVar *cp); +#ifdef CH_USE_CONDVARS_TIMEOUT + msg_t chCondWaitTimeout(CondVar *cp, systime_t time); + msg_t chCondWaitTimeoutS(CondVar *cp, systime_t time); +#endif +#ifdef __cplusplus +} +#endif + +#endif /* defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) */ + +#endif /* _CONDVARS_H_ */ + +/** @} */ diff --git a/src/include/threads.h b/src/include/threads.h index a0b0b362b..d5f82ea9f 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -56,29 +56,34 @@ struct Thread { * thread in the system. */ union { - /** Thread wakeup code (only valid when exiting the \p PRREADY state). */ + /** Thread wakeup code (only valid when exiting the \p PRREADY state).*/ msg_t p_rdymsg; /** The thread exit code (only while in \p PREXIT state).*/ msg_t p_exitcode; #ifdef CH_USE_SEMAPHORES - /** Semaphore where the thread is waiting on (only in \p PRWTSEM state). */ + /** Semaphore where the thread is waiting on (only in \p PRWTSEM state).*/ Semaphore *p_wtsemp; #endif #ifdef CH_USE_MUTEXES - /** Mutex where the thread is waiting on (only in \p PRWTMTX state). */ + /** 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). */ + /** Destination thread for message send (only in \p PRSNDMSG state).*/ Thread *p_wtthdp; #endif #ifdef CH_USE_EVENTS - /** Enabled events mask (only while in \p PRWTEVENT state). */ + /** Enabled events mask (only while in \p PRWTOREVT or \p PRWTANDEVT + states). */ eventmask_t p_ewmask; #endif #ifdef CH_USE_TRACE /** Kernel object where the thread is waiting on. It is only valid when - the thread is some sleeping states. */ + the thread is some sleeping states.*/ void *p_wtobjp; #endif }; diff --git a/src/kernel.mk b/src/kernel.mk index 7f6d93fdd..75ab83d93 100644 --- a/src/kernel.mk +++ b/src/kernel.mk @@ -4,6 +4,7 @@ KERNSRC = ../../src/chinit.c ../../src/chdebug.c \ ../../src/chlists.c ../../src/chvt.c \ ../../src/chschd.c ../../src/chthreads.c \ ../../src/chsem.c ../../src/chmtx.c \ - ../../src/chevents.c ../../src/chmsg.c \ - ../../src/chqueues.c ../../src/chserial.c \ - ../../src/chheap.c ../../src/chmempools.c + ../../src/chcond.c ../../src/chevents.c \ + ../../src/chmsg.c ../../src/chqueues.c \ + ../../src/chheap.c ../../src/chmempools.c \ + ../../src/chserial.c diff --git a/src/templates/chconf.h b/src/templates/chconf.h index 9e34e381f..d59801291 100644 --- a/src/templates/chconf.h +++ b/src/templates/chconf.h @@ -60,6 +60,16 @@ * the kernel.*/ #define CH_USE_MUTEXES +/** Configuration option: if specified then the Conditional Variables APIs are + * included in the kernel. + * @note requires \p CH_USE_MUTEXES.*/ +#define CH_USE_CONDVARS + +/** Configuration option: if specified then the Conditional Variables APIs are + * included in the kernel. + * @note requires \p CH_USE_CONDVARS and \p CH_USE_MUTEXES.*/ +#define CH_USE_CONDVARS_TIMEOUT + /** Configuration option: if specified then the Events APIs are included in * the kernel.*/ #define CH_USE_EVENTS