FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
mutex.hpp
Go to the documentation of this file.
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 #ifndef MUTEX_HPP_
41 #define MUTEX_HPP_
42 
50 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
51 #include <exception>
52 #include <string>
53 #include <cstdio>
54 #ifdef CPP_FREERTOS_NO_CPP_STRINGS
55 #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
56 #endif
57 #endif
58 #include "FreeRTOS.h"
59 #include "semphr.h"
60 
61 namespace cpp_freertos {
62 
63 
64 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
65 
68 class MutexCreateException : public std::exception {
69 
70  public:
75  {
76  sprintf(errorString, "Mutex Constructor Failed");
77  }
78 
83  virtual const char *what() const throw()
84  {
85  return errorString;
86  }
87 
88  private:
92  char errorString[80];
93 };
94 #endif
95 
96 
107 class Mutex {
108 
110  //
111  // Public API
112  //
114  public:
124  virtual bool Lock(TickType_t Timeout = portMAX_DELAY) = 0;
125 
132  virtual bool Unlock() = 0;
133 
137  virtual ~Mutex();
138 
140  //
141  // Protected API
142  // Not intended for use by application code.
143  //
145  protected:
149  SemaphoreHandle_t handle;
150 
154  Mutex();
155 };
156 
157 
169 class MutexStandard : public Mutex {
170 
172  //
173  // Public API
174  //
176  public:
182  MutexStandard();
183 
190  virtual bool Lock(TickType_t Timeout = portMAX_DELAY);
191 
198  virtual bool Unlock();
199 };
200 
201 
202 #if (configUSE_RECURSIVE_MUTEXES == 1)
203 
217 class MutexRecursive : public Mutex {
218 
220  //
221  // Public API
222  //
224  public:
230  MutexRecursive();
231 
238  virtual bool Lock(TickType_t Timeout = portMAX_DELAY);
239 
246  virtual bool Unlock();
247 };
248 
249 #endif
250 
251 
262 class LockGuard {
263 
265  //
266  // Public API
267  //
269  public:
276  explicit LockGuard(Mutex& m);
277 
283  ~LockGuard();
284 
286  //
287  // Private API
288  //
290  private:
294  LockGuard(const LockGuard&);
295 
301 };
302 
303 
304 }
305 #endif
virtual const char * what() const
Definition: mutex.hpp:83
SemaphoreHandle_t handle
Definition: mutex.hpp:149