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,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 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-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
|
|
|
|
2010-09-18 06:48:56 +00:00
|
|
|
#if CH_USE_SEMAPHORES || defined(__DOXYGEN__)
|
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
|
|
|
*/
|
|
|
|
typedef struct Semaphore {
|
2013-07-19 09:43:11 +00:00
|
|
|
threads_queue_t s_queue; /**< @brief Queue of the threads sleeping
|
2010-02-06 12:31:09 +00:00
|
|
|
on this semaphore. */
|
|
|
|
cnt_t s_cnt; /**< @brief The semaphore counter. */
|
2009-08-16 13:07:24 +00:00
|
|
|
} Semaphore;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
void chSemInit(Semaphore *sp, cnt_t n);
|
|
|
|
void chSemReset(Semaphore *sp, cnt_t n);
|
|
|
|
void chSemResetI(Semaphore *sp, cnt_t n);
|
|
|
|
msg_t chSemWait(Semaphore *sp);
|
|
|
|
msg_t chSemWaitS(Semaphore *sp);
|
|
|
|
msg_t chSemWaitTimeout(Semaphore *sp, systime_t time);
|
|
|
|
msg_t chSemWaitTimeoutS(Semaphore *sp, systime_t time);
|
|
|
|
void chSemSignal(Semaphore *sp);
|
|
|
|
void chSemSignalI(Semaphore *sp);
|
2011-04-09 15:10:15 +00:00
|
|
|
void chSemAddCounterI(Semaphore *sp, cnt_t n);
|
2009-08-16 13:07:24 +00:00
|
|
|
#if CH_USE_SEMSW
|
|
|
|
msg_t chSemSignalWait(Semaphore *sps, Semaphore *spw);
|
|
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2013-07-19 09:43:11 +00:00
|
|
|
#define _SEMAPHORE_DATA(name, n) {_threads_queue_t_DATA(name.s_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
|
|
|
*/
|
|
|
|
#define SEMAPHORE_DECL(name, n) Semaphore name = _SEMAPHORE_DATA(name, n)
|
|
|
|
|
2011-08-23 10:09:08 +00:00
|
|
|
/**
|
|
|
|
* @name Macro 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
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
#define chSemFastWaitI(sp) ((sp)->s_cnt--)
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
#define chSemFastSignalI(sp) ((sp)->s_cnt++)
|
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Returns the semaphore counter current value.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
#define chSemGetCounterI(sp) ((sp)->s_cnt)
|
2011-08-23 10:09:08 +00:00
|
|
|
/** @} */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
#endif /* CH_USE_SEMAPHORES */
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHSEM_H_ */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|