FreeRTOS Addons
Loading...
Searching...
No Matches
semaphore.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 SEMAPHORE_HPP_
43#define SEMAPHORE_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
63
64namespace cpp_freertos {
65
66
67#ifndef CPP_FREERTOS_NO_EXCEPTIONS
71class SemaphoreCreateException : public std::exception {
72
73 public:
78 {
79 sprintf(errorString, "Semaphore Constructor Failed");
80 }
81
85 explicit SemaphoreCreateException(const char *info)
86 {
87 snprintf(errorString, sizeof(errorString),
88 "Semaphore Constructor Failed %s", info);
89 }
90
95 virtual const char *what() const throw()
96 {
97 return errorString;
98 }
99
100 private:
104 char errorString[80];
105};
106#endif
107
108
122
124 //
125 // Public API
126 //
128 public:
141 bool Take(TickType_t Timeout = portMAX_DELAY);
142
148 bool Give();
149
157 bool TakeFromISR(BaseType_t *pxHigherPriorityTaskWoken);
158
166 bool GiveFromISR(BaseType_t *pxHigherPriorityTaskWoken);
167
171 virtual ~Semaphore();
172
174 //
175 // Protected API
176 // Not intended for use by application code.
177 //
179 protected:
183 SemaphoreHandle_t handle;
184
189 Semaphore();
190};
191
192
197
199 //
200 // Public API
201 //
203 public:
211 explicit BinarySemaphore(bool set = false);
212};
213
214
219
221 //
222 // Public API
223 //
225 public:
235 CountingSemaphore(UBaseType_t maxCount, UBaseType_t initialCount);
236};
237
238
239}
240#endif
Definition: semaphore.hpp:196
Definition: semaphore.hpp:218
Definition: semaphore.hpp:71
SemaphoreCreateException(const char *info)
Definition: semaphore.hpp:85
SemaphoreCreateException()
Definition: semaphore.hpp:77
virtual const char * what() const
Definition: semaphore.hpp:95
Definition: semaphore.hpp:121
bool GiveFromISR(BaseType_t *pxHigherPriorityTaskWoken)
Definition: csemaphore.cpp:76
SemaphoreHandle_t handle
Definition: semaphore.hpp:183
virtual ~Semaphore()
Definition: csemaphore.cpp:91
bool TakeFromISR(BaseType_t *pxHigherPriorityTaskWoken)
Definition: csemaphore.cpp:56
bool Give()
Definition: csemaphore.cpp:66
Semaphore()
Definition: csemaphore.cpp:86
bool Take(TickType_t Timeout=portMAX_DELAY)
Definition: csemaphore.cpp:46
Definition: condition_variable.hpp:57