ChibiOS/RT  5.1.0
chsem.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 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  threads_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) {_THREADS_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 chSemReset(semaphore_t *sp, cnt_t n);
93  void chSemResetI(semaphore_t *sp, cnt_t n);
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 Decreases the semaphore counter.
112  * @details This macro can be used when the counter is known to be positive.
113  *
114  * @param[in] sp pointer to a @p semaphore_t structure
115  *
116  * @iclass
117  */
118 static inline void chSemFastWaitI(semaphore_t *sp) {
119 
121 
122  sp->cnt--;
123 }
124 
125 /**
126  * @brief Increases the semaphore counter.
127  * @details This macro can be used when the counter is known to be not
128  * negative.
129  *
130  * @param[in] sp pointer to a @p semaphore_t structure
131  *
132  * @iclass
133  */
134 static inline void chSemFastSignalI(semaphore_t *sp) {
135 
137 
138  sp->cnt++;
139 }
140 
141 /**
142  * @brief Returns the semaphore counter current value.
143  *
144  * @param[in] sp pointer to a @p semaphore_t structure
145  * @return The semaphore counter value.
146  *
147  * @iclass
148  */
149 static inline cnt_t chSemGetCounterI(const semaphore_t *sp) {
150 
152 
153  return sp->cnt;
154 }
155 
156 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
157 
158 #endif /* CHSEM_H */
159 
160 /** @} */
msg_t chSemWait(semaphore_t *sp)
Performs a wait operation on a semaphore.
Definition: chsem.c:175
cnt_t cnt
The semaphore counter.
Definition: chsem.h:55
void chSemAddCounterI(semaphore_t *sp, cnt_t n)
Adds the specified value to the semaphore counter.
Definition: chsem.c:349
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:150
msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
Definition: chsem.c:264
msg_t chSemWaitS(semaphore_t *sp)
Performs a wait operation on a semaphore.
Definition: chsem.c:197
void chSemSignal(semaphore_t *sp)
Performs a signal operation on a semaphore.
Definition: chsem.c:294
static void chSemFastWaitI(semaphore_t *sp)
Decreases the semaphore counter.
Definition: chsem.h:118
Generic threads bidirectional linked list header and element.
Definition: chschd.h:142
struct ch_semaphore semaphore_t
Semaphore structure.
void chDbgCheckClassI(void)
I-class functions context check.
Definition: chdebug.c:235
static void chSemFastSignalI(semaphore_t *sp)
Increases the semaphore counter.
Definition: chsem.h:134
void chSemReset(semaphore_t *sp, cnt_t n)
Performs a reset operation on the semaphore.
Definition: chsem.c:120
Semaphore structure.
Definition: chsem.h:52
threads_queue_t queue
Queue of the threads sleeping on this semaphore.
Definition: chsem.h:53
msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw)
Performs atomic signal and wait operations on two semaphores.
Definition: chsem.c:378
void chSemObjectInit(semaphore_t *sp, cnt_t n)
Initializes a semaphore with the specified counter value.
Definition: chsem.c:97
msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
Definition: chsem.c:235
static cnt_t chSemGetCounterI(const semaphore_t *sp)
Returns the semaphore counter current value.
Definition: chsem.h:149
void chSemSignalI(semaphore_t *sp)
Performs a signal operation on a semaphore.
Definition: chsem.c:319
void chSemResetI(semaphore_t *sp, cnt_t n)
Performs a reset operation on the semaphore.
Definition: chsem.c:147