FreeRTOS-Cpp
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 
34 namespace 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, void* ptr) { return ptr; }
58  static void* operator new[](size_t, void* ptr) { return ptr; }
59  static void* operator new(size_t) = delete;
60  static void* operator new[](size_t) = delete;
61 
71  inline bool isValid() const { return (handle != NULL); }
72 
88  inline UBaseType_t getCount() const { return uxSemaphoreGetCount(handle); }
89 
115  inline bool take(const TickType_t ticksToWait = portMAX_DELAY) const {
116  return (xSemaphoreTake(handle, ticksToWait) == pdTRUE);
117  }
118 
148  inline bool takeFromISR(bool& higherPriorityTaskWoken) const {
149  BaseType_t taskWoken = pdFALSE;
150  bool result = (xSemaphoreTakeFromISR(handle, &taskWoken) == pdTRUE);
151  if (taskWoken == pdTRUE) {
152  higherPriorityTaskWoken = true;
153  }
154  return result;
155  }
156 
167  inline bool takeFromISR() const {
168  return (xSemaphoreTakeFromISR(handle, NULL) == pdTRUE);
169  }
170 
192  inline bool give() const { return (xSemaphoreGive(handle) == pdTRUE); }
193 
217  inline bool giveFromISR(bool& higherPriorityTaskWoken) const {
218  BaseType_t taskWoken = pdFALSE;
219  bool result = (xSemaphoreGiveFromISR(handle, &taskWoken) == pdTRUE);
220  if (taskWoken == pdTRUE) {
221  higherPriorityTaskWoken = true;
222  }
223  return result;
224  }
225 
236  inline bool giveFromISR() const {
237  return (xSemaphoreGiveFromISR(handle, NULL) == pdTRUE);
238  }
239 
240  private:
241  SemaphoreBase() = default;
242 
254  ~SemaphoreBase() { vSemaphoreDelete(this->handle); }
255 
256  SemaphoreBase(SemaphoreBase&&) noexcept = default;
257  SemaphoreBase& operator=(SemaphoreBase&&) noexcept = default;
258 
263  SemaphoreHandle_t handle = NULL;
264 };
265 
266 #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
267 
310  public:
326  BinarySemaphore() { this->handle = xSemaphoreCreateBinary(); }
327  ~BinarySemaphore() = default;
328 
329  BinarySemaphore(const BinarySemaphore&) = delete;
330  BinarySemaphore& operator=(const BinarySemaphore&) = delete;
331 
332  BinarySemaphore(BinarySemaphore&&) noexcept = default;
333  BinarySemaphore& operator=(BinarySemaphore&&) noexcept = default;
334 };
335 
371  public:
388  explicit CountingSemaphore(const UBaseType_t maxCount,
389  const UBaseType_t initialCount = 0) {
390  this->handle = xSemaphoreCreateCounting(maxCount, initialCount);
391  }
392  ~CountingSemaphore() = default;
393 
394  CountingSemaphore(const CountingSemaphore&) = delete;
395  CountingSemaphore& operator=(const CountingSemaphore&) = delete;
396 
397  CountingSemaphore(CountingSemaphore&&) noexcept = default;
398  CountingSemaphore& operator=(CountingSemaphore&&) noexcept = default;
399 };
400 
401 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
402 
403 #if (configSUPPORT_STATIC_ALLOCATION == 1)
404 
447  public:
465  this->handle = xSemaphoreCreateBinaryStatic(&staticBinarySemaphore);
466  }
467  ~StaticBinarySemaphore() = default;
468 
470  StaticBinarySemaphore& operator=(const StaticBinarySemaphore&) = delete;
471 
472  StaticBinarySemaphore(StaticBinarySemaphore&&) noexcept = default;
473  StaticBinarySemaphore& operator=(StaticBinarySemaphore&&) noexcept = default;
474 
475  private:
476  StaticSemaphore_t staticBinarySemaphore;
477 };
478 
480  public:
501  explicit StaticCountingSemaphore(const UBaseType_t maxCount,
502  const UBaseType_t initialCount = 0) {
503  this->handle = xSemaphoreCreateCountingStatic(maxCount, initialCount,
504  &staticCountingSemaphore);
505  }
506  ~StaticCountingSemaphore() = default;
507 
509  StaticCountingSemaphore& operator=(const StaticCountingSemaphore&) = delete;
510 
511  StaticCountingSemaphore(StaticCountingSemaphore&&) noexcept = default;
512  StaticCountingSemaphore& operator=(StaticCountingSemaphore&&) noexcept =
513  default;
514 
515  private:
516  StaticSemaphore_t staticCountingSemaphore;
517 };
518 
519 #endif /* configSUPPORT_STATIC_ALLOCATION */
520 
521 } // namespace FreeRTOS
522 
523 #endif // FREERTOS_SEMAPHORE_HPP
Class that encapsulates the functionality of a FreeRTOS binary semaphore.
Definition: Semaphore.hpp:309
BinarySemaphore()
Construct a new BinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinary( void )
Definition: Semaphore.hpp:326
Class that encapsulates the functionality of a FreeRTOS counting semaphore.
Definition: Semaphore.hpp:370
CountingSemaphore(const UBaseType_t maxCount, const UBaseType_t initialCount=0)
Construct a new CountingSemaphore by calling SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t ...
Definition: Semaphore.hpp:388
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:167
bool take(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait )
Definition: Semaphore.hpp:115
bool give() const
Function that calls xSemaphoreGive( SemaphoreHandle_t xSemaphore )
Definition: Semaphore.hpp:192
SemaphoreHandle_t handle
Handle used to refer to the semaphore when using the FreeRTOS interface.
Definition: Semaphore.hpp:263
bool takeFromISR(bool &higherPriorityTaskWoken) const
Function that calls xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition: Semaphore.hpp:148
UBaseType_t getCount() const
Function that calls UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore )
Definition: Semaphore.hpp:88
bool isValid() const
Function that checks if the underlying semaphore handle is not NULL. This should be used to ensure a ...
Definition: Semaphore.hpp:71
bool giveFromISR() const
Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition: Semaphore.hpp:236
~SemaphoreBase()
Destroy the SemaphoreBase object by calling void vSemaphoreDelete( SemaphoreHandle_t xSemaphore )
Definition: Semaphore.hpp:254
bool giveFromISR(bool &higherPriorityTaskWoken) const
Function that calls xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherP...
Definition: Semaphore.hpp:217
Class that encapsulates the functionality of a FreeRTOS binary semaphore.
Definition: Semaphore.hpp:446
StaticBinarySemaphore()
Construct a new StaticBinarySemaphore object by calling SemaphoreHandle_t xSemaphoreCreateBinaryStati...
Definition: Semaphore.hpp:464
Definition: Semaphore.hpp:479
StaticCountingSemaphore(const UBaseType_t maxCount, const UBaseType_t initialCount=0)
Construct a new StaticCountingSemaphore object by calling SemaphoreHandle_t xSemaphoreCreateCountingS...
Definition: Semaphore.hpp:501