FreeRTOS C++ Wrappers
MutexCPP.h
Go to the documentation of this file.
1 
37 #ifndef MUTEXCPP_H
38 #define MUTEXCPP_H
39 
40 #include "FreeRTOS.h"
41 #include "semphr.h"
42 
49 class Mutex {
50 public:
55  Mutex(char const* name) {
56  handle = xSemaphoreCreateMutex();
57 #if configQUEUE_REGISTRY_SIZE > 0
58  vQueueAddToRegistry(handle, name);
59 #endif
60  }
66  ~Mutex() {
67  vSemaphoreDelete(handle);
68  }
69 
70  bool take(TickType_t wait = portMAX_DELAY) {
71  return xSemaphoreTake(handle, wait);
72  }
73 
74  bool give() {
75  return xSemaphoreGive(handle);
76  }
77 private:
78  SemaphoreHandle_t handle;
79 #if __cplusplus < 201101L
80  Mutex(Mutex const&);
81  void operator =(Mutex const&);
82 #else
83  Mutex(Mutex const&) = delete;
84  void operator =(Mutex const&) = delete;
85 #endif // __cplusplus
86 
87 };
88 
89 #if configUSE_RECURSIVE_MUTEXES > 0
90 
96 class RecursiveMutex {
97 public:
98  RecursiveMutex(char const* name) {
99  handle = xSemaphoreCreateRecursiveMutex();
100 #if configQUEUE_REGISTRY_SIZE > 0
101  vQueueAddToRegistry(handle, name);
102 #endif
103  }
104  ~RecursiveMutex() {
105  vSemaphoreDelete(handle);
106  }
107 
108  bool take(TickType_t wait = portMAX_DELAY) {
109  return xSemaphoreTakeRecursive(handle, wait);
110 
111  }
112 
113  bool give() {
114  return xSemaphoreGiveRecursive(handle);
115  }
116 
117 private:
118  SemaphoreHandle_t handle;
119 #if __cplusplus < 201101L
120  RecursiveMutex(RecursiveMutex const&);
121  void operator =(RecursiveMutex const&);
122 #else
123  RecursiveMutex(RecursiveMutex const&) = delete;
124  void operator =(RecursiveMutex const&) = delete;
125 #endif // __cplusplus
126 };
127 #endif // configUSE_RECURSIVE_MUTEXES
128 
129 #endif // MUTEXCPP_H
bool take(TickType_t wait=portMAX_DELAY)
Definition: MutexCPP.h:70
~Mutex()
Destructor.
Definition: MutexCPP.h:66
bool give()
Definition: MutexCPP.h:74
Mutex Wrapper.
Definition: MutexCPP.h:49
Mutex(char const *name)
Constructor.
Definition: MutexCPP.h:55