ChibiOS  21.6.0
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  */
52 typedef 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. */
56 } semaphore_t;
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
89 extern "C" {
90 #endif
91  void chSemObjectInit(semaphore_t *sp, cnt_t n);
92  void chSemResetWithMessage(semaphore_t *sp, cnt_t n, msg_t msg);
98  void chSemSignal(semaphore_t *sp);
99  void chSemSignalI(semaphore_t *sp);
100  void chSemAddCounterI(semaphore_t *sp, cnt_t n);
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  */
123 static 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  */
145 static 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  */
158 static inline void chSemFastWaitI(semaphore_t *sp) {
159 
160  chDbgCheckClassI();
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  */
174 static inline void chSemFastSignalI(semaphore_t *sp) {
175 
176  chDbgCheckClassI();
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  */
189 static inline cnt_t chSemGetCounterI(const semaphore_t *sp) {
190 
191  chDbgCheckClassI();
192 
193  return sp->cnt;
194 }
195 
196 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
197 
198 #endif /* CHSEM_H */
199 
200 /** @} */
chSemAddCounterI
void chSemAddCounterI(semaphore_t *sp, cnt_t n)
Adds the specified value to the semaphore counter.
Definition: rt/src/chsem.c:345
MSG_RESET
#define MSG_RESET
Wakeup caused by a reset condition.
Definition: chschd.h:42
chSemSignal
void chSemSignal(semaphore_t *sp)
Performs a signal operation on a semaphore.
Definition: rt/src/chsem.c:290
chSemGetCounterI
static cnt_t chSemGetCounterI(const semaphore_t *sp)
Returns the semaphore counter current value.
Definition: rt/include/chsem.h:189
chSemWait
msg_t chSemWait(semaphore_t *sp)
Performs a wait operation on a semaphore.
Definition: rt/src/chsem.c:169
msg_t
int32_t msg_t
Definition: chearly.h:88
chSemWaitS
msg_t chSemWaitS(semaphore_t *sp)
Performs a wait operation on a semaphore.
Definition: rt/src/chsem.c:191
semaphore_t
struct ch_semaphore semaphore_t
Semaphore structure.
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
chSemObjectInit
void chSemObjectInit(semaphore_t *sp, cnt_t n)
Initializes a semaphore with the specified counter value.
Definition: rt/src/chsem.c:97
chSemFastSignalI
static void chSemFastSignalI(semaphore_t *sp)
Increases the semaphore counter.
Definition: rt/include/chsem.h:174
ch_semaphore
Semaphore structure.
Definition: rt/include/chsem.h:52
ch_queue
Structure representing a generic bidirectional linked list header and element.
Definition: chlists.h:69
ch_semaphore::queue
ch_queue_t queue
Queue of the threads sleeping on this semaphore.
Definition: rt/include/chsem.h:53
ch_semaphore::cnt
cnt_t cnt
The semaphore counter.
Definition: rt/include/chsem.h:55
sysinterval_t
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
chSemReset
static void chSemReset(semaphore_t *sp, cnt_t n)
Performs a reset operation on the semaphore.
Definition: rt/include/chsem.h:123
chSemFastWaitI
static void chSemFastWaitI(semaphore_t *sp)
Decreases the semaphore counter.
Definition: rt/include/chsem.h:158
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
chSemSignalWait
msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw)
Performs atomic signal and wait operations on two semaphores.
Definition: rt/src/chsem.c:374
chSemResetI
static void chSemResetI(semaphore_t *sp, cnt_t n)
Performs a reset operation on the semaphore.
Definition: rt/include/chsem.h:145
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