mn_task.hpp
Go to the documentation of this file.
1 
19 #ifndef MINLIB_ESP32_THREAD_
20 #define MINLIB_ESP32_THREAD_
21 
22 #include "mn_config.hpp"
23 
24 #include <freertos/FreeRTOS.h>
25 #include <freertos/semphr.h>
26 #include <freertos/task.h>
27 
28 #include <string>
29 
30 #include "mn_autolock.hpp"
31 #include "mn_error.hpp"
32 #include "mn_timespan.hpp"
33 #include "mn_sleep.hpp"
34 #include "mn_micros.hpp"
35 #include "mn_eventgroup.hpp"
36 
37 namespace mn {
38 
89  public:
93  enum class priority {
100  };
101 
103  enum class state {
104  Running = 0,
105  Ready,
106  Blocked,
107  Suspended,
108  Deleted
109  };
110  public:
111  using native_handle_type = xTaskHandle;
112 
125  explicit basic_task(std::string strName, basic_task::priority uiPriority = basic_task::priority::Normal,
126  unsigned short usStackDepth = MN_THREAD_CONFIG_MINIMAL_STACK_SIZE) noexcept;
127 
128 
132  virtual ~basic_task();
133 
155  virtual int start(int uiCore = MN_THREAD_CONFIG_DEFAULT_CORE);
156 
164  int kill();
165 
166 
172  bool is_running();
173 
179  std::string get_name();
191  unsigned short get_stackdepth();
198  xTaskHandle get_handle();
199 
205  int get_return_value();
217  int32_t get_id();
223  int32_t get_on_core();
224 
230  void set_priority(basic_task::priority uiPriority);
231 
238  void suspend();
242  void resume();
243 
244  bool joinable() const noexcept;
254  int join(unsigned int xTickTimeout = portMAX_DELAY);
255 
265  int join(timespan_t time);
266 
276  int wait(unsigned int xTimeOut);
277 
287  int wait(timespan_t time);
288 
293  virtual void on_start() { }
298  virtual void on_kill() { }
299 
306  virtual int on_task() { return ERR_TASK_OK; }
307 
314  virtual void on_cleanup() { }
315 
321  state get_state();
322 
327  operator xTaskHandle () { return (xTaskHandle)get_handle(); }
328 
333  operator int () { return get_id(); }
334  public:
335  bool operator == (const basic_task &r) const {
336  return m_pHandle == r.m_pHandle;
337  }
338 
339  bool operator != (const basic_task &r) const {
340  return !operator==(r);
341  }
342 
343  bool operator < (const basic_task &r) const {
344  return m_pHandle < r.m_pHandle;
345  }
346 
347  bool operator > (const basic_task &r) const {
348  return m_pHandle > r.m_pHandle;
349  }
350  public:
359  static void suspend(basic_task *t) { t->suspend(); }
360 
366  static void resume(basic_task *t) { t->resume(); }
367 
371  static void yield() { taskYIELD(); }
377  static void sleep(unsigned int secs) { mn::delay(timespan_t(0, 0, 0, secs)); }
383  static void usleep(unsigned int usec) { mn::delay(timespan_t(0, 0, 0, 0, usec)); }
388  static void nsleep(const timespan_t& req, timespan_t* rem) {
389  mn::ndelay(req, rem);
390  }
391 
400  static uint32_t get_tasks();
401 
406  static bool is_current(basic_task* task) {
407  return basic_task::get_self()->m_pHandle == task->m_pHandle;
408  }
409 
415  static basic_task* get_self();
416  protected:
421  static void runtaskstub(void* parm);
422  protected:
427  protected:
431  std::string m_strName;
439  unsigned short m_usStackDepth;
443  int m_retval;
451  int32_t m_iID;
455  int32_t m_iCore;
461 
463 
464  #if( configSUPPORT_STATIC_ALLOCATION == 1 )
465  StaticTask_t m_TaskBuffer;
466  StackType_t m_stackBuffer[MN_THREAD_CONFIG_STACK_DEPTH];
467  #endif
468  };
473 }
474 
475 
476 
477 #endif
Definition: mn_binary_semaphore.hpp:29
Wrapper class around FreeRTOS's implementation of a event_group.
Definition: mn_eventgroup.hpp:35
Wrapper class around FreeRTOS's implementation of a task.
Definition: mn_task.hpp:88
static void nsleep(const timespan_t &req, timespan_t *rem)
pause execution for a specified time
Definition: mn_task.hpp:388
priority
Task priority.
Definition: mn_task.hpp:93
bool operator<(const basic_task &r) const
Definition: mn_task.hpp:343
std::string get_name()
Get the debug name of this task.
Definition: mn_task.cpp:236
void set_priority(basic_task::priority uiPriority)
Set the priority of this task.
Definition: mn_task.cpp:328
unsigned short m_usStackDepth
Stack depth of this task, in words.
Definition: mn_task.hpp:439
state
Definition: mn_task.hpp:103
static void sleep(unsigned int secs)
sleep this task for n seconds
Definition: mn_task.hpp:377
LockType_t m_continuemutex
Definition: mn_task.hpp:426
basic_task::priority m_uiPriority
A saved / cached copy of what the task's priority is.
Definition: mn_task.hpp:435
int32_t m_iID
The FreeRTOS task Number.
Definition: mn_task.hpp:451
state get_state()
Get the state of the task.
Definition: mn_task.cpp:283
basic_task() noexcept
Definition: mn_task.hpp:117
bool is_running()
Is the Task running?
Definition: mn_task.cpp:212
static void yield()
Yield the scheduler.
Definition: mn_task.hpp:371
static bool is_current(basic_task *task)
Is the given task the current running task ?
Definition: mn_task.hpp:406
basic_task::priority get_priority()
Get the priority of this task.
Definition: mn_task.cpp:245
native_handle_type m_pHandle
Reference to the underlying task handle for this task.
Definition: mn_task.hpp:460
virtual void on_start()
This virtual function call on creating, use for user code It is optional whether you implement this o...
Definition: mn_task.hpp:293
virtual int start(int uiCore=MN_THREAD_CONFIG_DEFAULT_CORE)
Create and starts the Task.
Definition: mn_task.cpp:77
virtual void on_kill()
This virtual function call on kill, use for user code It is optional whether you implement this or no...
Definition: mn_task.hpp:298
int get_return_value()
Get the return value of this task - after run.
Definition: mn_task.cpp:291
int wait(unsigned int xTimeOut)
Wait for start the task.
Definition: mn_task.cpp:174
int32_t get_on_core()
Get the core number of this task run.
Definition: mn_task.cpp:228
static void usleep(unsigned int usec)
sleep this task for n micro seconds
Definition: mn_task.hpp:383
int m_retval
The return value from user task routine.
Definition: mn_task.hpp:443
static uint32_t get_tasks()
Get current number of tasks.
Definition: mn_task.cpp:276
bool m_bRunning
Flag whether or not the task was started.
Definition: mn_task.hpp:447
LockType_t m_runningMutex
Lock Objekt for task safty.
Definition: mn_task.hpp:426
bool operator!=(const basic_task &r) const
Definition: mn_task.hpp:339
int kill()
Destroy and delete the task and call the function 'on_kill'.
Definition: mn_task.cpp:189
std::string m_strName
The name of this task.
Definition: mn_task.hpp:431
virtual ~basic_task()
Our destructor. Delete the task.
Definition: mn_task.cpp:64
unsigned short get_stackdepth()
Get the stack depth of this task.
Definition: mn_task.cpp:260
static basic_task * get_self()
Get the current task.
Definition: mn_task.cpp:309
bool operator>(const basic_task &r) const
Definition: mn_task.hpp:347
void resume()
Resume a specific task.
Definition: mn_task.cpp:346
int32_t m_iCore
A saved / cached copy of which core this task is running on.
Definition: mn_task.hpp:455
event_group_t m_eventGroup
Definition: mn_task.hpp:462
xTaskHandle get_handle()
Accessor to get the task's backing task handle. There is no setter, on purpose.
Definition: mn_task.cpp:268
LockType_t m_contextMutext
Definition: mn_task.hpp:426
static void suspend(basic_task *t)
Suspend the given task.
Definition: mn_task.hpp:359
int32_t get_id()
Get the FreeRTOS task Numberid of this task.
Definition: mn_task.cpp:220
static void runtaskstub(void *parm)
Adapter function that allows you to write a class specific on_task() function that interfaces with Fr...
Definition: mn_task.cpp:353
static void resume(basic_task *t)
Resume the given task.
Definition: mn_task.hpp:366
virtual void on_cleanup()
Called on exit from your on_task() routine.
Definition: mn_task.hpp:314
virtual int on_task()
Implementation of your actual task code. You must override this function.
Definition: mn_task.hpp:306
int join(unsigned int xTickTimeout=portMAX_DELAY)
join the task, Wait in other task to end this task.
Definition: mn_task.cpp:157
timespan_t get_time_since_start() const
Get the time since start of this task.
Definition: mn_task.cpp:299
void suspend()
Suspend this task.
Definition: mn_task.cpp:338
bool joinable() const noexcept
Definition: mn_task.cpp:151
bool operator==(const basic_task &r) const
Definition: mn_task.hpp:335
xTaskHandle native_handle_type
Definition: mn_task.hpp:111
This class represents a time span (using microseconds)
Definition: mn_timespan.hpp:35
#define portMAX_DELAY
Definition: mn_autolock.hpp:34
#define MN_THREAD_CONFIG_CORE_PRIORITY_CRITICAL
Task priority for basic_task::PriorityCritical.
Definition: mn_config.hpp:202
#define MN_THREAD_CONFIG_CORE_PRIORITY_HALFCRT
Task priority for basic_task::PriorityHalfCritical.
Definition: mn_config.hpp:188
#define MN_THREAD_CONFIG_CORE_PRIORITY_NORM
Task priority for basic_task::PriorityNormal.
Definition: mn_config.hpp:181
#define MN_THREAD_CONFIG_CORE_PRIORITY_IDLE
Task priority for basic_task::PriorityIdle.
Definition: mn_config.hpp:167
#define MN_THREAD_CONFIG_CORE_PRIORITY_LOW
Task priority for basic_task::PriorityLow.
Definition: mn_config.hpp:174
#define MN_THREAD_CONFIG_CORE_PRIORITY_URGENT
Task priority for basic_task::PriorityUrgent.
Definition: mn_config.hpp:195
#define MN_THREAD_CONFIG_MINIMAL_STACK_SIZE
Definition: mn_config.hpp:219
#define MN_THREAD_CONFIG_DEFAULT_CORE
Pre defined on which core must run the task, can override in the create function.
Definition: mn_config.hpp:98
#define MN_ONSIGLETN_CLASS
Definition: mn_copyable.hpp:27
A list of all error codes in this lib. This file is part of the Mini Thread Library (https://github....
#define ERR_TASK_OK
Definition: mn_error.hpp:87
Definition: mn_allocator_typetraits.hpp:25
void ndelay(const timespan_t &req, timespan_t *rem)
Definition: mn_sleep.cpp:112
basic_timespan timespan_t
Definition: mn_timespan.hpp:225
void delay(const timespan_t &ts)
Definition: mn_sleep.cpp:147