FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
semaphore.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 SEMAPHORE_HPP_
41 #define SEMAPHORE_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 
62 namespace cpp_freertos {
63 
64 
65 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
66 
69 class SemaphoreCreateException : public std::exception {
70 
71  public:
76  {
77  sprintf(errorString, "Semaphore Constructor Failed");
78  }
79 
83  explicit SemaphoreCreateException(const char *info)
84  {
85  snprintf(errorString, sizeof(errorString),
86  "Semaphore Constructor Failed %s", info);
87  }
88 
93  virtual const char *what() const throw()
94  {
95  return errorString;
96  }
97 
98  private:
102  char errorString[80];
103 };
104 #endif
105 
106 
119 class Semaphore {
120 
122  //
123  // Public API
124  //
126  public:
139  bool Take(TickType_t Timeout = portMAX_DELAY);
140 
146  bool Give();
147 
155  bool TakeFromISR(BaseType_t *pxHigherPriorityTaskWoken);
156 
164  bool GiveFromISR(BaseType_t *pxHigherPriorityTaskWoken);
165 
169  virtual ~Semaphore();
170 
172  //
173  // Protected API
174  // Not intended for use by application code.
175  //
177  protected:
181  SemaphoreHandle_t handle;
182 
187  Semaphore();
188 };
189 
190 
194 class BinarySemaphore : public Semaphore {
195 
197  //
198  // Public API
199  //
201  public:
209  explicit BinarySemaphore(bool set = false);
210 };
211 
212 
216 class CountingSemaphore : public Semaphore {
217 
219  //
220  // Public API
221  //
223  public:
233  CountingSemaphore(UBaseType_t maxCount, UBaseType_t initialCount);
234 };
235 
236 
237 }
238 #endif
SemaphoreCreateException(const char *info)
Definition: semaphore.hpp:83
SemaphoreHandle_t handle
Definition: semaphore.hpp:181
virtual const char * what() const
Definition: semaphore.hpp:93