Open FFBoard
Open source force feedback firmware
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"
61
62namespace cpp_freertos {
63
64
77class 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)
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)
208 inline void ResumeFromISR()
209 {
210 xTaskResumeFromISR(GetHandle());
211 }
212#endif
213
218#ifdef configUSE_TASK_NOTIFICATIONS
222 inline void Notify()
223 {
224 xTaskNotifyGive( GetHandle() );
225 }
226
230 inline void NotifyFromISR()
231 {
232 BaseType_t pxHigherPriorityTaskWoken;
233 vTaskNotifyGiveFromISR( GetHandle(), &pxHigherPriorityTaskWoken );
234 portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
235 }
236
246 inline uint32_t WaitForNotification( TickType_t Timeout = portMAX_DELAY )
247 {
248 return ulTaskNotifyTake( pdTRUE, Timeout );
249 }
250#endif
251
252#if (INCLUDE_uxTaskPriorityGet == 1)
258 inline UBaseType_t GetPriority()
259 {
260 return (uxTaskPriorityGet(GetHandle()));
261 }
262
268 inline UBaseType_t GetPriorityFromISR()
269 {
270 return (uxTaskPriorityGetFromISR(GetHandle()));
271 }
272#endif
273
274
275#if (INCLUDE_vTaskPrioritySet == 1)
281 inline void SetPriority(UBaseType_t NewPriority)
282 {
283 Priority = NewPriority;
284 vTaskPrioritySet(GetHandle(), NewPriority);
285 }
286#endif
287
293#ifndef CPP_FREERTOS_NO_CPP_STRINGS
294 inline std::string GetName()
295 {
296 return Name;
297 }
298#else
299 inline char* GetName()
300 {
301 return pcTaskGetName(handle);
302 }
303#endif
304
306 //
307 // Protected API
308 // Available from inside your Thread implementation.
309 // You should make sure that you are only calling these methods
310 // from within your Run() method, or that your Run() method is on the
311 // callstack.
312 //
314 protected:
324 virtual void Run() = 0;
325
326#if (INCLUDE_vTaskDelete == 1)
338 virtual void Cleanup();
339#else
344#endif
345
346#if (INCLUDE_vTaskDelay == 1)
352 void inline Delay(const TickType_t Delay)
353 {
354 vTaskDelay(Delay);
355 }
356#endif
357
358#if (INCLUDE_vTaskDelayUntil == 1)
368 void DelayUntil(const TickType_t Period);
369
374 void ResetDelayUntil();
375#endif
376
377
378#ifdef CPP_FREERTOS_CONDITION_VARIABLES
379
394 bool Wait( ConditionVariable &Cv,
395 Mutex &CvLock,
396 TickType_t Timeout = portMAX_DELAY);
397
398#endif
399
400
402 //
403 // Private API
404 // The internals of this wrapper class.
405 //
407 private:
412 TaskHandle_t handle;
413
417 static volatile bool SchedulerActive;
418
422#ifndef CPP_FREERTOS_NO_CPP_STRINGS
423 const std::string Name;
424#else
425 char Name[configMAX_TASK_NAME_LEN];
426#endif
427
431 const uint16_t StackDepth;
432
436 UBaseType_t Priority;
437
442
447
454 static void TaskFunctionAdapter(void *pvParameters);
455
456#if (INCLUDE_vTaskDelayUntil == 1)
461
466#endif
467
468#ifdef CPP_FREERTOS_CONDITION_VARIABLES
469
476
480 inline void Signal()
481 {
483 }
484
491 friend class ConditionVariable;
492
493#endif
494
495
496};
497
498
499}
500#endif
501
UBaseType_t GetPriorityFromISR()
Definition: thread.hpp:268
static volatile bool SchedulerActive
Definition: thread.hpp:417
TaskHandle_t GetHandle()
Definition: thread.hpp:143
uint32_t WaitForNotification(TickType_t Timeout=portMAX_DELAY)
Definition: thread.hpp:246
const std::string Name
Definition: thread.hpp:423
const uint16_t StackDepth
Definition: thread.hpp:431
static MutexStandard StartGuardLock
Definition: thread.hpp:446
static void TaskFunctionAdapter(void *pvParameters)
Definition: cthread.cpp:201
std::string GetName()
Definition: thread.hpp:294
void SetPriority(UBaseType_t NewPriority)
Definition: thread.hpp:281
void Delay(const TickType_t Delay)
Definition: thread.hpp:352
BinarySemaphore ThreadWaitSem
Definition: thread.hpp:475
static void StartScheduler()
Definition: thread.hpp:162
bool delayUntilInitialized
Definition: thread.hpp:460
UBaseType_t Priority
Definition: thread.hpp:436
TickType_t delayUntilPreviousWakeTime
Definition: thread.hpp:465
bool Wait(ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY)
Definition: cthread.cpp:244
virtual void Run()=0
virtual void Cleanup()
Definition: cthread.cpp:179
void DelayUntil(const TickType_t Period)
Definition: cthread.cpp:222
TaskHandle_t handle
Definition: thread.hpp:412
Thread(const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
Definition: cthread.cpp:56
static void Yield()
Definition: thread.hpp:151
UBaseType_t GetPriority()
Definition: thread.hpp:258
static void EndScheduler()
Definition: thread.hpp:177