TinyRobotics
Loading...
Searching...
No Matches
MutexRTOS.h
1#pragma once
2#include "TinyRobotics/utils/Config.h"
3#include "TinyRobotics/concurrency/Mutex.h"
4
5#ifdef ESP32
6# include "freertos/FreeRTOS.h"
7# include "freertos/semphr.h"
8#elif defined(__linux__)
9#else
10# include "FreeRTOS.h"
11# include "semphr.h"
12#endif
13
14namespace tinyrobotics {
15
16/**
17 * @brief Mutex implemntation using FreeRTOS
18 * @ingroup concurrency
19 * @author Phil Schatzmann
20 *
21 */
22class MutexRTOS : public MutexBase {
23public:
24 MutexRTOS() {
25 xSemaphore = xSemaphoreCreateBinary();
26 unlock();
27 }
28 virtual ~MutexRTOS() {
29 vSemaphoreDelete(xSemaphore);
30 }
31 void lock() override {
32 xSemaphoreTake(xSemaphore, portMAX_DELAY);
33 }
34 void unlock() override {
35 xSemaphoreGive(xSemaphore);
36 }
37
38protected:
39 SemaphoreHandle_t xSemaphore = NULL;
40};
41
42/**
43 * @brief Recursive Mutex implemntation using FreeRTOS
44 * @ingroup concurrency
45 * @author Phil Schatzmann
46 *
47 */
48
49class MutexRecursiveRTOS : public MutexBase {
50public:
51 MutexRecursiveRTOS() {
52 xSemaphore = xSemaphoreCreateBinary();
53 unlock();
54 }
55 virtual ~MutexRecursiveRTOS() {
56 vSemaphoreDelete(xSemaphore);
57 }
58 void lock() override {
59 xSemaphoreTakeRecursive(xSemaphore, portMAX_DELAY);
60 }
61 void unlock() override {
62 xSemaphoreGiveRecursive(xSemaphore);
63 }
64
65protected:
66 SemaphoreHandle_t xSemaphore = NULL;
67};
68
69
70/// @brief Default Mutex implementation using RTOS semaphores
71/// @ingroup concurrency
72using Mutex = MutexRTOS;
73
74} // namespace tinyrobotics
Empty Mutex implementation which does nothing.
Definition: Mutex.h:18
Mutex implemntation using FreeRTOS.
Definition: MutexRTOS.h:22
Recursive Mutex implemntation using FreeRTOS.
Definition: MutexRTOS.h:49