Friendly LWM2M client
wpp::WppTaskQueue Class Reference

Represents a task queue. More...

#include <WppTaskQueue.h>

+ Collaboration diagram for wpp::WppTaskQueue:

Public Types

using task_id_t = uint32_t
 
using task_t = std::function< bool(WppClient &, void *)>
 

Public Member Functions

 ~WppTaskQueue ()
 

Static Public Member Functions

static task_id_t addTask (time_t delaySec, task_t task)
 Add task to queue, ctx that passed to task equals to NULL. More...
 
static task_id_t addTask (void *ctx, time_t delaySec, task_t task)
 Add task to queue, ctx passed to task by pointer without copy. More...
 
static task_id_t addTaskWithCopy (const void *ctx, size_t size, time_t delaySec, task_t task)
 Add task to queue, ctx passed to task by pointer with copy, allocated memory will be relesed after deleting task from queue. More...
 
static size_t getTaskCnt ()
 Returns count of tasks in the queue. Tasks count does not immediately updated after request to remove task. More...
 
static bool isTaskExist (task_id_t id)
 Returns true if task exists in the queue. More...
 
static bool isTaskIdle (task_id_t id)
 Returns true if state corresponds to function. More...
 
static bool isTaskExecuting (task_id_t id)
 
static bool isTaskShouldBeDeleted (task_id_t id)
 
static void requestToRemoveTask (task_id_t id)
 This function does not immediately delete the task, it only marks it as one that should be deleted at the first opportunity, namely when executingEachTask() is called. At the same time, calling requestToRemoveTask() guarantees that the task will no longer be executed. There is only one exception if the requestToRemoveTask() call refers to a task that is being executed right at this moment, then the deletion will take place only after the execution is completed, so the user must guarantee the validity of the context and the parameters passed through the capture before the execution of this task is completed. If after exiting this method the task is not in the EXECUTING state, the user can be sure that the task is not executed and will not be executed again, and the context will no longer be used. If, after exiting, the task is in EXECUTING, the user can wait until it is completed by polling the isTaskExecuting() method. More...
 
static void requestToRemoveEachTask ()
 This function does not immediately delete all tasks, it only marks them as one that should be deleted at the first opportunity, namely when executingEachTask() is called. At the same time, calling requestToRemoveEachTask() guarantees that the tasks will no longer be executed. There is only one exception if the requestToRemoveEachTask() calls during task execution right at this moment, then the deletion will take place only after the execution is completed, so the user must guarantee the validity of the context and the parameters passed through the capture before the execution of this task is completed. If after exiting this method the task is not in the EXECUTING state, the user can be sure that the task is not executed and will not be executed again, and the context will no longer be used. If, after exiting, the task is in EXECUTING, the user can wait until it is completed by polling the isTaskExecuting() method. More...
 
static void hardReset ()
 Blocks task handling, calls of other methods, and deletes all tasks from the queue, after that returns control over the queue. More...
 
static time_t handleEachTask (WppClient &client)
 Execute each task in the queue and delete it from queue if task returns false or task state is SHOULD_BE_DELETED. This method is called by the WppClient in its loop() method. But it can be also called by the user if he wants to process immediately. More...
 

Detailed Description

Represents a task queue.

The queue of tasks does not guarantee timely calling of a task with a fixed delay, all tasks in the queue are processed sequentially, it is only guaranteed that the task will be called after the specified delay. The queue should not be used for critical tasks. When a task is created via addTask(), the party that creates the task must guarantee the validity of the ctx_t ctx during the entire existence of the task. Tasks may be deleted not immediately, but with the next call to handleEachTask(), but it is guaranteed that the deleted task will not be executed. It is forbidden to use any delays in the task, because all the tasks are executed sequentially in the same thread, this will cause an execution delay for other tasks. WppTaskQueue should be used only for tasks that require the WppClient context.

The implementation is completely thread-safe and allows calling its methods from tasks.

Note
The WppTaskQueue class is a singleton.

Definition at line 50 of file WppTaskQueue.h.

Member Typedef Documentation

◆ task_id_t

using wpp::WppTaskQueue::task_id_t = uint32_t

Definition at line 52 of file WppTaskQueue.h.

◆ task_t

using wpp::WppTaskQueue::task_t = std::function<bool(WppClient&, void *)>

Keep in mind that while std::function itself is always copy able, it might hold a callable object (like a lambda) that captures variables which may not be copy able. If you try to copy a std::function that holds a non-copyable callable, it will compile, but will throw a std::bad_function_call exception at runtime if you try to call the copied std::function.

Definition at line 61 of file WppTaskQueue.h.

Constructor & Destructor Documentation

◆ ~WppTaskQueue()

wpp::WppTaskQueue::~WppTaskQueue ( )

Definition at line 17 of file WppTaskQueue.cpp.

Member Function Documentation

◆ addTask() [1/2]

WppTaskQueue::task_id_t wpp::WppTaskQueue::addTask ( time_t  delaySec,
task_t  task 
)
static

Add task to queue, ctx that passed to task equals to NULL.

Parameters
delaySec- Min time after which task will be run first time, and time beatween next calls of this task while it returns false. Minimum value is WPP_TASK_MIN_DELAY_S, max value is WPP_TASK_MAX_DELAY_S.
task- Task for execute, while task returns false it will be called with specified delay, after returning true task deleted from queue.
Returns
id of created task or WPP_ERR_TASK_ID

