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