ChibiOS 21.11.5
nil/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 nil/include/chsem.h
21 * @brief Nil RTOS semaphores header file.
22 *
23 * @addtogroup NIL_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/* Module macros. */
50/*===========================================================================*/
51
52/**
53 * @name Semaphores macros
54 * @{
55 */
56/**
57 * @brief Data part of a static semaphore initializer.
58 * @details This macro should be used when statically initializing a semaphore
59 * that is part of a bigger structure.
60 *
61 * @param[in] name the name of the semaphore variable
62 * @param[in] n the counter initial value, this value must be
63 * non-negative
64 */
65#define __SEMAPHORE_DATA(name, n) {n}
66
67/**
68 * @brief Static semaphore initializer.
69 * @details Statically initialized semaphores require no explicit
70 * initialization using @p chSemInit().
71 *
72 * @param[in] name the name of the semaphore variable
73 * @param[in] n the counter initial value, this value must be
74 * non-negative
75 */
76#define SEMAPHORE_DECL(name, n) semaphore_t name = __SEMAPHORE_DATA(name, n)
77/** @} */
78
79/**
80 * @name Macro Functions
81 * @{
82 */
83/**
84 * @brief Initializes a semaphore with the specified counter value.
85 *
86 * @param[out] sp pointer to a @p semaphore_t structure
87 * @param[in] n initial value of the semaphore counter. Must be
88 * non-negative.
89 *
90 * @init
91 */
92#define chSemObjectInit(sp, n) ((sp)->cnt = (n))
93
94/**
95 * @brief Performs a reset operation on the semaphore.
96 * @post After invoking this function all the threads waiting on the
97 * semaphore, if any, are released and the semaphore counter is set
98 * to the specified, non negative, value.
99 * @note This function implicitly sends @p MSG_RESET as message.
100 *
101 * @param[in] sp pointer to a @p semaphore_t structure
102 * @param[in] n the new value of the semaphore counter. The value must
103 * be non-negative.
104 *
105 * @api
106 */
107#define chSemReset(sp, n) chSemResetWithMessage(sp, n, MSG_RESET)
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 * @post This function does not reschedule so a call to a rescheduling
115 * function must be performed before unlocking the kernel. Note that
116 * interrupt handlers always reschedule on exit so an explicit
117 * reschedule must not be performed in ISRs.
118 * @note This function implicitly sends @p MSG_RESET as message.
119 *
120 * @param[in] sp pointer to a @p semaphore_t structure
121 * @param[in] n the new value of the semaphore counter. The value must
122 * be non-negative.
123 *
124 * @iclass
125 */
126#define chSemResetI(sp, n) chSemResetWithMessageI(sp, n, MSG_RESET)
127
128/**
129 * @brief Performs a wait operation on a semaphore.
130 *
131 * @param[in] sp pointer to a @p semaphore_t structure
132 * @return A message specifying how the invoking thread has been
133 * released from the semaphore.
134 * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
135 * semaphore has been signaled.
136 * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
137 *
138 * @api
139 */
140#define chSemWait(sp) chSemWaitTimeout(sp, TIME_INFINITE)
141
142/**
143 * @brief Performs a wait operation on a semaphore.
144 *
145 * @param[in] sp pointer to a @p semaphore_t structure
146 * @return A message specifying how the invoking thread has been
147 * released from the semaphore.
148 * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
149 * semaphore has been signaled.
150 * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
151 *
152 * @sclass
153 */
154#define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE)
155
156/**
157 * @brief Decreases the semaphore counter.
158 * @details This macro can be used when the counter is known to be positive.
159 *
160 * @param[in] sp pointer to a @p semaphore_t structure
161 *
162 * @iclass
163 */
164#define chSemFastWaitI(sp) ((sp)->cnt--)
165
166/**
167 * @brief Increases the semaphore counter.
168 * @details This macro can be used when the counter is known to be not
169 * negative.
170 *
171 * @param[in] sp pointer to a @p semaphore_t structure
172 *
173 * @iclass
174 */
175#define chSemFastSignalI(sp) ((sp)->cnt++)
176
177/**
178 * @brief Returns the semaphore counter current value.
179 *
180 * @iclass
181 */
182#define chSemGetCounterI(sp) ((sp)->cnt)
183/** @} */
184
185/*===========================================================================*/
186/* External declarations. */
187/*===========================================================================*/
188
189#ifdef __cplusplus
190extern "C" {
191#endif
194 void chSemSignal(semaphore_t *sp);
195 void chSemSignalI(semaphore_t *sp);
198#ifdef __cplusplus
199}
200#endif
201
202#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
203
204#endif /* CHSEM_H */
205
206/** @} */
int32_t cnt_t
Definition chearly.h:91
int32_t msg_t
Definition chearly.h:87
msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
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