Definition at line 22 of file WppTaskQueue.cpp.

◆ addTask() [2/2]

WppTaskQueue::task_id_t wpp::WppTaskQueue::addTask ( void *  ctx,
time_t  delaySec,
task_t  task 
)
static

Add task to queue, ctx passed to task by pointer without copy.

Parameters
ctx- User data ptr that will be passed to task, without coping. User must guarantee the validity of the context during the entire existence of the task.
delaySec- Min time after which task will be run first time, and time beatween next calls of this task while it returns false. Minimum value is WPP_TASK_MIN_DELAY_S, max value is WPP_TASK_MAX_DELAY_S.
task- Task for execute, while task returns false it will be called with specified delay, after returning true task deleted from queue.
Returns
id of created task or WPP_ERR_TASK_ID

Definition at line 26 of file WppTaskQueue.cpp.

◆ addTaskWithCopy()

WppTaskQueue::task_id_t wpp::WppTaskQueue::addTaskWithCopy ( const void *  ctx,
size_t  size,
time_t  delaySec,
task_t  task 
)
static

Add task to queue, ctx passed to task by pointer with copy, allocated memory will be relesed after deleting task from queue.

Parameters
ctx- Ptr to user data that will be copied and passed to task.
size- User data size that will be copied.
delaySec- Min time after which task will be run first time, and time beatween next calls of this task while it returns false. Minimum value is WPP_TASK_MIN_DELAY_S, max value is WPP_TASK_MAX_DELAY_S.
task- Task for execute, while task returns false it will be called, with specified delay,after returning true task deleted from the queue, and relese allocated memory for ctx.
Returns
id of created task or WPP_ERR_TASK_ID

Definition at line 53 of file WppTaskQueue.cpp.

◆ getTaskCnt()

size_t wpp::WppTaskQueue::getTaskCnt ( )
static

Returns count of tasks in the queue. Tasks count does not immediately updated after request to remove task.

Definition at line 81 of file WppTaskQueue.cpp.

+ Here is the caller graph for this function:

◆ handleEachTask()

time_t wpp::WppTaskQueue::handleEachTask ( WppClient client)
static

Execute each task in the queue and delete it from queue if task returns false or task state is SHOULD_BE_DELETED. This method is called by the WppClient in its loop() method. But it can be also called by the user if he wants to process immediately.

Returns
time in sec to next task execution, if tasks are not exist then returns WPP_TASK_MAX_DELAY_S.

Definition at line 173 of file WppTaskQueue.cpp.

+ Here is the caller graph for this function:

◆ hardReset()

void wpp::WppTaskQueue::hardReset ( )
static

Blocks task handling, calls of other methods, and deletes all tasks from the queue, after that returns control over the queue.

Definition at line 156 of file WppTaskQueue.cpp.

+ Here is the caller graph for this function:

◆ isTaskExecuting()

bool wpp::WppTaskQueue::isTaskExecuting ( task_id_t  id)
static

Definition at line 109 of file WppTaskQueue.cpp.

◆ isTaskExist()

bool wpp::WppTaskQueue::isTaskExist ( task_id_t  id)
static

Returns true if task exists in the queue.

Definition at line 85 of file WppTaskQueue.cpp.

◆ isTaskIdle()

bool wpp::WppTaskQueue::isTaskIdle ( task_id_t  id)
static

Returns true if state corresponds to function.

Definition at line 95 of file WppTaskQueue.cpp.

◆ isTaskShouldBeDeleted()

bool wpp::WppTaskQueue::isTaskShouldBeDeleted ( task_id_t  id)
static

Definition at line 123 of file WppTaskQueue.cpp.

◆ requestToRemoveEachTask()

void wpp::WppTaskQueue::requestToRemoveEachTask ( )
static

This function does not immediately delete all tasks, it only marks them as one that should be deleted at the first opportunity, namely when executingEachTask() is called. At the same time, calling requestToRemoveEachTask() guarantees that the tasks will no longer be executed. There is only one exception if the requestToRemoveEachTask() calls during task execution right at this moment, then the deletion will take place only after the execution is completed, so the user must guarantee the validity of the context and the parameters passed through the capture before the execution of this task is completed. If after exiting this method the task is not in the EXECUTING state, the user can be sure that the task is not executed and will not be executed again, and the context will no longer be used. If, after exiting, the task is in EXECUTING, the user can wait until it is completed by polling the isTaskExecuting() method.

Definition at line 150 of file WppTaskQueue.cpp.

◆ requestToRemoveTask()

void wpp::WppTaskQueue::requestToRemoveTask ( task_id_t  id)
static

This function does not immediately delete the task, it only marks it as one that should be deleted at the first opportunity, namely when executingEachTask() is called. At the same time, calling requestToRemoveTask() guarantees that the task will no longer be executed. There is only one exception if the requestToRemoveTask() call refers to a task that is being executed right at this moment, then the deletion will take place only after the execution is completed, so the user must guarantee the validity of the context and the parameters passed through the capture before the execution of this task is completed. If after exiting this method the task is not in the EXECUTING state, the user can be sure that the task is not executed and will not be executed again, and the context will no longer be used. If, after exiting, the task is in EXECUTING, the user can wait until it is completed by polling the isTaskExecuting() method.

Definition at line 137 of file WppTaskQueue.cpp.

+ Here is the caller graph for this function: