2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
This file is part of ChibiOS.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS is free software; you can redistribute it and/or modify
|
2009-08-16 13:07:24 +00:00
|
|
|
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.
|
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS is distributed in the hope that it will be useful,
|
2009-08-16 13:07:24 +00:00
|
|
|
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-02-27 08:54:22 +00:00
|
|
|
* @file chsem.h
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Semaphores macros and structures.
|
|
|
|
*
|
2009-08-30 06:59:43 +00:00
|
|
|
* @addtogroup semaphores
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHSEM_H_
|
|
|
|
#define _CHSEM_H_
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2015-03-05 21:28:51 +00:00
|
|
|
#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 13:17:42 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Semaphore structure.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2015-03-06 15:33:35 +00:00
|
|
|
typedef struct ch_semaphore {
|
2016-02-16 10:07:00 +00:00
|
|
|
threads_queue_t queue; /**< @brief Queue of the threads sleeping
|
2010-02-06 12:31:09 +00:00
|
|
|
on this semaphore. */
|
2016-02-16 10:07:00 +00:00
|
|
|
cnt_t cnt; /**< @brief The semaphore counter. */
|
2013-07-19 13:17:42 +00:00
|
|
|
} semaphore_t;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 13:17:42 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Data part of a static semaphore initializer.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details This macro should be used when statically initializing a semaphore
|
2010-02-06 12:31:09 +00:00
|
|
|
* that is part of a bigger structure.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] name the name of the semaphore variable
|
|
|
|
* @param[in] n the counter initial value, this value must be
|
|
|
|
* non-negative
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2016-02-16 10:07:00 +00:00
|
|
|
#define _SEMAPHORE_DATA(name, n) {_THREADS_QUEUE_DATA(name.queue), n}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Static semaphore initializer.
|
|
|
|
* @details Statically initialized semaphores require no explicit
|
|
|
|
* initialization using @p chSemInit().
|
|
|
|
*
|
|
|
|
* @param[in] name the name of the semaphore variable
|
|
|
|
* @param[in] n the counter initial value, this value must be
|
2010-02-21 07:24:53 +00:00
|
|
|
* non-negative
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 13:17:42 +00:00
|
|
|
#define SEMAPHORE_DECL(name, n) semaphore_t name = _SEMAPHORE_DATA(name, n)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-07-20 07:24:12 +00:00
|
|
|
void chSemObjectInit(semaphore_t *sp, cnt_t n);
|
2013-07-19 13:17:42 +00:00
|
|
|
void chSemReset(semaphore_t *sp, cnt_t n);
|
|
|
|
void chSemResetI(semaphore_t *sp, cnt_t n);
|
|
|
|
msg_t chSemWait(semaphore_t *sp);
|
|
|
|
msg_t chSemWaitS(semaphore_t *sp);
|
|
|
|
msg_t chSemWaitTimeout(semaphore_t *sp, systime_t time);
|
|
|
|
msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t time);
|
|
|
|
void chSemSignal(semaphore_t *sp);
|
|
|
|
void chSemSignalI(semaphore_t *sp);
|
|
|
|
void chSemAddCounterI(semaphore_t *sp, cnt_t n);
|
|
|
|
msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw);
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module inline functions. */
|
|
|
|
/*===========================================================================*/
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Decreases the semaphore counter.
|
|
|
|
* @details This macro can be used when the counter is known to be positive.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
2014-12-11 15:12:21 +00:00
|
|
|
* @param[in] sp pointer to a @p semaphore_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
|
|
|
static inline void chSemFastWaitI(semaphore_t *sp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
sp->cnt--;
|
2013-07-19 13:17:42 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Increases the semaphore counter.
|
2010-09-21 10:22:06 +00:00
|
|
|
* @details This macro can be used when the counter is known to be not
|
|
|
|
* negative.
|
|
|
|
*
|
2014-12-11 15:12:21 +00:00
|
|
|
* @param[in] sp pointer to a @p semaphore_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
|
|
|
static inline void chSemFastSignalI(semaphore_t *sp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
sp->cnt++;
|
2013-07-19 13:17:42 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Returns the semaphore counter current value.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
2014-12-11 15:12:21 +00:00
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @return The semaphore counter value.
|
|
|
|
*
|
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
|
|
|
static inline cnt_t chSemGetCounterI(semaphore_t *sp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return sp->cnt;
|
2013-07-19 13:17:42 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2015-03-05 21:28:51 +00:00
|
|
|
#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHSEM_H_ */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|