FreeRTOS-Cpp
Loading...
Searching...
No Matches
Semaphore.hpp
1/*
2 * FreeRTOS-Cpp
3 * Copyright (C) 2021 Jon Enz. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * https://github.com/jonenz/FreeRTOS-Cpp
26 */
27
28#ifndef FREERTOS_SEMAPHORE_HPP
29#define FREERTOS_SEMAPHORE_HPP
30
31#include "FreeRTOS.h"
32#include "semphr.h"
33
34namespace FreeRTOS {
35
48 public:
49 friend class BinarySemaphore;
50 friend class StaticBinarySemaphore;
51 friend class CountingSemaphore;
52 friend class StaticCountingSemaphore;
53
54 SemaphoreBase(const SemaphoreBase&) = delete;
55 SemaphoreBase& operator=(const SemaphoreBase&) = delete;
56
57 static void* operator new(size_t) = delete;
58 static void* operator new[](size_t) = delete;
59
60 static void* operator new(size_t, void* ptr) {
61 return ptr;
62 }
63
64 static void* operator new[](size_t, void* ptr) {
65 return ptr;
66 }
67
77 inline bool isValid() const {
78 return (handle != NULL);
79 }
80
96 inline UBaseType_t getCount() const {
97 return uxSemaphoreGetCount(handle);
98 }
99
125 inline bool take(const TickType_t ticksToWait = portMAX_DELAY) const {
126 return (xSemaphoreTake(handle, ticksToWait) == pdTRUE);
127 }
128
158 inline bool takeFromISR(bool& higherPriorityTaskWoken) const {
159 BaseType_t taskWoken = pdFALSE;
160 const bool result = (xSemaphoreTakeFromISR(handle, &taskWoken) == pdTRUE);
161 if (taskWoken == pdTRUE) {
162 higherPriorityTaskWoken = true;
163 }
164 return result;
165 }
166
177 inline bool takeFromISR() const {
178 return (xSemaphoreTakeFromISR(handle, NULL) == pdTRUE);
179 }
180
202 inline bool give() const {
203 return (xSemaphoreGive(handle) == pdTRUE);
204 }
205
229 inline bool giveFromISR(bool& higherPriorityTaskWoken) const {
230 BaseType_t taskWoken = pdFALSE;
231 const bool result = (xSemaphoreGiveFromISR(handle, &taskWoken) == pdTRUE);
232 if (taskWoken == pdTRUE) {
233 higherPriorityTaskWoken = true;
234 }
235 return result;
236 }
237
248 inline bool giveFromISR() const {
249 return (xSemaphoreGiveFromISR(handle, NULL) == pdTRUE);
250 }
251
252 private:
253 SemaphoreBase() = default;
254
267 vSemaphoreDelete(this->handle);
268 }
269
270 SemaphoreBase(SemaphoreBase&&) noexcept = default;
271 SemaphoreBase& operator=(SemaphoreBase&&) noexcept = default;
272
277 SemaphoreHandle_t handle = NULL;
278};
279
280#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
281
324 public:
341 this->handle = xSemaphoreCreateBinary();
342 }
343 ~BinarySemaphore() = default;
344
345 BinarySemaphore(const BinarySemaphore&) = delete;
346 BinarySemaphore& operator=(const BinarySemaphore&) = delete;
347
348 BinarySemaphore(BinarySemaphore&&) noexcept = default;
349 BinarySemaphore& operator=(BinarySemaphore&&) noexcept = default;
350};
351
387 public:
404 explicit CountingSemaphore(const UBaseType_t maxCount,
405 const UBaseType_t initialCount = 0) {
406 this->handle = xSemaphoreCreateCounting(maxCount, initialCount);
407 }
408 ~CountingSemaphore() = default;
409
410 CountingSemaphore(const CountingSemaphore&) = delete;
411 CountingSemaphore& operator=(const CountingSemaphore&) = delete;
412
413 CountingSemaphore(CountingSemaphore&&) noexcept = default;
414 CountingSemaphore& operator=(CountingSemaphore&&) noexcept = default;
415};
416
417#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
418
419#if (configSUPPORT_STATIC_ALLOCATION == 1)
420
463 public:
481 this->handle = xSemaphoreCreateBinaryStatic(&staticBinarySemaphore);
482 }
483 ~StaticBinarySemaphore() = default;
484
486 StaticBinarySemaphore& operator=(const StaticBinarySemaphore&) = delete;
487
488 StaticBinarySemaphore(StaticBinarySemaphore&&) noexcept = default;
489 StaticBinarySemaphore& operator=(StaticBinarySemaphore&&) noexcept = default;
490
491 private:
492 StaticSemaphore_t staticBinarySemaphore;
493};
494
496 public:
517 explicit StaticCountingSemaphore(const UBaseType_t maxCount,
518 const UBaseType_t initialCount = 0) {
519 this->handle = xSemaphoreCreateCountingStatic(maxCount, initialCount,
520 &staticCountingSemaphore);
521 }
522 ~StaticCountingSemaphore() = default;
523
525 StaticCountingSemaphore& operator=(const StaticCountingSemaphore&) = delete;
526
528 StaticCountingSemaphore& operator=(StaticCountingSemaphore&&) noexcept =
529 default;
530
531 private:
532 StaticSemaphore_t staticCountingSemaphore;
533};
534
535#endif /* configSUPPORT_STATIC_ALLOCATION */
536
537} // namespace FreeRTOS
538
539#endif // FREERTOS_SEMAPHORE_HPP
Class that encapsulates the functionality of a FreeRTOS binary semaphore.
Definition Semaphore.hpp:323
BinarySemaphore()
Construct a new BinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinary( void )
Definition Semaphore.hpp:340
Class that encapsulates the functionality of a FreeRTOS counting semaphore.
Definition Semaphore.hpp:386
CountingSemaphore(const UBaseType_t maxCount, const UBaseType_t initialCount=0)
Construct a new CountingSemaphore by calling SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t ...
Definition Semaphore.hpp:404
Base class that provides the standard semaphore interface to FreeRTOS::BinarySemaphore,...
Definition Semaphore.hpp:47
bool takeFromISR() const
Function that calls xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition Semaphore.hpp:177
bool take(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait )
Definition Semaphore.hpp:125
bool give() const
Function that calls xSemaphoreGive( SemaphoreHandle_t xSemaphore )
Definition Semaphore.hpp:202
SemaphoreHandle_t handle
Handle used to refer to the semaphore when using the FreeRTOS interface.
Definition Semaphore.hpp:277
bool takeFromISR(bool &higherPriorityTaskWoken) const
Function that calls xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition Semaphore.hpp:158
UBaseType_t getCount() const
Function that calls UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore )
Definition Semaphore.hpp:96
bool isValid() const
Function that checks if the underlying semaphore handle is not NULL. This should be used to ensure a ...
Definition Semaphore.hpp:77
bool giveFromISR() const
Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition Semaphore.hpp:248
~SemaphoreBase()
Destroy the SemaphoreBase object by calling void vSemaphoreDelete( SemaphoreHandle_t xSemaphore )
Definition Semaphore.hpp:266
bool giveFromISR(bool &higherPriorityTaskWoken) const
Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition Semaphore.hpp:229
Class that encapsulates the functionality of a FreeRTOS binary semaphore.
Definition Semaphore.hpp:462
StaticBinarySemaphore()
Construct a new StaticBinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinaryStati...
Definition Semaphore.hpp:480
Definition Semaphore.hpp:495
StaticCountingSemaphore(const UBaseType_t maxCount, const UBaseType_t initialCount=0)
Construct a new StaticCountingSemaphore object by calling SemaphoreHandle_t xSemaphoreCreateCountingS...
Definition Semaphore.hpp:517