2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2014-07-26 09:06:28 +00:00
|
|
|
2011,2012,2013,2014 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
2010-06-24 14:19:52 +00:00
|
|
|
Concepts and parts of this file have been contributed by Leon Woestenberg.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @file chcond.c
|
|
|
|
* @brief Condition Variables code.
|
|
|
|
*
|
2014-11-23 17:14:56 +00:00
|
|
|
* @addtogroup condvars
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details This module implements the Condition Variables mechanism. Condition
|
2013-07-19 13:17:42 +00:00
|
|
|
* variables are an extensions to the mutex subsystem and cannot
|
2010-03-16 19:36:21 +00:00
|
|
|
* work alone.
|
|
|
|
* <h2>Operation mode</h2>
|
|
|
|
* The condition variable is a synchronization object meant to be
|
2013-07-19 13:17:42 +00:00
|
|
|
* used inside a zone protected by a mutex. Mutexes and condition
|
|
|
|
* variables together can implement a Monitor construct.
|
2013-07-20 10:12:44 +00:00
|
|
|
* @pre In order to use the condition variable APIs the @p CH_CFG_USE_CONDVARS
|
2010-03-16 19:36:21 +00:00
|
|
|
* option must be enabled in @p chconf.h.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.h"
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_CONDVARS || defined(__DOXYGEN__)
|
2013-07-19 13:17:42 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported functions. */
|
|
|
|
/*===========================================================================*/
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2013-07-19 13:17:42 +00:00
|
|
|
* @brief Initializes s @p condition_variable_t structure.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[out] cp pointer to a @p condition_variable_t structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @init
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-20 07:24:12 +00:00
|
|
|
void chCondObjectInit(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck(cp != NULL);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
queue_init(&cp->c_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals one thread that is waiting on the condition variable.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
void chCondSignal(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck(cp != NULL);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2013-06-16 16:12:33 +00:00
|
|
|
if (queue_notempty(&cp->c_queue))
|
2013-08-17 11:52:50 +00:00
|
|
|
chSchWakeupS(queue_fifo_remove(&cp->c_queue), MSG_OK);
|
2009-08-16 13:07:24 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals one thread that is waiting on the condition variable.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @post This function does not reschedule so a call to a rescheduling
|
|
|
|
* function must be performed before unlocking the kernel. Note that
|
|
|
|
* interrupt handlers always reschedule on exit so an explicit
|
|
|
|
* reschedule must not be performed in ISRs.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
void chCondSignalI(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassI();
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck(cp != NULL);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-08-14 15:25:23 +00:00
|
|
|
if (queue_notempty(&cp->c_queue)) {
|
|
|
|
thread_t *tp = queue_fifo_remove(&cp->c_queue);
|
2013-08-17 11:52:50 +00:00
|
|
|
tp->p_u.rdymsg = MSG_OK;
|
2013-08-14 15:25:23 +00:00
|
|
|
chSchReadyI(tp);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals all threads that are waiting on the condition variable.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
void chCondBroadcast(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
chCondBroadcastI(cp);
|
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals all threads that are waiting on the condition variable.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @post This function does not reschedule so a call to a rescheduling
|
|
|
|
* function must be performed before unlocking the kernel. Note that
|
|
|
|
* interrupt handlers always reschedule on exit so an explicit
|
|
|
|
* reschedule must not be performed in ISRs.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
void chCondBroadcastI(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassI();
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck(cp != NULL);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 12:22:31 +00:00
|
|
|
/* Empties the condition variable queue and inserts all the threads into the
|
2013-08-17 11:52:50 +00:00
|
|
|
ready list in FIFO order. The wakeup message is set to @p MSG_RESET in
|
2010-02-06 10:55:53 +00:00
|
|
|
order to make a chCondBroadcast() detectable from a chCondSignal().*/
|
2009-08-16 13:07:24 +00:00
|
|
|
while (cp->c_queue.p_next != (void *)&cp->c_queue)
|
2013-08-17 11:52:50 +00:00
|
|
|
chSchReadyI(queue_fifo_remove(&cp->c_queue))->p_u.rdymsg = MSG_RESET;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits on the condition variable releasing the mutex lock.
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details Releases the currently owned mutex, waits on the condition
|
|
|
|
* variable, and finally acquires the mutex again. All the sequence
|
|
|
|
* is performed atomically.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @pre The invoking thread <b>must</b> have at least one owned mutex.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-18 06:48:56 +00:00
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the condition variable.
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_OK if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondSignal().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_RESET if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondBroadcast().
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
msg_t chCondWait(condition_variable_t *cp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
msg = chCondWaitS(cp);
|
|
|
|
chSysUnlock();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits on the condition variable releasing the mutex lock.
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details Releases the currently owned mutex, waits on the condition
|
|
|
|
* variable, and finally acquires the mutex again. All the sequence
|
|
|
|
* is performed atomically.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @pre The invoking thread <b>must</b> have at least one owned mutex.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2010-09-18 06:48:56 +00:00
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the condition variable.
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_OK if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondSignal().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_RESET if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondBroadcast().
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
msg_t chCondWaitS(condition_variable_t *cp) {
|
2013-07-19 12:22:31 +00:00
|
|
|
thread_t *ctp = currp;
|
2013-07-19 14:51:35 +00:00
|
|
|
mutex_t *mp;
|
2009-08-16 13:07:24 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassS();
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck(cp != NULL);
|
|
|
|
chDbgAssert(ctp->p_mtxlist != NULL, "not owning a mutex");
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-02-12 13:48:38 +00:00
|
|
|
mp = chMtxGetNextMutexS();
|
2014-02-12 09:43:27 +00:00
|
|
|
chMtxUnlockS(mp);
|
2010-01-21 18:06:33 +00:00
|
|
|
ctp->p_u.wtobjp = cp;
|
2013-06-16 16:30:00 +00:00
|
|
|
queue_prio_insert(ctp, &cp->c_queue);
|
2013-07-24 14:54:26 +00:00
|
|
|
chSchGoSleepS(CH_STATE_WTCOND);
|
2010-01-21 18:06:33 +00:00
|
|
|
msg = ctp->p_u.rdymsg;
|
2010-01-20 14:39:34 +00:00
|
|
|
chMtxLockS(mp);
|
|
|
|
return msg;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_CONDVARS_TIMEOUT || defined(__DOXYGEN__)
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits on the condition variable releasing the mutex lock.
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details Releases the currently owned mutex, waits on the condition
|
|
|
|
* variable, and finally acquires the mutex again. All the sequence
|
|
|
|
* is performed atomically.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @pre The invoking thread <b>must</b> have at least one owned mutex.
|
2013-07-20 10:12:44 +00:00
|
|
|
* @pre The configuration option @p CH_CFG_USE_CONDVARS_TIMEOUT must be enabled
|
2010-09-18 06:48:56 +00:00
|
|
|
* in order to use this function.
|
|
|
|
* @post Exiting the function because a timeout does not re-acquire the
|
2010-03-16 19:36:21 +00:00
|
|
|
* mutex, the mutex ownership is lost.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2011-02-21 19:06:46 +00:00
|
|
|
* @param[in] time the number of ticks before the operation timeouts, the
|
|
|
|
* special values are handled as follow:
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* - @a TIME_IMMEDIATE this value is not allowed.
|
|
|
|
* .
|
2010-09-18 06:48:56 +00:00
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the condition variable.
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_OK if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondSignal().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_RESET if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondBroadcast().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_TIMEOUT if the condition variable has not been signaled within
|
2013-07-19 13:17:42 +00:00
|
|
|
* the specified timeout.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
msg_t chCondWaitTimeout(condition_variable_t *cp, systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
msg = chCondWaitTimeoutS(cp, time);
|
|
|
|
chSysUnlock();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits on the condition variable releasing the mutex lock.
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details Releases the currently owned mutex, waits on the condition
|
|
|
|
* variable, and finally acquires the mutex again. All the sequence
|
|
|
|
* is performed atomically.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @pre The invoking thread <b>must</b> have at least one owned mutex.
|
2013-07-20 10:12:44 +00:00
|
|
|
* @pre The configuration option @p CH_CFG_USE_CONDVARS_TIMEOUT must be enabled
|
2010-09-18 06:48:56 +00:00
|
|
|
* in order to use this function.
|
|
|
|
* @post Exiting the function because a timeout does not re-acquire the
|
2010-03-16 19:36:21 +00:00
|
|
|
* mutex, the mutex ownership is lost.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2013-07-19 13:17:42 +00:00
|
|
|
* @param[in] cp pointer to the @p condition_variable_t structure
|
2011-02-21 19:06:46 +00:00
|
|
|
* @param[in] time the number of ticks before the operation timeouts, the
|
|
|
|
* special values are handled as follow:
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* - @a TIME_IMMEDIATE this value is not allowed.
|
|
|
|
* .
|
2010-09-18 06:48:56 +00:00
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the condition variable.
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_OK if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondSignal().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_RESET if the condition variable has been signaled using
|
2010-09-18 06:48:56 +00:00
|
|
|
* @p chCondBroadcast().
|
2013-08-17 11:52:50 +00:00
|
|
|
* @retval MSG_TIMEOUT if the condition variable has not been signaled within
|
2013-07-19 13:17:42 +00:00
|
|
|
* the specified timeout.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
msg_t chCondWaitTimeoutS(condition_variable_t *cp, systime_t time) {
|
2013-07-19 14:51:35 +00:00
|
|
|
mutex_t *mp;
|
2009-08-16 13:07:24 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassS();
|
2013-08-10 14:50:32 +00:00
|
|
|
chDbgCheck((cp != NULL) && (time != TIME_IMMEDIATE));
|
|
|
|
chDbgAssert(currp->p_mtxlist != NULL, "not owning a mutex");
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-02-12 13:48:38 +00:00
|
|
|
mp = chMtxGetNextMutexS();
|
2014-02-12 09:43:27 +00:00
|
|
|
chMtxUnlockS(mp);
|
2010-02-28 13:32:45 +00:00
|
|
|
currp->p_u.wtobjp = cp;
|
2013-06-16 16:30:00 +00:00
|
|
|
queue_prio_insert(currp, &cp->c_queue);
|
2013-07-24 14:54:26 +00:00
|
|
|
msg = chSchGoSleepTimeoutS(CH_STATE_WTCOND, time);
|
2013-08-17 11:52:50 +00:00
|
|
|
if (msg != MSG_TIMEOUT)
|
2010-03-16 19:36:21 +00:00
|
|
|
chMtxLockS(mp);
|
2010-01-20 14:39:34 +00:00
|
|
|
return msg;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_USE_CONDVARS_TIMEOUT */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_USE_CONDVARS */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|