HCore SysTask

Files CCore/inc/sys/SysTask.h CCore/src/sys/SysTask.cpp

This part provides OS backed thread functions. We often use the term task instead of thread.


#ifndef CCore_inc_sys_SysTask_h
#define CCore_inc_sys_SysTask_h

#include <CCore/inc/sys/SysError.h>
 
namespace CCore {
namespace Sys {

/* types */ 

using TaskIdType = .... ; // integral type <= Atomic::Type

/* functions */ 

void YieldTask() noexcept;
 
void SleepTask(MSec time) noexcept;
 
TaskIdType GetTaskId() noexcept; // !=0

/* classes */

struct TaskEntry;

/* struct TaskEntry */

using TaskEntryFunc = void (*)(TaskEntry *arg) ;

struct TaskEntry
 {
  // private

  ....

  // public
  
  void init(TaskEntryFunc entry) noexcept;
  
  ErrorType create() noexcept;
 };
 
} // namespace Sys
} // namespace CCore
 
#endif

To run a task you create an instance of the structure TaskEntry and initialize it. The you can run a task using the method create(). The instance may be deleted once it's not in use (it may be used by the running task). Copying of the instances is prohibited. The private content of the structure is target-dependent.

init() initialize the instance. The argument is a task entry function. It is a pointer to the function. Once the task is started, this function is called in the task context with the argument — the pointer to the instance. This method is always successful.

create() runs the task. The error code is returned.

YieldTask() gives control to another running task, if any.

SleepTask() sleeps the current task by the specified number of milliseconds.

GetTaskId() returns the current task identity. It is represented by the type TaskIdType. This type must be an unsigned integral type, and it must not be wider than the Atomic::Type. Task identity uniquely identifies the task among running tasks.