-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMutexCPP.h
executable file
·203 lines (188 loc) · 6.17 KB
/
MutexCPP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @file MutexCPP.h
* @brief FreeRTOS Mutex Wrapper
*
* This file contains a set of lightweight wrappers for mutexes using FreeRTOS
*
* @copyright (c) 2007-2024 Richard Damon
* @author Richard Damon <[email protected]>
* @parblock
* MIT License:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* It is requested (but not required by license) that any bugs found or
* improvements made be shared, preferably to the author.
* @endparblock
*
* @ingroup FreeRTOSCpp
*/
#ifndef MUTEXCPP_H
#define MUTEXCPP_H
#include "FreeRTOScpp.h"
#include "Lock.h"
#include "semphr.h"
#if FREERTOSCPP_USE_NAMESPACE
namespace FreeRTOScpp {
#endif
/**
* @brief Mutex Wrapper.
*
* A Mutex is a basic synchronization primitive allowing mutual exclusion to be
* handled which also implements priority inheritance. Note, the basic mutex is
* NOT recursive, ie if a Task has taken the Mutex, it must not try to take it
* again before giving it. If you need this, use a RecurviseMutex.
*
* The usage of a Mutex is similar to a semaphore, but the task that takes the
* Mutex is also supposed to be the Task that eventually gives it back. It also
* doesn't normally make sense for an ISR to use a Mutex, so no _ISR routines
* have been made available.
*
* Example Usage:
* @code
* RecursiveMutex mutex("MyMutex");
*
* // In some other task
* mutex.take();
* ... // some code that needs mutual exclusion
* mutex.give();
* @endcode
*
* @ingroup FreeRTOSCpp
*/
class Mutex : public Lockable {
public:
/**
* @brief Constructor.
* @param name Name to give mutex, used for Debug Registry if setup
*/
Mutex(char const* name) {
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
mutexHandle = xSemaphoreCreateMutexStatic(&mutexBuffer);
#else
mutexHandle = xSemaphoreCreateMutex();
#endif
#if configQUEUE_REGISTRY_SIZE > 0
vQueueAddToRegistry(mutexHandle, name);
#endif
}
/**
* @brief Destructor.
*
* Deletes the semaphore.
*/
~Mutex() {
vSemaphoreDelete(mutexHandle);
}
bool take(TickType_t wait = portMAX_DELAY) override {
return xSemaphoreTake(mutexHandle, wait);
}
#if FREERTOSCPP_USE_CHRONO
bool take(Time_ms wait) {
return xSemaphoreTake(mutexHandle, ms2ticks(wait));
}
#endif
bool give() override {
return xSemaphoreGive(mutexHandle);
}
private:
SemaphoreHandle_t mutexHandle;
#if __cplusplus < 201101L
Mutex(Mutex const&); ///< We are not copyable.
void operator =(Mutex const&); ///< We are not assignable.
#else
Mutex(Mutex const&) = delete; ///< We are not copyable.
void operator =(Mutex const&) = delete; ///< We are not assignable.
#endif // __cplusplus
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
StaticSemaphore_t mutexBuffer;
#endif
};
#if configUSE_RECURSIVE_MUTEXES > 0
/**
* @brief Recursive Mutex Wrapper.
*
* A RecursiveMutex adds the ability to nest takes, so that if you have taken the
* RecursiveMutex and take it again, this works and requires you to give the
* RecursiveMutex back as many times as it was taken before it is released.
*
* One very common application for this is for messages to the user on a console.
* Generally, you don't want pieces of a message interrupted by pieces of other
* messages, so message output routines use a mutex on the console port.
* These routines often use lower level routines that also want to make sure their
* output isn't interspersed, so each level takes the RecursiveMutex at their start
* and releases it at the end. Being a RecursiveMutex this works.
*
* Example Usage:
* @code
* RecursiveMutex mutex("MyMutex");
*
* // In some other task
* mutex.take();
* ...
* // possible in a recursive call or call to lower level routine.
* mutex.take();
* ...
* mutex.give();
* ...
* mutex.give();
*
* @endcode
* @ingroup FreeRTOSCpp
*/
class RecursiveMutex : public Lockable{
public:
RecursiveMutex(char const* name = nullptr) {
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
mutexHandle = xSemaphoreCreateRecursiveMutexStatic(&mutexBuffer);
#else
mutexHandle = xSemaphoreCreateRecursiveMutex();
#endif
#if configQUEUE_REGISTRY_SIZE > 0
if(name)
vQueueAddToRegistry(mutexHandle, name);
#endif
}
~RecursiveMutex() {
vSemaphoreDelete(mutexHandle);
}
bool take(TickType_t wait = portMAX_DELAY) override {
return xSemaphoreTakeRecursive(mutexHandle, wait);
}
bool give() override {
return xSemaphoreGiveRecursive(mutexHandle);
}
private:
SemaphoreHandle_t mutexHandle;
#if __cplusplus < 201101L
RecursiveMutex(RecursiveMutex const&); ///< We are not copyable.
void operator =(RecursiveMutex const&); ///< We are not assignable.
#else
RecursiveMutex(RecursiveMutex const&) = delete; ///< We are not copyable.
void operator =(RecursiveMutex const&) = delete; ///< We are not assignable.
#endif // __cplusplus
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
StaticSemaphore_t mutexBuffer;
#endif
};
#endif // configUSE_RECURSIVE_MUTEXES
#if FREERTOSCPP_USE_NAMESPACE
} // namespace FreeRTOScpp
#endif
#endif // MUTEXCPP_H