aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/os/rt/include/chsem.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/os/rt/include/chsem.h')
-rw-r--r--lib/chibios/os/rt/include/chsem.h200
1 files changed, 200 insertions, 0 deletions
diff --git a/lib/chibios/os/rt/include/chsem.h b/lib/chibios/os/rt/include/chsem.h
new file mode 100644
index 000000000..a88978834
--- /dev/null
+++ b/lib/chibios/os/rt/include/chsem.h
@@ -0,0 +1,200 @@
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. */
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
89extern "C" {
90#endif
91 void chSemObjectInit(semaphore_t *sp, cnt_t n);
92 void chSemResetWithMessage(semaphore_t *sp, cnt_t n, msg_t msg);
93 void chSemResetWithMessageI(semaphore_t *sp, cnt_t n, msg_t msg);
94 msg_t chSemWait(semaphore_t *sp);
95 msg_t chSemWaitS(semaphore_t *sp);
96 msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout);
97 msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout);
98 void chSemSignal(semaphore_t *sp);
99 void chSemSignalI(semaphore_t *sp);
100 void chSemAddCounterI(semaphore_t *sp, cnt_t n);
101 msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw);
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
125 chSemResetWithMessage(sp, n, MSG_RESET);
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
147 chSemResetWithMessageI(sp, n, MSG_RESET);
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
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 */
174static 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 */
189static 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/** @} */