FreeRTOScpp
SemaphoreCPP.h
Go to the documentation of this file.
1 /**
2  * @file SemaphoreCPP.h
3  * @brief FreeRTOS Semaphore Wrapper
4  *
5  * This file contains a set of lightweight wrappers for semaphores using FreeRTOS
6  *
7  * @copyright (c) 2007-2024 Richard Damon
8  * @author Richard Damon <richard.damon@gmail.com>
9  * @parblock
10  * MIT License:
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  * THE SOFTWARE.
29  *
30  * It is requested (but not required by license) that any bugs found or
31  * improvements made be shared, preferably to the author.
32  * @endparblock
33  *
34  * @todo Add Counting Semaphores
35  * @ingroup FreeRTOSCpp
36  */
37 
38 #ifndef SEMAPHORE_CPP_H
39 #define SEMAPHORE_CPP_H
40 
41 #include "FreeRTOScpp.h"
42 #include "Lock.h"
43 #include "FreeRTOS.h"
44 #include "semphr.h"
45 
46 #if FREERTOSCPP_USE_NAMESPACE
47 namespace FreeRTOScpp {
48 #endif
49 
50 
51 /**
52  * @brief Binary Semaphore Wrapper.
53  *
54  * Example Usage:
55  * @code
56  * Semaphore sema("MySema");
57  *
58  * // In some task
59  * sema.give();
60  *
61  * // In some other task
62  * sema.take();
63  *
64  * // In some ISR
65  *
66  * portBASE_TYPE woken = 0;
67  * ...
68  * sema.give_ISR(woken);
69  * ...
70  * portYIELD_FROM_ISR(woken);
71  * return;
72  *
73  * @endcode
74  * @ingroup FreeRTOSCpp
75  */
76 
77 class BinarySemaphore : public Lockable {
78 public:
79  /**
80  * @brief Constructor.
81  * @param name Name to give semaphore, used for Debug Registry if setup
82  */
83  BinarySemaphore(char const* name = nullptr) {
84 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
85  sema = xSemaphoreCreateBinaryStatic(&semaBuffer);
86 #else
87  sema = xSemaphoreCreateBinary();
88 #endif
89 #if configQUEUE_REGISTRY_SIZE > 0
90  if(name)
91  vQueueAddToRegistry(sema, name);
92 #endif
93  }
94  /**
95  * @brief Destructor.
96  *
97  * Delete the semaphore.
98  */
100  vQueueDelete(sema);
101  }
102  /**
103  * @brief Give the Semaphore.
104  */
105  bool give() override {
106  return xSemaphoreGive(sema);
107  }
108 
109  /**
110  * @brief Take the semaphore.
111  *
112  * @param delay The number of ticks to wait for the semaphore
113  */
114  bool take(TickType_t delay = portMAX_DELAY) override {
115  return xSemaphoreTake(sema, delay);
116  }
117  bool take_ISR(portBASE_TYPE& waswoken) {
118  return xSemaphoreTakeFromISR(sema, &waswoken);
119  }
120 
121  #if FREERTOSCPP_USE_CHRONO
122  /**
123  * @brief Take the semaphore.
124  *
125  * @param delay The number of ticks to wait for the semaphore
126  */
127  bool take(Time_ms delay){
128  return xSemaphoreTake(sema, ms2ticks(delay));
129  }
130 #endif
131  /**
132  * @brief Give the Semaphore inside an ISR
133  *
134  * @param waswoken The flag variable used to indicate if we need to run the
135  * scheduler when we exit the ISR.
136  */
137  bool give_ISR(portBASE_TYPE& waswoken) {
138  return xSemaphoreGiveFromISR(sema, &waswoken);
139  }
140 private:
141  SemaphoreHandle_t sema;
142 
143 #if __cplusplus < 201101L
144  Semaphore(Semaphore const&); ///< We are not copyable.
145  void operator =(Semaphore const&); ///< We are not assignable.
146 #else
147  BinarySemaphore(BinarySemaphore const&) = delete; ///< We are not copyable.
148  void operator =(BinarySemaphore const&) = delete; ///< We are not assignable.
149 #endif // __cplusplus
150 
151 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
152  StaticSemaphore_t semaBuffer;
153 #endif
154 
155 };
156 
157 typedef BinarySemaphore Semaphore [[deprecated("Rename to BinarySemaphore")]];
158 #if FREERTOSCPP_USE_NAMESPACE
159 } // namespace FreeRTOScpp
160 #endif
161 
162 
163 #endif
void operator=(BinarySemaphore const &)=delete
We are not assignable.
bool take(Time_ms delay)
Take the semaphore.
Definition: SemaphoreCPP.h:127
bool take_ISR(portBASE_TYPE &waswoken)
Definition: SemaphoreCPP.h:117
FreeRTOS Lock wrapper.
constexpr TickType_t ms2ticks(Time_ms ms)
Definition: FreeRTOScpp.h:81
std::chrono::milliseconds Time_ms
Definition: FreeRTOScpp.h:79
BinarySemaphore(char const *name=nullptr)
Constructor.
Definition: SemaphoreCPP.h:83
Binary Semaphore Wrapper.
Definition: SemaphoreCPP.h:77
bool take(TickType_t delay=portMAX_DELAY) override
Take the semaphore.
Definition: SemaphoreCPP.h:114
Definition: CallBack.h:63
~BinarySemaphore()
Destructor.
Definition: SemaphoreCPP.h:99
FreeRTOS Wrapper.
A Base class to provide block based locking capability.
Definition: Lock.h:58
BinarySemaphore Semaphore[[deprecated("Rename to BinarySemaphore")]]
Definition: SemaphoreCPP.h:157
bool give_ISR(portBASE_TYPE &waswoken)
Give the Semaphore inside an ISR.
Definition: SemaphoreCPP.h:137
SemaphoreHandle_t sema
Definition: SemaphoreCPP.h:141
bool give() override
Give the Semaphore.
Definition: SemaphoreCPP.h:105