ChibiOS 21.11.4
rt/include/chsem.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4
5 This file is part of ChibiOS.
6
7 ChibiOS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation version 3 of the License.
10
11 ChibiOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20/**
21 * @file rt/include/chsem.h
22 * @brief Semaphores macros and structures.
23 *
24 * @addtogroup semaphores
25 * @{
26 */
27
28#ifndef CHSEM_H
29#define CHSEM_H
30
31#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
32
33/*===========================================================================*/
34/* Module constants. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Module pre-compile time settings. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Derived constants and error checks. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Module data structures and types. */
47/*===========================================================================*/
48
49/**
50 * @brief Semaphore structure.
51 */
52typedef struct ch_semaphore {
53 ch_queue_t queue; /**< @brief Queue of the threads sleeping
54 on this semaphore. */
55 cnt_t cnt; /**< @brief The semaphore counter. */
57
58/*===========================================================================*/
59/* Module macros. */
60/*===========================================================================*/
61
62/**
63 * @brief Data part of a static semaphore initializer.
64 * @details This macro should be used when statically initializing a semaphore
65 * that is part of a bigger structure.
66 *
67 * @param[in] name the name of the semaphore variable
68 * @param[in] n the counter initial value, this value must be
69 * non-negative
70 */
71#define __SEMAPHORE_DATA(name, n) {__CH_QUEUE_DATA(name.queue), n}
72
73/**
74 * @brief Static semaphore initializer.
75 * @details Statically initialized semaphores require no explicit
76 * initialization using @p chSemInit().
77 *
78 * @param[in] name the name of the semaphore variable
79 * @param[in] n the counter initial value, this value must be
80 * non-negative
81 */
82#define SEMAPHORE_DECL(name, n) semaphore_t name = __SEMAPHORE_DATA(name, n)
83
84/*===========================================================================*/
85/* External declarations. */
86/*===========================================================================*/
87
88#ifdef __cplusplus
89extern "C" {
90#endif
98 void chSemSignal(semaphore_t *sp);
99 void chSemSignalI(semaphore_t *sp);
102#ifdef __cplusplus
103}
104#endif
105
106/*===========================================================================*/
107/* Module inline functions. */
108/*===========================================================================*/
109
110/**
111 * @brief Performs a reset operation on the semaphore.
112 * @post After invoking this function all the threads waiting on the
113 * semaphore, if any, are released and the semaphore counter is set
114 * to the specified, non negative, value.
115 * @note This function implicitly sends @p MSG_RESET as message.
116 *
117 * @param[in] sp pointer to a @p semaphore_t structure
118 * @param[in] n the new value of the semaphore counter. The value must
119 * be non-negative.
120 *
121 * @api
122 */
123static inline void chSemReset(semaphore_t *sp, cnt_t n) {
124
126}
127
128/**
129 * @brief Performs a reset operation on the semaphore.
130 * @post After invoking this function all the threads waiting on the
131 * semaphore, if any, are released and the semaphore counter is set
132 * to the specified, non negative, value.
133 * @post This function does not reschedule so a call to a rescheduling
134 * function must be performed before unlocking the kernel. Note that
135 * interrupt handlers always reschedule on exit so an explicit
136 * reschedule must not be performed in ISRs.
137 * @note This function implicitly sends @p MSG_RESET as message.
138 *
139 * @param[in] sp pointer to a @p semaphore_t structure
140 * @param[in] n the new value of the semaphore counter. The value must
141 * be non-negative.
142 *
143 * @iclass
144 */
145static inline void chSemResetI(semaphore_t *sp, cnt_t n) {
146
148}
149
150/**
151 * @brief Decreases the semaphore counter.
152 * @details This macro can be used when the counter is known to be positive.
153 *
154 * @param[in] sp pointer to a @p semaphore_t structure
155 *
156 * @iclass
157 */
158static inline void chSemFastWaitI(semaphore_t *sp) {
159
161
162 sp->cnt--;
163}
164
165/**
166 * @brief Increases the semaphore counter.
167 * @details This macro can be used when the counter is known to be not
168 * negative.
169 *
170 * @param[in] sp pointer to a @p semaphore_t structure
171 *
172 * @iclass
173 */
174static inline void chSemFastSignalI(semaphore_t *sp) {
175
177
178 sp->cnt++;
179}
180
181/**
182 * @brief Returns the semaphore counter current value.
183 *
184 * @param[in] sp pointer to a @p semaphore_t structure
185 * @return The semaphore counter value.
186 *
187 * @iclass
188 */
189static inline cnt_t chSemGetCounterI(const semaphore_t *sp) {
190
192
193 return sp->cnt;
194}
195
196#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
197
198#endif /* CHSEM_H */
199
200/** @} */
#define chSemReset(sp, n)
Performs a reset operation on the semaphore.
#define chSemFastWaitI(sp)
Decreases the semaphore counter.
#define chSemWait(sp)
Performs a wait operation on a semaphore.
#define chSemFastSignalI(sp)
Increases the semaphore counter.
#define chSemGetCounterI(sp)
Returns the semaphore counter current value.
#define chSemResetI(sp, n)
Performs a reset operation on the semaphore.
#define chSemWaitS(sp)
Performs a wait operation on a semaphore.
#define chSemObjectInit(sp, n)
Initializes a semaphore with the specified counter value.
#define chDbgCheckClassI()
Definition chdebug.h:99
struct ch_queue ch_queue_t
Type of a generic bidirectional linked list header and element.
Definition chlists.h:63
int32_t cnt_t
Definition chearly.h:92
int32_t msg_t
Definition chearly.h:88
#define MSG_RESET
Wakeup caused by a reset condition.
Definition chschd.h:42
msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
void chSemAddCounterI(semaphore_t *sp, cnt_t n)
Adds the specified value to the semaphore counter.
msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw)
Performs atomic signal and wait operations on two semaphores.
void chSemSignal(semaphore_t *sp)
Performs a signal operation on a semaphore.
struct ch_semaphore semaphore_t
Semaphore structure.
void chSemResetWithMessageI(semaphore_t *sp, cnt_t n, msg_t msg)
Performs a reset operation on the semaphore.
void chSemResetWithMessage(semaphore_t *sp, cnt_t n, msg_t msg)
Performs a reset operation on the semaphore.
msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
void chSemSignalI(semaphore_t *sp)
Performs a signal operation on a semaphore.
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:119
Semaphore structure.
ch_queue_t queue
Queue of the threads sleeping on this semaphore.
cnt_t cnt
The semaphore counter.