FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
thread.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 THREAD_HPP_
41 #define THREAD_HPP_
42 
53 #ifndef CPP_FREERTOS_NO_CPP_STRINGS
54 #include <string>
55 #endif
56 #include "FreeRTOS.h"
57 #include "task.h"
58 #include "mutex.hpp"
59 #include "semaphore.hpp"
60 #include "condition_variable.hpp"
61 
62 namespace cpp_freertos {
63 
64 
77 class Thread {
78 
80  //
81  // Public API
82  // Available from anywhere. Many of these require a Thread reference
83  // if they are operating on a thread.
84  //
86  public:
94 #ifndef CPP_FREERTOS_NO_CPP_STRINGS
95  Thread( const std::string Name,
96  uint16_t StackDepth,
97  UBaseType_t Priority);
98 #else
99  Thread( const char *Name,
100  uint16_t StackDepth,
101  UBaseType_t Priority);
102 #endif
103 
110  Thread( uint16_t StackDepth,
111  UBaseType_t Priority);
112 
129  bool Start();
130 
135  virtual ~Thread();
136 
143  inline TaskHandle_t GetHandle()
144  {
145  return handle;
146  }
147 
151  static inline void Yield()
152  {
153  taskYIELD();
154  }
155 
162  static inline void StartScheduler()
163  {
164  SchedulerActive = true;
165  vTaskStartScheduler();
166  }
167 
177  static inline void EndScheduler()
178  {
179  vTaskEndScheduler();
180  SchedulerActive = false;
181  }
182 
183 #if (INCLUDE_vTaskSuspend == 1)
184 
190  inline void Suspend()
191  {
192  vTaskSuspend(GetHandle());
193  }
194 
198  inline void Resume()
199  {
200  vTaskResume(GetHandle());
201  }
202 #endif
203 
204 #if (INCLUDE_xTaskResumeFromISR == 1)
205 
208  inline void ResumeFromISR()
209  {
210  xTaskResumeFromISR(GetHandle());
211  }
212 #endif
213 
214 #if (INCLUDE_uxTaskPriorityGet == 1)
215 
220  inline UBaseType_t GetPriority()
221  {
222  return (uxTaskPriorityGet(GetHandle()));
223  }
224 
230  inline UBaseType_t GetPriorityFromISR()
231  {
232  return (uxTaskPriorityGetFromISR(GetHandle()));
233  }
234 #endif
235 
236 
237 #if (INCLUDE_vTaskPrioritySet == 1)
238 
243  inline void SetPriority(UBaseType_t NewPriority)
244  {
245  Priority = NewPriority;
246  vTaskPrioritySet(GetHandle(), NewPriority);
247  }
248 #endif
249 
255 #ifndef CPP_FREERTOS_NO_CPP_STRINGS
256  inline std::string GetName()
257  {
258  return Name;
259  }
260 #else
261  inline char* GetName()
262  {
263  return pcTaskGetName(handle);
264  }
265 #endif
266 
268  //
269  // Protected API
270  // Available from inside your Thread implementation.
271  // You should make sure that you are only calling these methods
272  // from within your Run() method, or that your Run() method is on the
273  // callstack.
274  //
276  protected:
286  virtual void Run() = 0;
287 
288 #if (INCLUDE_vTaskDelete == 1)
289 
300  virtual void Cleanup();
301 #else
302 
306 #endif
307 
308 #if (INCLUDE_vTaskDelay == 1)
309 
314  void inline Delay(const TickType_t Delay)
315  {
316  vTaskDelay(Delay);
317  }
318 #endif
319 
320 #if (INCLUDE_vTaskDelayUntil == 1)
321 
330  void DelayUntil(const TickType_t Period);
331 
336  void ResetDelayUntil();
337 #endif
338 
339 
340 #ifdef CPP_FREERTOS_CONDITION_VARIABLES
341 
356  bool Wait( ConditionVariable &Cv,
357  Mutex &CvLock,
358  TickType_t Timeout = portMAX_DELAY);
359 
360 #endif
361 
362 
364  //
365  // Private API
366  // The internals of this wrapper class.
367  //
369  private:
374  TaskHandle_t handle;
375 
379  static volatile bool SchedulerActive;
380 
384 #ifndef CPP_FREERTOS_NO_CPP_STRINGS
385  const std::string Name;
386 #else
387  char Name[configMAX_TASK_NAME_LEN];
388 #endif
389 
393  const uint16_t StackDepth;
394 
398  UBaseType_t Priority;
399 
404 
409 
416  static void TaskFunctionAdapter(void *pvParameters);
417 
418 #if (INCLUDE_vTaskDelayUntil == 1)
419 
423 
428 #endif
429 
430 #ifdef CPP_FREERTOS_CONDITION_VARIABLES
431 
438 
442  inline void Signal()
443  {
444  ThreadWaitSem.Give();
445  }
446 
453  friend class ConditionVariable;
454 
455 #endif
456 
457 };
458 
459 
460 }
461 #endif
462 
static MutexStandard StartGuardLock
Definition: thread.hpp:408
UBaseType_t GetPriorityFromISR()
Definition: thread.hpp:230
bool delayUntilInitialized
Definition: thread.hpp:422
static void StartScheduler()
Definition: thread.hpp:162
void SetPriority(UBaseType_t NewPriority)
Definition: thread.hpp:243
Thread(const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
Definition: cthread.cpp:56
std::string GetName()
Definition: thread.hpp:256
const std::string Name
Definition: thread.hpp:385
static void TaskFunctionAdapter(void *pvParameters)
Definition: cthread.cpp:201
virtual void Cleanup()
Definition: cthread.cpp:179
UBaseType_t GetPriority()
Definition: thread.hpp:220
static volatile bool SchedulerActive
Definition: thread.hpp:379
TickType_t delayUntilPreviousWakeTime
Definition: thread.hpp:427
TaskHandle_t handle
Definition: thread.hpp:374
const uint16_t StackDepth
Definition: thread.hpp:393
static void EndScheduler()
Definition: thread.hpp:177
virtual void Run()=0
TaskHandle_t GetHandle()
Definition: thread.hpp:143
static void Yield()
Definition: thread.hpp:151
bool Wait(ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY)
Definition: cthread.cpp:244
BinarySemaphore ThreadWaitSem
Definition: thread.hpp:437
void Delay(const TickType_t Delay)
Definition: thread.hpp:314
void DelayUntil(const TickType_t Period)
Definition: cthread.cpp:222
UBaseType_t Priority
Definition: thread.hpp:398