FreeRTOS-Cpp
Loading...
Searching...
No Matches
Mutex.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_MUTEX_HPP
29#define FREERTOS_MUTEX_HPP
30
31#include "FreeRTOS.h"
32#include "semphr.h"
33
34namespace FreeRTOS {
35
47class MutexBase {
48 public:
49 friend class Mutex;
50 friend class StaticMutex;
51 friend class RecursiveMutexBase;
52 friend class RecursiveMutex;
53 friend class StaticRecursiveMutex;
54
55 MutexBase(const MutexBase&) = delete;
56 MutexBase& operator=(const MutexBase&) = delete;
57
58 static void* operator new(size_t) = delete;
59 static void* operator new[](size_t) = delete;
60
61 static void* operator new(size_t, void* ptr) {
62 return ptr;
63 }
64
65 static void* operator new[](size_t, void* ptr) {
66 return ptr;
67 }
68
78 inline bool isValid() const {
79 return (handle != NULL);
80 }
81
99 inline bool lock(const TickType_t ticksToWait = portMAX_DELAY) const {
100 return (xSemaphoreTake(handle, ticksToWait) == pdTRUE);
101 }
102
123 inline bool lockFromISR(bool& higherPriorityTaskWoken) const {
124 BaseType_t taskWoken = pdFALSE;
125 const bool result = (xSemaphoreTakeFromISR(handle, &taskWoken) == pdTRUE);
126 if (taskWoken == pdTRUE) {
127 higherPriorityTaskWoken = true;
128 }
129 return result;
130 }
131
142 inline bool lockFromISR() const {
143 return (xSemaphoreTakeFromISR(handle, NULL) == pdTRUE);
144 }
145
164 inline bool unlock() const {
165 return (xSemaphoreGive(handle) == pdTRUE);
166 }
167
168 private:
169 MutexBase() = default;
170
183 vSemaphoreDelete(this->handle);
184 }
185
186 MutexBase(MutexBase&&) noexcept = default;
187 MutexBase& operator=(MutexBase&&) noexcept = default;
188
193 SemaphoreHandle_t handle = NULL;
194};
195
208 public:
209 friend class RecursiveMutex;
210 friend class StaticRecursiveMutex;
211
212 RecursiveMutexBase(const RecursiveMutexBase&) = delete;
213 RecursiveMutexBase& operator=(const RecursiveMutexBase&) = delete;
214
215 static void* operator new(size_t, void*);
216 static void* operator new[](size_t, void*);
217 static void* operator new(size_t) = delete;
218 static void* operator new[](size_t) = delete;
219
239 inline bool lock(const TickType_t ticksToWait = portMAX_DELAY) const {
240 return (xSemaphoreTakeRecursive(handle, ticksToWait) == pdTRUE);
241 }
242
263 inline bool unlock() const {
264 return (xSemaphoreGiveRecursive(handle) == pdTRUE);
265 }
266
267 private:
268 RecursiveMutexBase() = default;
269 ~RecursiveMutexBase() = default;
270
271 RecursiveMutexBase(RecursiveMutexBase&&) noexcept = default;
272 RecursiveMutexBase& operator=(RecursiveMutexBase&&) noexcept = default;
273};
274
275#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
276
303class Mutex : public MutexBase {
304 public:
321 this->handle = xSemaphoreCreateMutex();
322 }
323 ~Mutex() = default;
324
325 Mutex(const Mutex&) = delete;
326 Mutex& operator=(const Mutex&) = delete;
327
328 Mutex(Mutex&&) noexcept = default;
329 Mutex& operator=(Mutex&&) noexcept = default;
330};
331
359 public:
376 this->handle = xSemaphoreCreateRecursiveMutex();
377 }
378 ~RecursiveMutex() = default;
379
380 RecursiveMutex(const RecursiveMutex&) = delete;
381 RecursiveMutex& operator=(const RecursiveMutex&) = delete;
382
383 RecursiveMutex(RecursiveMutex&&) noexcept = default;
384 RecursiveMutex& operator=(RecursiveMutex&&) noexcept = default;
385};
386
387#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
388
389#if (configSUPPORT_STATIC_ALLOCATION == 1)
390
417class StaticMutex : public MutexBase {
418 public:
436 this->handle = xSemaphoreCreateMutexStatic(&staticMutex);
437 }
438 ~StaticMutex() = default;
439
440 StaticMutex(const StaticMutex&) = delete;
441 StaticMutex& operator=(const StaticMutex&) = delete;
442
443 StaticMutex(StaticMutex&&) noexcept = default;
444 StaticMutex& operator=(StaticMutex&&) noexcept = default;
445
446 private:
447 StaticSemaphore_t staticMutex;
448};
449
477 public:
495 this->handle = xSemaphoreCreateRecursiveMutexStatic(&staticRecursiveMutex);
496 }
497 ~StaticRecursiveMutex() = default;
498
500 StaticRecursiveMutex& operator=(const StaticRecursiveMutex&) = delete;
501
502 StaticRecursiveMutex(StaticRecursiveMutex&&) noexcept = default;
503 StaticRecursiveMutex& operator=(StaticRecursiveMutex&&) noexcept = default;
504
505 private:
506 StaticSemaphore_t staticRecursiveMutex;
507};
508
509#endif /* configSUPPORT_STATIC_ALLOCATION */
510
511} // namespace FreeRTOS
512
513#endif // FREERTOS_MUTEX_HPP
Base class that provides the standard mutex interface to FreeRTOS::Mutex, FreeRTOS::StaticMutex,...
Definition Mutex.hpp:47
bool lockFromISR(bool &higherPriorityTaskWoken) const
Function that calls xSemaphoreTakeFromISR ( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigher...
Definition Mutex.hpp:123
bool lockFromISR() const
Function that calls xSemaphoreTakeFromISR ( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigher...
Definition Mutex.hpp:142
SemaphoreHandle_t handle
Handle used to refer to the semaphore when using the FreeRTOS interface.
Definition Mutex.hpp:193
bool lock(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait )
Definition Mutex.hpp:99
~MutexBase()
Destroy the MutexBase object by calling void vSemaphoreDelete( SemaphoreHandle_t xSemaphore )
Definition Mutex.hpp:182
bool unlock() const
Function that calls xSemaphoreGive( SemaphoreHandle_t xSemaphore )
Definition Mutex.hpp:164
bool isValid() const
Function that checks if the underlying semaphore handle is not NULL. This should be used to ensure a ...
Definition Mutex.hpp:78
Class that encapsulates the functionality of a FreeRTOS mutex.
Definition Mutex.hpp:303
Mutex()
Construct a new Mutex object by calling SemaphoreHandle_t xSemaphoreCreateMutex( void )
Definition Mutex.hpp:320
Base class that provides the recursive mutex interface to FreeRTOS::RecursiveMutex and FreeRTOS::Stat...
Definition Mutex.hpp:207
bool unlock() const
Function that calls xSemaphoreGiveRecursive( SemaphoreHandle_t xSemaphore )
Definition Mutex.hpp:263
bool lock(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex, TickType_t xTicksToWait )
Definition Mutex.hpp:239
Class that encapsulates the functionality of a FreeRTOS recursive mutex.
Definition Mutex.hpp:358
RecursiveMutex()
Construct a new RecursiveMutex object by calling SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( vo...
Definition Mutex.hpp:375
Class that encapsulates the functionality of a FreeRTOS mutex.
Definition Mutex.hpp:417
StaticMutex()
Construct a new StaticMutex object by calling SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSe...
Definition Mutex.hpp:435
Class that encapsulates the functionality of a FreeRTOS recursive mutex.
Definition Mutex.hpp:476
StaticRecursiveMutex()
Construct a new StaticRecursiveMutex object by calling SemaphoreHandle_t xSemaphoreCreateRecursiveMut...
Definition Mutex.hpp:494