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