FreeRTOS Addons
Loading...
Searching...
No Matches
thread.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 THREAD_HPP_
43#define THREAD_HPP_
44
55#ifndef CPP_FREERTOS_NO_CPP_STRINGS
56#include <string>
57#endif
58#include "FreeRTOS.h"
59#include "task.h"
60#include "mutex.hpp"
61#include "semaphore.hpp"
62#include "condition_variable.hpp"
63
64namespace cpp_freertos {
65
66
79class Thread {
80
82 //
83 // Public API
84 // Available from anywhere. Many of these require a Thread reference
85 // if they are operating on a thread.
86 //
88 public:
96#ifndef CPP_FREERTOS_NO_CPP_STRINGS
97 Thread( const std::string Name,
98 uint16_t StackDepth,
99 UBaseType_t Priority);
100#else
101 Thread( const char *Name,
102 uint16_t StackDepth,
103 UBaseType_t Priority);
104#endif
105
112 Thread( uint16_t StackDepth,
113 UBaseType_t Priority);
114
131 bool Start();
132
133#ifdef ESP32
134 bool Start(int core);
135#endif
140 virtual ~Thread();
141
148 inline TaskHandle_t GetHandle()
149 {
150 return handle;
151 }
152
156 static inline void Yield()
157 {
158 taskYIELD();
159 }
160
167 static inline void StartScheduler()
168 {
169 SchedulerActive = true;
170 vTaskStartScheduler();
171 }
172
182 static inline void EndScheduler()
183 {
184 vTaskEndScheduler();
185 SchedulerActive = false;
186 }
187
188#if (INCLUDE_vTaskSuspend == 1)
195 inline void Suspend()
196 {
197 vTaskSuspend(GetHandle());
198 }
199
203 inline void Resume()
204 {
205 vTaskResume(GetHandle());
206 }
207#endif
208
209#if (INCLUDE_xTaskResumeFromISR == 1)
213 inline void ResumeFromISR()
214 {
215 xTaskResumeFromISR(GetHandle());
216 }
217#endif
218
219#if (INCLUDE_uxTaskPriorityGet == 1)
225 inline UBaseType_t GetPriority()
226 {
227 return (uxTaskPriorityGet(GetHandle()));
228 }
229
235 inline UBaseType_t GetPriorityFromISR()
236 {
237 return (uxTaskPriorityGetFromISR(GetHandle()));
238 }
239#endif
240
241
242#if (INCLUDE_vTaskPrioritySet == 1)
248 inline void SetPriority(UBaseType_t NewPriority)
249 {
250 Priority = NewPriority;
251 vTaskPrioritySet(GetHandle(), NewPriority);
252 }
253#endif
254
260#ifndef CPP_FREERTOS_NO_CPP_STRINGS
261 inline std::string GetName()
262 {
263 return Name;
264 }
265#else
266 inline char* GetName()
267 {
268 return pcTaskGetName(handle);
269 }
270#endif
271
273 //
274 // Protected API
275 // Available from inside your Thread implementation.
276 // You should make sure that you are only calling these methods
277 // from within your Run() method, or that your Run() method is on the
278 // callstack.
279 //
281 protected:
291 virtual void Run() = 0;
292
293#if (INCLUDE_vTaskDelete == 1)
305 virtual void Cleanup();
306#else
311#endif
312
313#if (INCLUDE_vTaskDelay == 1)
319 void inline Delay(const TickType_t Delay)
320 {
321 vTaskDelay(Delay);
322 }
323#endif
324
325#if (INCLUDE_vTaskDelayUntil == 1)
335 void DelayUntil(const TickType_t Period);
336
341 void ResetDelayUntil();
342#endif
343
344
345#ifdef CPP_FREERTOS_CONDITION_VARIABLES
346
361 bool Wait( ConditionVariable &Cv,
362 Mutex &CvLock,
363 TickType_t Timeout = portMAX_DELAY);
364
365#endif
366
367
369 //
370 // Private API
371 // The internals of this wrapper class.
372 //
374 private:
379 TaskHandle_t handle;
380
384 static volatile bool SchedulerActive;
385
389#ifndef CPP_FREERTOS_NO_CPP_STRINGS
390 const std::string Name;
391#else
392 char Name[configMAX_TASK_NAME_LEN];
393#endif
394
398 const uint16_t StackDepth;
399
403 UBaseType_t Priority;
404
408 bool ThreadStarted;
409
413 static MutexStandard StartGuardLock;
414
421 static void TaskFunctionAdapter(void *pvParameters);
422
423#if (INCLUDE_vTaskDelayUntil == 1)
427 bool delayUntilInitialized;
428
432 TickType_t delayUntilPreviousWakeTime;
433#endif
434
435#ifdef CPP_FREERTOS_CONDITION_VARIABLES
436
442 BinarySemaphore ThreadWaitSem;
443
447 inline void Signal()
448 {
449 ThreadWaitSem.Give();
450 }
451
458 friend class ConditionVariable;
459
460#endif
461
462};
463
464
465}
466#endif
467
Definition: semaphore.hpp:196
Definition: condition_variable.hpp:76
Definition: mutex.hpp:109
Definition: mutex.hpp:171
bool Give()
Definition: csemaphore.cpp:66
Definition: thread.hpp:79
UBaseType_t GetPriorityFromISR()
Definition: thread.hpp:235
void Resume()
Definition: thread.hpp:203
TaskHandle_t GetHandle()
Definition: thread.hpp:148
bool Start()
Definition: cthread.cpp:123
virtual ~Thread()
Definition: cthread.cpp:227
std::string GetName()
Definition: thread.hpp:261
void SetPriority(UBaseType_t NewPriority)
Definition: thread.hpp:248
void Delay(const TickType_t Delay)
Definition: thread.hpp:319
static void StartScheduler()
Definition: thread.hpp:167
void ResetDelayUntil()
Definition: cthread.cpp:276
bool Wait(ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY)
Definition: cthread.cpp:287
virtual void Run()=0
virtual void Cleanup()
Definition: cthread.cpp:222
void Suspend()
Definition: thread.hpp:195
void DelayUntil(const TickType_t Period)
Definition: cthread.cpp:265
void ResumeFromISR()
Definition: thread.hpp:213
static void Yield()
Definition: thread.hpp:156
UBaseType_t GetPriority()
Definition: thread.hpp:225
static void EndScheduler()
Definition: thread.hpp:182
Definition: condition_variable.hpp:57