FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
csemaphore.cpp
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 #include "semaphore.hpp"
41 
42 
43 using namespace cpp_freertos;
44 
45 
46 bool Semaphore::Take(TickType_t xBlockTime)
47 {
48  BaseType_t success;
49 
50  success = xSemaphoreTake(handle, xBlockTime);
51 
52  return success == pdTRUE ? true : false;
53 }
54 
55 
56 bool Semaphore::TakeFromISR(BaseType_t *pxHigherPriorityTaskWoken)
57 {
58  BaseType_t success;
59 
60  success = xSemaphoreTakeFromISR(handle, pxHigherPriorityTaskWoken);
61 
62  return success == pdTRUE ? true : false;
63 }
64 
65 
67 {
68  BaseType_t success;
69 
70  success = xSemaphoreGive(handle);
71 
72  return success == pdTRUE ? true : false;
73 }
74 
75 
76 bool Semaphore::GiveFromISR(BaseType_t *pxHigherPriorityTaskWoken)
77 {
78  BaseType_t success;
79 
80  success = xSemaphoreGiveFromISR(handle, pxHigherPriorityTaskWoken);
81 
82  return success == pdTRUE ? true : false;
83 }
84 
85 
87 {
88 }
89 
90 
92 {
93  vSemaphoreDelete(handle);
94 }
95 
96 
98 {
99  handle = xSemaphoreCreateBinary();
100 
101  if (handle == NULL) {
102 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
103  throw SemaphoreCreateException();
104 #else
105  configASSERT(!"BinarySemaphore Constructor Failed");
106 #endif
107  }
108 
109  if (set) {
110  xSemaphoreGive(handle);
111  }
112 }
113 
114 
115 CountingSemaphore::CountingSemaphore(UBaseType_t maxCount, UBaseType_t initialCount)
116 {
117  if (maxCount == 0) {
118 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
119  throw SemaphoreCreateException("bad maxCount");
120 #else
121  configASSERT(!"CountingSemaphore Constructor bad maxCount");
122 #endif
123  }
124 
125  if (initialCount > maxCount) {
126 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
127  throw SemaphoreCreateException("bad initialCount");
128 #else
129  configASSERT(!"CountingSemaphore Constructor bad initialCount");
130 #endif
131  }
132 
133  handle = xSemaphoreCreateCounting(maxCount, initialCount);
134 
135  if (handle == NULL) {
136 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
137  throw SemaphoreCreateException();
138 #else
139  configASSERT(!"CountingSemaphore Constructor Failed");
140 #endif
141  }
142 }
143 
144 
CountingSemaphore(UBaseType_t maxCount, UBaseType_t initialCount)
Definition: csemaphore.cpp:115
bool TakeFromISR(BaseType_t *pxHigherPriorityTaskWoken)
Definition: csemaphore.cpp:56
bool GiveFromISR(BaseType_t *pxHigherPriorityTaskWoken)
Definition: csemaphore.cpp:76
bool Take(TickType_t Timeout=portMAX_DELAY)
Definition: csemaphore.cpp:46
SemaphoreHandle_t handle
Definition: semaphore.hpp:181
BinarySemaphore(bool set=false)
Definition: csemaphore.cpp:97