openthread-br  0.3.0-72c0388
Classes | Public Types | Public Member Functions | List of all members
otbr::TaskRunner Class Reference

#include <src/common/task_runner.hpp>

Inheritance diagram for otbr::TaskRunner:
otbr::MainloopProcessor NonCopyable

Public Types

template<class T >
using Task = std::function< T(void)>
 
typedef uint64_t TaskId
 

Public Member Functions

 TaskRunner (void)
 
 ~TaskRunner (void) override
 
void Post (Task< void > aTask)
 
TaskId Post (Milliseconds aDelay, Task< void > aTask)
 
void Cancel (TaskId aTaskId)
 
template<class T >
PostAndWait (const Task< T > &aTask)
 
void Update (MainloopContext &aMainloop) override
 
void Process (const MainloopContext &aMainloop) override
 

Detailed Description

This class implements the Task Runner that executes tasks on the mainloop.

Member Typedef Documentation

◆ Task

template<class T >
using otbr::TaskRunner::Task = std::function<T(void)>

This type represents the generic executable task.

◆ TaskId

typedef uint64_t otbr::TaskRunner::TaskId

This type represents a unique task ID to an delayed task.

Note: A valid task ID is never zero.

Constructor & Destructor Documentation

◆ TaskRunner()

otbr::TaskRunner::TaskRunner ( void  )

This constructor initializes the Task Runner instance.

◆ ~TaskRunner()

otbr::TaskRunner::~TaskRunner ( void  )
override

This destructor destroys the Task Runner instance.

Member Function Documentation

◆ Cancel()

void otbr::TaskRunner::Cancel ( TaskRunner::TaskId  aTaskId)

This method cancels a delayed task from the task runner. It is safe to call this method in different threads concurrently.

Parameters
[in]aTaskIdThe unique task ID of the delayed task to cancel.

◆ Post() [1/2]

TaskRunner::TaskId otbr::TaskRunner::Post ( Milliseconds  aDelay,
Task< void >  aTask 
)

This method posts a task to the task runner and returns immediately.

The task will be executed on the mainloop after aDelay milliseconds from now. It is safe to call this method in different threads concurrently.

Parameters
[in]aDelayThe delay before executing the task (in milliseconds).
[in]aTaskThe task to be executed.
Returns
The unique task ID of the delayed task.

◆ Post() [2/2]

void otbr::TaskRunner::Post ( Task< void >  aTask)

This method posts a task to the task runner and returns immediately.

Tasks are executed sequentially and follow the First-Come-First-Serve rule. It is safe to call this method in different threads concurrently.

Parameters
[in]aTaskThe task to be executed.

◆ PostAndWait()

template<class T >
T otbr::TaskRunner::PostAndWait ( const Task< T > &  aTask)
inline

This method posts a task and waits for the completion of the task.

Tasks are executed sequentially and follow the First-Come-First-Serve rule. This method must be called in a thread other than the mainloop thread. Otherwise, the caller will be blocked forever.

Returns
The result returned by the task aTask.

◆ Process()

void otbr::TaskRunner::Process ( const MainloopContext aMainloop)
overridevirtual

This method processes mainloop events.

Parameters
[in]aMainloopA reference to the mainloop context.

Implements otbr::MainloopProcessor.

◆ Update()

void otbr::TaskRunner::Update ( MainloopContext aMainloop)
overridevirtual

This method updates the mainloop context.

Parameters
[in,out]aMainloopA reference to the mainloop to be updated.

Implements otbr::MainloopProcessor.


The documentation for this class was generated from the following files: