ChibiOS/NIL  4.0.1
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 Macro Functions
55  * @{
56  */
57 /**
58  * @brief Initializes a semaphore with the specified counter value.
59  *
60  * @param[out] sp pointer to a @p semaphore_t structure
61  * @param[in] n initial value of the semaphore counter. Must be
62  * non-negative.
63  *
64  * @init
65  */
66 #define chSemObjectInit(sp, n) ((sp)->cnt = (n))
67 
68 /**
69  * @brief Performs a reset operation on the semaphore.
70  * @post After invoking this function all the threads waiting on the
71  * semaphore, if any, are released and the semaphore counter is set
72  * to the specified, non negative, value.
73  * @note This function implicitly sends @p MSG_RESET as message.
74  *
75  * @param[in] sp pointer to a @p semaphore_t structure
76  * @param[in] n the new value of the semaphore counter. The value must
77  * be non-negative.
78  *
79  * @api
80  */
81 #define chSemReset(sp, n) chSemResetWithMessage(sp, n, MSG_RESET)
82 
83 /**
84  * @brief Performs a reset operation on the semaphore.
85  * @post After invoking this function all the threads waiting on the
86  * semaphore, if any, are released and the semaphore counter is set
87  * to the specified, non negative, value.
88  * @post This function does not reschedule so a call to a rescheduling
89  * function must be performed before unlocking the kernel. Note that
90  * interrupt handlers always reschedule on exit so an explicit
91  * reschedule must not be performed in ISRs.
92  * @note This function implicitly sends @p MSG_RESET as message.
93  *
94  * @param[in] sp pointer to a @p semaphore_t structure
95  * @param[in] n the new value of the semaphore counter. The value must
96  * be non-negative.
97  *
98  * @iclass
99  */
100 #define chSemResetI(sp, n) chSemResetWithMessageI(sp, n, MSG_RESET)
101 
102 /**
103  * @brief Performs a wait operation on a semaphore.
104  *
105  * @param[in] sp pointer to a @p semaphore_t structure
106  * @return A message specifying how the invoking thread has been
107  * released from the semaphore.
108  * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
109  * semaphore has been signaled.
110  * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
111  *
112  * @api
113  */
114 #define chSemWait(sp) chSemWaitTimeout(sp, TIME_INFINITE)
115 
116 /**
117  * @brief Performs a wait operation on a semaphore.
118  *
119  * @param[in] sp pointer to a @p semaphore_t structure
120  * @return A message specifying how the invoking thread has been
121  * released from the semaphore.
122  * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
123  * semaphore has been signaled.
124  * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
125  *
126  * @sclass
127  */
128 #define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE)
129 
130 /**
131  * @brief Decreases the semaphore counter.
132  * @details This macro can be used when the counter is known to be positive.
133  *
134  * @param[in] sp pointer to a @p semaphore_t structure
135  *
136  * @iclass
137  */
138 #define chSemFastWaitI(sp) ((sp)->cnt--)
139 
140 /**
141  * @brief Increases the semaphore counter.
142  * @details This macro can be used when the counter is known to be not
143  * negative.
144  *
145  * @param[in] sp pointer to a @p semaphore_t structure
146  *
147  * @iclass
148  */
149 #define chSemFastSignalI(sp) ((sp)->cnt++)
150 
151 /**
152  * @brief Returns the semaphore counter current value.
153  *
154  * @iclass
155  */
156 #define chSemGetCounterI(sp) ((sp)->cnt)
157 /** @} */
158 
159 /*===========================================================================*/
160 /* External declarations. */
161 /*===========================================================================*/
162 
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
166  msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout);
167  msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout);
168  void chSemSignal(semaphore_t *sp);
169  void chSemSignalI(semaphore_t *sp);
170  void chSemResetWithMessage(semaphore_t *sp, cnt_t n, msg_t msg);
171  void chSemResetWithMessageI(semaphore_t *sp, cnt_t n, msg_t msg);
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
177 
178 #endif /* CHSEM_H */
179 
180 /** @} */
chSemWaitTimeoutS
msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
Definition: chsem.c:104
nil_threads_queue
Structure representing a queue of threads.
Definition: ch.h:446
chSemSignalI
void chSemSignalI(semaphore_t *sp)
Performs a signal operation on a semaphore.
Definition: chsem.c:153
chSemResetWithMessage
void chSemResetWithMessage(semaphore_t *sp, cnt_t n, msg_t msg)
Performs a reset operation on the semaphore.
Definition: chsem.c:184
sysinterval_t
uint32_t sysinterval_t
Type of time interval.
Definition: ch.h:385
chSemResetWithMessageI
void chSemResetWithMessageI(semaphore_t *sp, cnt_t n, msg_t msg)
Performs a reset operation on the semaphore.
Definition: chsem.c:209
chSemWaitTimeout
msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
Definition: chsem.c:75
chSemSignal
void chSemSignal(semaphore_t *sp)
Performs a signal operation on a semaphore.
Definition: chsem.c:134