FreeRTOS Addons
Loading...
Searching...
No Matches
mutex.hpp
1/****************************************************************************
2 *
3 * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4 *
5 * This file is part of the FreeRTOS Add-ons project.
6 *
7 * Source Code:
8 * https://github.com/michaelbecker/freertos-addons
9 *
10 * Project Page:
11 * http://michaelbecker.github.io/freertos-addons/
12 *
13 * On-line Documentation:
14 * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files
18 * (the "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so,subject to the
22 * following conditions:
23 *
24 * + The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
26 * + Credit is appreciated, but not required, if you find this project
27 * useful enough to include in your application, product, device, etc.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 ***************************************************************************/
38
39
40#include "freertos-config.h"
41
42#ifndef MUTEX_HPP_
43#define MUTEX_HPP_
44
52#ifndef CPP_FREERTOS_NO_EXCEPTIONS
53#include <exception>
54#include <string>
55#include <cstdio>
56#ifdef CPP_FREERTOS_NO_CPP_STRINGS
57#error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
58#endif
59#endif
60#include "FreeRTOS.h"
61#include "semphr.h"
62
63namespace cpp_freertos {
64
65
66#ifndef CPP_FREERTOS_NO_EXCEPTIONS
70class MutexCreateException : public std::exception {
71
72 public:
77 {
78 sprintf(errorString, "Mutex Constructor Failed");
79 }
80
85 virtual const char *what() const throw()
86 {
87 return errorString;
88 }
89
90 private:
94 char errorString[80];
95};
96#endif
97
98
109class Mutex {
110
112 //
113 // Public API
114 //
116 public:
126 virtual bool Lock(TickType_t Timeout = portMAX_DELAY) = 0;
127
134 virtual bool Unlock() = 0;
135
139 virtual ~Mutex();
140
142 //
143 // Protected API
144 // Not intended for use by application code.
145 //
147 protected:
151 SemaphoreHandle_t handle;
152
156 Mutex();
157};
158
159
171class MutexStandard : public Mutex {
172
174 //
175 // Public API
176 //
178 public:
185
192 virtual bool Lock(TickType_t Timeout = portMAX_DELAY);
193
200 virtual bool Unlock();
201};
202
203
204#if (configUSE_RECURSIVE_MUTEXES == 1)
205
219class MutexRecursive : public Mutex {
220
222 //
223 // Public API
224 //
226 public:
233
240 virtual bool Lock(TickType_t Timeout = portMAX_DELAY);
241
248 virtual bool Unlock();
249};
250
251#endif
252
253
265
267 //
268 // Public API
269 //
271 public:
278 explicit LockGuard(Mutex& m);
279
285 ~LockGuard();
286
288 //
289 // Private API
290 //
292 private:
296 LockGuard(const LockGuard&);
297
302 Mutex& mutex;
303};
304
305
306}
307#endif
Definition: mutex.hpp:264
~LockGuard()
Definition: cmutex.cpp:124
Definition: mutex.hpp:70
MutexCreateException()
Definition: mutex.hpp:76
virtual const char * what() const
Definition: mutex.hpp:85
Definition: mutex.hpp:109
Mutex()
Definition: cmutex.cpp:46
SemaphoreHandle_t handle
Definition: mutex.hpp:151
virtual bool Unlock()=0
virtual ~Mutex()
Definition: cmutex.cpp:51
virtual bool Lock(TickType_t Timeout=portMAX_DELAY)=0
Definition: mutex.hpp:219
MutexRecursive()
Definition: cmutex.cpp:87
virtual bool Lock(TickType_t Timeout=portMAX_DELAY)
Definition: cmutex.cpp:101
virtual bool Unlock()
Definition: cmutex.cpp:108
Definition: mutex.hpp:171
virtual bool Lock(TickType_t Timeout=portMAX_DELAY)
Definition: cmutex.cpp:71
MutexStandard()
Definition: cmutex.cpp:57
virtual bool Unlock()
Definition: cmutex.cpp:78
Definition: condition_variable.hpp:57